15.5 Games and the Message Loop


15.5 Games and the Message Loop

We have thus far defined an application class. Its main function is called at application startup to initialize the fundamental game classes ClanLib is to use, such as the core display and the display window. However, the main function as it stands will not last long; the program will begin, create game objects, then create the window. But the window will appear and vanish almost in a flash, and the game terminates because the main function ends as soon as the window is created. There is nothing to prevent the program from terminating.

In short, the program needs a lifespan. The program terminates as the end of the main function is reached; to continue its existence beyond a few milliseconds, it needs some code to sustain it. It needs code that allows the main function to exit only under certain prescribed conditions, such as when the user presses Quit to exit, or presses the Escape key. Ultimately, a While loop is needed, and this is designed to repeat the application, continually updating the display on each iteration until the user decides to exit. This kind of loop is called the game loop or message loop, and each iteration of the loop is a frame. On each iteration, the display will continually be updated to make sure the game is always drawing images to the display. Consider the following code:

      //Main App Class      class cClanLibApp : public CL_ClanApplication {      public:         cClanLibApp();         ~cClanLibApp();         virtual int main(int, char **)         {            CL_SetupCore setup_core;            CL_SetupDisplay setup_display;            CL_SetupGL setup_gl;            CL_SetupSound setup_sound;            CL_DisplayWindow window("Test", 640, 480);            //Enter game loop            while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))            {               CL_Display::clear(CL_Color(0, 0, 0, 255));               UpdateFrames();               CL_Display::flip();               CL_System::sleep(10);               CL_System::keep_alive();            }            return 0;         }      }app; 

The game loop code is especially important. The While loop sustains the application, and code within its scope (between the braces {}) will repeat until the user presses the Escape key. The process of reading input is detailed in Chapter 16. Each iteration of the loop is a frame of the application. On each frame the window must be refreshed, redrawn, and shown to the display. This ensures that the application is drawing itself properly to the user, showing all the latest graphics and letting the user know that nothing has corrupted the display. Within the loop, several methods are continually called. First, the screen is cleared using the clear method of class CL_Display. This method simply clears the screen and fills it with a single color, providing a blank canvas ready to be drawn upon. Afterward, an UpdateFrames() function is called. Thus far this function has not been defined or shown, and for now it can be assumed that nothing occurs within it. This function, explained further in Chapter 16, will be an application-defined function where graphics are drawn to the display and other game related data can be updated on each frame.

The method declaration and argument list for the clear function of class CL_Display follow.

      void clear(const CL_Color& color = CL_Color(0, 0, 0)); 
  • const CL_Color& color

    Color to fill the screen with. This is specified as a CL_Color class. This class works by accepting three color values-R (red), G (green), and B (blue)-each of which can range from 0 to 255 and together represent the mix of components that make the color. So RGB (255,0,0) is red, RGB (0,255,0) is green, RGB (0,0,255) is blue, and so on.




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net