这个2d引擎只是一个引擎雏形,以后还将进行扩展,甚至扩充为一个3d引擎,让我们拭目以待吧~!
<script src="http://www.best4c.com/js/pasteblog.js" type=text/javascript></script> <script type=text/javascript> file="/Best4cUserFiles/20080712/17143_1215848141812";showImage();</script>代码清单:
//----------------------------------------------------------------- // GameEngine.h //----------------------------------------------------------------- #pragma once //----------------------------------------------------------------- // 包含文件 //----------------------------------------------------------------- #include <windows.h> //----------------------------------------------------------------- // Windows 函数声明 //----------------------------------------------------------------- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow); LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); //----------------------------------------------------------------- // 游戏引擎函数声明 //----------------------------------------------------------------- BOOL GameInitialize(HINSTANCE hInstance); void GameStart(HWND hWindow); void GameEnd(); void GameActivate(HWND hWindow); void GameDeactivate(HWND hWindow); void GamePaint(HDC hDC); void GameCycle(); //----------------------------------------------------------------- // GameEngine类 //----------------------------------------------------------------- class GameEngine { protected: // 成员变量 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(); // 常规方法 static GameEngine* GetEngine() { return m_pGameEngine; }; BOOL Initialize(int iCmdShow); LRESULT HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam); // 访问方法 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; }; }; //----------------------------------------------------------------- // GameEngine.cpp //----------------------------------------------------------------- //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include "GameEngine.h" //----------------------------------------------------------------- // 静态变量初始化 //----------------------------------------------------------------- GameEngine *GameEngine::m_pGameEngine = NULL; //----------------------------------------------------------------- // Windows 函数 //----------------------------------------------------------------- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; static int iTickTrigger = 0; int iTickCount; if (GameInitialize(hInstance)) { // 初始化游戏引擎 if (!GameEngine::GetEngine()->Initialize(iCmdShow)) return FALSE; // 进入主消息循环 while (TRUE) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // 处理消息 if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { // 确保游戏引擎没有休眠 if (!GameEngine::GetEngine()->GetSleep()) { // 检查滴答计数,查看是否过了一个周期 iTickCount = GetTickCount(); if (iTickCount > iTickTrigger) { iTickTrigger = iTickCount + GameEngine::GetEngine()->GetFrameDelay(); //游戏循环 GameCycle(); } } } } return (int)msg.wParam; } // 游戏结束 GameEnd(); return TRUE; } LRESULT CALLBACK WndProc(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam) { // 将所有Windows消息传递给游戏引擎 return GameEngine::GetEngine()->HandleEvent(hWindow, msg, wParam, lParam); } //----------------------------------------------------------------- // GameEngine Constructor(s)/Destructor //----------------------------------------------------------------- GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass, LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight) { // Set the member variables for the game engine 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; // 默认为20帧/秒 m_bSleep = TRUE; } GameEngine::~GameEngine() { } //----------------------------------------------------------------- //游戏引擎常规方法 //----------------------------------------------------------------- BOOL GameEngine::Initialize(int iCmdShow) { 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())); //MAKEINTRESOURCE()这是一个把数字类型转换为指针类型的宏 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; // 注册窗口类 if (!RegisterClassEx(&wndclass)) return FALSE; // 根据游戏大小计算窗口大小和位置 int iWindowWidth = m_iWidth + GetSystemMetrics(SM_CXFIXEDFRAME) * 2, 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, iYWindowPos = (GetSystemMetrics(SM_CYSCREEN) - iWindowHeight) / 2; // 创建窗口 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; // 显示和更新窗口 ShowWindow(m_hWindow, iCmdShow); UpdateWindow(m_hWindow); return TRUE; } LRESULT GameEngine::HandleEvent(HWND hWindow, UINT msg, WPARAM wParam, LPARAM lParam) { // 将Windows消息传递给游戏引擎成员函数 switch (msg) { case WM_CREATE: // Set the game window and start the game SetWindow(hWindow); GameStart(hWindow); return 0; case WM_SETFOCUS: // Activate the game and update the Sleep status GameActivate(hWindow); SetSleep(FALSE); return 0; case WM_KILLFOCUS: // Deactivate the game and update the Sleep status 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 the application GameEnd(); PostQuitMessage(0); return 0; } return DefWindowProc(hWindow, msg, wParam, lParam); } //----------------------------------------------------------------- // Blizzard.h //----------------------------------------------------------------- #pragma once //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include <windows.h> #include "Resource.h" #include "GameEngine.h" //----------------------------------------------------------------- // Global Variables //----------------------------------------------------------------- GameEngine* g_pGame; //----------------------------------------------------------------- // Resource.h //----------------------------------------------------------------- //----------------------------------------------------------------- // Icons Range : 1000 - 1999 //----------------------------------------------------------------- #define IDI_BLIZZARD 1000 #define IDI_BLIZZARD_SM 1001 //----------------------------------------------------------------- // 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); }
//----------------------------------------------------------------- // Blizzard Resources // RC Source - Blizzard.rc //----------------------------------------------------------------- //----------------------------------------------------------------- // Include Files //----------------------------------------------------------------- #include "Resource.h" //----------------------------------------------------------------- // Icons //----------------------------------------------------------------- IDI_BLIZZARD ICON "Res//Blizzard.ico" IDI_BLIZZARD_SM ICON "Res//Blizzard_sm.ico"