1

私はhttp://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/ OpenGL チュートリアルに従っており、その作業からコードを取得しました。現在、複数のクラスを使用して物事を整理しようとしています。このクラスを作成していると、デバイス コンテキスト、HWND を解放できなくなり、Windows クラスの登録を解除できなくなりました。以下のコードは、リリースできるかどうかを確認するために使用されるコードです。

GLvoid KillGLWindow(GLvoid){ 
if (fullscreen){
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(true);
}
if (hRC){
    if (!wglMakeCurrent(NULL, NULL)){
        MessageBox(NULL, "Release of DC and RC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    }
    if (!wglDeleteContext(hRC)){
        MessageBox(NULL, "Release of RC failed", "Shutdown error", MB_OK | MB_ICONEXCLAMATION);
    }
    hRC = NULL;
}
if (hDC && !ReleaseDC(hWnd, hDC)){
    MessageBox(NULL, "Release of DC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hDC = NULL;
}
if (hWnd && !DestroyWindow(hWnd)){
    MessageBox(NULL, "Release of hWnd failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hWnd = NULL;
}
if (!UnregisterClass("OpenGL", hInstance)){
    MessageBox(NULL, "Could not unregister window class", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    hInstance = NULL;
}
}

(最後の 3 つの if ステートメントが発行されます)

これらのエラーの原因となったコードは、WinMain 関数のキー検出コードです。これは私が変更した唯一のコードです。

else{
        if (active){

            if (testKey.isEsc()){
                done = true;
            }
            if (testKey.isA()){
                KillGLWindow();
            }
            else{
                DrawGLScene();
                SwapBuffers(hDC);
            }
        }
            if (testKey.isF1()){
                //Keys::keys[VK_F1] = false;
                KillGLWindow();
                fullscreen = !fullscreen;
                if (!CreateGLWindow("XcoxGL", 640, 480, 16, fullscreen)){
                    return 0;
                }
            }

変更したのは testKey.THING の部分です。testKey は、次の行によってメイン クラスで開始されます。

Keys testKey

Keys.cpp は次のようになります。

bool Keys::keys[256] = { false };

bool Keys::isA(){
    if (&keys[0x41]){
        return true;
    }
    else{
        return false;
    }
}

bool Keys::isF1(){
    if (&keys[VK_F1]){
        return true;
    }
    else{
        return false;
    }
}

bool Keys::isEsc(){
    if (&keys[VK_ESCAPE]){
        return true;
    }
    else{
        return false;
    }
}

最後に、Keys.h は次のようになります。

#pragma once
class Keys{
public:
    static bool keys[256];
     bool isA();

     bool isF1();

     bool isEsc();
};

必要に応じて完全なコードを投稿できますが、DC と HWND を作成する方法は、上記で投稿したチュートリアルに示され、説明されています。

キーコードの何が原因で DC と HWND が解放されないのか知っている人はいますか?

4

1 に答える 1

0

おそらく KillGLWindow は複数回呼び出され、実際には適切ではありません。これを試してください:

GLvoid KillGLWindow(GLvoid){ 
if (fullscreen){
    ChangeDisplaySettings(NULL, 0);
    ShowCursor(true);
}
if (hRC){
    if (!wglMakeCurrent(NULL, NULL)){
        MessageBox(NULL, "Release of DC and RC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
    }
    if (!wglDeleteContext(hRC)){
        MessageBox(NULL, "Release of RC failed", "Shutdown error", MB_OK | MB_ICONEXCLAMATION);
    }
}
hRC = NULL;
if (hDC && !ReleaseDC(hWnd, hDC)){
    MessageBox(NULL, "Release of DC failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hDC = NULL;
if (hWnd && !DestroyWindow(hWnd)){
    MessageBox(NULL, "Release of hWnd failed", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hWnd = NULL;
if (hInstance && !UnregisterClass("OpenGL", hInstance)){
    MessageBox(NULL, "Could not unregister window class", "Shutdown error", MB_OK | MB_ICONINFORMATION);
}
hInstance = NULL;
}
于 2014-12-29T15:54:51.640 に答える