Code Examples
  Home arrow Code Examples arrow Page 5 - 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 - The GameEngine Class


    (Page 5 of 11 )

    The GameEngine Class

    The game event handler functions are actually separated from the game engine itself, even though there is a close tie between them. This is necessary because it is organizationally better to place the game engine in its own C++ class. This class is called GameEngine and is shown in Listing 2.1.


    Note - If you were trying to adhere strictly to object-oriented design principles, you would place the game event handler functions in the GameEngine class as virtual methods to be overridden. However, although that would represent good OOP design, it would also make it a little messier to assemble a game because you would have to derive your own custom game engine class from GameEngine in every game. By using functions for the event handlers, you simplify the coding of games at the expense of breaking an OOP design rule. Such are the trade-offs of game programming.


    Listing 2.1 The GameEngine Class Definition Reveals How the Game Engine Is Designed

    class GameEngine
    {
    protected:
    // Member Variables
    static GameEngine* m_pGameEngine;
    HINSTANCE          m_hInstance;
    HWND               m_hWindow;
    TCHAR              m_szWindowClass[32];
    TCHAR              m_szTitle[32];
    WORD               m_wIcon, m_wSmallIcon;
    int                m_iWidth, m_iHeight;
    int                m_iFrameDelay;
    BOOL               m_bSleep;
    public:
    // Constructor(s)/Destructor
    GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle,
    WORD wIcon, WORD wSmallIcon, int iWidth = 640, int iHeight = 480);
    virtual ~GameEngine();
    // General Methods
    static GameEngine* GetEngine() { return m_pGameEngine; };
    BOOL               Initialize(int iCmdShow);
    LRESULT            HandleEvent(HWND hWindow, UINT msg, WPARAM wParam,
    LPARAM lParam);
    // Accessor Methods
    HINSTANCE GetInstance() { return m_hInstance; };
    HWND      GetWindow() { return m_hWindow; };
    void      SetWindow(HWND hWindow) { m_hWindow = hWindow; };
    LPTSTR    GetTitle() { return m_szTitle; };
    WORD     GetIcon() { return m_wIcon; };
    WORD      GetSmallIcon() { return m_wSmallIcon; };
    int     GetWidth() { return m_iWidth; };
    int     GetHeight() { return m_iHeight; };
    int     GetFrameDelay() { return m_iFrameDelay; };
    void     SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 /
    iFrameRate; };
    BOOL      GetSleep() { return m_bSleep; };
    void      SetSleep(BOOL bSleep) { m_bSleep = bSleep; };
    };

    The GameEngine class definition reveals a subtle variable naming convention that you might or might not be familiar with. This naming convention involves naming member variables of a class with an initial m_ to indicate that they are class members. Additionally, global variables are named with a leading g_ to indicate that they are globals. This convention is useful because it helps you to immediately distinguish between local variables, member variables, and global variables in a program. The member variables for the GameEngine class all take advantage of this naming convention.

    The GameEngine class defines a static pointer to itself, m_pGameEngine, which is used for outside access by a game program. The application instance and main window handles of the game program are stored away in the game engine using the m_hInstance and m_hWindow member variables. The name of the window class and the title of the main game window are stored in the m_szWindowClass and m_szTitle member variables. The numeric IDs of the two program icons for the game are stored in the m_wIcon and m_wSmallIcon members. The width and height of the game screen are stored in the m_iWidth and m_iHeight members. It's important to note that this width and height correspond to the size of the game screen, or play area, not the size of the overall program window, which is larger to accommodate borders, a title bar, menus, and so on. The m_iFrameDelay member variable indicates the amount of time between game cycles in milliseconds. Finally, m_bSleep is a Boolean member variable that indicates whether the game is sleeping (paused).


    Note - If the naming convention of preceding variable names with characters to indicate their variable type is unfamiliar to you, take a quick look at "Unconventional Coding Conventions" in Appendix C. The idea is to use one or two characters to convey the data type of a variable; for example, m_iWidth is a member variable of type integer (i), whereas m_szTitle is a member variable that is a null-terminated string (string with a trailing zero).


    The GameEngine constructor and destructor are defined after the member variables, as you might expect. The constructor is very important because it accepts arguments that dramatically impact the game being created. More specifically, the GameEngine() constructor accepts an instance handle, window classname, title, icon ID, small icon ID, width, and height. Notice that the iWidth and iHeight arguments default to values of 640 and 480, respectively, which is a reasonable minimum size for game screens. The ~GameEngine() destructor doesn't do anything, but it's worth defining in case you need to add some cleanup code to it later.

    I mentioned that the GameEngine class maintains a static pointer to itself. This pointer is accessed from outside the engine using the static GetEngine() method. The Initialize() method is another important general method in the GameEngine class, and its job is to initialize the game program once the engine is created. The HandleEvent() method is responsible for handling standard Windows events within the game engine and is a good example of how the game engine hides the details of generic Windows code from game code.

    The remaining methods in the GameEngine class are accessor methods used to access member variables; these methods are all used to get and set member variables. The one accessor method to pay special attention to is SetFrameRate(), which sets the frame rate or number of cycles per second of the game engine. Because the actual member variable that controls the number of game cycles per second is m_iFrameDelay, which is measured in milliseconds, it's necessary to perform a quick calculation to convert the frame rate in SetFrameRate() to milliseconds.

    The source code for the GameEngine class provides implementations for the methods described in the header that you just saw, as well as the standard WinMain() and WndProc() functions that tie into the game engine. The GameEngine source code also initializes the static game engine pointer, like this:

    GameEngine *GameEngine::m_pGameEngine = NULL;

    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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek