The TestEngine Source Code

We’re just going to jump right into the source code for the library test project, and then I will show you how to configure Dev-C++ and Visual C++ to build with the game engine library. First, create a new Win32 standard executable project using whichever compiler you prefer. Save the project file at the same folder level where you created the Engine folder, so that TestEngine (the name I have used) is in the same root folder as Engine. The reason for this is that we must tell our test project to look “up” one folder into Engine in order to locate the library file. Remember we had the engine project output the lib into Enginelib? That is where we expect it to be located. In other words, you must have compiled the engine already for this test program to work.

Add a new source code file to the project called Main.cpp. This test program is going to be very small! Type the code into Main.cpp using whichever compiler you’ve chosen to use while working through this book. At a certain point, I will stop going over the project creation and configurations and just present source code for study.

#include <iostream>
#include "..EngineAdvanced2D.h"
bool game_preload()
{
    //display engine version in a message box
    g_engine->message(g_engine->getVersionText(), "TEST ENGINE");
    //return fail to terminate the engine
    return false;
}
bool game_init(HWND hwnd) { return 0;}
void game_update() {}
void game_end() {}

See, I told you it was a short one! We’re taking advantage of the Windows API MessageBox() function (wrapped into the engine via the message() method). This function will display a pop-up message box with any text you want to display. Normally the message box is used to report critical errors in the game, but we’ll cheat a bit and use it for output! The alternative is to load up a font, initialize the rendering system, and display text on the program window—which, of course, is ridiculously ambitious at this early stage.

There are four unknown functions in this program:

  • bool game_preload()

  • bool game_init(HWND hwnd)

  • void game_update()

  • void game_end()

These are the first of many such game events that will be called automatically by the game engine at key times during the runtime of the game engine. One such time is at the very beginning of WinMain, when game_preload() is called. This is a very fortuitous time for us to initialize the game’s screen resolution, depth, fullscreen/windowed mode, and other basic settings. It’s important to set these things before the program window is created. Thus, game_preload() is called near the beginning of WinMain.

The game_init() event is called after the program window has been created, Direct3D has been initialized, and the rendering device is available for loading textures and mesh objects and so forth. This event function is where you will load game assets.

The last two event functions—game_update() and game_end()—are self-explanatory. game_update() is called once per frame from the untimed portion of the game loop. There is also a timed portion of the game loop that tries to achieve a stable 60 FPS. We’ll get into rendering in the next chapter.

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

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