C++ for games – pros and cons

There is a never-ending debate between C++ and Java. However, we will not go into the controversy and will try to look at them from the perspective of game development. C++ has a slight performance edge over Java, and Java is known for its simplicity.

There may be many programmers who are more comfortable in C++ than Java, or vice versa. In game development, personal choice of programming language does not matter. Hence, using NDK or SDK has to be determined depending on the requirements. It is always recommended that you use the Android SDK to develop an application rather than using the NDK.

Let's discuss the advantages and disadvantages of using native language for game programming.

Advantages of using C++

Let's first have a look at the positive side of using C++ for game programming through the following points:

  • Universal game programming language
  • Cross platform portability
  • Faster execution
  • CPU architecture support

Universal game programming language

In the case of game development, C++ is widely used for many platforms, especially for consoles and PC game development. This is the reason many game engines opted for C++ as the primary programming language.

Sometimes, it is difficult to learn many programming languages to work on different platforms with different architecture. C++ provides a most common solution to this problem, as most of the programmers are familiar with C++ library and API use.

Cross-platform portability

The same C++ code is compiled into a library targeting a specific operating platform. Thus, the same project can be compiled for different platforms. Hence, it is super easy to port a game to various platforms if it is written in C++.

For example, the famous and effective cross-platform game engine Cocos2d-x uses C++ as the development language. Hence, the same game is easily ported for many platforms such as Android, iOS, Mac OS, and Windows.

Faster execution

C++ is well capable of interacting with platform hardware, and writing games in C++ helps boost their performance. However, in the case of Android, the performance boost is hardly noticeable if the game is not CPU intensive.

CPU architecture support

C++ code can be compiled for specific target CPU architectures such as x86, ARM, Neon, or MIPS. This specification indicates better performance on that particular processor.

Compiler configuration for CPU architecture in Android NDK ensures the best possible result in each platform. However, it is not always necessary to define each and every platform to avoid extra compilation.

Disadvantages of using C++

Now, let's discuss the other side of the coin through these points:

  • High program complexity
  • Platform-dependent compiler
  • Manual memory management

High program complexity

C++ comes with extra program complexity. In the case of Java programming, JVM takes care of memory completely and follows the OOP concept. C++ lags in providing this feature. Thus, it becomes extra overhead for the developer to take care of every programming aspect.

C++ itself has a complex architecture compared to Java. The chances of facing exceptions and errors increases if C++ is used.

Platform-dependent compiler

Going cross platform is easy when using C++. However, configuring the build script can be a pain in most of the cases. It is a very common scenario that the same game fails to run on a ported platform due to the wrong configuration. Moreover, it becomes difficult to find out the issue as the game is successfully running on some other platform.

Most of the time, different platforms use different C++ compilers. So, it requires an extra effort to identify platform-specific code and find out an alternative for each platform, if required.

Manual memory management

Java does not require memory management to be implemented by the developer, and the memory is efficiently managed by JVM (DVM in the case of Android). So, there is no chance of facing memory leakage or fragmentation. JVM runs the garbage collector to free the unused memory automatically. However, garbage collector invocation costs a bit of performance, and frequent garbage collector calls can cause a severe drop in performance.

The developer should use optimum memory, because the garbage collector cannot identify unused memory block if there is any active reference in the code.

Conclusion

C++ has its own advantages. However, when it comes to game programming for Android, it does not help much in the technical sense. So, if we compare the amount of effort and risk taken by opting for C++ than coding in Java for Android, Java should always be preferred for Android. DVM runs Java code efficiently enough to achieve reasonable performance on Android devices. Moreover, the Android NDK library is not actually designed to develop a standalone Android application. Even though it has native activity support, which acts as a middle layer between DVM and native application written in C++, it does not help much.

If the developer chooses not to go for cross platform and keeps the game scope within Android only, then it is recommended that you use Android SDK rather Android NDK. It will decrease the development hustle and complexity with a negligible amount of performance loss.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.36