Proper window resize handling in GLES test application.
authorMike Gorchak <lestat@i.com.ua>
Thu, 11 Jun 2009 06:03:05 +0000
changeset 3194 c18c03927a77
parent 3193 64e4442676a6
child 3195 08747e24a50f
Proper window resize handling in GLES test application.
test/common.c
test/common.h
test/testgles.c
--- a/test/common.c	Thu Jun 11 05:57:32 2009 +0000
+++ b/test/common.c	Thu Jun 11 06:03:05 2009 +0000
@@ -11,59 +11,56 @@
 #define AUDIO_USAGE \
 "[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
 
-struct pformat
-{
+struct pformat {
     Uint32 id;
-    const char *idstr;
-} pixel_format[] = {
-    {
-    SDL_PIXELFORMAT_INDEX1LSB, "SDL_PIXELFORMAT_INDEX1LSB"}, {
-    SDL_PIXELFORMAT_INDEX1MSB, "SDL_PIXELFORMAT_INDEX1MSB"}, {
-    SDL_PIXELFORMAT_INDEX4LSB, "SDL_PIXELFORMAT_INDEX4LSB"}, {
-    SDL_PIXELFORMAT_INDEX4MSB, "SDL_PIXELFORMAT_INDEX4MSB"}, {
-    SDL_PIXELFORMAT_INDEX8, "SDL_PIXELFORMAT_INDEX8"}, {
-    SDL_PIXELFORMAT_RGB332, "SDL_PIXELFORMAT_RGB332"}, {
-    SDL_PIXELFORMAT_RGB444, "SDL_PIXELFORMAT_RGB444"}, {
-    SDL_PIXELFORMAT_RGB555, "SDL_PIXELFORMAT_RGB555"}, {
-    SDL_PIXELFORMAT_BGR555, "SDL_PIXELFORMAT_BGR555"}, {
-    SDL_PIXELFORMAT_ARGB4444, "SDL_PIXELFORMAT_ARGB4444"}, {
-    SDL_PIXELFORMAT_ABGR4444, "SDL_PIXELFORMAT_ABGR4444"}, {
-    SDL_PIXELFORMAT_ARGB1555, "SDL_PIXELFORMAT_ARGB1555"}, {
-    SDL_PIXELFORMAT_ABGR1555, "SDL_PIXELFORMAT_ABGR1555"}, {
-    SDL_PIXELFORMAT_RGB565, "SDL_PIXELFORMAT_RGB565"}, {
-    SDL_PIXELFORMAT_BGR565, "SDL_PIXELFORMAT_BGR565"}, {
-    SDL_PIXELFORMAT_RGB24, "SDL_PIXELFORMAT_RGB24"}, {
-    SDL_PIXELFORMAT_BGR24, "SDL_PIXELFORMAT_BGR24"}, {
-    SDL_PIXELFORMAT_RGB888, "SDL_PIXELFORMAT_RGB888"}, {
-    SDL_PIXELFORMAT_BGR888, "SDL_PIXELFORMAT_BGR888"}, {
-    SDL_PIXELFORMAT_ARGB8888, "SDL_PIXELFORMAT_ARGB8888"}, {
-    SDL_PIXELFORMAT_RGBA8888, "SDL_PIXELFORMAT_RGBA8888"}, {
-    SDL_PIXELFORMAT_ABGR8888, "SDL_PIXELFORMAT_ABGR8888"}, {
-    SDL_PIXELFORMAT_BGRA8888, "SDL_PIXELFORMAT_BGRA8888"}, {
-    SDL_PIXELFORMAT_ARGB2101010, "SDL_PIXELFORMAT_ARGB2101010"}, {
-    SDL_PIXELFORMAT_YV12, "SDL_PIXELFORMAT_YV12"}, {
-    SDL_PIXELFORMAT_IYUV, "SDL_PIXELFORMAT_IYUV"}, {
-    SDL_PIXELFORMAT_YUY2, "SDL_PIXELFORMAT_YUY2"}, {
-    SDL_PIXELFORMAT_UYVY, "SDL_PIXELFORMAT_UYVY"}, {
-    SDL_PIXELFORMAT_YVYU, "SDL_PIXELFORMAT_YVYU"}
+    const char* idstr;
+} pixel_format[]={
+    {SDL_PIXELFORMAT_INDEX1LSB, "SDL_PIXELFORMAT_INDEX1LSB"},
+    {SDL_PIXELFORMAT_INDEX1MSB, "SDL_PIXELFORMAT_INDEX1MSB"},
+    {SDL_PIXELFORMAT_INDEX4LSB, "SDL_PIXELFORMAT_INDEX4LSB"},
+    {SDL_PIXELFORMAT_INDEX4MSB, "SDL_PIXELFORMAT_INDEX4MSB"},
+    {SDL_PIXELFORMAT_INDEX8, "SDL_PIXELFORMAT_INDEX8"},
+    {SDL_PIXELFORMAT_RGB332, "SDL_PIXELFORMAT_RGB332"},
+    {SDL_PIXELFORMAT_RGB444, "SDL_PIXELFORMAT_RGB444"},
+    {SDL_PIXELFORMAT_RGB555, "SDL_PIXELFORMAT_RGB555"},
+    {SDL_PIXELFORMAT_BGR555, "SDL_PIXELFORMAT_BGR555"},
+    {SDL_PIXELFORMAT_ARGB4444, "SDL_PIXELFORMAT_ARGB4444"},
+    {SDL_PIXELFORMAT_ABGR4444, "SDL_PIXELFORMAT_ABGR4444"},
+    {SDL_PIXELFORMAT_ARGB1555, "SDL_PIXELFORMAT_ARGB1555"},
+    {SDL_PIXELFORMAT_ABGR1555, "SDL_PIXELFORMAT_ABGR1555"},
+    {SDL_PIXELFORMAT_RGB565, "SDL_PIXELFORMAT_RGB565"},
+    {SDL_PIXELFORMAT_BGR565, "SDL_PIXELFORMAT_BGR565"},
+    {SDL_PIXELFORMAT_RGB24, "SDL_PIXELFORMAT_RGB24"},
+    {SDL_PIXELFORMAT_BGR24, "SDL_PIXELFORMAT_BGR24"},
+    {SDL_PIXELFORMAT_RGB888, "SDL_PIXELFORMAT_RGB888"},
+    {SDL_PIXELFORMAT_BGR888, "SDL_PIXELFORMAT_BGR888"},
+    {SDL_PIXELFORMAT_ARGB8888, "SDL_PIXELFORMAT_ARGB8888"},
+    {SDL_PIXELFORMAT_RGBA8888, "SDL_PIXELFORMAT_RGBA8888"},
+    {SDL_PIXELFORMAT_ABGR8888, "SDL_PIXELFORMAT_ABGR8888"},
+    {SDL_PIXELFORMAT_BGRA8888, "SDL_PIXELFORMAT_BGRA8888"},
+    {SDL_PIXELFORMAT_ARGB2101010, "SDL_PIXELFORMAT_ARGB2101010"},
+    {SDL_PIXELFORMAT_YV12, "SDL_PIXELFORMAT_YV12"},
+    {SDL_PIXELFORMAT_IYUV, "SDL_PIXELFORMAT_IYUV"},
+    {SDL_PIXELFORMAT_YUY2, "SDL_PIXELFORMAT_YUY2"},
+    {SDL_PIXELFORMAT_UYVY, "SDL_PIXELFORMAT_UYVY"},
+    {SDL_PIXELFORMAT_YVYU, "SDL_PIXELFORMAT_YVYU"}
 };
 
-const char *
-PixelFormatToString(Uint32 pformat)
+const char* PixelFormatToString(Uint32 pformat)
 {
-    Uint32 it = 0;
+   Uint32 it=0;
 
-    do {
-        if (pixel_format[it].idstr == NULL) {
-            break;
-        }
-        if (pixel_format[it].id == pformat) {
-            return pixel_format[it].idstr;
-        }
-        it++;
-    } while (1);
+   do {
+      if (pixel_format[it].idstr == NULL) {
+          break;
+      }
+      if (pixel_format[it].id == pformat) {
+          return pixel_format[it].idstr;
+      }
+      it++;
+   } while(1);
 
-    return "SDL_PIXELFORMAT_UNKNOWN";
+   return "SDL_PIXELFORMAT_UNKNOWN";
 }
 
 CommonState *
@@ -82,8 +79,8 @@
     state->window_flags = SDL_WINDOW_SHOWN;
     state->window_x = SDL_WINDOWPOS_UNDEFINED;
     state->window_y = SDL_WINDOWPOS_UNDEFINED;
-    state->window_w = 640;
-    state->window_h = 480;
+    state->window_w = DEFAULT_WINDOW_WIDTH;
+    state->window_h = DEFAULT_WINDOW_HEIGHT;
     state->num_windows = 1;
     state->audiospec.freq = 22050;
     state->audiospec.format = AUDIO_S16;
@@ -709,7 +706,7 @@
         fullscreen_mode.w = state->window_w;
         fullscreen_mode.h = state->window_h;
         fullscreen_mode.refresh_rate = state->refresh_rate;
-        if (SDL_SetFullscreenDisplayMode(&fullscreen_mode) < 0) {
+        if (SDL_SetFullscreenDisplayMode(&fullscreen_mode)<0) {
             fprintf(stderr, "Can't switch to fullscreen display mode: %s\n",
                     SDL_GetError());
             return SDL_FALSE;
--- a/test/common.h	Thu Jun 11 05:57:32 2009 +0000
+++ b/test/common.h	Thu Jun 11 06:03:05 2009 +0000
@@ -3,6 +3,9 @@
 
 #include "SDL.h"
 
+#define DEFAULT_WINDOW_WIDTH  640
+#define DEFAULT_WINDOW_HEIGHT 480
+
 #define VERBOSE_VIDEO   0x00000001
 #define VERBOSE_MODES   0x00000002
 #define VERBOSE_RENDER  0x00000004
--- a/test/testgles.c	Thu Jun 11 05:57:32 2009 +0000
+++ b/test/testgles.c	Thu Jun 11 06:03:05 2009 +0000
@@ -242,6 +242,7 @@
             continue;
         }
 
+        glViewport(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
@@ -260,10 +261,31 @@
         /* Check for events */
         ++frames;
         while (SDL_PollEvent(&event)) {
+            switch (event.type) {
+            case SDL_WINDOWEVENT:
+                switch (event.window.event) {
+                    case SDL_WINDOWEVENT_RESIZED:
+                        for (i = 0; i < state->num_windows; ++i) {
+                            if (event.window.windowID == state->windows[i]) {
+                                status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
+                                if (status) {
+                                    printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
+                                    break;
+                                }
+                                /* Change view port to the new window dimensions */
+                                glViewport(0, 0, event.window.data1, event.window.data2);
+                                /* Update window content */
+                                Render();
+                                SDL_GL_SwapWindow(state->windows[i]);
+                                break;
+                            }
+                        }
+                        break;
+                }
+            }
             CommonEvent(state, &event, &done);
         }
         for (i = 0; i < state->num_windows; ++i) {
-            int w, h;
             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
             if (status) {
                 printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
@@ -271,8 +293,6 @@
                 /* Continue for next window */
                 continue;
             }
-            SDL_GetWindowSize(state->windows[i], &w, &h);
-            glViewport(0, 0, w, h);
             Render();
             SDL_GL_SwapWindow(state->windows[i]);
         }