test/testgles.c
author Mike Gorchak <lestat@i.com.ua>
Thu, 11 Jun 2009 06:03:05 +0000
changeset 3194 c18c03927a77
parent 3139 7f684f249ec9
child 3397 c64ae6ee6a6d
permissions -rw-r--r--
Proper window resize handling in GLES test application.
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 
     6 #include "common.h"
     7 
     8 #ifdef __IPHONEOS__
     9 #define HAVE_OPENGLES
    10 #endif
    11 
    12 #ifdef HAVE_OPENGLES
    13 
    14 #include "SDL_opengles.h"
    15 
    16 static CommonState *state;
    17 static SDL_GLContext *context = NULL;
    18 
    19 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    20 static void
    21 quit(int rc)
    22 {
    23     int i;
    24 
    25     if (context != NULL) {
    26         for (i = 0; i < state->num_windows; i++) {
    27             if (context[i]) {
    28                 SDL_GL_DeleteContext(context[i]);
    29             }
    30         }
    31 
    32         SDL_free(context);
    33     }
    34 
    35     CommonQuit(state);
    36     exit(rc);
    37 }
    38 
    39 static void
    40 Render()
    41 {
    42     static GLubyte color[8][4] = { {255, 0, 0, 0},
    43     {255, 0, 0, 255},
    44     {0, 255, 0, 255},
    45     {0, 255, 0, 255},
    46     {0, 255, 0, 255},
    47     {255, 255, 255, 255},
    48     {255, 0, 255, 255},
    49     {0, 0, 255, 255}
    50     };
    51     static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
    52     {0.5f, -0.5f, -0.5f},
    53     {-0.5f, -0.5f, -0.5f},
    54     {-0.5f, 0.5f, -0.5f},
    55     {-0.5f, 0.5f, 0.5f},
    56     {0.5f, 0.5f, 0.5f},
    57     {0.5f, -0.5f, 0.5f},
    58     {-0.5f, -0.5f, 0.5f}
    59     };
    60     static GLubyte indices[36] = { 0, 3, 4,
    61         4, 5, 0,
    62         0, 5, 6,
    63         6, 1, 0,
    64         6, 7, 2,
    65         2, 1, 6,
    66         7, 4, 3,
    67         3, 2, 7,
    68         5, 4, 7,
    69         7, 6, 5,
    70         2, 3, 1,
    71         3, 0, 1
    72     };
    73 
    74 
    75     /* Do our drawing, too. */
    76     glClearColor(0.0, 0.0, 0.0, 1.0);
    77     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    78 
    79     /* Draw the cube */
    80     glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    81     glEnableClientState(GL_COLOR_ARRAY);
    82     glVertexPointer(3, GL_FLOAT, 0, cube);
    83     glEnableClientState(GL_VERTEX_ARRAY);
    84     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
    85 
    86     glMatrixMode(GL_MODELVIEW);
    87     glRotatef(5.0, 1.0, 1.0, 1.0);
    88 }
    89 
    90 int
    91 main(int argc, char *argv[])
    92 {
    93     int fsaa, accel;
    94     int value;
    95     int i, done;
    96     SDL_DisplayMode mode;
    97     SDL_Event event;
    98     Uint32 then, now, frames;
    99     int status;
   100 
   101     /* Initialize parameters */
   102     fsaa = 0;
   103     accel = 0;
   104 
   105     /* Initialize test framework */
   106     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   107     if (!state) {
   108         return 1;
   109     }
   110     for (i = 1; i < argc;) {
   111         int consumed;
   112 
   113         consumed = CommonArg(state, i);
   114         if (consumed == 0) {
   115             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   116                 ++fsaa;
   117                 consumed = 1;
   118             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   119                 ++accel;
   120                 consumed = 1;
   121             } else {
   122                 consumed = -1;
   123             }
   124         }
   125         if (consumed < 0) {
   126             fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
   127                     CommonUsage(state));
   128             quit(1);
   129         }
   130         i += consumed;
   131     }
   132 
   133     /* Set OpenGL parameters */
   134     state->window_flags |= SDL_WINDOW_OPENGL;
   135     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   136     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   137     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   138     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   139     if (fsaa) {
   140         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
   141         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
   142     }
   143     if (accel) {
   144         SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
   145     }
   146     if (!CommonInit(state)) {
   147         quit(2);
   148     }
   149 
   150     context = SDL_calloc(state->num_windows, sizeof(SDL_GLContext));
   151     if (context == NULL) {
   152         fprintf(stderr, "Out of memory!\n");
   153         quit(2);
   154     }
   155 
   156     /* Create OpenGL ES contexts */
   157     for (i = 0; i < state->num_windows; i++) {
   158         context[i] = SDL_GL_CreateContext(state->windows[i]);
   159         if (!context[i]) {
   160             fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   161             quit(2);
   162         }
   163     }
   164 
   165     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   166         SDL_GL_SetSwapInterval(1);
   167     } else {
   168         SDL_GL_SetSwapInterval(0);
   169     }
   170 
   171     SDL_GetCurrentDisplayMode(&mode);
   172     printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
   173     printf("\n");
   174     printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   175     printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   176     printf("Version    : %s\n", glGetString(GL_VERSION));
   177     printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   178     printf("\n");
   179 
   180     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   181     if (!status) {
   182         printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   183     } else {
   184         fprintf(stderr, "Failed to get SDL_GL_RED_SIZE: %s\n",
   185                 SDL_GetError());
   186     }
   187     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   188     if (!status) {
   189         printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   190     } else {
   191         fprintf(stderr, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
   192                 SDL_GetError());
   193     }
   194     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   195     if (!status) {
   196         printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   197     } else {
   198         fprintf(stderr, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
   199                 SDL_GetError());
   200     }
   201     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   202     if (!status) {
   203         printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   204     } else {
   205         fprintf(stderr, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
   206                 SDL_GetError());
   207     }
   208     if (fsaa) {
   209         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   210         if (!status) {
   211             printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   212         } else {
   213             fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   214                     SDL_GetError());
   215         }
   216         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   217         if (!status) {
   218             printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   219                    value);
   220         } else {
   221             fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   222                     SDL_GetError());
   223         }
   224     }
   225     if (accel) {
   226         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   227         if (!status) {
   228             printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   229         } else {
   230             fprintf(stderr, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   231                     SDL_GetError());
   232         }
   233     }
   234 
   235     /* Set rendering settings for each context */
   236     for (i = 0; i < state->num_windows; ++i) {
   237         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   238         if (status) {
   239             printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   240 
   241             /* Continue for next window */
   242             continue;
   243         }
   244 
   245         glViewport(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
   246         glMatrixMode(GL_PROJECTION);
   247         glLoadIdentity();
   248         glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   249         glMatrixMode(GL_MODELVIEW);
   250         glLoadIdentity();
   251         glEnable(GL_DEPTH_TEST);
   252         glDepthFunc(GL_LESS);
   253         glShadeModel(GL_SMOOTH);
   254     }
   255 
   256     /* Main render loop */
   257     frames = 0;
   258     then = SDL_GetTicks();
   259     done = 0;
   260     while (!done) {
   261         /* Check for events */
   262         ++frames;
   263         while (SDL_PollEvent(&event)) {
   264             switch (event.type) {
   265             case SDL_WINDOWEVENT:
   266                 switch (event.window.event) {
   267                     case SDL_WINDOWEVENT_RESIZED:
   268                         for (i = 0; i < state->num_windows; ++i) {
   269                             if (event.window.windowID == state->windows[i]) {
   270                                 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   271                                 if (status) {
   272                                     printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   273                                     break;
   274                                 }
   275                                 /* Change view port to the new window dimensions */
   276                                 glViewport(0, 0, event.window.data1, event.window.data2);
   277                                 /* Update window content */
   278                                 Render();
   279                                 SDL_GL_SwapWindow(state->windows[i]);
   280                                 break;
   281                             }
   282                         }
   283                         break;
   284                 }
   285             }
   286             CommonEvent(state, &event, &done);
   287         }
   288         for (i = 0; i < state->num_windows; ++i) {
   289             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   290             if (status) {
   291                 printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   292 
   293                 /* Continue for next window */
   294                 continue;
   295             }
   296             Render();
   297             SDL_GL_SwapWindow(state->windows[i]);
   298         }
   299     }
   300 
   301     /* Print out some timing information */
   302     now = SDL_GetTicks();
   303     if (now > then) {
   304         printf("%2.2f frames per second\n",
   305                ((double) frames * 1000) / (now - then));
   306     }
   307     quit(0);
   308     return 0;
   309 }
   310 
   311 #else /* HAVE_OPENGLES */
   312 
   313 int
   314 main(int argc, char *argv[])
   315 {
   316     printf("No OpenGL ES support on this system\n");
   317     return 1;
   318 }
   319 
   320 #endif /* HAVE_OPENGLES */