Learning to Code

Learning C/C++

Some online tutorials are listed below:

You will need to know a little about C++ classes and templates in order to interpret code examples and some books that use classes. Classes are used to make things into objects, which are quite suitable for gaming objects as they contain properties for each object which can then be changed. (eg: a Jonathan apple is red, juicy, usually small, etc). Pointers and memory management are also reasonably hard topics until you get used to them.

Just to make things complicated, Microsoft also created their own version of C/C++ called C# – you may see some reference to this as well and some sample code for it. Also Apple have Objective C and Swift instead.

To learn these variations on C/C++, some tutorials are below. Apple is making Swift the choice language to use to make iOS and Mac OS apps, so you will need to learn it.

Some Example Apps

Example Console Windows App – Calculator

Do the following tutorial – it will show you how to create a console app. using C++. You can use the console app to practice your C / C++ coding. That way you can complete any tutorials using C and C++ code and learn the languages.

For example, after creating a console app from the tutorial in Visual Community Studio:

I added some C code to the initial C++ code given when the app was created. This shows you that you can learn both C and C++ using the console app – the more you “play” with to add, delete and change the code, the more you will learn.

// Calculator.cpp : This file contains the 'main' function. Program execution begins and ends there.

 include  // header file for C
 include  // header file for C++
 int main()
     printf("Hello World C!\n"); // C code
     std::cout << "Hello World C++!\n"; //C++ code

 // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
 // Debug program: F5 or Debug > Start Debugging menu
 // Tips for Getting Started: 
 //   1. Use the Solution Explorer window to add/manage files
 //   2. Use the Team Explorer window to connect to source control
 //   3. Use the Output window to see build output and other messages
 //   4. Use the Error List window to view errors
 //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
 //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

When I run my program, I get the following:

Learning Windows Programming

Unfortunately once you have learnt the basics of C/C++ programming, you then need to learn how to code for Windows. This means another learning curve. Microsoft Windows expects a program to behave in a certain way. Hence we need to set up our game to be able to “talk” to Windows.

Some books / websites on Windows programming tutorials include:

A Windows program or app is event driven and it will always wait for the user to do something like press the keyboard or click with a mouse – then the app will react or do something. In the background, Windows is sending your program messages all the time. Your program has to handle these messages or return them back to Windows.

First Windows App in C

First create a Windows Desktop Application. I named it as WindowsFirstApp

When your project is created, it will be prepopulated with code. Delete all the code in the WindowsFirstApp.cpp

Then using the file WindowsFirstApp.cpp just copy and paste the code from this file into your black open window in Visual Studio Community.

Build and run the program. It should give you the following window (also click onto the window as well):

The fundamentals of the code is as follows:

  • There is a WinMain() function (similar to main () in C)
  • In it, you create a window class (WNDCLASS wc in the code) to give Windows some information and then you register the window class using RegisterClass() so that windows knows about it.
  • Once registered, create the window. You give the creation function CreateWindow() information about the window’s physical characteristics. Creating a window will return a pointer to the window so that we can reference it later (HWND ghMainWnd which is also called the window’s handle).
  • Create an infinite loop Windows_Message_Loop() so that the program will wait for user input. Windows is event driven. This means that once the program starts, it will be waiting for the user to do something (eg click on something, etc.) For a game, this is what keeps the game waiting until the gamer does something.
  • Note that the loop contains commands to check for messages from Windows. If it doesn’t do this, the program will lock up or even lock up Windows. Windows always uses messages to talk to the program.
  • The loop will break upon a WM_QUIT message (user clicks on the Close button on the X on the top right hand corner of the window or presses the ESC key). This will return control back to Windows using return (msg.wParam) and quit the program.
  • You need to setup a message handler for your program to process messages (eg. to handle a user doubleclicking on something). This is done via a WndProc() function.
  • WM_LBUTTONDOWN message is passed when a user clicks inside the window. In this case, our program displays a message box.
  • WM_KEYDOWN message tests if the user presses the ESC key
  • WM_DESTROY is what happens when the window gets destroyed or closed. In this case, the program tells Windows it wants to quit. In the infinite loop part in WinMain(), the WM_QUIT message will be invoked and the program will exit.
  • DefWindowProc() function will pass any messages not handled back to Windows for processing.

Learning iOS / Mac OS Programming

Like Windows, the Mac OS and also iOS operating systems are even driven – they wait for a user to do something. The code looks a bit different for the Windows code, but similar principles apply.

Some resources for this from Apple is here and also the main Apple documentation is here. A good tutorial on using Xcode is here

Some sample apps are below:

Game Programming

Now that you have learnt a bit about coding in C++ and Swift (or Objective C) and then learnt how to make an app in either Windows or iOS/ Mac, then you have to learn to code your game using tools specific to your game.

The code for games makes what is called a games engine. This means you use certain APIs (Application Programmable Interfaces) which are different for Windows and iOS/Mac.

The APIs are used to display either 2D or 3D graphics to the screen, play music and sound effects, recognise input from your mouse, keyboard, controller or touch screen, display menus, help with physics, AI and networking (for multiplayer games).

Windows / Xbox API for Games

To make games on this platform, Microsoft uses a template called a UWP (Universal Windows Platform) which will work for Windows 10 and the Xbox. The game technology used is called DirectX.

Look through the getting started guide as well as the side menus to look at this technology in detail. Microsoft has written a DirectX12 Tool Kit which has code you can use to help create a game.

I will use this to make my games.

iOS / Mac API for Games

To make games on this platform, Apple uses a variety of technology such as Metal (this is similar to DirectX), SpriteKit for 2D games and SceneKit for 3D effects in games as well as other kits to help with making the game.

I will concentrate on SpriteKit for 2D games, and use Metal for 3D games.

Look through the game technologies page in detail to look through them.

Other Game Engines

Note that some game developers may opt to use some other Games Engines such as Unity (more games engines are in the Resources page). These are alternatives to the above as they may make learning how to make games a little easier as much code has already been written. You use the APIs of those game engines to make your games.