使用OpenGL绘制三角形:如何使用两个类进行管理

时间:2016-04-30 12:32:17

标签: c++ winapi opengl

我的代码有问题。没有错误或警告,但仅在窗口调整大小时出现三角形。

我需要纠正这个问题。头文件包含这两个类:Window和WindowGL(基于类的继承)。这段代码出了什么问题?

Vector v = new Vector(length);
Arrays.fill(v.elements, value);
return v;

和.cpp文件:

#ifndef OPENGL_H
#define OPENGL_H
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

class Window
{
protected:
    HWND hwnd;
    long clientWidh;
    long clientHeight;
public:
    Window() :hwnd(NULL){};
    LRESULT WndProc(HWND , UINT , WPARAM , LPARAM );
    bool Initialize(HINSTANCE appHandle, POINT windowPosition, POINT windowSize);
    WPARAM Run();
};

class WindowGL : public Window
{
private:
    HGLRC handleRC;
    HDC handleDC;
    bool InitWGL(HWND hwnd);
    void DestroyWGL();
    void SetScene(bool isometricProjection);
    void Render();
public:
    WindowGL() :Window(), handleRC(NULL), handleDC(NULL){};
    LRESULT WndProc(HWND, UINT , WPARAM, LPARAM );
    bool SetPixels(HDC) const;
}window;

#endif

1 个答案:

答案 0 :(得分:2)

您仅在发送Render()时呼叫WM_PAINT,尝试每隔几毫秒在Run()中呼叫一次。 使用PeekMessage更改GetMessage以防止代码停止,只有在有消息需要读取时才使用GetMessage。

修改

while(msg.message != WM_QUIT)
{
    if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
    {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
    }
    Render();
    sleep(10); // If you don't want to update the screen too fast
}

编辑2

LRESULT Window::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_SIZE || WM_PAINT: // <- the problem was here
        RECT rect;
        GetClientRect(hWnd, &rect);
        clientWidh = rect.right - rect.left;
        clientHeight = rect.bottom - rect.top;
        break;
    default:
        return(DefWindowProc(hWnd, message, wParam, lParam));
    }
    return 0L;
}