diff src/video/SDL_renderer_gl.c @ 5150:ad50b3db78bd

The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 01 Feb 2011 19:19:43 -0800
parents c8e049de174c
children 5429daf5e3f9
line wrap: on
line diff
--- a/src/video/SDL_renderer_gl.c	Tue Feb 01 19:15:42 2011 -0800
+++ b/src/video/SDL_renderer_gl.c	Tue Feb 01 19:19:43 2011 -0800
@@ -66,8 +66,8 @@
 static const float inv255f = 1.0f / 255.0f;
 
 static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
-static int GL_ActivateRenderer(SDL_Renderer * renderer);
-static int GL_DisplayModeChanged(SDL_Renderer * renderer);
+static void GL_WindowEvent(SDL_Renderer * renderer,
+                           const SDL_WindowEvent *event);
 static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
 static int GL_QueryTexturePixels(SDL_Renderer * renderer,
                                  SDL_Texture * texture, void **pixels,
@@ -277,8 +277,7 @@
         return NULL;
     }
 
-    renderer->ActivateRenderer = GL_ActivateRenderer;
-    renderer->DisplayModeChanged = GL_DisplayModeChanged;
+    renderer->WindowEvent = GL_WindowEvent;
     renderer->CreateTexture = GL_CreateTexture;
     renderer->QueryTexturePixels = GL_QueryTexturePixels;
     renderer->SetTexturePalette = GL_SetTexturePalette;
@@ -408,14 +407,19 @@
     return renderer;
 }
 
+static SDL_GLContext SDL_CurrentContext = NULL;
+
 static int
 GL_ActivateRenderer(SDL_Renderer * renderer)
 {
     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
     SDL_Window *window = renderer->window;
 
-    if (SDL_GL_MakeCurrent(window, data->context) < 0) {
-        return -1;
+    if (SDL_CurrentContext != data->context) {
+        if (SDL_GL_MakeCurrent(window, data->context) < 0) {
+            return -1;
+        }
+        SDL_CurrentContext = data->context;
     }
     if (data->updateSize) {
         data->glMatrixMode(GL_PROJECTION);
@@ -430,14 +434,16 @@
     return 0;
 }
 
-static int
-GL_DisplayModeChanged(SDL_Renderer * renderer)
+static void
+GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
 {
     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
 
-    /* Rebind the context to the window area and update matrices */
-    data->updateSize = SDL_TRUE;
-    return GL_ActivateRenderer(renderer);
+    if (event->event == SDL_WINDOWEVENT_RESIZED) {
+        /* Rebind the context to the window area and update matrices */
+        SDL_CurrentContext = NULL;
+        data->updateSize = SDL_TRUE;
+    }
 }
 
 static __inline__ int
@@ -717,6 +723,8 @@
     GLuint shader = 0;
     GLenum result;
 
+    GL_ActivateRenderer(renderer);
+
     if (!convert_format(renderdata, texture->format, &internalFormat,
                         &format, &type)) {
         SDL_SetError("Texture format %s not supported by OpenGL",
@@ -874,6 +882,8 @@
     GL_TextureData *data = (GL_TextureData *) texture->driverdata;
     Uint8 *palette;
 
+    GL_ActivateRenderer(renderer);
+
     if (!data->palette) {
         SDL_SetError("Texture doesn't have a palette");
         return -1;
@@ -938,6 +948,8 @@
     GL_TextureData *data = (GL_TextureData *) texture->driverdata;
     GLenum result;
 
+    GL_ActivateRenderer(renderer);
+
     renderdata->glGetError();
     SetupTextureUpdate(renderdata, texture, pitch);
     renderdata->glEnable(data->type);
@@ -1018,6 +1030,8 @@
 {
     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
 
+    GL_ActivateRenderer(renderer);
+
     data->glClearColor((GLfloat) renderer->r * inv255f,
                        (GLfloat) renderer->g * inv255f,
                        (GLfloat) renderer->b * inv255f,
@@ -1035,6 +1049,8 @@
     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
     int i;
 
+    GL_ActivateRenderer(renderer);
+
     GL_SetBlendMode(data, renderer->blendMode);
 
     data->glColor4f((GLfloat) renderer->r * inv255f,
@@ -1058,6 +1074,8 @@
     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
     int i;
 
+    GL_ActivateRenderer(renderer);
+
     GL_SetBlendMode(data, renderer->blendMode);
 
     data->glColor4f((GLfloat) renderer->r * inv255f,
@@ -1126,6 +1144,8 @@
     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
     int i, x, y;
 
+    GL_ActivateRenderer(renderer);
+
     GL_SetBlendMode(data, renderer->blendMode);
 
     data->glColor4f((GLfloat) renderer->r * inv255f,
@@ -1164,6 +1184,8 @@
     GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
     int i;
 
+    GL_ActivateRenderer(renderer);
+
     GL_SetBlendMode(data, renderer->blendMode);
 
     data->glColor4f((GLfloat) renderer->r * inv255f,
@@ -1189,6 +1211,8 @@
     int minx, miny, maxx, maxy;
     GLfloat minu, maxu, minv, maxv;
 
+    GL_ActivateRenderer(renderer);
+
     if (texturedata->dirty.list) {
         SDL_DirtyRect *dirty;
         void *pixels;
@@ -1276,6 +1300,8 @@
     Uint8 *src, *dst, *tmp;
     int length, rows;
 
+    GL_ActivateRenderer(renderer);
+
     if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
         /* FIXME: Do a temp copy to a format that is supported */
         SDL_SetError("Unsupported pixel format");
@@ -1323,6 +1349,8 @@
     Uint8 *src, *dst, *tmp;
     int length, rows;
 
+    GL_ActivateRenderer(renderer);
+
     if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
         /* FIXME: Do a temp copy to a format that is supported */
         SDL_SetError("Unsupported pixel format");
@@ -1360,6 +1388,8 @@
 static void
 GL_RenderPresent(SDL_Renderer * renderer)
 {
+    GL_ActivateRenderer(renderer);
+
     SDL_GL_SwapWindow(renderer->window);
 }
 
@@ -1369,6 +1399,8 @@
     GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
     GL_TextureData *data = (GL_TextureData *) texture->driverdata;
 
+    GL_ActivateRenderer(renderer);
+
     if (!data) {
         return;
     }