Reza Ghobadinic

Installing Cocos2d-x (Linux Mint)

Installing Cocos2d-x in Linux Mint (Debian/Ubuntu) is quite similar to Mac and Windows that we have already gone through in the previous posts. If you don’t use linux, just skip this post.

For Linux development there are a few options to consider including Eclipse, QtCreator and more. My favorite is QtCreator which I am going to install in this post.

 

Install QtCreator

You can download the Open Source version of QT and use the editor and compiler to work on your game. We won’t need to actually use the QT libraries itself at all. We just need the editor! It is easy and just a standard install with no hassle.

 

Install CMake

You need to have CMake installed on your machine to be able to create and open the cocos2d-x projects. If you already have CMake installed, make sure the version is bigger than 3.5.1 by checking in terminal.

cmake --version

If the version is older, remove CMake first.

sudo apt remove cmake

Then go to cmake.org and download the latest version (3.16.2) and extract the compressed file in a folder.
In a terminal go to that folder and run ./bootstrap to compile it.

./bootstrap

Then run make to link the files.

make

Now, you can install it by typing:

sudo make install

 

Install Cocos2d-x

Lets install our game engine now to be able to create some projects:

python --version
  • If by any chance you get an error and realize that you don’t have Python installed, just type:
sudo apt-get install python
  • 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. ~/devapps/
  • Setup Cocos2d-x by running the setup.py in a terminal from inside of the unzipped folder e.g. ~/devapps/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:
    • For NDK_ROOT question, put /home/rezagn/Android/Sdk/ndk-bundle
    • For ANDROID_SDK_ROOT question, put /home/rezagn/Android/Sdk/
    • It should automatically find your Ant folder.

This is how the setup ran on my machine:

rezagn@Dell490 ~/devapps/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 : /home/rezagn/devapps/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 : /home/rezagn/devapps

->Check environment variable COCOS_TEMPLATES_ROOT
  ->Search for environment variable COCOS_TEMPLATES_ROOT...
    ->COCOS_TEMPLATES_ROOT is found : /home/rezagn/devapps/cocos2d-x-4.0/templates

->Configuration for Android platform only, you can also skip and manually edit "/home/rezagn/.bashrc"

->Check environment variable NDK_ROOT
  ->Search for environment variable NDK_ROOT...
    ->NDK_ROOT is found : /home/rezagn/Android/Sdk/ndk-bundle

->Check environment variable ANDROID_SDK_ROOT
  ->Search for environment variable ANDROID_SDK_ROOT...
    ->ANDROID_SDK_ROOT is found : /home/rezagn/Android/Sdk/


Please execute command: "source /home/rezagn/.bashrc" to make added system variables take effect

rezagn@Dell490 ~/devapps/cocos2d-x-4.0 $

After going through the steps I mentioned above, I have these lines added to my ~/.bashrc settings file:

# Add environment variable COCOS_CONSOLE_ROOT for cocos2d-x
export COCOS_CONSOLE_ROOT=/home/rezagn/devapps/cocos2d-x-4.0/tools/cocos2d-console/bin
export PATH=$COCOS_CONSOLE_ROOT:$PATH

# Add environment variable COCOS_X_ROOT for cocos2d-x
export COCOS_X_ROOT="/home/rezagn/devapps"
export PATH=$COCOS_X_ROOT:$PATH

# Add environment variable COCOS_TEMPLATES_ROOT for cocos2d-x
export COCOS_TEMPLATES_ROOT=/home/rezagn/devapps/cocos2d-x-4.0/templates
export PATH=$COCOS_TEMPLATES_ROOT:$PATH

# Add environment variable NDK_ROOT for cocos2d-x
export NDK_ROOT="/home/rezagn/Android/Sdk/ndk-bundle"
export PATH=$NDK_ROOT:$PATH

# Add environment variable ANDROID_SDK_ROOT for cocos2d-x
export ANDROID_SDK_ROOT="/home/rezagn/Android/Sdk/"
export PATH=$ANDROID_SDK_ROOT:$PATH
export PATH=$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$PATH

# Add environment variable ANT_ROOT for cocos2d-x
export ANT_ROOT="/usr/share/ant/bin"
export PATH=$ANT_ROOT:$PATH

If you don’t have these, you can add them to your setup file manually. Just replace my username “rezagn” with yours and make sure the paths are correct.

 

Create a new project

To make new projects, we will use the cocos command in the terminal. If you just type cocos and press Enter, you will get the command line options help.

rezagn@Dell490 ~/Projects/games $ cocos

/home/rezagn/devapps/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
rezagn@Dell490 ~/Projects/games $

Create a new project by typing the full command as bellow. Feel free to change the folder as you wish.

cocos new install_test -p com.games.install_test -l cpp -d ~/Projects

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!

 

Compile and run for Linux

You probably noticed that the compile business took ages to complete (17 minutes and 49 seconds for me)! There is a way to make it a few times faster.

Just add the -j flag to your build settings and after -j specify the number of cores that you want to use for compiling. I put -j8 to use all my 8 cores on my ancient Linux machine!

That reduced the compile time to 2 minutes and 11 seconds! Feels much better!

Cool! We have QtCreator and Cocos2d-x installed and working on a Linux Mint now and can start working on our project in Linux too.

By the way, you could also use the QtCreator in both Windows and macOS if you wanted to.

Next

Exit mobile version