SDL窗口没有响应

时间:2014-12-03 16:11:59

标签: c++ opengl sdl

单击或最小化时,我的SDL窗口崩溃,背景为白色,OpenGL不会更新屏幕。遵循代码:

#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const char * SCREEN_NAME = "Tetris SDL";

SDL_Window *window;
SDL_GLContext glcontext;

int init();
void main_loop();
void quit();
void events();
void update();
void draw();

int main(int argc, char* args[])
{
    if(!init())
        quit();
    main_loop();
    quit();
}

int init()
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
       return 0;

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    window = SDL_CreateWindow(SCREEN_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);

    if(!window)
        return 0;

    glcontext = SDL_GL_CreateContext(window);
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    return 1;

}
void main_loop()
{
    while(true)
    {
        events();
        update();
        draw();
        //only for test
        SDL_Delay(16);
    }
}
void events(){}
void update(){}
void draw()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glBegin(GL_TRIANGLES);
    glColor3f(1,0,0); glVertex2f(0.0f,0.5f);
    glColor3f(0,1,0); glVertex2f(-0.5f, -0.5f);
    glColor3f(0,0,1); glVertex2f(0.5f, -0.5f);
    glEnd();
    SDL_GL_SwapWindow(window);
}
void quit()
{
    SDL_GL_DeleteContext(glcontext);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

我正在使用SDL 2.0和GNU GCC编译器和Windows XP。有谁知道可能是什么问题?

1 个答案:

答案 0 :(得分:3)

在循环中你不处理任何事件。

如果你不这样做,那么最小化的事件将不会被处理(通常意味着窗口缓冲区被释放以供其他人使用)。