Why C++ and Not ?

People have asked me this question many times over the years when I say I like to develop in C++. Usually followed by a statement about a single advantage for their language of choice.

The advantage is usually about easier/quicker to code, don’t have to worry about memory management/exceptions, or it uses some form of coding style they think advantageous.

My reasons for liking/using c++ are the following:

1) Familiarity

I originally learnt C++ back in TAFE and University (2003) and while I have worked with many languages over the years, many of them have been derivatives of C or very similar in many ways or syntax (C#, Objective C, Java, Javascript).

2) Flexibility

Many programming languages will force you to use either Procedural, Object Orientated or Functional programming paradigms and while they will have some support for another, they usually don’t support  all three.

C++ on the other hand is built on top of C and maintains the support for procedural programming from there while also adding in Object Oriented programming. Modern C++ has also added in many Functional programming features and many other things making it a very flexible language.

3) Functionality

C++ and C are have been around for a long time and are still a basis for many libraries to perform many different functionalities. While these libraries are available to other languages, its ofter just a wrapper around the original.

4) Fast

C++ is fairly low level and is often a very performant language. Compilers for c++ have been around for a long time too and many optimisations have been made to speed up resulting code.

5) F IDE Support?

Again since C++ has been around for a long time, there are many IDE’s that are fully featured. And while an IDE is not required, it can help greatly especially when coding against an unknown library or in an unfamiliar new language like <Language of the Month>

That’s pretty much it, and as much as I wouldn’t mind trying out some of the newer languages, I don’t have the time or patience these days to learn them without the help of a good IDE.

Long time no blog?

It’s been a long time since the last update and I thought I would just give a quick status update.

Currently I am working on a framework I call NinjaFramework made up of many small libraries with very specific purposes that can be used individually or together.

While not specifically for game development I do plan to use them together as part of a game engine to help me develop games.

Progress is slow as I find myself without much spare time to code these days but I will continue to work on it when I can.

The framework can be found here: https://github.com/nbollom/NinjaFramework

Python vs Speed

So after a few days playing around with python I noticed a few interesting things:

1) PyPy is not always the fastest!

PyPy is an version of Python with a JIT compiler that is supposed to speed-up python code dramatically. It especially is good at optimising loops but has trouble with python modules that are not pure python, but have C/C++ backend libs.

Unfortunately in my case both PyGLET and PyMunk are backed by C/C++ and the event loop of my game is handled by PyGLET and calls events on the python code.

This causes PyPy to not perform as well as CPython in my case.

2) CPython performance is weird???

With my very simple early prototype I am getting weirdly inconsistent frame-rates between runs of the exact same code.

While I usually get a nice smooth 60fps, every now and then it will only run at 20fps for no apparent reason.

This raises the possibility that the finished game will not perform as well as I would like and makes me consider dropping python in general.

Of course that has its own set of problems, while the chipmunk code can be fairly easily ported to c++, changing the windowing/graphics library to something like SDL will be a bit of a pain.

Compiling for Windows & destroying motivation

A few days ago I tried to set-up a Windows 7 build vm and compile SpaceShooter in VisualStudio.

This resulted in a massive drop in my motivation due to a real depressing discovery: the Visual Studio compiler has horribly obsolete support for C.

Microsoft Visual Studio and (lack of) C standards

I set up the Visual Studio project and tried to compile and what awaited me was about 50 errors, way more then I would expect for a simple program.

Worse, the errors were cryptic and didn’t really explain the problem.

After searching the net for what these errors mean, I found the problem wasn’t really the code, it was the compiler.

It turns out that the Visual Studio doesn’t support either the C11 or C99 standards leaving it left with only c90 support.

Now the C90 standard is 24 years old (25 if you count the fact that it was actually developed in 1989) and quite a few things have been changed and added since then.

Microsoft also have stated that they will not add support for C99 (and C11) as they consider C to be a dead language.

What does that mean for me?

What it meant was that if I planned on compiling with Visual Studio I had to adapt my code to support the C90 standard.

For me it meant a few things, firstly all instances where I was assigning values to structs I had to do it property by property instead of using a quicker syntax.

For example:

pos = (struct Point){5,6};

had to be changed to:

pos.x = 5;
pos.y = 6;

This might not seem to be a big issue, but when you have a struct with 8 properties, 3 of which are structs with 2 properties that ends up being 11 lines of code instead of 1.

On the other hand having the properties set individually makes it easier when adding new properties and maintenance so it was probably a good idea to fix that anyway.

The other problem was that the C90 standard requires all variables to be defined at the top of the scope. This issue I have a problem with, I believe code is more readable/understandable when they are declared close to where they are used.

There was one last minor issue, the round() function that I was calling doesn’t exist in the C90 standard (it was added in the C99 standard), but it was easily fixed with a quick self-defined version of the function:

double round(double val) {
    return floor(val + 0.5);
}

 Compiling without Visual Studio

After I changed all the code to support C90, I then decided to not support C90 (yes a bit late I know). That meant finding an alternative compiler.

I decided to set an a MinGW environment and use gcc (a proper compiler) and a Makefile to build the Windows version.

But gcc didn’t like to compile because the version of the library I had was compiled under Visual Studio.

I could have easily gotten the correct version but because the versions available for gcc on MinGW but since the version number of the available binaries was lower then the one I was using on Mac OSX (5.0 and 5.1 respectively) and there were some slight differences, I chose to try and compile it from the source.

Again I was stopped, this time it was the DirectX SDK that wouldn’t install, and the error was saying that the Microsoft Visual C++ Redistributable was already installed.

Two issues here:

  1. It wasn’t installed
  2. So what if it was? Why does the installer for the DirectX SDK “need” to install the C++ Redistributable itself, if its already installed can’t it detect that and skip that step rather then dying???

Motivation destroyed

And so that was it, I had had enough of Microsoft and their motivation sucking stupidity. It had made me so frustrated that it stopped my progress.

I now have continued work on my game and will forget about a Windows version.

If I have enough time at the end of the month I might try again but for now the very though of trying again just makes me annoyed.

Game 1 – Space Shooter – The beginning

Setup

After signing up for the #1GAM the next thing was to decide on the three L’s: Language,  Library, Location (IDE).

I have been playing around with C/C++ for a while and decided that C would be a nice low-level language to work with, and a bit of a break from OO languages which I personally think over-complicates things sometimes when there is no need to. Also has the advantage that I could make it a lot more cross-platform rather then using something like Objective-C.

Location is also an easy choice, on OS X I haven’t really found an IDE that compares to XCode, not that I think XCode is the best IDE that I have used either, but for a native IDE it seems to be the best.

Now for the library.

Interestingly the inspiration for this seem to come out of nowhere. I was looking through the MacPorts devel category and I saw Allegro. I had briefly looked at Allegro probably around 6 years ago, but at the time I was using c#.net and I didn’t want to leave that language, and ended up using SDL.net which I think put me off SDL in general (or maybe it was the bad software rendering it did by default).

I played around with it and without too much hassle I had it installed and a test project up and running.

Choosing a game

So now I had my three L’s I just had to start coding… right?

Well, without an idea of sorts to base the game around there really is nothing I could start coding for.

What sort of a game would it be?

Side-scroller? Top-down? Action? Adventure? Sports? RPG? Platformer? First-person? Third-person? The options are endless.

The optional theme for #1GAM this month is money, but not money as in in-game money, money as in making money for the game. That doesn’t really help for designing the game and I don’t think I am ready to start monetising my games yet, that can wait until I get better at making games and building more worthwhile games.

I spaced out over this for a while, and then I realised, Space, a space shooter.

Its an easy choice for a 1st game working with a new framework, no need to find/build a level editor or handcraft levels instead, I could easily generate the maps programmatically very easily, after all, it’s just stars and planets randomly placed on-top of a black background.

If I have enough time I might add in-game resources and purchasable upgrades, but thats as close to the theme as I will get.

Progressing

Working with Allegro has been great and I have found it fairly easy so far. The only complaint I have is that there isn’t a docset for it in dash (an API browser that I quite like) and I have to use the online version, which can be a hassle since I do a lot of my coding on the train with little to no internet connection.

So far I have made an engine that loads all the required resources, builds the map, has a working and optimised render loop (only renders visible sprites), and has keyboard interaction that uses the dpad to scroll the viewport around the map.

Here is a screenshot of the current work-in-progress:

Space Shooter early days
Space Shooter early days

Next on the todo list is to add a space ship, bind the viewport to it, and work on the movement of the ship using a impulse/inertia control system.