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