[SDL] SDL 1.3, opengl context switch / mix

Christian Leger chrism.leger at gmail.com
Sat Jul 9 13:28:17 PDT 2011


Hi,

I've never tried to play much with contexts, besides just creating them and
using them.

My recommendation is that you use either SDL for everything, or (the more
flexible solution if you want to do 3D) opengl for everything.

I'd be happy to help you out with the basic techniques for rendering a GUI
with opengl, if you need help with that.

I can't prove to you that this is the best approach, but I can guarantee
that it will work, because many finished games use this approach.

Best of luck,

Christian

On Wed, Jun 29, 2011 at 7:04 AM, valefor <paul.chavent at fnac.net> wrote:

> **
> Hi.
>
> I've already posted to the mailing list this message but had no answers. Is
> it a stupid question ?
>
> In order to separate the game content (world rendering) and the game
> interface (menu and button), i try to mix sdl calls and opengl ones.
>
> I made a little test program for testing (see below).
>
> It works as expected unless i enable "SDL_GL_MakeCurrent" (uncomment lines
> 70, 83 and 94).
>
> It seems that "SDL_CreateRenderer" call "SDL_GL_CreateContext". So i assume
> that i have two contexts in my sample code.
> So i would expect to see the monochrome background then the smiley. But i
> don't see the smiley.
>
> So "SDL_RenderCopy" don't call "SDL_GL_MakeCurrent" ?
> Is there any way to make the renderer context current ?
>
> For my application, if i create a renderer without creating a second
> context explicitly, it will work.
> But my question is for design/best-practice consideration.
>
> Thank you for your help.
>
> Regards.
>
> Paul.
>
>
>
>
>
>  Code:
>
>  #include **
> #include **
> #include **
> #include **
> #include **
>
> #define WINDOW_WIDTH    640
> #define WINDOW_HEIGHT   480
>
> /* A simple function that prints a message, the error code returned by SDL,
>  * and quits the application */
> void sdldie(char *msg)
> {
>     printf("%s: %s\n", msg, SDL_GetError());
>     SDL_Quit();
>     exit(1);
> }
>
> /* Our program's entry point */
> int main(int argc, char *argv[])
> {
>   SDL_Window *mainwindow; /* Our window handle */
>   SDL_GLContext maincontext; /* Our opengl context handle */
>   SDL_Renderer *renderer;
>   SDL_Surface *sprite_surface;
>   SDL_Texture *sprite_texture;
>   SDL_Rect sprite_position;
>
>   /* Initialize SDL's Video subsystem */
>   if (SDL_Init(SDL_INIT_VIDEO) < 0)
>     sdldie("Unable to initialize SDL");
>
>   /* Load the sprite image */
>   sprite_surface = SDL_LoadBMP("icon.bmp");
>   if (sprite_surface == NULL)
>     sdldie("Unable to load bmp");
>   sprite_position.x = rand() % (WINDOW_WIDTH - sprite_surface->w);
>   sprite_position.y = rand() % (WINDOW_HEIGHT - sprite_surface->h);
>   sprite_position.w = sprite_surface->w;
>   sprite_position.h = sprite_surface->h;
>
>   /* Turn on double buffering  */
>   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
>
>   /* Create our window */
>   mainwindow = SDL_CreateWindow("Test opengl",
>                                 SDL_WINDOWPOS_UNDEFINED,
> SDL_WINDOWPOS_UNDEFINED,
>                                 WINDOW_WIDTH, WINDOW_HEIGHT,
>                                 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
>   if (!mainwindow)
>     sdldie("Unable to create window");
>
>   /* Create our opengl context and attach it to our window */
>   maincontext = SDL_GL_CreateContext(mainwindow);
>
>   /* This makes our buffer swap syncronized with the monitor's vertical
> refresh */
>   SDL_GL_SetSwapInterval(1);
>
>   /* Create a renderer */
>   renderer = SDL_CreateRenderer(mainwindow, -1, 0);
>   if (!renderer)
>     sdldie("Unable to create renderer");
>
>   /* Create textures from the image */
>   sprite_texture = SDL_CreateTextureFromSurface(renderer, sprite_surface);
>   if (!sprite_texture)
>     sdldie("Unable to create texture");
>
>
>   //SDL_GL_MakeCurrent(mainwindow, maincontext);
>
>   /* Clear our buffer with a red background */
>   glClearColor ( 1.0, 0.0, 0.0, 1.0 );
>   glClear ( GL_COLOR_BUFFER_BIT );
>   /* Swap our back buffer to the front */
>   SDL_GL_SwapWindow(mainwindow);
>   SDL_Delay(1000);
>   /* Update the screen with the sprite texture */
>   SDL_RenderCopy(renderer, sprite_texture, NULL, &sprite_position);
>   SDL_RenderPresent(renderer);
>   SDL_Delay(1000);
>
>   //SDL_GL_MakeCurrent(mainwindow, maincontext);
>
>   /* Same as above, but green */
>   glClearColor ( 0.0, 1.0, 0.0, 1.0 );
>   glClear ( GL_COLOR_BUFFER_BIT );
>   SDL_GL_SwapWindow(mainwindow);
>   SDL_Delay(1000);
>   SDL_RenderCopy(renderer, sprite_texture, NULL, &sprite_position);
>   SDL_RenderPresent(renderer);
>   SDL_Delay(1000);
>
>   //SDL_GL_MakeCurrent(mainwindow, maincontext);
>
>   /* Same as above, but blue */
>   glClearColor ( 0.0, 0.0, 1.0, 1.0 );
>   glClear ( GL_COLOR_BUFFER_BIT );
>   SDL_GL_SwapWindow(mainwindow);
>   SDL_Delay(1000);
>   SDL_RenderCopy(renderer, sprite_texture, NULL, &sprite_position);
>   SDL_RenderPresent(renderer);
>   SDL_Delay(1000);
>
>   /* Delete our opengl context, destroy our window, and shutdown SDL */
>
>   SDL_GL_DeleteContext(maincontext);
>
>   SDL_DestroyTexture(sprite_texture);
>   SDL_DestroyRenderer(renderer);
>
>   SDL_DestroyWindow(mainwindow);
>
>   SDL_FreeSurface(sprite_surface);
>
>   SDL_Quit();
>
>   return 0;
> }
>
>
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20110709/95c3cf70/attachment-0007.htm>


More information about the SDL mailing list