您的位置:首页 > 编程语言

01-windows游戏编程-框架

2015-01-04 23:34 399 查看
#include <Windows.h>

#pragma comment(lib,"winmm.lib")

#define dim(x) (sizeof(x)/sizeof(*x))
HINSTANCE _hInst;
HWND _hwnd;

char _szAppName[] = "GameEngine";
char _szTitle[] = "GameEngine";

typedef struct MESSAGE_ENTRY
{
UINT nMessage;
LONG (*pfn)(HWND,UINT,WPARAM,LPARAM);
};

LONG OnCreate(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LONG OnPaint(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LONG OnKeyDown(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LONG OnDestroy(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

MESSAGE_ENTRY _messageEntry[] =
{
WM_CREATE , OnCreate,
WM_PAINT , OnPaint,
WM_KEYDOWN, OnKeyDown,
WM_DESTROY,OnDestroy,
};

BOOL InitApplication(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow);

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(
__in  HINSTANCE hInstance,
__in  HINSTANCE hPrevInstance,
__in  LPSTR lpCmdLine,
__in  int nCmdShow
)
{
//1.注册窗口类
if (!InitApplication(hInstance))
{
return false;
}

//2.创建 更新 显示窗口
if (!InitInstance(hInstance,nCmdShow))
{
return false;
}
//3.消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//4.返回
return msg.wParam;
}

//窗口函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

for (int i = 0; i < dim(_messageEntry); ++i)
{
if (uMsg == _messageEntry[i].nMessage)
{
return ((*_messageEntry[i].pfn)(hwnd,uMsg,wParam,lParam));
}
}

return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

BOOL InitApplication(HINSTANCE hInstance)
{
WNDCLASS wndclass = {0};

wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndclass.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = (HICON)LoadImage(NULL,"hina.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = (WNDPROC)WndProc;
wndclass.lpszClassName = _szAppName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_VREDRAW |CS_HREDRAW;

return RegisterClass(&wndclass);
}

BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
{
_hInst = hInstance;
HWND _hwnd = CreateWindow(
_szAppName,
_szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (!_hwnd)
{
return FALSE;
}
ShowWindow(_hwnd,nCmdShow);
MoveWindow(_hwnd,200,0,800,600,TRUE);
UpdateWindow(_hwnd);

return TRUE;
}

LONG OnCreate(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PlaySound("FirstBlood.wav", NULL, SND_FILENAME | SND_ASYNC);   //播放音效
return 0;
}

LONG OnPaint(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rect;

hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
//DrawText(hdc,_szTitle,strlen(_szTitle),&rect,DT_VCENTER|DT_CENTER | DT_SINGLELINE);

EndPaint(hwnd,&ps);

ValidateRect(hwnd,&rect);

return 0;
}

LONG OnKeyDown(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (wParam == VK_ESCAPE)
{
DestroyWindow(hwnd);
}
return 0;
}

LONG OnDestroy(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PostQuitMessage(0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 游戏编程 windows