Windows游戏编程之从零开始-Page96-GDI通用框架

本文深入探讨了使用GDI进行游戏开发的程序框架,详细介绍了窗口过程的修改,以及如何通过处理不同消息来实现游戏的初始化、绘制和清理。代码示例展示了如何响应WM_PAINT、WM_KEYDOWN和WM_DESTROY等消息,以及如何使用GDI函数进行文本渲染。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改了窗口过程,这样看起来简单点

#include <Windows.h>

typedef WNDPROC WMPROC;
typedef struct{
	UINT msg;
	WMPROC wmProc;
}MsgWithProc;

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"致我们永不熄灭的游戏开发梦想——GDI程序核心框架"


HDC g_hdc = NULL;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

BOOL Game_Init(HWND hwnd);
VOID Game_Paint(HWND hwnd);
BOOL Game_CleanUP(HWND hwnd);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
	WNDCLASSEX wndClass = {0};
	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc = WndProc;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hInstance = hInstance;
	wndClass.hIcon = NULL;
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = L"ForTheDreamOfGameDevelop";

	if(!RegisterClassEx(&wndClass)){
		return -1;
	}

	HWND hwnd = CreateWindow(
		L"ForTheDreamOfGameDevelop",
		WINDOW_TITLE,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT,
		NULL, NULL, hInstance, NULL
	);

	ShowWindow(hwnd, nShowCmd);
	UpdateWindow(hwnd);

	if(!Game_Init(hwnd)){
		MessageBox(hwnd, L"资源初始化失败", L"消息窗口", 0);
		return FALSE;
	}

	MSG msg = {0};
	while(msg.message != WM_QUIT){
		if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	UnregisterClass(L"ForTheDreamOfGameDevelop", hInstance);
	return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){

	MsgWithProc msgWithProc[] = {
		{WM_PAINT,WMpaint},
		{WM_KEYDOWN,WMkeydown},
		{WM_DESTROY,WMdestory}
	};

	for(MsgWithProc mwp : msgWithProc){
		if(mwp.msg == message){
			mwp.wmProc(hwnd, message, wParam, lParam);
			return 0;
		}
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	PAINTSTRUCT paintStruct;
	g_hdc = BeginPaint(hwnd, &paintStruct);
	Game_Paint(hwnd);
	EndPaint(hwnd, &paintStruct);
	ValidateRect(hwnd, NULL);
	return 0;
}

LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	if(wParam == VK_ESCAPE){
		DestroyWindow(hwnd);
	}
	return 0;
}

LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	Game_CleanUP(hwnd);
	PostQuitMessage(0);
	return 0;
}

BOOL Game_Init(HWND hwnd){
	g_hdc = GetDC(hwnd);
	Game_Paint(hwnd);
	ReleaseDC(hwnd, g_hdc);
	return TRUE;
}

VOID Game_Paint(HWND hwnd){
	HFONT hFont = CreateFont(30, 0, 0, 0, 0, 0, 0, 0, GB2312_CHARSET, 0, 0, 0, 0, L"微软雅黑");
	SelectObject(g_hdc, hFont);
	SetBkMode(g_hdc, TRANSPARENT);
	wchar_t text1[] = L"我们所有的梦想都可以成真,只要我们有勇气去追求它们。";
	wchar_t text2[] = L"All our dreams can come true, if we have the courage to pursue them. ";
	wchar_t text3[] = L"-------沃尔特 迪斯尼";

	SetTextColor(g_hdc, RGB(50, 255, 50));
	TextOut(g_hdc, 30, 150, text1, wcslen(text1));

	SetTextColor(g_hdc, RGB(50, 50, 255));
	TextOut(g_hdc, 30, 200, text2, wcslen(text2));

	SetTextColor(g_hdc, RGB(255, 150, 50));
	TextOut(g_hdc, 500, 250, text3, wcslen(text3));

	DeleteObject(hFont);
}

BOOL Game_CleanUP(HWND hwnd){
	return TRUE;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值