Cocos2d-x 3.x Migration – C++11 Subtle Mistakes

I’ll probably repeat myself that c++11 makes developing in c++ much more pleasant. However, there are some interesting issues when migrating from various class structures to the new std:: template containers and algorithms.

std::remove removes all elements of a given type, not just the first one found. Normally I try and use the for (auto& element : arr ), but in these cases I use the full iterator syntax instead. Could also use an index based loop, or no doubt another approach which I haven’t learned.

// v is vector<string> may contain duplicate strings
for ( auto& el : v ) {
    // tried to use remove, but removes all found elements
    v.erase(std::remove(v.begin(), v.end(), el), v.end()); 

// instead use iterator pointer
for ( auto it = v.begin(); it != v.end(); it++ )
  std::string element = (*it);
  if(element == "string to match")
     // want to remove first string match
     v.erase(it); // iterator is invalid afterward

[I’ll continue to append new issues as they’re encountered]

Cocos2d-x 3.x Migration – std::string & nullptr

As I move our code base toward pure c++11 where it makes sense when changing or adding functionality. Once I moved from using cocos2d-x’s __String class to std::string and associated std::vector<std::string> in a couple places I figured it was a good candidate to migrate fully away from the __String class altogether.

This was mostly a simple find/replace exercise with various regular expressions, but one thing I wasn’t aware of is that the act of assigning nullptr to a std::string does not trigger a warning or error at compile time, but rather fails as expected at runtime. This was cause for more time spent to correctly handle all cases where the idea of no string or an empty string is necessary.

I would think there is a flag for each compiler to detect these at compile time, but I haven’t looked into it much since it wasn’t all that much effort to fix the code, but it did take much longer than if the compiler had flagged every location which it should be able to do.

I realize this is fairly specific to the migration process as well how the code was originally written, but still an unexpected annoyance in the process.

Cocos2d-x 3.x Migration – Visitor

This will no longer be necessary once std:: containers are used everywhere instead of the deprecated value classes. Until then, however, I’ll be using the following to pretty print out arrays and dictionaries.


class CC_DLL Visitable
 /** should call concrete visit method */
 virtual void acceptVisitor(DataVisitor &visitor) = 0;
 virtual ~Visitable() {}

Also updated the PrettyPrinter class in CCDataVisitor.cpp

Changed the three visit methods:

// in visit(const __Array *p)

// in visit(const __Dictionary *p)

// in visit(const __Set *p)

Replaced with:

// in visit(const __Array *p)
Visitable* v = dynamic_cast<Visitable*>(obj);
if(v) v->acceptVisitor(v);

// in visit(const __Dictionary *p)
Visitable* v = dynamic_cast<Visitable*>(element->getObject()); 
if(v) v->acceptVisitor(v);

// in visit(const __Set *p)
Visitable* v = dynamic_cast<Visitable*>((*it)); 
if(v) v->acceptVisitor(v);

Added `public Visitable` to each of the following classes: __Bool, __Integer, __Float, __Double, __String, __Array, __Dictionary, __Set


class CC_DLL __Bool : public Ref, public Clonable, public Visitable

What I hope to see at WWDC 2014

These are some of the items I hope Apple announces at their big keynote for both the public and developers alike. I’m excited to sit in on some of the sessions this year, and I’m hoping that Apple is doubling down on the developer side of things for both iOS and OSX. The following lists some of the things that I hope to see at WWDC (or at least at the fall event).

  • Apple TV SDK and Store Platforms
  • Apple TV Controller Support
  • Macbook Air 12″ with Detachable Screen that can run iOS as an iPad :]
  • MacMini refresh
  • iCloud Storage Increase on all tiers
  • Improvements to iCloud and Apple’s cloud services
  • Inter-app communication similar to intents and contracts
  • Split Screen functionality for any device (smaller “HUD” app format, 2 at once)
  • AirDrop to and from both OSX and iOS
  • AirPlay to iOS
  • File Store so that any app can open a file from the global store w/permission
  • Improvements to Finder to better support tabbed browsing and power users
  • Apple’s Fitness Accessory and Health SDK
  • Apple Payment Service
  • Pressure sensitive stylus capabilities
  • Major improvements and additions to SpriteKit
  • Xcode C++ Refactoring Support to extract methods and other refactors
  • Support to access iCloud / GameCenter from any platform

Live Keynote Stream:

Thoughts and Predictions from others:

One Game A Month

I’m going to work on finishing projects this year with the help of motivation by joining the community over at (#1GAM). I don’t know if I’ll write anything more than the initial post so I setup a new tumblr just for this event. Assuming things go well, I’ll repost the articles over here.

I hope to finally start blogging again about development in conjunction with attempting to finish developing one project each month. The first will be Star Command assuming we at least submit it this month, and while there will be continued work on it throughout this year, once it’s finally out I plan to devote a bit more time to blogging and #1GAM projects.

I wrote an initial entry and will follow up with one more “overview” or “brainstorm” type post soon.

Background Music Queue with AVPlayer and iOS4

Update 2010-Nov-11: Apple has come out with AVQueuePlayer in iOS 4.1, and presumably most people are going to move to the latest version now that it performs well even on the 3G, but the AVPlayer can still be used to manually create a queuing of items, but if you want all the hard work to be done for you, with only a few limitations, then use AVQueuePlayer instead.

Update 2010-Oct-19: Apple has been updating their documentation slightly, and it has more information now with regard to setting up an asset. This is more specifically for video, but it might help fill in the blanks for just audio. AVPlayer Playback Guide

The iOS 4.0 framework introduced AVPlayer which allows playing a MP3s from the iTunes Music Library in the background without using the iPod music player. This has a couple benefits over the MPMusicPlayerController in that you can add new items, stop, or play music in the background. Also, a custom icon can represent your app in the multitasking menu where the ipod controls are located. If you stick with [MPMusicPlayerController iPodMusicPlayer] you can play music in the background through the ipod player, but once an app goes into the background, it no longer has control over the playback of the music.
Continue reading

How To Securely Manage Your Online Identity

Today we use hundreds of websites on a daily or weekly basis. Many of these require us to create and login with a password. Over time managing all of your identities is a serious challenge. I have decided to give an initial look at the problems and some solutions that you are likely already using to help in this regard.

Problem with Passwords

We’re Human

People use simple passwords that are common words, names, or are patterns of keys such as ‘qwerty’ or ‘asdfasdf’ because they are easier to remember. Many people also use a small number of passwords to login to hundreds of websites that they visit each day. I would also guess that many people use the save password feature with their browser in order to save from typing their passwords in each time they visit a site that requires it. Anyone can walk up to your computer and login to any website for which you have a saved password. So we use simple passwords, and reuse them, and store them in our browsers. How can we prevent ourselves from this inherently insecure behavior?

How secure is your password? Find out.

Continue reading

Random ‘Draw’ With Shuffle

While writing Naval Fight I wanted to have the easy computer opponent select a random position on the board grid every turn.

First I implemented a naive solution creating an array of all possible positions that would be randomly selected from, but after a selection was used it was removed from this array.

validPositions = gridPositions.clone();

// selection
position = validPositions[random() * validPositions.length];

This solution was adequate, but there can always be improvements, and now that I am storing information in a second array, how can it be used more efficiently. Thinking about the problem further it became apparent that this was akin to listening to a playlist on shuffle. Which then gives the light bulb moment that instead of thinking about random selection with no repeats I should just shuffle the array of positions, just like having a deck of cards and removing a card for each turn, noting it’s value (ie: grid position).

After a little searching I came across the Fisher-Yates shuffle algorithm, and found an efficient and unbiased implementation:

// setup
int currentPosition = 0;

for (int i = gridPositions.length; i > 1; i--) {
  // Pick a random element to swap with the i-th element.
  int j = uniform_random() * i;
  // Swap elements.
  int tmp = gridPositions[j];
  gridPositions[j] = items[i-1];
  gridPositions[i-1] = tmp;

// selection on each turn
position = gridPositions[currentPosition++];

This solution generates an array of possible positions which are then shuffled, and finally an index into the array is stored where the next position can be read. Each time the position is accessed from the array this counter increases to always point at the next position. There are other solutions that would work, but this seems to be a good final solution for my problem.


Listen to Your Passion

What’s your passion? What do you find interesting? Funny? Inspiring? There is very likely an audio or video show out there with your specific interests in mind. Podcasts – as they are usually referred to – can be found by searching the iTunes Store, Zune Social, Miro Guide, or just a generic search on your favorite trusty search engine. You can then download and listen (or watch) these shows on your commute, while working out, or traveling. I mostly enjoy learning about my trade – technology and programming – but also enjoy keeping up with sports, news, and the economy, as well as having a good laugh or two. I find it difficult to choose which podcasts to listen to since there’s only so much time in the day, and while it’s great to constantly learn, at some point you have to make your own contribution using the knowledge gained from listening and apply it to your own projects and life.

The following is a list of many of the shows that I tune into. Some I listen to weekly, but most of them I only listen to the episodes that interest me. I find podcasts are especially beneficial for when I’m working out and traveling, and if you have a long commute I’d imagine they’re a lifesaver.
Continue reading

iPad – It’s Just A Big iPhone

The Larger Screen Matters

Ever since watching the first Harry Potter movie, I’ve been fascinated with the idea of having newspapers with video and continually updated content. I have always hoped that someday we would have the same experience, and the iPad is the first gadget to finally succeed in bringing us this reality. After using the iPad for a few months now, I think that it is still a luxury consumption device that could definitely fit between the laptop and phone markets. It really is just a larger iPhone, and with similar resolution to the new iPhone 4 Retina Display ™, but it’s amazing how having a larger form factor makes apps and games so much more enjoyable to use. While it is currently meant for consuming content, the addition of a bluetooth keyboard and more powerful apps will allow for creating content such as drawing, composing music, documents, presentations, and writing screenplays as just a few examples.

While it can replace the Amazon Kindle for most people, I still enjoy reading on the Kindle due to it being very light to hold, and it’s excellent for reading in bright sunlight. But, if you only want to buy or carry around a single reading device, then the iPad is definitely the device to have. It is a great replacement for reading books, magazines, newspapers, and cookbooks. It works very well as a PDF reader, especially with the new support in the Apple’s official e-reader app iBooks. It can also be used for playing games, watching movies, viewing photos, browsing the internet, looking at maps, and much more.

Continue reading