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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <GL/gl.h>
#include <SDL/SDL.h>
#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;
}