- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 583字
- 2021-06-30 14:45:48
Creating the event handler
In order to have a properly functioning window, or to even compile the application, at this point, the event processing function, WndProc, must be defined. The implementation here will be very simple, mostly focusing on how to destroy the window:
- Start implementing the WndProc function in WinMain.cpp:
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg,
WPARAM wParam, LPARAM lParam) {
switch (iMsg) {
- When the WM_CLOSE message is received, you need to shut down the Application class and emit a destroy window message. Once the application is shut down, don't forget to delete it:
case WM_CLOSE:
if (gApplication != 0) {
gApplication->Shutdown();
delete gApplication;
gApplication = 0;
DestroyWindow(hwnd);
}
else {
std::cout << "Already shut down!\n";
}
break;
- When the destroy message is received, the window's OpenGL resources need to be released. This means deleting the global vertex array object, and then deleting the OpenGL context:
case WM_DESTROY:
if (gVertexArrayObject != 0) {
HDC hdc = GetDC(hwnd);
HGLRC hglrc = wglGetCurrentContext();
glBindVertexArray(0);
glDeleteVertexArrays(1, &gVertexArrayObject);
gVertexArrayObject = 0;
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hglrc);
ReleaseDC(hwnd, hdc);
PostQuitMessage(0);
}
else {
std::cout << "Multiple destroy messages\n";
}
break;
- The paint and erase background messages are safe to ignore since OpenGL is managing rendering to the window. If the message received isn't one of the messages already handled, forward it to the default window message function:
case WM_PAINT:
case WM_ERASEBKGND:
return 0;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
Now that you have written the windows event loop, you should be able to compile and run a blank window. In the following section, you'll explore the downloadable samples for this book.
- UML和模式應(yīng)用(原書第3版)
- Node.js 10實戰(zhàn)
- PostgreSQL for Data Architects
- Mastering Ubuntu Server
- 游戲程序設(shè)計教程
- Mastering Kali Linux for Web Penetration Testing
- Mastering macOS Programming
- Android Native Development Kit Cookbook
- 快人一步:系統(tǒng)性能提高之道
- jQuery Mobile移動應(yīng)用開發(fā)實戰(zhàn)(第3版)
- Creating Mobile Apps with jQuery Mobile(Second Edition)
- Java并發(fā)編程之美
- Programming Microsoft Dynamics? NAV 2015
- 零基礎(chǔ)學HTML+CSS
- 深度學習入門:基于Python的理論與實現(xiàn)