All pastes #18439 Raw Edit

Stuff

public cpp v1 · immutable
#18439 ·published 2005-07-25 11:17 UTC
rendered paste body
GameEngine.h========================================================================// Game Event functions prototypesBOOL GameInitialize( HINSTANCE hWindow );void GameStart( HWND hWindow );void GameEnd( );void GameActivate( HWND hWindow );void GameDeactivate( HWND hWindow );void GamePaint( HDC hDC );void GameCycle( );// GameEngine Classclass GameEngine{      protected:      // Define member vars      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      GameEngine( HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth = 640, int iHeight = 480 );            // Destructor      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; };};// ConstructorGameEngine::GameEngine( HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight ){      // Set the member vars      m_pGameEngine = this;      m_hInstance = hInstance;      m_hWindow = NULL;      if( lstrlen( szWindowClass ) > 0 )          lstrcpy( m_szWindowClass, szWindowClass );      if( lstrlen( szTitle ) > 0 )          lstrcpy( m_szTitle, szTitle );      m_wIcon = wIcon;      m_wSmallIcon = wSmallIcon;      m_iWidth = iWidth;      m_iHeight = iHeight;      m_iFrameDelay = 50;      m_bSleep = TRUE;}// DestructorGameEngine::~GameEngine( ){}// WinMain, the function that every windows app needsint WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow ){    MSG             msg;    static int      iTickTrigger = 0;    int             iTickCount;        // Init game        if( GameInitialize( hInstance ) )    {        // Init game engine        if( ! GameEngine::GetEngine( )->Initialize( iCmdShow ) )            return FALSE;                    // Enter main msg loop        while( TRUE )        {               if( PeekMessage( & msg, NULL, 0, 0, PM_REMOVE ) )               {                   // Process message                   if( msg.message == WM_QUIT )                       break;                   TranslateMessage( & msg );                   DispatchMessage( & msg );               }               else               {                   // Make sure the game engine isn't sleeping                   if( ! GameEngine::GetEngine( )->GetSleep( ) )                   {                         // Check the tick count to see if a game cycle has elapsed                         iTickCount = GetTickCount( );                         if( iTickCount > iTickTrigger )                         {                             iTickTrigger = iTickCount + GameEngine::GetEngine( )->GetFrameDelay( );                             GameCycle( );                         }                   }               }        }        return ( int ) msg.wParam;    }        // End game    GameEnd( );        return TRUE;}// WndProc, just route all messages to HandleEventLRESULT CALLBACK WndProc( HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam ){        return GameEngine::GetEngine( )->HandleEvent( hWindow, msg, wParam, lParam );}// Function that creates the main window, etc. ( Normally done in WinMain() )BOOL GameEngine::Initialize( int iCmdShow ){     // Create the windowclass     WNDCLASSEX wndclass;     wndclass.cbSize          = sizeof( wndclass );     wndclass.style           = CS_HREDRAW | CS_VREDRAW;     wndclass.lpfnWndProc     = WndProc;     wndclass.cbClsExtra      = 0;     wndclass.cbWndExtra      = 0;     wndclass.hInstance       = m_hInstance;     wndclass.hIcon           = LoadIcon( m_hInstance, MAKEINTRESOURCE( GetIcon( ) ) );     wndclass.hIconSm         = LoadIcon( m_hInstance, MAKEINTRESOURCE( GetSmallIcon( ) ) );     wndclass.hCursor         = LoadCursor( NULL, IDC_ARROW );     wndclass.hbrBackground   = ( HBRUSH ) ( COLOR_WINDOW + 1 );     wndclass.lpszMenuName    = NULL;     wndclass.lpszClassName   = m_szWindowClass;          // Register the class with windows     if( ! RegisterClassEx( & wndclass ) )         return FALSE;              // Calculate window size and position     int iWindowWidth    = m_iWidth + GetSystemMetrics( SM_CXFIXEDFRAME ) * 2;     int iWindowHeight   = m_iHeight + GetSystemMetrics( SM_CYFIXEDFRAME ) * 2 + GetSystemMetrics( SM_CYCAPTION );     if( wndclass.lpszMenuName != NULL )         iWindowHeight += GetSystemMetrics( SM_CYMENU );     int iXWindowPos     = ( GetSystemMetrics( SM_CXSCREEN ) - iWindowWidth ) / 2;     int iYWindowPos     = ( GetSystemMetrics( SM_CYSCREEN ) - iWindowHeight ) / 2;          // Create the window     m_hWindow = CreateWindow( m_szWindowClass, m_szTitle, WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX, iXWindowPos, iYWindowPos, iWindowWidth, iWindowHeight, NULL, NULL, m_hInstance, NULL );     if( ! m_hWindow )         return false;          // Show & update the window     ShowWindow( m_hWindow, iCmdShow );     UpdateWindow( m_hWindow );          return TRUE;} // Function to handle Windows messages ( normally done in WndProc )LRESULT GameEngine::HandleEvent( HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam ){        // Route Windows messages to game engine functions        switch( msg )        {                case WM_CREATE:                     // Set the window and start the game                     SetWindow( hWindow );                     GameStart( hWindow );                     return 0;                                     case WM_ACTIVATE:                     // Activate/deactivate the game and update sleep status                     if( wParam != WA_INACTIVE )                     {                         GameActivate( hWindow );                         SetSleep( FALSE );                     }                     else                     {                         GameDeactivate( hWindow );                         SetSleep( TRUE );                     }                     return 0;                                     case WM_PAINT:                     HDC           hDC;                     PAINTSTRUCT   ps;                     hDC = BeginPaint( hWindow, & ps );                                          // Paint the game                     GamePaint( hDC );                     EndPaint( hWindow, & ps );                     return 0;                              case WM_DESTROY:                    // End the game, and exit app                    GameEnd( );                    PostQuitMessage( 0 );                    return 0;        }        return DefWindowProc( hWindow, msg, wParam, lParam );}                                   ==============================================================Resource.h==============================================================// Define icons#define IDI_SKELETON    1000#define IDI_SKELETON_SM 1001==============================================================Skeleton.cpp==============================================================// Include the file that includes the headers ^^#include "Skeleton.h"// Init pointerGameEngine* GameEngine::m_pGameEngine = NULL;// Game Engine functionsBOOL GameInitialize( HINSTANCE hInstance ){     // Create the Game Engine     _pGame = new GameEngine( hInstance, TEXT( "Game Skeleton" ), TEXT( "Game Skeleton" ), IDI_SKELETON, IDI_SKELETON_SM );     if( _pGame == NULL )         return FALSE;              // Set the framerate     _pGame->SetFrameRate( 15 );          return TRUE;}void GameStart( HWND hWindow ){     // Seed the random number generator     srand( GetTickCount( ) );}void GameEnd( ){     // Cleanup the game engine     delete _pGame;}void GameActivate( HWND hWindow ){     HDC  hDC;     RECT rect;          // Draw activated in the window     GetClientRect( hWindow, & rect );     hDC = GetDC( hWindow );     DrawText( hDC, TEXT( "Activated!" ), -1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );     ReleaseDC( hWindow, hDC );}void GameDeactivate( HWND hWindow ){     HDC  hDC;     RECT rect;          // Draw deactivated on the screen     GetClientRect( hWindow, & rect );     hDC = GetDC( hWindow );     DrawText( hDC, TEXT( "Activated!" ), -1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );     ReleaseDC( hWindow, hDC );}void GamePaint( HDC hDC ){}void GameCycle( ){     HDC   hDC;     HWND  hWindow = _pGame->GetWindow( );          // Draw the skeleton icon at random positions in the window     hDC = GetDC( hWindow );     DrawIcon( hDC, rand( ) % _pGame->GetWidth( ), rand( ) % _pGame->GetHeight( ), ( HICON ) ( WORD ) GetClassLong( hWindow, GCL_HICON ) );     ReleaseDC( hWindow, hDC );}=========================================================Skeleton.h=========================================================// Make sure its only included once#pragma once// Include headers#include <windows.h>#include "Resource.h"#include "GameEngine.h"#include <stdlib.h>// Global varsGameEngine* _pGame;===========================================================Resource.rc===========================================================// Include resource ID header#include "Resource.h"// Give the location of the iconfiles :)IDI_SKELETON         ICON   "Skeleton.ico"IDI_SKELETON_SM      ICON   "Skeleton_sm.ico"