美文网首页
SDL——创建一个简单的OpenGL窗口

SDL——创建一个简单的OpenGL窗口

作者: 偶是星爷 | 来源:发表于2016-08-07 21:37 被阅读665次
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

SDL_Window *mainWindow;
SDL_GLContext mainGLContext;

void Render() {
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    
    SDL_GL_SwapWindow(mainWindow);
}

int main(int argc, const char * argv[]) {
    
    SDL_Init(SDL_INIT_EVERYTHING);
    
    Uint32 flags = SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL;
    int width = 640;
    int height = 480;
    
    
    mainWindow = SDL_CreateWindow("SDL2 OpenGL Example", SDL_WINDOWPOS_UNDEFINED,
                                  SDL_WINDOWPOS_UNDEFINED, width, height, flags);
    mainGLContext = SDL_GL_CreateContext(mainWindow);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    
    SDL_Event e;
    bool done = false;
    while(!done && SDL_WaitEvent(&e)) {
        switch (e.type) {
            case SDL_QUIT:
                done = true;
                break;
                
            default:
                break;
        }
        Render();
        SDL_Delay(20);
    }
    /*
    SDL_GL_DeleteContext(mainGLContext);
    SDL_DestroyWindow(mainWindow);
    SDL_Quit();
    */
    return 0;
}

相关文章

网友评论

      本文标题:SDL——创建一个简单的OpenGL窗口

      本文链接:https://www.haomeiwen.com/subject/ytilsttx.html