gpt4 book ai didi

c++ - 无法释放设备上下文、HWND,也无法注销 Windows 类 (OpenGL) -

转载 作者:太空宇宙 更新时间:2023-11-04 13:40:44 24 4
gpt4 key购买 nike

我正在关注 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;
}
}

(最后三个 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 的方式。

有谁知道我的 Key 代码中有什么导致我的 DC 和 HWND 无法发布?

最佳答案

可能 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;
}

关于c++ - 无法释放设备上下文、HWND,也无法注销 Windows 类 (OpenGL) -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27689725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com