Cocos2d-x: We are writing the first cross-platform application
Programming for mobile platforms is becoming increasingly popular. Every day, new applications and games are born, which, of course, increases competition in this market. And each self-respecting developer should develop and maintain his software product on at least two of the most popular platforms, which, in my opinion, are android and iOS . In the recent past, for such support, it was required to develop two different programs in different programming languages. But now, there are a lot of cross-platform engines that help developers save valuable time.
In this article I want to show the ease of use of one of them. Cocos2d-x- a free cross-platform 2D game engine - a port of the popular cocos2d engine for iPhone . The multi-platform coconut supports iOS, Android, WoPhone and Win32 . Win32 support is not full-fledged, it works through OpenGL ES emulation and is used only for developing and debugging applications. The creators of the engine do not plan to implement full-fledged support for Windows, and also claim that not all computers will run OpenGL ES emulation . A little lower, in the article, we will create a simple project that, in the future, can be assembled for the above platforms.
From words to action. C ++ programming language, as an environment, we use Microsoft Visual Studio 2010 . First of all, you need to download the engine itself (At the time of writing, the version of the engine cocos2d-1.0.1-x-0.9.1.zip was used ). Unpack the zip archive into a folder convenient for you (in my case c: \ android \ rep \ cocos2d-1.0.1-x-0.9.1 \ ). The structure of the working directory can be seen below:
- The Box2D and chipmunk subdirectories contain the source files for the corresponding libraries of physical engines;
- In the lua directory , the source code for the lua language support library;
- The cocos2dx folder contains the source code for the main coconut engine;
- In templates, auxiliary files are templates for generating projects for different platforms;
- There are also several test projects here, such as HelloWorld , HelloLua, and Tests .
Before creating our project, execute files the install-msvc.bat-the templates , which will create templates for Visual Studio 2010. Then, open the solution in VS (solyushen) the cocos2d-win32.vc2010.sln . In the list of projects, for further work, we need to build libraries in our solution (solution). If you are too lazy to rebuild each project individually, you can build a test application tests , libraries will be assembled automatically. And finally, create a new project ( Ctrl-Shift-N ). As a template, select Cocos2d-win32 Application , let the name be MyCocosProject , and for simplicity, the project will be in the same folder as the engine itself ( c: \ android \ rep \ cocos2d-1.0.1-x-0.9.1 \), if you choose a different location, it will be necessary to correctly specify the paths to the libraries in the project settings, we leave the solution (solution) the same. Click OK .
In the “wizard” window that appears, you can turn off the physical engines for now, by removing the corresponding checkmarks, press the Finish button . All! We created our first project:
Open the HelloWorldScene.cpp file in the project and look for the line: replace the string “ Hello World ” with any text, for example, “ My First Crossplatform Application ”, change the font size from 64 to 24! We make our project run, and execute it ( F5 ). If the libraries were built successfully, then our application should start.
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Thonburi", 64);
If you managed to run the application on your PC running Windows, then perhaps you took the first step to writing your own multiplatform game, the step that you still have not dared to take. So far, our application is built only under Win32, but we can build it on both the iPhone and Android . In my opinion, the main advantage of this engine is that the developer for debugging does not have to have either “live” mobile devices, their emulators, or compilation tools for these platforms. We write and check everything under Win32, and everything else will be needed only at the stage of final assembly, when the basic algorithms are already implemented. The description of the process of building the project for other platforms is worthy of a separate article, which I will definitely try to write in the near future.
In my opinion, it is logical that not a single cross-platform engine can allow you to use all the unique capabilities of each platform. But the main backbone of the game to implement on such an engine is quite realistic. And I hope that for a certain circle of people, such a simple application will help solve the problem of choice.
- The official website of the engine ;
- Engine page on Twitter ;
- Games and applications using cocos2d-x