Welcome to the GameFromScratch.com Cocos2d-x tutorial series. Cocos2d-x is a cross platform, C based port of the popular Cocos2D ObjectiveC game development library. Using the most recent version of Cocos2D-x you can target Windows, Mac, Linux, iOS and Android. Previous versions enabled you to target even more targets including Blackberry and Tizen. The history of Cocos2D-x is actually very important, as it factored in many of the design decisions that the library has taken. This series will walk through the all aspects of using Cocos2d-x to create a game. This particular part covers the process of getting Cocos2d-x installed, creating an initial project and looks at a bare bones Hello World example.
Cocos2D History As I mentioned earlier, the history of Cocos2D is fairly important to understanding how it works, so we are going to start of with a quick, hopefully not-boring, history lesson. I promise you, this will be the only history lesson in this entire series! Unless of course I do more of them If you want a more thorough history, you can always check out the. Ok history time. Way back in 2008, Cocos came to be, named after the town of, Argentina, in case you were wondering where exactly the name came from. It started from a gathering of Python developers in, you guessed it, Los Cocos. As you may be able to guess from the fact it was started by a bunch of Python developers, Cocos started off being written in Python.
Cocos studio 1.6 social advice Mac users interested in Cocos studio 1.6 generally download: Cocos Studio 2.2 Free. Cocos Studio is a game development tool kit based on Cocos2d-x. It breaks down tasks in game development into different roles. More results for 'Cocos studio 1.6'.
Then came along this little phone named the iPhone, and a version of Cocos2D was ported to ObjectiveC for use on iOS, the aptly named cocos2d-iphone. A number of Cocos2d-iphone developed apps started appearing on the iPhone, including StickWars, which hit number 1 on the App Store. Now we hit the fast forward button on history and see that Cocos2d is ported to a number of platforms. One of those ports was of course Cocos2d-x, which was a port of Cocos2D to C, the subject of our tutorial here.
Cocos2d-x itself also spawned a number of ports, including HTML and XNA. Along the way a number of tools were developed as well, including an editor named (itself the spawn of a number of child projects ) and and IDE for Lua and JavaScript scripting in Cocos2d-x. So, why does this all matter? Well, it’s important that you be aware that Cocos2d-x is a port of an Objective-C library which itself was a port of a Python library. Each language and platform has had an effect on the development of Cocos2d-x, for better or worse. You will run into some concepts and think “why the hell did they do this?”. More often than not, it’s Cocos2D’s history that provides the reason.
One final important thing to realize with Cocos2d-x, a number of the most active developers behind the project are not primarily English speakers. Cocos2D-x is extremely popular in China for example. This is by no means a negative, but we aware sometimes language can be a bit of a barrier when looking for help and reading documentation. What Version are you using? At the point I am writing this, I am using Version 3.3beta 0 to create tutorials, and as new versions are released I will try to stay with the most recent version. This is because I am trying to future proof this series as much as possible.
In all honesty, I know this is going to be quite annoying as well, when I created by tutorial series, the number one problem was version changes. Cocos2d-x is a library that get’s refactored quite a bit, so if you are far in the future and some code I provided doesn’t work, this is probably why. Always make sure to read the comments at the bottom of each part, it may contains clues to the problem you are facing. So then, what version should you use? That answer is a bit trickier. You have a choice between Cocos2d 2.x, 3.0 or 3.x right now. The 2.x version is obviously the older version and less actively developed, if at all ( edit – according, 2.x is no longer being supported).
That said, 2.x also supports the most platforms, including Windows Phone, Marmalade, Tizen, Blackberry and more. Additionally, as of writing, every single book targets 2.x. 3.2 is the (currently) stable release of the most current version, while 3.x is the development version. Again, I will be using the most current version as I go, and if history has taught me anything, this is going to lead to tons of issues!;) Warning, Here be dragons! Setting Up Cocos2D-x In order to get started with Cocos2d-x, you need to have a couple things installed already, depending on platform you are developing on. Obviously you need a C compiler. If you are working on Windows, Visual Studio 2013 is currently the recommended version.
You can download a free version named ( note, there is also a version called Visual Studio Express for Windows, you do NOT want this version yeah, brilliant naming by Microsoft there eh? Of course if you have a complete version installed it will work fine as well. You can also use older versions of Visual Studio, back to 2010 I believe, but this series will assume you are using the most recent version.
On Mac OS, is the obvious solution. It’s also free, so that’s nice. As of writing Xcode 6 is currently in late beta, but will work just fine. Xcode 5 should also work just fine. Personally I am not a huge Xcode fan and use for development on Mac, but it is not a free tool. You may see it on occasion in screenshots, so I figured I would put it out there. By default Xcode does not install the command line tools, so I would install those as well, you can find instructions and official documentation.
You also need to have Python installed. Don’t worry, Python isn’t used to code in Cocos2d-x, but some of the tools require it, including the tool you use to create your project, so obviously this install is pretty important. Another important note, so I’m going to use bold and shout at you for a second. YOU NEED TO INSTALL PYTHON 2.7x! The newest version, Python 3.x does not work and you will be wasting your time.
So go ahead download. On Windows you want to make sure Python is added to the PATH environment variable.
If it isn’t, you can get. Finally if you are intending to develop for Android, you need to have a version of the,. You need at least version 9 of the NDK ( 10 is the current as of writing. EDIT – NDK 10 currently doesn’t work! Here for the.
There is a work-around, but using NDK 9 is probably your easiest bet ) to work with Cocos2d-x. Now to make life slightly more complicated, if you are on Windows, the Android NDK also requires or higher to be installed. Fortunately, Cygwin has no further requirements. When downloading the Android SDK, do not download the ADT package, but instead scroll further down the page and install using the “Get the SDK for an existing IDE” link. As an FYI, the SDK is the Java SDK along with the tools needed for Android development, the NDK is the C toolchain for Android development, while Ant is a Java build system. Please note, Cocos2d-x and be used with other IDE’s such as Eclipse or Qt Creator, but I will not be covering the process in this tutorial. Oh and of course you need to!
Simply download and extract the library somewhere on your system. Creating a Cocos2d-x project Ok, now that you’ve got everything installed and configured, it’s time to create a project. Open up a terminal window or command line and change to the directory you extracted cocos2d-x.
MacOS Cocos2d Config and New Project Instructions: Enter:./setup.py source /.profile cocos new -l cpp -p com.gamefromscratch.gamename -d /Documents/Projects/cocos2d gamename Windows Cocos2d Config and New Project Instructions: Open a command prompt and CD to the directory you extracted Cocos2D to. Run the command: python setup.py If you get an error about not being able to find Python, that PATH is not configured correctly. Depending if you have certain environment variables set or not, the install may now ask you the install directory of your Android NDK, SDK as well as Ant, provide them. If you’ve not installed the NDK and SDK before now, do so before performing this step. The next step depends on your operating system version.
If you are running Windows XP ( and possibly Vista ), you now need to restarted your computer for the changes to take effect. If you are running Windows 7 or 8.x, simply close your command prompt and open a new one.
Now type: cocos new -l cpp -p com.gamefromscratch.gamename -d C: path to game here gamename Creating a New Project: The tool to create cocos projects is “cocos” and it resides in cocosSDKfolder/tools/cocos2d-console/bin.l is an L by the way, this is where you specify the language for the project you want to create. The options are cpp and lua currently, in this case we want cpp.p is for specifying the package, mostly for Android I assume. This uses Java’s standard reverse domain name format. Don’t worry if you don’t have a website, make something up.
The -d parameter is the directory where you want to create the project. Now that our project is (hopefully!) created, lets take a look at what it’s created for us. Here you can see it has created a number of key directories for you, we will take a closer look at each one. Each folder prefixed with proj. Is where project files and platform specific code goes, be it android, iOS and Mac, linux, Windows or Windows Metro (.
What was previously known as Metro ). The cocos2d folder however is where the cocos SDK itself is copied. This is a complete copy of Cocos2d, including docs, libraries, headers, etc. Just a warning, this folder is 250MB in size and will be created for each cocos2D project you create using cocos new!
You can set up your projects to use a common install of cocos2d-x, by specifying the engine path when calling cocos new. Just be aware, if you are tight on space and are going to be making a number of cocos2d-x projects, you may want to look into this further. The resources folder is a common repository for all the various assets that your game will use, such as graphics, sound, etc. The Classes folder is perhaps most important of all, this is where your non platform specific code goes! Right now the contents should look like: These code files create a simple application to get you started, although we are ultimately going to replace the contents.
The expression AppDelegate comes from Mac programming, so if you are a Windows or Linux developer, it might be a bit alien to you. An AppDelegate is a helper object that goes with the main window and handles events common to applications such as starting up, minimizing and closing. You won’t really spend much time here, that instead is where the Scene file comes in. We will look at code shortly so each piece will make a bit more sense. Now let’s look at the platform specific portions for both win32 and iosmac.
Win32: iosmac: As you can see, each folder contains all the platform specific code, resources and most importantly, project files for each platform. In the case of iosmac, it further contains platform specific folders for each platform.
All platforms have their own unique entry point ( main, WinMain, etc ) and different ways of handling different things. Most of this is just relevant on start up and cocos2d-x takes care of this for you. However at some point in the future you may need to add platform specific code, such as perhaps an ad network that only works on iOS. This is where you would add platform specific code. That said, 99% of your game logic should be put in the common Classes folder. This make’s it so you can write your code in one platform, then simply open up the project files for another platform and run your game. This is how you are able to handle many platforms with a single code base using cocos2d-x.
Getting Started — MacOS/XCode To get started developing on MacOS, double click the.xcodeproj in the folder proj.iosmac. This should automatically load Xcode for you.
Now at the top bar you should be able to select which project you want, iOS or Mac. As iOS requires the simulator or a device to execute, initially developing on the Mac can be a great deal quicker.
Getting Started — Windows/Visual Studio To get started developing on Windows, double click the.sln file in the folder proj.win32. This will load Visual Studio for you. Simply press Play (Local Windows Debugger) to start the compilation process: Once you’ve selected, simply click the Play icon. Your project will now compile and a few minutes later you should see: If you are new to C, don’t worry, the first compilation is always the nastiest. From now on when you press play, the compilation should be many times faster. You can also run directly from the terminal using the cocos utility, like so: Use -p ios to run iOS. This command requires you to have installed the command line tools mentioned earlier.
Running from the terminal makes it so you don’t have to open the Xcode IDE if you prefer. Hello World Now let’s take a look at the minimal useful cocos2d-x application. While the cocos new created project creates a Hello World application of sorts, it’s a pretty sloppy starting point. First, it has needlessly complications for an app that is supposed to be a minimum example, it’s commented in a manner that only makes sense if you come from an Objective-C background and finally, it even uses deprecated methods. Therefore we are going to look at a cleaner Hello World sample. We simply replace the code in each file with the code I provide below. Don’t worry, all of the functionality we hack out will be covered in future tutorials.
Let’s start with the AppDelegate class. Aren't Singletons bad?
You may notice in the above code that Director is very important to the operation of Cocos2d-x. Director is an implementation of a design pattern known as a, or as some would say, an anti-pattern. If you’ve spent much time on programming forums, you will see after calling Singletons evil. In a nutshell, a singleton is a delayed, but guaranteed to be instantiated global variable in a pretty dress. Sometimes too, a global variable is just what you need, which is why you will find a number of game engines make use of singletons to provide globally available interfaces. At this point the matter is pretty much moot, if you use Cocos2d-x, you use Director, or you don’t use Cocos2d-x.
One of the major source of bugs with Singletons, especially in C, is multithreading. When you have this global instance being access from all kinds of locations and controlling so many things, how do you handle concurrent requests? Well have I got good news for you! You don’t.:) That’s because Cocos2d-x isn’t thread safe. Or more accurately, Cocos2d-x’s memory management ( anything derived from ) and OpenGL rendering aren’t thread safe. To make this clear, you can use threads in a Cocos2d-x application, but you need to be very careful what those threads interact with. Other than the threading problems, some of the biggest problems that come from using Singletons are related to code maintenence, as you are coupling so many systems together.
Cocos Studio For Mac Pro
Fortunately, this is the cocos2d-x team’s problem to deal with, not yours. Unless of course you are on the Cocos2d-x team that is. Now let's take a look at our scene, HelloWorldScene. Aren't we leaking memory like mad?
So you may have noticed we create all kinds of pointers using create calls, but never once called delete. As you can see in the code generated by the CREATEFUNC, we are creating a new instance of our class, but we never do delete it. Aren’t we leaking memory here?
Thankfully the answer is no and the reason is in the call to init and autorelease that the macro made and why it was so important we called our base classes init in our own init method. This is another legacy of Cocos2d-x’s Objective-C roots.
Objective-C provides a form of memory management via ARC,. Basically each time something references an object, it’s count is increased, each time something reference an object goes away, the count is decreased, when the count hits zero, the object is released. In many ways, this is pretty much the same functionality C smart pointers provide, but Cocos2d predates the standardization of smart pointers. There is more to their usage that we will cover later on. For now though, you can safely assume any cocos2d object created with a create function, that inherits from Ref, does not need to be delete. In fact, such objects must not be deleted! In the next part we will take a look at working with graphics in Cocos2d-x.
Don’t worry, it will be much less verbose and much heavier in code!