[英]SDL2 OpenGL Window Instantly closes
我正在尝试使用OpenGL和SDL2编写基本游戏,但是每当我运行该程序时,窗口都会立即关闭
Window.cpp
#include "Window.h"
#include <GL/glew.h>
Window::Window(const char* title)
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_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(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 900, 900, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
GLenum status = glewInit();
}
Window::~Window()
{
SDL_DestroyWindow(window);
SDL_GL_DeleteContext(context);
SDL_Quit();
}
void Window::Input()
{
SDL_Event e;
while (true)
{
if (e.type = SDL_QUIT)
{
exit(0);
}
}
}
void Window::Update()
{
SDL_GL_SwapWindow(window);
Input();
}
窗口
#pragma once
#include <SDL.h>
#include <GL/glew.h>
class Window
{
SDL_Window* window;
SDL_GLContext context;
public:
void Input();
void Update();
Window(const char* title);
~Window();
};
Main.cpp
#include <SDL.h>
#include <GL\glew.h>
#include "Window.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
Window window("Window");
while (true)
{
glClearColor(0, 1, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
window.Update();
}
return 0;
}
运行代码时,我会短暂闪烁一个绿色窗口,然后它立即崩溃。 当我删除Input(); 从我的更新功能来看,该窗口不会崩溃,但是没有响应。 我试图将SDL_PollEVent更改为SDL_WaitEvent并向Input函数添加延迟,但是没有任何效果
首先,当您可能想检查对等时,您正在使用赋值运算符:
if (e.type = SDL_QUIT)
应该:
if (e.type == SDL_QUIT)
此外,您还有其他问题。 您声明SDL_Event e;
在测试之前进行并集,但不要将其初始化为任何值。 然后,您继续遍历该变量,等待将其设置为退出。 没有什么可以改变该变量的值,那么循环将如何退出?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.