官术网_书友最值得收藏!

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:

  1. Start implementing the WndProc function in WinMain.cpp:

    LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg,

                        WPARAM wParam, LPARAM lParam) {

        switch (iMsg) {

  2. 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;

  3. 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;

  4. 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.

主站蜘蛛池模板: 梅州市| 乌审旗| 临猗县| 永福县| 南木林县| 德兴市| 依兰县| 天长市| 衢州市| 临夏市| 剑川县| 云南省| 赣州市| 桐柏县| 柘荣县| 苍南县| 连江县| 青神县| 张北县| 临夏市| 尤溪县| 沅江市| 静宁县| 化州市| 临夏县| 成都市| 北川| 永福县| 和顺县| 太仆寺旗| 准格尔旗| 类乌齐县| 东乌珠穆沁旗| 太仓市| 藁城市| 辽宁省| 济源市| 长顺县| 鞍山市| 灯塔市| 梅河口市|