Fixed loading textures when the window starts hidden.
authorSam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 21:46:06 -0500
changeset 6260 fd494c5f305b
parent 6259 4aef5b792087
child 6261 caf3439402fd
Fixed loading textures when the window starts hidden. The viewport automatically resets to the window size when you programmatically resize the window.
src/render/SDL_render.c
src/render/SDL_sysrender.h
src/render/opengl/SDL_render_gl.c
src/render/opengles/SDL_render_gles.c
src/render/opengles2/SDL_render_gles2.c
src/video/SDL_video.c
src/video/cocoa/SDL_cocoaopengl.m
--- a/src/render/SDL_render.c	Sun Jan 22 19:47:33 2012 -0500
+++ b/src/render/SDL_render.c	Sun Jan 22 21:46:06 2012 -0500
@@ -100,7 +100,7 @@
                 renderer->WindowEvent(renderer, &event->window);
             }
 
-            if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
+            if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
                 /* Try to keep the previous viewport centered */
                 int w, h;
                 SDL_Rect viewport;
@@ -116,10 +116,40 @@
                     viewport.h = renderer->viewport.h;
                     SDL_RenderSetViewport(renderer, &viewport);
                 }
+                renderer->resized = SDL_TRUE;
+            } else if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
+                int w, h;
+                SDL_Rect viewport;
+
+                if (!renderer->resized) {
+                    /* Window was programmatically resized, reset viewport */
+                    SDL_GetWindowSize(window, &w, &h);
+                    if (renderer->target) {
+                        renderer->viewport_backup.x = 0;
+                        renderer->viewport_backup.y = 0;
+                        renderer->viewport_backup.w = w;
+                        renderer->viewport_backup.h = h;
+                    } else {
+                        viewport.x = 0;
+                        viewport.y = 0;
+                        viewport.w = w;
+                        viewport.h = h;
+                        SDL_RenderSetViewport(renderer, &viewport);
+                    }
+                    renderer->resized = SDL_FALSE;
+                }
+            } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
+                renderer->hidden = SDL_TRUE;
+            } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
+                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
+                    renderer->hidden = SDL_FALSE;
+                }
             } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
-                renderer->minimized = SDL_TRUE;
+                renderer->hidden = SDL_TRUE;
             } else if (event->window.event == SDL_WINDOWEVENT_RESTORED) {
-                renderer->minimized = SDL_FALSE;
+                if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
+                    renderer->hidden = SDL_FALSE;
+                }
             }
         }
     }
@@ -218,10 +248,10 @@
         renderer->magic = &renderer_magic;
         renderer->window = window;
 
-        if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
-            renderer->minimized = SDL_TRUE;
+        if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
+            renderer->hidden = SDL_TRUE;
         } else {
-            renderer->minimized = SDL_FALSE;
+            renderer->hidden = SDL_FALSE;
         }
 
         SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
@@ -974,8 +1004,8 @@
 {
     CHECK_RENDERER_MAGIC(renderer, -1);
 
-    /* Don't draw while we're minimized */
-    if (renderer->minimized) {
+    /* Don't draw while we're hidden */
+    if (renderer->hidden) {
         return 0;
     }
     return renderer->RenderClear(renderer);
@@ -1004,8 +1034,8 @@
     if (count < 1) {
         return 0;
     }
-    /* Don't draw while we're minimized */
-    if (renderer->minimized) {
+    /* Don't draw while we're hidden */
+    if (renderer->hidden) {
         return 0;
     }
     return renderer->RenderDrawPoints(renderer, points, count);
@@ -1036,8 +1066,8 @@
     if (count < 2) {
         return 0;
     }
-    /* Don't draw while we're minimized */
-    if (renderer->minimized) {
+    /* Don't draw while we're hidden */
+    if (renderer->hidden) {
         return 0;
     }
     return renderer->RenderDrawLines(renderer, points, count);
@@ -1089,8 +1119,8 @@
         return 0;
     }
 
-    /* Don't draw while we're minimized */
-    if (renderer->minimized) {
+    /* Don't draw while we're hidden */
+    if (renderer->hidden) {
         return 0;
     }
     for (i = 0; i < count; ++i) {
@@ -1132,8 +1162,8 @@
     if (count < 1) {
         return 0;
     }
-    /* Don't draw while we're minimized */
-    if (renderer->minimized) {
+    /* Don't draw while we're hidden */
+    if (renderer->hidden) {
         return 0;
     }
     return renderer->RenderFillRects(renderer, rects, count);
@@ -1193,8 +1223,8 @@
         texture = texture->native;
     }
 
-    /* Don't draw while we're minimized */
-    if (renderer->minimized) {
+    /* Don't draw while we're hidden */
+    if (renderer->hidden) {
         return 0;
     }
     return renderer->RenderCopy(renderer, texture, &real_srcrect,
@@ -1246,8 +1276,8 @@
 {
     CHECK_RENDERER_MAGIC(renderer, );
 
-    /* Don't draw while we're minimized */
-    if (renderer->minimized) {
+    /* Don't draw while we're hidden */
+    if (renderer->hidden) {
         return;
     }
     renderer->RenderPresent(renderer);
--- a/src/render/SDL_sysrender.h	Sun Jan 22 19:47:33 2012 -0500
+++ b/src/render/SDL_sysrender.h	Sun Jan 22 21:46:06 2012 -0500
@@ -100,7 +100,8 @@
 
     /* The window associated with the renderer */
     SDL_Window *window;
-    SDL_bool minimized;
+    SDL_bool hidden;
+    SDL_bool resized;
 
     /* The drawable area within the window */
     SDL_Rect viewport;
--- a/src/render/opengl/SDL_render_gl.c	Sun Jan 22 19:47:33 2012 -0500
+++ b/src/render/opengl/SDL_render_gl.c	Sun Jan 22 21:46:06 2012 -0500
@@ -412,7 +412,9 @@
 static void
 GL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
 {
-    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
+    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
+        event->event == SDL_WINDOWEVENT_SHOWN ||
+        event->event == SDL_WINDOWEVENT_HIDDEN) {
         /* Rebind the context to the window area and update matrices */
         SDL_CurrentContext = NULL;
     }
--- a/src/render/opengles/SDL_render_gles.c	Sun Jan 22 19:47:33 2012 -0500
+++ b/src/render/opengles/SDL_render_gles.c	Sun Jan 22 21:46:06 2012 -0500
@@ -370,7 +370,9 @@
 {
     GLES_RenderData *data = (GLES_RenderData *) renderer->driverdata;
     
-    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
+    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
+        event->event == SDL_WINDOWEVENT_SHOWN ||
+        event->event == SDL_WINDOWEVENT_HIDDEN) {
         /* Rebind the context to the window area and update matrices */
         SDL_CurrentContext = NULL;
     }
--- a/src/render/opengles2/SDL_render_gles2.c	Sun Jan 22 19:47:33 2012 -0500
+++ b/src/render/opengles2/SDL_render_gles2.c	Sun Jan 22 21:46:06 2012 -0500
@@ -241,7 +241,9 @@
 {
     GLES2_DriverContext *rdata = (GLES2_DriverContext *)renderer->driverdata;
 
-    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
+    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
+        event->event == SDL_WINDOWEVENT_SHOWN ||
+        event->event == SDL_WINDOWEVENT_HIDDEN) {
         /* Rebind the context to the window area */
         SDL_CurrentContext = NULL;
     }
--- a/src/video/SDL_video.c	Sun Jan 22 19:47:33 2012 -0500
+++ b/src/video/SDL_video.c	Sun Jan 22 21:46:06 2012 -0500
@@ -107,6 +107,11 @@
         return retval; \
     }
 
+#define INVALIDATE_GLCONTEXT() \
+    _this->current_glwin = NULL; \
+    _this->current_glctx = NULL;
+
+
 /* Support for framebuffer emulation using an accelerated renderer */
 
 #define SDL_WINDOWTEXTUREDATA   "_SDL_WindowTextureData"
@@ -1846,12 +1851,14 @@
 void
 SDL_OnWindowShown(SDL_Window * window)
 {
+    INVALIDATE_GLCONTEXT();
     SDL_OnWindowRestored(window);
 }
 
 void
 SDL_OnWindowHidden(SDL_Window * window)
 {
+    INVALIDATE_GLCONTEXT();
     SDL_UpdateFullscreenMode(window, SDL_FALSE);
 }
 
--- a/src/video/cocoa/SDL_cocoaopengl.m	Sun Jan 22 19:47:33 2012 -0500
+++ b/src/video/cocoa/SDL_cocoaopengl.m	Sun Jan 22 21:46:06 2012 -0500
@@ -200,14 +200,16 @@
         SDL_WindowData *windowdata = (SDL_WindowData *)window->driverdata;
         NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
 
+        if (window->flags & SDL_WINDOW_SHOWN) {
 #ifndef FULLSCREEN_TOGGLEABLE
-        if (window->flags & SDL_WINDOW_FULLSCREEN) {
-            [nscontext setFullScreen];
-        } else
+            if (window->flags & SDL_WINDOW_FULLSCREEN) {
+                [nscontext setFullScreen];
+            } else
 #endif
-        {
-            [nscontext setView:[windowdata->nswindow contentView]];
-            [nscontext update];
+            {
+                [nscontext setView:[windowdata->nswindow contentView]];
+                [nscontext update];
+            }
         }
         [nscontext makeCurrentContext];
     } else {