Code Examples
  Home arrow Code Examples arrow Page 10 - Creating an Engine for Games for Windows
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Creating an Engine for Games for Windows
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 43
    2004-10-13

    Table of Contents:
  • Creating an Engine for Games for Windows
  • What is a Game Engine?
  • Breaking a Game Down into Events
  • Developing a Game Engine
  • The GameEngine Class
  • Source Code for the WinMain Function
  • Initializing Variables
  • HandleEvent Method
  • Put the Engine to Work
  • Resource.h Header File
  • Testing the Finished Product

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating an Engine for Games for Windows - Resource.h Header File


    (Page 10 of 11 )

    Listing 2.9 The Resource.h Header File Declares Identifiers for Icons Used in the Blizzard Example

    //————————————————————————————————-
    // Icons          Range : 1000 - 1999
    //————————————————————————————————-
    #define IDI_BLIZZARD    1000
    #define IDI_BLIZZARD_SM   1001

    Resources are typically numbered in certain ranges to make it easier to distinguish them from each other and to help ensure that they have unique values. As you get into more interesting examples throughout the book, you'll be adding additional types of resources to the Resource.h header file, such as bitmap images and wave sound resources.

    Speaking of resources, the icons in the Blizzard example are listed in the Blizzard.rc resource script, which is shown in Listing 2.10.

    Listing 2.10 The Blizzard.rc Resource Script Lists Icon Resources Used in the Blizzard Example

    #include "Resource.h"
    //————————————————————————————————-
    // Icons
    //————————————————————————————————-
    IDI_BLIZZARD    ICON     "Res\\Blizzard.ico"
    IDI_BLIZZARD_SM  ICON     "Res\\Blizzard_sm.ico"

    The Blizzard.rc resource script is where the resource identifiers in the Resource.h file are linked to actual resource files. In this example, the resource files are icon files, which is evident by their .ICO file extension. When you compile the Blizzard example, the resources listed in the Blizzard.rc resource script are compiled into binary form and are linked into the Blizzard.exe executable.

    Now that you've gotten a look at the helper files that go into the Blizzard example, let's move on to the main source code. The main Blizzard program is fleshed out in the Blizzard.cpp source code file, which is shown in Listing 2.11

    Listing 2.11 The Blizzard.cpp Source Code File Reveals how Straightforward the Program Code for a Minimal Windows Program (Game) Becomes when a Game Engine Is Used

    //————————————————————————————————-
    // Blizzard Application
    // C++ Source - Blizzard.cpp
    //————————————————————————————————-
    //————————————————————————————————-
    // Include Files
    //————————————————————————————————-
    #include "Blizzard.h"
    //————————————————————————————————-
    // Game Engine Functions
    //————————————————————————————————-
    BOOL GameInitialize(HINSTANCE hInstance)
    {
    // Create the game engine
    g_pGame = new GameEngine(hInstance, TEXT("Blizzard"),
    TEXT("Blizzard"), IDI_BLIZZARD, IDI_BLIZZARD_SM);
    if (g_pGame == NULL)
    return FALSE;
    // Set the frame rate
    g_pGame->SetFrameRate(15);
    return TRUE;
    }
    void GameStart(HWND hWindow)
    {
    // Seed the random number generator
    srand(GetTickCount());
    }
    void GameEnd()
    {
    // Cleanup the game engine
    delete g_pGame;
    }
    void GameActivate(HWND hWindow)
    {
    HDC  hDC;
    RECT rect;
    // Draw activation text on the game screen
    GetClientRect(hWindow, &rect);
    hDC = GetDC(hWindow);
    DrawText(hDC, TEXT("Here comes the blizzard!"), -1, &rect,
    DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    ReleaseDC(hWindow, hDC);
    }
    void GameDeactivate(HWND hWindow)
    {
    HDC  hDC;
    RECT rect;
    // Draw deactivation text on the game screen
    GetClientRect(hWindow, &rect);
    hDC = GetDC(hWindow);
    DrawText(hDC, TEXT("The blizzard has passed."), -1, &rect,
    DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    ReleaseDC(hWindow, hDC);
    }
    void GamePaint(HDC hDC)
    {
    }
    void GameCycle()
    {
    HDC  hDC;
    HWND hWindow = g_pGame->GetWindow();
    // Draw the snowflake icon at random positions on the game screen
    hDC = GetDC(hWindow);
    DrawIcon(hDC, rand() % g_pGame->GetWidth(), rand() % g_pGame->GetHeight(),
    (HICON)(WORD)GetClassLong(hWindow, GCL_HICON));
    ReleaseDC(hWindow, hDC);
    }

    The really interesting thing about the code for the Blizzard example is how the only functions present in the code are the game event functions described in GameEngine.h (refer to Listing 2.6). The first of these functions is GameInitialize(), whose responsibility is to get the program started off on the right foot. More specifically, the GameInitialize() function creates a GameEngine object and assigns it to the g_pGame global variable. The GameInitialize() function then sets the frame rate for the game to 15 frames per second, which is a little slower than the default setting of 20 frames per second. This change is primarily to demonstrate how you will often change the default frame rate for games depending on their specific needs.

    The GameStart() function is next, and its job is to initialize game data and start a game. In the case of the Blizzard example, there really isn't any game data, so the only code in GameStart() is code to seed a random number generator. I mentioned earlier that the Blizzard example draws snowflake icons at random positions on the screen. In order to successfully generate random numbers for these positions, you have to seed the random number generator. This is accomplished with a call to the standard C library function, srand().


    Note - Random number generators don't truly generate random numbers. Instead, they generate a pattern of numbers that has the appearance of being random if you make sure that the pattern is selected differently each time a program is run. This is accomplished by seeding the random number generator with an unpredictable value. It turns out that the internal Windows tick count changes so rapidly that if you look at it at any given instant, you will generally get a unique value, which is sufficient to seed the random number generator and get "random" results.


    Similar to the GameStart() function, the GameEnd() function is designed to clean up game data once a game is over. In this case, the GameEnd() function is only required to clean up the game engine.

    The GameActivate() and GameDeactivate() functions are very similar to each other in the Blizzard example. Both are here just to demonstrate how you can respond to game activations and deactivations, and they do so by drawing text on the game screen. For example, the GameActivate() function obtains the client rectangle for the game window and then uses it as the basis for drawing a line of text centered on the game screen. I realize that some of this graphics code probably looks a little strange, but don't worry too much about it because the next chapter gives you the whole scoop on how to draw graphics in Windows. Speaking of strange graphics code, the GamePaint() function is responsible for painting the game screen, but in this case, all the painting takes place in the GameCycle() function, so GamePaint() does nothing.

    The GameCycle() function is the last function in the Blizzard example and is, without a doubt, the most interesting. The job of this function is to draw a snowflake icon at a random location on the game screen. This might not seem like a big deal, but keep in mind that you set the frame rate to 15 frames per second, which means that the GameCycle() function is getting called 15 times every second; this means that 15 icons get drawn in random locations every second! The first step in the GameCycle() function is to obtain a window handle for the main game window; this window handle is important because it allows you to draw on the game screen. The drawing actually takes place when the Win32 DrawIcon() function is called to draw the Blizzard icon. The standard rand() function is called to determine a random location on the game screen, and the icon is extracted from the game window class using the Win32 GetClassLong() function.


    Note - I used a little trick in this example to avoid the task of loading and drawing bitmap images. Because a Windows application's icon is automatically loaded into its window class, I chose to draw the icon, as opposed to loading a bitmap image and drawing it. You learn how to load and draw a bitmap image in Chapter 4, "Drawing Graphical Images," but for now, drawing an icon proved much simpler.


    Although I admittedly threw you a few curves with the graphics code in the Blizzard example, hopefully, you were able to follow along with most of the code. You were also hopefully able to see the benefit of relying on the game engine to take care of a lot of the dirty work associated with Windows game programming.

    SamsThis chapter is from Beginning Game Programming, by Michael Morrison (Sams, ISBN: 0672326590). Check it out at your favorite bookstore today.

    Buy this book now.

    More Code Examples Articles
    More By Sams Publishing


       · I haven't read it yet, but only by the title i love it !when i've read it i'll...
     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek