SDL 窗口在打开后关闭
SDL window closes after opening
我试图制作一个游戏状态系统,我在我的游戏状态中制作了一个窗口类和一个 MainLoop 函数,起初我相信这是因为循环不在 main 中,但结果相同。
窗口.cpp:
#include "Window.h"
using namespace std;
Window::Window(char* title, int width, int height, Uint32 flags)
{
if (SDL_Init(SDL_INIT_EVERYTHING))
{
cerr << "SDL failed to init: " << SDL_GetError() << endl;
throw;
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
window = SDL_CreateWindow("DirtyCraft", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
if (window == NULL)
throw;
context = SDL_GL_CreateContext(window);
GLenum error = glewInit();
if (error != GLEW_OK)
{
cerr << "Glew: " << glewGetErrorString(error) << endl;
throw;
}
}
Window::~Window()
{
SDL_DestroyWindow(window);
SDL_Quit();
}
窗口.h:
#pragma once
#include <iostream>
#include <SDL.h>
#define GLEW_STATIC
#include <GL/glew.h>
class Window
{
public:
Window(char* title, int width, int height, Uint32 flags);
~Window();
SDL_Window *window;
SDL_GLContext context;
};
主.cpp循环在哪里:
#include <iostream>
#include <SDL.h>
#define GLEW_STATIC
#include <GL/glew.h>
#include "Window.h"
#include "MainGame.h"
using namespace std;
#define WIDTH 640
#define HEIGHT 480
int main(int argc, char* argv[])
{
Window window("DirtyCraft", WIDTH, HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI);
//MainGame MainGame(window);
//MainGame.MainLoop();
while (true)
{
SDL_Event E;
if (SDL_PollEvent(&E))
{
if (E.type == SDL_QUIT);
break;
}
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.15f, 0.5f, 0.5f, 1.0f);
SDL_GL_SwapWindow(window.window);
}
window.~Window();
return 0;
}
那么问题出在哪里呢?我很确定我错过了一个细节...
我认为您的消息轮询循环是错误的。您应该轮询所有事件,然后才执行交换等。现在,您的窗口很可能没有正确显示,因为它没有完成初始化消息的处理。所以你应该把它改成
while(SDL_PollEvent(&E))
相关文章:
- SDL 窗口不会弹出
- 在 OSX 上打开 SDL 窗口时不要打开终端窗口
- 我可以在 SDL 窗口外跟踪我的鼠标 pos 吗?
- SDL 窗口在打开后关闭
- SDL窗口的错误
- SDL 窗口在 Eclipse 中未出现
- 使用 OpenGL 窗口(如 GLFW)或周围的窗口(如 GTK 或 SDL)是否存在性能差异?
- 为什么 SDL 在最后一个窗口关闭时不发出SDL_QUIT
- SDL 窗口在移动时消失
- SDL:窗口冻结
- 通过引用/地址传递SDL窗口
- SDL 窗口弹出,但它是空白的,完全没有响应
- SDL调整窗口尺寸
- 设置窗口大小后,SDL不会渲染到整个窗口
- SDL视频显示窗口:如何连接窗口关闭事件回调
- 最小化后无法恢复 SDL 窗口
- SDL窗口根本不显示
- c++ SDL - 在窗口的特定区域内呈现
- 如何保持我在 Visual Studio SDL 中编写的图像窗口处于打开状态
- c++半透明窗口SDL