01-windows游戏编程-框架

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值