在Mac OS X 10.8.4上使用SDL2时没有OpenGL上下文

时间:2013-09-12 22:09:21

标签: c opengl sdl

在Mac OS X 10.8.4上,以下程序的行为就像没有OpenGL上下文与窗口关联一样。

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

static void render( void );
static void gl_info( void );
static void dump_sdl_error( void );

SDL_Window* window = NULL;
SDL_GLContext ctx = NULL;

GLuint program = 0;
GLint attribute_vertex = 0;
GLint attribute_color = 0;


int main( int argc, char* argv[] )
{
    if( SDL_Init(SDL_INIT_VIDEO) < 0 )
    {
        goto quit;
    }

    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 2 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );

    window = SDL_CreateWindow( "Test Shaders", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );

    if( window == NULL )
    {
        dump_sdl_error( );
        goto quit;
    }

    ctx = SDL_GL_CreateContext( window );

    if( !ctx )
    {
        dump_sdl_error( );
        goto quit;
    }

    dump_sdl_error( );

    gl_info( );


    render( );

quit:
    if( ctx ) SDL_GL_DeleteContext( ctx );
    if( window ) SDL_DestroyWindow( window );
    SDL_Quit( );
    return 0;
}


void render( )
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
    glClearColor( 1.0, 0.0, 0.0, 1.0 );

    SDL_GL_SwapWindow( window );
}

void gl_info( void )
{
    const char* renderer     = (const char*) glGetString(GL_RENDERER);
    const char* version      = (const char*) glGetString(GL_VERSION);
    const char* glsl_version = (const char*) glGetString(GL_SHADING_LANGUAGE_VERSION);

    fprintf( stdout, "[GL] Renderer: %s\n", renderer ? renderer : "unknown" );
    fprintf( stdout, "[GL] Version: %s\n", version ? version : "unknown" );
    fprintf( stdout, "[GL] Shading Language: %s\n", glsl_version ? glsl_version : "unknown" );
}


void dump_sdl_error( void )
{
    const char* sdl_error = SDL_GetError( );

    if( sdl_error && *sdl_error != '\0' )
    {
        fprintf( stderr, "[SDL] %s\n", sdl_error );
    }
}

上述程序产生以下输出:

$ ./bin/test-shaders 
[GL] Renderer: unknown
[GL] Version: unknown
[GL] Shading Language: unknown

似乎我没有背景,但我迷失了。我期待看到GLSL版本和我的图形硬件显示,但似乎glGetString()返回NULL。那里有SDL2专家吗?

1 个答案:

答案 0 :(得分:2)

我发现了问题。显然,如果您缺少“-framework OpenGL”

,代码仍将编译

我通过检查SDL2附带的测试程序解决了这个问题,发现它们确实有效并且我错过了框架OpenGL。我希望这有助于其他人。

谢谢Andon。

我改变了这个:

__top_builddir__bin_test_sdl_SOURCES = test-sdl.c
__top_builddir__bin_test_sdl_LDFLAGS = -lGL -lSDL2 $(top_builddir)/lib/.libs/libsimplegl.a

对此:

__top_builddir__bin_test_sdl_SOURCES = test-sdl.c
__top_builddir__bin_test_sdl_LDFLAGS = -lGL -lSDL2 -framework OpenGL $(top_builddir)/lib/.libs/libsimplegl.a