小菜鸟的C++游戏编程学习日记(一)
作者:杨帆
参考书籍:Beginning Game ProGramming(中文版)
学习时间:2016.7.26
以下附所敲源代码与部分注释:
//编译环境 visual studio 2015
#include <windows.h>
#include <iostream>
LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) //消息回调函数
{
RECT rect;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
for (int i = 0; i < 10; i++) //这是
{ //一些
int x = i * 20; //没有
int y = i * 20; //卵用
rect = { x,y,x + 100,y + 20 }; //的
DrawText(hdc, "OmdSAuK", 7, &rect, DT_CENTER); //东西
}
EndPaint(hwnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hwnd, message, wParam, lParam); //返回默认
}
ATOM WinRegisterClass(HINSTANCE hInstance) //注册一个窗口类
{
WNDCLASSEX wndclassex;
wndclassex.cbClsExtra = 0;
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.cbWndExtra = 0;
wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclassex.hCursor = NULL;
wndclassex.hIcon = LoadCursor(NULL,IDC_ARROW);
wndclassex.hIconSm = NULL;
wndclassex.hInstance = hInstance;
wndclassex.lpszClassName = NULL;
wndclassex.lpszMenuName = NULL;
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = (WNDPROC)WinProc;
return RegisterClassEx(&wndclassex);
}
bool InitInstance(HINSTANCE hInstance, int nCmdShow) //初始化一个窗口实例
{
HWND hwnd = CreateWindow(
"MyTest", "test",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 500,
NULL, NULL,
hInstance, NULL
);
if (hwnd == 0)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return 1;
}
int WINAPI WinMain(HINSTANCE hInstance, //此窗口实例
HINSTANCE hPrevInstance, //上一个窗口实例
WPARAM wParam,LPARAM lParam, //无视
LPSTR lpCmdLine, //包含传递给函数的命令行参数字符串
int nCmdShow //制定创建窗口时的显示方式
)
{
WinRegisterClass(hInstance);
if (!InitInstance(hInstance,nCmdShow))
{
return 0;
}
MSG msg;
while (GetMessage(&msg,NULL,0,0)) //Main获得消息后对消息的处理
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
对于刚从命令行转到窗口的我来说对于这部分还需要更多的联系与深入的理解,对于windows的消息处理机制做更深入学习。目前只是对于这些参数有概念性的知识,恐怕没有书本自己也写不出来。继续加油吧。这里附上本人的[GitHub](https://github.com/Amnnny)地址,希望有同样兴趣的伙伴联系我一起学习相互交流。