Handling window resizing using OpenGL and SDL

后端 未结 2 1718
日久生厌
日久生厌 2021-02-03 14:34

From the code I have of a program that draws and moves a square around an application window, I\'m having trouble when resizing the application window. When I handle a resize an

2条回答
  •  别跟我提以往
    2021-02-03 14:54

    One problem you probably have is, that resizing a window with SDL a new OpenGL context is created, which means that all the things you uploaded before (textures, vertex buffer objects) and state you set (like vertex array pointers) are lost. You need to reinitialize them if using SDL. If you want to keep them, don't use SDL for window management. I recommend GLFW.

    This

    void ResizeWindow()
    
    {
    
    screen_width = event.resize.w;
    screen_height = event.resize.h;
    
    SDL_SetVideoMode(screen_width, screen_height, bpp, SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF);
    
    glViewport(0, 0, screen_width, screen_height);
    glMatrixMode(GL_PROJECTION);
    glOrtho(0, screen_width, 0, screen_height, -1, 1);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    
    }
    

    is the most common anti pattern found in beginners OpenGL code. Drawing commands and state management that influences drawing belongs in the drawing function. That is the following:

    glViewport(0, 0, screen_width, screen_height);
    glMatrixMode(GL_PROJECTION);
    glOrtho(0, screen_width, 0, screen_height, -1, 1);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    

    If you want a robust OpenGL program never put them (only) into the resize handler. They belong with the other drawing commands.

提交回复
热议问题