Reza Ghobadinic

Installing Cocos2d-x (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.

 

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.

 

Install Cocos2d-x

Now that Visual Studio and Android Studio are installed, it is time to install Cocos2d-x itself:

python --version
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.

 

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

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!

Next

Exit mobile version