-
# pragma once
-
//-----------------------------------------
//包含文件
//-----------------------------------------
# include
-
//-----------------------------------------
//window 函数声明
//-----------------------------------------
-
int WINAPI WinMain(HINSTANCE hInsce, HINSTANCE hPeInsce,
PSTR szCmdLine, int icmdShow);
-
-
-
//-------------------------------------------
//引擎类
//-------------------------------------------
-
class GameEngine
{
public:
//构造函数
GameEngine(HINSTANCE hInsce, LPSTR szwndClass, LPSTR szTle,
WORD wIcon, WORD wSmlIcon, int iWidth = 800, int Height = 600);
-
virtual ~GameEngine();
static GameEngine* GetEngine() {return m_pGameEngine; };
BOOL Init(int icmdShow);
LRESULT HandleEvent(HWND hwnd, UINT msg, WPARAM wp,
LPARAM lp);
-
//访问方法
HINSTANCE GetInstance() {return m_hInsce; };
HWND GetWnd() {return m_hwnd; };
void SetWnd(HWND hwnd) {m_hwnd = hwnd; };
LPSTR GetTitle() {return m_szTle;};
WORD GetIcon() {return m_wIcon; };
WORD GetSmallIcon() {return m_smlIcon; };
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; };
-
-
protected:
static GameEngine* m_pGameEngine;
HINSTANCE m_hInsce;
HWND m_hwnd;
TCHAR m_szwndClass[32];
TCHAR m_szTle[32];
WORD m_wIcon, m_smlIcon;
int m_iWidth, m_iHeight;
int m_iFrameDelay;
BOOL m_bSleep;
-
};
-
-
-
-
-
# include "GameEngine.h"
# include "resource.h"
-
//----------------------------------
//静态变量初始化
//-----------------------------------
-
GameEngine* GameEngine::m_pGameEngine = NULL;
-
GameEngine* g_pGame = NULL;
-
//------------------------------------------
//游戏引擎函数声明 和定义
//-------------------------------------------
-
BOOL GameInit(HINSTANCE hInsce)
{
g_pGame = new GameEngine(hInsce, TEXT("billzzard"), TEXT("billzzard"), IDI_BLIZZARD, IDI_BLIZZARD_SM);
if(NULL == g_pGame)
return FALSE;
g_pGame -> SetFrameRate(15);
return TRUE;
};
void GameStart(HWND hwnd)
{//生成随机数
srand(GetTickCount());
};
void GameEnd()
{
//清理游戏引擎
delete g_pGame;
};
void GameActivate(HWND hwnd)
{
HDC hdc;
RECT rct;
-
//绘制文本
GetClientRect(hwnd, &rct);
hdc = GetDC(hwnd);
DrawText(hdc, TEXT("here the blizzard!"), -1 , &rct,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hwnd, hdc);
};
-
void GameDeactive(HWND hwnd)
{
HDC hdc;
RECT rct;
//绘制文本
GetClientRect(hwnd, &rct);
hdc = GetDC(hwnd);
DrawText(hdc, TEXT("here the blizzard is passed!"), -1 , &rct,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
ReleaseDC(hwnd, hdc);
};
-
void GamePaint(HDC hdc)
{
-
};
-
void GameCycle()
{
HDC hdc;
HWND hwnd = g_pGame -> GetWnd();
-
//随机出现雪花
hdc = GetDC(hwnd);
DrawIcon(hdc, rand() % g_pGame -> GetWidth(), rand() % g_pGame ->GetHeight(),
(HICON)(WORD)GetClassLong(hwnd, GCL_HICON));
ReleaseDC(hwnd, hdc);
};
-
-
//------------------------------------
//window 函数
//------------------------------------
-
int WINAPI WinMain(HINSTANCE hInsce, HINSTANCE hPeInsce,
PSTR szCmdLine, int icmdShow)
-
{
MSG msg;
static int iTickTrigger = 0;
int iTickCount;
-
if(GameInit(hInsce))
{
//初始化游戏引擎
if(!GameEngine::GetEngine() ->Init(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 wpoc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
//将所有消息传递给引擎
return GameEngine::GetEngine() ->HandleEvent(hwnd, msg, wp, lp);
}
-
GameEngine::GameEngine(HINSTANCE hInsce, LPSTR szwndClass, LPSTR szTle,
WORD wIcon, WORD wSmlIcon, int iWidth /* = 800 */, int Height /* = 600 */)
{
//设置游戏引擎成员变量
m_pGameEngine = this;
m_hInsce = hInsce;
m_hwnd = NULL;
if(lstrlen(szwndClass) > 0)
lstrcpy(m_szwndClass, szwndClass);
-
if(lstrlen(szTle) > 0)
lstrcpy(m_szTle, szTle);
-
m_wIcon = wIcon;
m_smlIcon = wSmlIcon;
m_iWidth = iWidth;
m_iHeight = Height;
m_iFrameDelay = 50;
m_bSleep = TRUE;
}
-
GameEngine::~GameEngine()
{
-
}
-
//---------------------------
//游戏引擎常规方法
//---------------------------
BOOL GameEngine::Init(int cmdShow)
{
WNDCLASSEX wnd;
-
wnd.cbSize = sizeof(wnd);
wnd.style = CS_HREDRAW | CS_VREDRAW;
wnd.lpfnWndProc = wpoc;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.hInstance = m_hInsce;
wnd.hIcon = LoadIcon(m_hInsce,
MAKEINTRESOURCE(GetIcon()));
wnd.hIconSm = LoadIcon(m_hInsce,
MAKEINTRESOURCE(GetSmallIcon()));
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wnd.lpszMenuName = NULL;
wnd.lpszClassName = m_szwndClass;
-
if(!RegisterClassEx(&wnd))
return FALSE;
-
m_hwnd = CreateWindow(m_szwndClass, m_szTle, WS_POPUPWINDOW |
WS_CAPTION | WS_MINIMIZEBOX, 120, 120, 800, 600, NULL, NULL, m_hInsce, NULL);
-
if(!m_hwnd)
return FALSE;
-
//显示和跟新窗口
ShowWindow(m_hwnd, cmdShow);
UpdateWindow(m_hwnd);
return TRUE;
}
-
LRESULT GameEngine::HandleEvent(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
//将Windows消息传递给游戏引擎成员函数
switch(msg)
{
case WM_CREATE:
//设置游戏窗口并开始游戏
SetWnd(hwnd);
GameStart(hwnd);
return 0;
-
case WM_SETFOCUS:
//激活并更新休眠状态
GameActivate(hwnd);
SetSleep(FALSE);
return 0;
-
case WM_KILLFOCUS:
//停用游戏并跟新
GameDeactive(hwnd);
SetSleep(TRUE);
return 0;
-
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
-
//绘制游戏
GamePaint(hdc);
-
EndPaint(hwnd, &ps);
return 0;
-
case WM_DESTROY:
//结束游戏并退出游戏
GameEnd();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
-
基本添加资源的方法在网上都有教的,就是在自己的工程文件夹下新建。。。。rc 然后插入你的图片资源然后编译 rc文件,就会出现resource.h
-
it is too late
-
go to sleep!!!ZZZ
-