- Install Microsoft Visual Studio
- Install CMake
- Install Cocos2d-x 4.0
- Create a new project
- Compile and run for Windows
We will install Microsoft Visual Studio to compile for Windows!
Install Microsoft Visual Studio
Microsoft has a free version of its IDE called “Visual Studio Community”. There is also a free open source version called “Visual Studio Code”. I am gonna go with the Community because that is what is supported by Cocos2d-x.
- Download and Run Visual Studio Community installer.
- From the Available Tab install Visual Sudio 2017 (You can also go for 2019!)
- From the Workloads tab, I recommend selecting “Python development”, “Game development with C++” and “Data storage and processing” units. You will probably need these down the road.
- Press Install (or Modify) and follow the instructions if any.
Install CMake
Cocos2d-x 4.0 uses CMake to create project files for different platforms. Because of that we need CMake installed on our machine.
- Download and run the CMake installer
- Choose CMake to be added to your PATH during the installation
Install Cocos2d-x
Now that Visual Studio and Android Studio are installed, it is time to install Cocos2d-x itself:
- Installing Cocos2d-x needs Python 2.7x installed. Download and install Python version 2.7.15 if you haven’t yet.
- After installing Python, open a powershell (or command line) window and check the python version by typing this command:
python --version
- Download Cocos2d-x (cocos2d-x-4.0.zip) from www.cocos2d-x.org and unzip it in a folder that you want it to reside e.g. C:\Dev\
- Setup Cocos2d-x by running the setup.py in a power shell from inside of the unzipped folder e.g. C:\Dev\cocos2d-x-4.0\
python setup.py
- It sets up some environment variables first as below and then asks for some paths.
- COCOS_CONSOLE_ROOT
- COCOS_X_ROOT
- COCOS_TEMPLATES_ROOT
- If you have Android Studio and Apache Ant installed in your system (in my case Ant is installed in Dev folder):
- For NDK_ROOT question, put C:\Users\rezag\AppData\Local\Android\sdk\ndk-bundle
- For ANDROID_SDK_ROOT question, put C:\Users\rezag\AppData\Local\Android\sdk
- For ANT_ROOT question, put C:\Dev\apache-ant-1.10.7\bin
PS C:\Dev\cocos2d-x-4.0> python .\setup.py Setting up cocos2d-x... ->Check environment variable COCOS_CONSOLE_ROOT ->Search for environment variable COCOS_CONSOLE_ROOT... ->COCOS_CONSOLE_ROOT is found : C:\Dev\cocos2d-x-4.0\tools\cocos2d-console\bin ->Check environment variable COCOS_X_ROOT ->Search for environment variable COCOS_X_ROOT... ->COCOS_X_ROOT is found : C:\Dev ->Check environment variable COCOS_TEMPLATES_ROOT ->Search for environment variable COCOS_TEMPLATES_ROOT... ->COCOS_TEMPLATES_ROOT is found : C:\Dev\cocos2d-x-4.0\templates ->Configuration for Android platform only, you can also skip and manually edit your environment variables ->Check environment variable NDK_ROOT ->Search for environment variable NDK_ROOT... ->NDK_ROOT is found : C:\Users\rezag\AppData\Local\Android\sdk\ndk-bundle ->Check environment variable ANDROID_SDK_ROOT ->Search for environment variable ANDROID_SDK_ROOT... ->ANDROID_SDK_ROOT is found : C:\Users\rezag\AppData\Local\Android\sdk Please restart the terminal or restart computer to make added system variables take effect PS C:\Dev\cocos2d-x-4.0>
- You can check or edit your variables in the System Properties Dialog by clicking on the Environment Variables button.
- If you don’t have these variables, add them manually. Just replace my username “rezag” with yours in the paths and make sure they are correct.
- Select Path from the User Variables list and press on edit button and make sure the cocos2d-x folders are in there. If not, manually add them as below.
- You may need to restart your computer to have these variables take effect!
Create a new project
To make new projects, we will use the cocos command in the powershell. If you just type cocos and press Enter, you will get the command line options help.
D:\projects\games> cocos C:\Dev\cocos2d-x-4.0\tools\cocos2d-console\bin\/cocos.py 2.3 - cocos console: A command line tool for Cocos2d-x. Available commands: run Compiles, deploy and run project on the target. luacompile Encrypt and/or compile lua files. deploy Compile and deploy a project to a device/simulator. compile Compile projects to binary. gen-simulator Generate Cocos Simulator. new Creates a new project. jscompile Compile and/or compress js files. Available arguments: -h, --help Show this help information. -v, --version Show the version of this command tool. --ol ['en', 'zh', 'zh_tr'] Specify the language of output messages. --agreement ['y', 'n'] Skip the agreement with specified value. Example: cocos new --help cocos run --help D:\projects\games>
Create a new project by typing the full command as bellow. Feel free to change the folder as you wish.
PS D:\projects\games> cocos new install_test -p com.games.install_test -l cpp -d .
This will make a project called “install_test” with all the necessary files and sub folders of the Cocos2d-x project in your specified folder!
Since the new release of Cocos2d-x v4.0, CMake is used to create projects and compile under different platforms. That means that the Cocos2d-x projects won’t come with a Visual Studio project file like in the earlier versions. We need to make one manually.
First create a folder that holds the windows build files and go into that folder:
cd install_test mkdir build_win && cd build_win
Then for Visual Studio 2017 run:
cmake .. -G"Visual Studio 15 2017" -Tv141
Or for Visual Studio 2019 run:
cmake .. -G"Visual Studio 16 2019" -A Win32
Compile and run for Windows
- Run Visual Studio.
- In Visual Studio open the solution (install_test.sln) file from “build_win” folder.
- First build the “ALL_BUILD” project.
- Then right click on “install_test” project and set it as the default project.
- From the tab press on the Local Windows Debugger button and wait for the compile finish and it will run the project.
The resolution will be small because it is not running on any device or device profile. You can change the resolution, but that is for another time!