SDL2 OpenGL3如何在函数内初始化SDL

时间:2012-12-11 18:22:20

标签: c++ opengl sdl sdl-2 opengl-3

我正在尝试新的SDL2测试版和OpenGL3上下文,我遇到了一个奇怪的问题:

如果我在main()函数中运行SDL初始化代码,它工作正常,但我想在separete init_sdl()函数中使用此代码。

如果我将初始化代码放在单独的init_sdl()函数中,并从main()调用此函数,则从不绘制背景颜色,程序开始疯狂地消耗我所有系统的资源。

有人能指出我在一个单独的函数中初始化SDL的工作示例吗?我似乎无法找到一个......也许这是不可能的?我想我依旧记得在SDL 1.2中遇到了类似的问题,但是自从我使用它以来已经有几年了,而且我认为我没有找到解决方案。事实上,这可能是我选择转而使用SFML的原因。

我真的想使用SDL2代替SFML,因为它运行在更多平台上,但是无法将内容分成小功能对我来说是一个交易障碍。这应该很容易,我错过了一些明显的东西吗?

修改

这有效:

#include <iostream>
#include <GL/glew.h>
#include <SDL.h>
#define PROGRAM_NAME "SDL2 OpenGL3 Example"

int main(int argc, char** argv)
{
    SDL_Window* sdl2_window = 0;
    SDL_GLContext opengl3_context;

    SDL_Init(SDL_INIT_VIDEO);

    // set the opengl context version
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    // turn on double buffering set the depth buffer to 24 bits
    // you may need to change this to 16 or 32 for your system
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    // create the sdl2 window
    sdl2_window = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED,
            SDL_WINDOWPOS_CENTERED, 512, 512,
            SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

    // create the opengl3 context
    opengl3_context = SDL_GL_CreateContext(sdl2_window);

    GLenum status = glewInit();
    if (status != GLEW_OK)
    {
        std::cerr << "GLEW Error: " << glewGetErrorString(status) << "\n";
        exit(1);
    }

    // sync buffer swap with monitor's vertical refresh rate
    SDL_GL_SetSwapInterval(1);

    // set background color
    glClearColor( 1.0, 0.0, 0.0, 1.0 );

    while (true)
    {
        int status = 0;

        glClear( GL_COLOR_BUFFER_BIT );

        SDL_GL_SwapWindow(sdl2_window);

        SDL_Event event;

        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_KEYDOWN:
                break;
            case SDL_KEYUP:
                // if escape is pressed, quit
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    status = 1; // set status to 1 to exit main loop
                break;
            case SDL_QUIT:
                status = 1;
                break;
            }
        }

        if (status == 1) // if received instruction to quit
            break;
    }

    // delete opengl3 context, destroy sdl2 window, and shut down sdl subsystems
    SDL_GL_DeleteContext(opengl3_context);
    SDL_DestroyWindow(sdl2_window);
    SDL_Quit();

    return 0;
}

这不起作用:

#include <iostream>
#include <GL/glew.h>
#include <SDL.h>
#define PROGRAM_NAME "SDL2 OpenGL3 Example"

void init_sdl(SDL_Window* sdl2_window, SDL_GLContext& opengl3_context)
{
    SDL_Init(SDL_INIT_VIDEO);

    // set the opengl context version
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    // turn on double buffering set the depth buffer to 24 bits
    // you may need to change this to 16 or 32 for your system
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    // create the sdl2 window
    sdl2_window = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED,
            SDL_WINDOWPOS_CENTERED, 512, 512,
            SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

    // create the opengl3 context
    opengl3_context = SDL_GL_CreateContext(sdl2_window);
}

int main(int argc, char** argv)
{
    SDL_Window* sdl2_window = 0;
    SDL_GLContext opengl3_context;

    init_sdl(sdl2_window, opengl3_context);

    GLenum status = glewInit();
    if (status != GLEW_OK)
    {
        std::cerr << "GLEW Error: " << glewGetErrorString(status) << "\n";
        exit(1);
    }

    // sync buffer swap with monitor's vertical refresh rate
    SDL_GL_SetSwapInterval(1);

    // set background color
    glClearColor( 1.0, 0.0, 0.0, 1.0 );

    while (true)
    {
        int status = 0;

        glClear( GL_COLOR_BUFFER_BIT );

        SDL_GL_SwapWindow(sdl2_window);

        SDL_Event event;

        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_KEYDOWN:
                break;
            case SDL_KEYUP:
                // if escape is pressed, quit
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    status = 1; // set status to 1 to exit main loop
                break;
            case SDL_QUIT:
                status = 1;
                break;
            }
        }

        if (status == 1) // if received instruction to quit
            break;
    }

    // delete opengl3 context, destroy sdl2 window, and shut down sdl subsystems
    SDL_GL_DeleteContext(opengl3_context);
    SDL_DestroyWindow(sdl2_window);
    SDL_Quit();

    return 0;
}

2 个答案:

答案 0 :(得分:3)

void init_sdl(SDL_Window* sdl2_window, SDL_GLContext& opengl3_context)

你的第一个论点也应该是一个参考。否则,当您返回main()时,sdl2_window仍为零。

答案 1 :(得分:0)

您确定上下文没有错误:

int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_SHOWN;
win = SDL_CreateWindow("3Doodle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, flags);
SDL_assert(win != NULL);

context = SDL_GL_CreateContext(win);
SDL_assert(context != NULL);