Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

9. November 2011



Along with many others, I don’t really recommend C++ as someone’s first language for various reasons.  Sometimes concrete examples aren’t easy to come by off the tip of your tongue, so I figured the next time I encountered one of those things that make C++ so beginner unfriendly, I would post it.  Not the obvious stuff like memory leaks, dealing with the linker or exceptionally cryptic template errors, but the more benign stuff that add up to frustrate new and experienced users.  If you are a veteran C++ coder, you will probably spot the problem in a second or two, but this is the kind of thing that will trip up a beginner completely and is a complete nightmare to solve.



Consider the following C++ header file:


#pragma once #include "SFML/Audio.hpp" class SoundFileCache { public: SoundFileCache(void); ~SoundFileCache(void); const sf::Sound& GetSound(std::string) const; const sf::Music& GetSong(std::string); private: static std::map<std::string, sf::Sound> _sounds; static std::map<std::string, sf::Music> _music; }; class SoundNotFoundExeception : public std::runtime_error { public: SoundNotFoundExeception(std::string const& msg): std::runtime_error(msg) {} }



Pretty straight forward stuff right?  Two class declarations, nothing really funky going on.  Now consider the following implementation:


#include "StdAfx.h" #include "SoundFileCache.h" SoundFileCache::SoundFileCache(void) {} SoundFileCache::~SoundFileCache(void) {} const sf::Sound& SoundFileCache::GetSound(std::string soundName) const { std::map<std::string,sf::Sound>::iterator itr = _sounds.find(soundName); if(itr == _sounds.end()) { sf::SoundBuffer soundBuffer; if(!soundBuffer.LoadFromFile(soundName)) { throw new SoundNotFoundExeception( soundName + " was not found in call to SoundFileCache::GetSound"); } sf::Sound sound; sound.SetBuffer(soundBuffer); _sounds.insert(std::pair<std::string,sf::Sound>(soundName,soundBuffer)); } else { return itr->second; } } const sf::Music& SoundFileCache::GetSong(std::string soundName) { //stub }



Again, pretty standard code, ignore the fact GetSound and GetSong don’t return values, they aren’t the issue here.



Now consider the error:


error C2533: 'SFMLSoundProvider::{ctor}' : constructors not allowed a return type



If you are new to the expression ctor, it basically just shorthand for constructor. For the record, it’s Visual Studio Express 2010 and if you double click that error, it brings you to this line:


SoundFileCache::SoundFileCache(void) {}



So… what’s the problem?  By the error, it is quite obvious that the constructor doesn’t in fact return a value, so the message is clearly not the problem.



What then is the problem?  I’ll show you after this brief message from our sponsors… Smile







Welcome back… figured it out yet?  If not, I don’t blame you, the warning sure as hell didn’t help.  Here then is the offending code:


class SoundNotFoundExeception : public std::runtime_error { public: SoundNotFoundExeception(std::string const& msg): std::runtime_error(msg) {} } <-- Missing semicolon


So, forgetting a semi colon ( something you will do A LOT! ) results in a message that your constructor cannot return a value.  Now, once you’ve been coding C++ for a while this kind of stuff becomes pretty much second nature.  Getting nonsensical error messages?  Check your header for a missing include guard or semi colon become pretty much the norm.  But for a new developer, this is the beginning of a complete train wreck.




Think about if for a second, you just developed some of your first code, the error says you are returning something you aren’t, the compiler is pointing at your constructor and now you need to figure out just WTF is going on..  What do you do?  Many run off to a forum and post the error verbatim and hope for an answer ( which they will probably get, learning nothing in the process ). Hopefully you Google it using exact quotes, but even then you get forum answers like this where you have a bunch of other new developers fixating on the error message itself and leading the developer down a wild goose chase.  Fortunately a moderator stepped in and gave a proper explanation, but that doesn’t always happen.  Even worse, it is a legit error, you really can’t return from a ctor, so if you encounter it again in the future you may actually have a real problem but find yourself instead on a missing semi-colon hunt!




How would this work in Java, C#, Python or Ruby?  Well it wouldn’t, as no other modern language I can think of use header files any more.  For good reason too, they add a level of complexity for very little positive return.  It could be argued that separating interface from implementation is “a good thing”, but even that is munged up by the fact you can actually have you implementation in your header file.  Also don’t get me wrong, other languages have their faults too, just wait till you get forced to go on your first Java XML configured error hunt, you will be begging for C++ errors again!




This is just a classic example of the little things experienced developer forget the pain of experiencing in the first place!  As I said, once you’ve got a bit of experience this kind of stuff becomes second nature, but while you are learning this kind of error is absolutely horrific.  It’s little things like this that add up and make it so hard to recommend new developers start with C++.  When I say it isn’t pointers and memory management that make C++ difficult, I mean it.  It’s crap like this.


7. November 2011



Now that PlayN has been pushed into Maven Central, life and installation just got a heck of a lot easier.  Not that I appreciate their timing… couldn’t you have done this on Friday before I wrote an installer! Winking smile



I will cover two ways to create a PlayN project.



Using Eclipse to create a new PlayN project



Load Eclipse.  If you haven’t already, install the JDK and Maven m2e plugins, which are described in this thread.


Select File->New->Other…






Scroll down and select Maven Project in the Maven folder. Click Next.





Click Next again, if you don’t want to change the Workspace that is.





In the “Filter” type Playn, then select com.googlecode.playn.  Click next.






Now fill in your game details.  There is a small bug in Eclipse in that the Finish button will not be available until you click focus away after filling in the gameName value.  Once you fill that in, click somewhere else on the dialog and the finish button will be enabled.  Click it.






Your game will be created, but you might get an error like I did:  Notice the red x by the testGame-android?





If you look down in the problems section, you will see details off the error:





Fixing this is pretty straight forward.  Right click each one and select, appropriately enough… Quick Fix.  Do that for both errors.





In the following dialog, select “Permanently mark goal…” then Finish.  Another dialog will pop up, simply click OK.  You may ( I did once, and not the second time ), get another error along the lines of your project needs synchronization.  Simply Quick Fix it as well.


Now right click your game ( root class as in “YourGame” not “YourGame-core” ), choose Run As->Maven Install.





Sadly, I am currently getting this error still, with the Flash build being broken:





As a temp work around, open pom.xml from your root project, find Flash in the modules section and remove it.





After removing it, you get that error I mentioned earlier.  Simply right click and select Quick Fix.




When you do, this dialog will appear.  Select all then click Finish.




And you are done.  The process is actually quite a bit easier than it looks, and if it weren’t for the few incompatibilities, it would be flawless.  Regardless, it is a much nicer process than before!


You can now run your project, for example, right click yourproject-core and choose Run As->Java Application.  Scroll down and select your main:




And there is your game running in Java!






All told, the process takes about 3-5 minutes, a very reasonable amount of time.





Using Netbeans to create a new PlayN project


Now we enter the ( in my opinion ) much nicer land of using Netbeans instead of the hateful Eclipse.  As you will see in a second, this process is an absolute joy!


Load up Netbeans.  I am using the 7.1 beta, but other versions should be similar enough.


Select File… New Project:





Select Maven, then Project From Archetype, click Next.





In the New Project dialog, click Add:




Fill in as follows, then click OK




Back in the New Project Dialog, select your newly added archetype and click Next.




Fill the next screen in like follows.  Make sure you fill in a value for newGame and click somewhere in the dialog to make sure it takes.   Then click Finish.




NetBeans will churn away for a minute while it creates your project and voila!





Right click your project Core and choose Run.  In the dialog that pops up, choose your main class ( there is only one ), then Select Main Class.




And voila:





To fully build your project, you need to right click MyProject MetaProject and choose Build:




And a familiar problem arises:





Same solution too.  Under your metaproject locate pom.xml





And remove this line:





Once you edit that line and save, right click->  Build your meta project again.  This time everything should work fine.



Can you tell why I prefer NetBeans over Eclipse any day?


7. November 2011

In one of those all time "my timing sucks" moments, Google's Fred Sauer just announced PlayN 1.0 was just added to the Maven repository, which makes the install process a few orders of magnitude easier to get up and running.


When I get off a tablet and back to a computer, I will document the process... it will be a short document, the process is now a heck of a lot easier.  It's a mix of ahh crap, since I just finished my installer and great news as frankly the process needed to get easier.


6. November 2011



Hmm, kinda realized I missed this part in my recent PlayN installer post.  After you go ahead and create your game, it creates a new project hierarchy and… then what?  This post shows the various ways you can start your game.


Assuming you used my installer to install PlayN, you can run your game from the command line using the following steps.  I will assume your game’s name was “mygame” in these examples and that you installed to c:\playn.




Running the desktop Java app with Maven



cd playn\playn\mygame\core

..\..\..\maven\apache-maven3.0.3\bin\mvn install compile exec:java



Running the HTML target with Maven


cd playn\playn\mygame

..\..\maven\apache-maven3.0.3\bin\mvn package  ( wait, this takes a bit )

..\..\maven\apache-maven3.0.3\bin\mvn –Ptest-html


This will currently through a bunch of errors in your face since the Flash build seems to currently be broken, ignore them.  Once you run that second command, a web server Jetty will be downloaded and installed.


Now open a browser and go to http://localhost:8080 to see your game running in a browser.


When done, you can use CTRL+C in the dos window to get Jetty to stop.


Running the Flash target with Maven


You can’t, it’s currently broken, I’m looking into it and will update with details or a fix if possible.


Running the Android target with Maven


This process is a bit too involved and requires other steps ( like configuring the emulator or a physical device, which we didn’t do during the initial install.  I will cover this in detail in a later post.


There is a problem in that the fact Flash is broken, and before Android in the build order, your Android target will never be built!  This can easily be fixed by opening pom.xml in your game folder and remove the line <module>flash</module>.


Running the Java and HTML targets with Ant


If you have Apache ant installed, you can also use it to run Java and HTML targets. Currently you cannot run Flash or Android this way.


Running desktop:

cd playn\playn\mygame



Running HTML:

cd playn\playn\mygame

ant run-html


Just like with Maven ( but a whole lot quicker ) it runs Jetty and you can reach your website using a browser.  However, unlike the maven install, for some reason the ant install is in a subdirectory, so your URL is http://localhost:8080/mygame.



Of course, if you are using an IDE like Eclipse or NetBeans, they can automate many of these tasks.  If you add Maven’s bin folder to your PATH variable, it will save you a ton of typing.  Of course you could also make a simply script to run your game in a much more terse syntax.


6. November 2011


 EDIT: 11/7/2011 Google has published to Maven Central, which makes this entire process obsolete.  Refer to here for new instructions.  Fortunately the new process is much nicer and faster.

PlayN is a wonder piece of technology with a horrible installation process, which is a shame as I think more people should check it out.  So I decided to do something about it, I wrote an installer!


I have made the process (almost ) as simple as possible.  For much more detailed process or if you encounter a problem more detailed instructions are available here.



First download the tools available here.


Extract the zip file to any folder ( make sure you extract though, do not run from the zip ) and run Install.cmd.


Now it is simply a matter of answering a couple Yes/No questions, and (optionally) point the installer at your JDK folder and telling it where you want PlayN to be installed.  Except the gotcha… read about that below before running the install!



The Gotcha


The only major gotcha is you *must* install the Android 3.0 SDK Platform, and you have to do it in a bit of a hurry, as the SDK install will happen in parallel to the PlayN install which is eventually going to need the Android SDK installed to continue.  I chose to do this because both installs are fairly long and the Android install *should* be completed long before it is needed.  When this screen appears, move quickly! To keep the install quick, click “deselect all” then choose Android 3.0 (API11)->SDK Platform.  You can run the Android SDK Manager again later if you want to add more features.  If for some reason the installer outpaced the Android SDK Manager, simply run the installer again.








If you already have the Android SDK installed, this process *may* hijack your ANDROID_HOME environment variable.  When complete, simply change it back if you wish.



Technical Details

The installer and wizard consist of about 500 lines of Powershell script.  For the record, I have never used Powershell before, so it’s 500 lines of ugly Powershell script! Smile


The entire script is documented, so before running you can see exactly what it is going to do, and if it has a problem or you want to install different versions of Maven, Git or the JDK, you can change those URLs right at the top.


Over the course of execution, the script:

- downloads and configured the JDK [optional]

- downloads 7zip for later use ( from )

- downloads and installs ( local ) Git

- downloads and runs the Android SDK

- Git’s PlayN from googlecode

- Downloads and installs Maven

- Runs Maven on the PlayN code

- Downloads and installed the New Game Wizard from

- Runs the New Game Wizard [optional]




System requirements:


Java runtime.  The java sdk will be installed as part of this process, but the java runtime will not.

Powershell 2.0.  Tested on Windows 7 with Powershell 2.0, should work on Windows Server 2008 but untested. *May* work on Vista, untested.  *Should* work on Windows XP, but you will need to download and install Powershell 2.0 first.  Powershell 2.0 ships with Windows 7, and Powershell 1.0 ships with Vista and may require updating using the link above.


What files are created?


Everything is created under the PlayN installation folder you selected, except a few temp files such as the JDK installer.  These are located in your %TEMP% folder and can be deleted whenever you wanted.


How do I uninstall?


Simply delete the downloaded scripts.  If you want to erase your PlayN install, first stop adb  ( the Android debugger ).  To do so, load the task manager ( CTRL + SHIFT + ESC ), find adb.exe, right click and end process.  Now simply delete the PlayN folder you created as part of the install.


Anything else I need to know?


Yeah, the install won’t have touched your Maven or Git configurations if those are already installed, but unfortunately as part of the process we had to update the ANDROID_HOME environment variable.  So if you already have the Android SDK installed on your machine, you will need to update ANDROID_HOME to point to your installed SDK.  This can be accomplished from a command prompt by typing:

    Setx ANDROID_HOME “c:\your\androidsdk\location”



The new game wizard is installed as part of this process and can be located at [PlayN Folder]\PlayN\NewGame.cmd if you want to create a new game in the future.



If you want to install more Android SDK features after the install is complete, the Android SDK is located at PlayN\Android\SDK Manager.exe.  Yes, there is a space in the name, and no, I don’t know what the hell Google was thinking when they did that…



Running Maven after the fact is a bit of a pain in the ass.  I did not set the environment variable as part of the installed ( in case you already had Maven installed I didn’t want to break anything ) so now you have to fully resolve Maven to run it, such as c:\Playn\maven\apache-maven-3.0.3\bin\mvn.bat  [your command]  if you think that gets old in a hurry, you are correct!  The easiest answer is to add the maven Bin directory to your path, such as:

Setx PATH = “%PATH%;c:\playn\maven\apache-maven-3.0.3\bin”

Be careful with the above syntax as you don’t want to screw up your PATH variable.  Try it with a straight SET first ( no X ), then type “SET PATH” to verify it looks correct.  If you are sure it does, run it again using SETX.  At this point you can run Mvn from any directory.








That’s not really shocking, there are a ton of moving pieces!  Also, I made NO checks to make sure files existed across the network or that your internet connection was up, as both of these would simply be catastrophic failures anyways.  My first suggestion is checking all the URLs at the top of the install script with your browser to make sure they are all still valid and connectivity to each site is fine.  I also have no control over if Google break their build, but if they do, let me know and I can patch the script accordingly.


If you have other problems, post them here, email me ( not preferred because others won’t be able to read the results ) or use the PlayN tag on and I will monitor there as well.


Happy PlayN!


Month List

Popular Comments