SDL2 OpenGL Window 立即关闭

SDL2 OpenGL Window Instantly closes

我正在尝试使用 OpenGL 和 SDL2 编写基本游戏,但每当我 运行 程序 window 立即关闭

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();
     }

Window.h

   #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;
  }

当我 运行 代码时,我得到一个短暂的绿色闪烁 window 然后它立即崩溃。当我删除 Input();从我的更新功能来看,window 不会崩溃,但它没有响应。我试图将 SDL_PollEVent 更改为 SDL_WaitEvent 并向输入函数添加延迟,但没有任何效果

首先,当您可能想要检查等价性时,您正在使用赋值运算符:

if (e.type = SDL_QUIT)

应该是:

if (e.type == SDL_QUIT)

此外,您还有其他问题。您在测试之前声明了 SDL_Event e; 联合,但没有将其初始化为任何值。然后继续遍历该变量,等待它被设置为退出。没有什么可以改变该变量的值,那么你的循环将如何退出?