Adding Rendering Support

We will not be covering the theory of 3D graphics programming in great detail, although a brief discussion of each major issue is needed. I will just present you with the key concepts, and we’ll take it one step at a time. First, we need to add rendering to the game engine that currently doesn’t know how to render. As you’ll recall from the last chapter, there are currently only four methods in the external game functions:

bool game_preload();
bool game_init();
void game_update();
void game_end();

Although we can do some initialization and updating, there is no way to render anything with this game engine. The best we were able to do last chapter was display a message in a message box.

Advice

The Advanced2D engine is being presented in a step-by-step format as an aid to learning, but as you might imagine, a game engine grows quite large very quickly, and our engine here is no exception. The “follow along and type in the code” style of learning will not survive beyond this chapter because there is too much code—even though the engine is being developed in parallel with each chapter. Instead, in future examples, we will just go over the engine code with the assumption that you have opened the project available on the CD-ROM.


Do you have the Advanced2D project open? Bring up the winmain.cpp file again and scroll down to the bottom, where the while loop is located. This is the primary loop of the game engine. The PeekMessage, TranslateMessage, and DispatchMessage function calls are just part of the Windows “message pump” that is standard in every winmain function. What I want you to take notice of is the call to g_engine->Update() located in the loop:

// main message loop
gameover = false;
while (!gameover)
{
   while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
   {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
   }
   g_engine->Update();
}

Although only one function call is located here, this single function call will give power to the entire game engine. Think of this winmain loop as the heart of the system, and that Update function call is the pulse. Or, from the engineering point of view, winmain is the distributor, and the Update function is the electrical pulses that fire the spark plugs. We will be doing a lot of things within that single function. Now you may close the winmain.cpp file because it is complete, and no changes are needed.

Next, let’s open the Advanced2D.h and Advanced2D.cpp files. You might recall from the previous chapter that these are the main source code files for the engine itself. Open the Advanced2D.h file and add the following line of code noted in bold. This is the next function we’ll add to the engine, giving our client source code file the ability to render something.

//external variables and functions
extern bool gameover;

extern bool game_preload();
extern bool game_init(HWND);
extern void game_update();
extern void game_end();
extern void game_render3d();

This new function must be added to your game’s source code because now the engine expects this function to exist. If you forget to add it to your game, you will get a linker error when you try to build the project. The engine is already capable of rendering 3D objects, and that will be possible as soon as we add the call to game_render3D to the engine’s Update method. Why don’t we just do that right now, while we’re on the subject?

Scroll down in Advanced2D.cpp until you find the Engine::Update method. You’ll be adding a single line of code, a call to game_render3D(), as discussed previously. By adding this function call between the RenderStart and RenderStopfunctions, we effectively give the game the ability to do 3D rendering. Here’s the whole Update method with the new lines highlighted:

void Engine::Update()
{
    static Timer timedUpdate;

    //calculate core framerate
    p_frameCount_core+ +;
    if (p_coreTimer.stopwatch(999)) {
        p_frameRate_core = p_frameCount_core;
        p_frameCount_core = 0;
    }


    //fast update with no timing
    game_update();

    //update with 60fps timing
    if (!timedUpdate.stopwatch(14)) {
        if (!this->getMaximizeProcessor())
        {
            Sleep(1);
        }
    }
    else {
        //calculate real framerate
        p_frameCount_real++;
        if (p_realTimer.stopwatch(999)) {
              p_frameRate_real = p_frameCount_real;
              p_frameCount_real = 0;
        }

        //begin rendering
        this->RenderStart();

        //allow game to render
					game_render3d();

        //done rendering
        this->RenderStop();
   }
}

We will come back to this method again in the next chapter, when we add 2D rendering support to the engine.

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

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