Learning to Code

Learning C/C++

Obviously you will need to learn C/C++. A good way is to write programs in the Windows console project, the same way we tested the compiler (see above section). This way you can learn the basics of C and C++. Some online tutorials are listed below, but you should be able to get hold of a good book or do searches on the Internet for other tutorials. Luckily the Internet has plenty of coding examples as well.

Also search for videos as there are plenty of C/C++ video tutorials.

You will need to know a little about C++ classes and templates in order to interpret code examples from DirectX samples 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.

Example Console 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.