test/testgles.c
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 09 Oct 2013 11:30:01 -0300
changeset 7794 7995e0920bf9
parent 7787 e6f3e8fc96ea
child 7796 75c80f36038e
permissions -rw-r--r--
Fixes testgles and testgl
     1 /*
     2   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 #include <stdlib.h>
    13 #include <stdio.h>
    14 #include <string.h>
    15 #include <math.h>
    16 
    17 #include "SDL_test_common.h"
    18 
    19 #if defined(__IPHONEOS__) || defined(__ANDROID__)
    20 #define HAVE_OPENGLES
    21 #endif
    22 
    23 #ifdef HAVE_OPENGLES
    24 
    25 #include "SDL_opengles.h"
    26 
    27 static SDLTest_CommonState *state;
    28 static SDL_GLContext *context = NULL;
    29 static int depth = 16;
    30 
    31 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    32 static void
    33 quit(int rc)
    34 {
    35     int i;
    36 
    37     if (context != NULL) {
    38         for (i = 0; i < state->num_windows; i++) {
    39             if (context[i]) {
    40                 SDL_GL_DeleteContext(context[i]);
    41             }
    42         }
    43 
    44         SDL_free(context);
    45     }
    46 
    47     SDLTest_CommonQuit(state);
    48     exit(rc);
    49 }
    50 
    51 static void
    52 Render()
    53 {
    54     static GLubyte color[8][4] = { {255, 0, 0, 0},
    55     {255, 0, 0, 255},
    56     {0, 255, 0, 255},
    57     {0, 255, 0, 255},
    58     {0, 255, 0, 255},
    59     {255, 255, 255, 255},
    60     {255, 0, 255, 255},
    61     {0, 0, 255, 255}
    62     };
    63     static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
    64     {0.5f, -0.5f, -0.5f},
    65     {-0.5f, -0.5f, -0.5f},
    66     {-0.5f, 0.5f, -0.5f},
    67     {-0.5f, 0.5f, 0.5f},
    68     {0.5f, 0.5f, 0.5f},
    69     {0.5f, -0.5f, 0.5f},
    70     {-0.5f, -0.5f, 0.5f}
    71     };
    72     static GLubyte indices[36] = { 0, 3, 4,
    73         4, 5, 0,
    74         0, 5, 6,
    75         6, 1, 0,
    76         6, 7, 2,
    77         2, 1, 6,
    78         7, 4, 3,
    79         3, 2, 7,
    80         5, 4, 7,
    81         7, 6, 5,
    82         2, 3, 1,
    83         3, 0, 1
    84     };
    85 
    86 
    87     /* Do our drawing, too. */
    88     glClearColor(0.0, 0.0, 0.0, 1.0);
    89     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    90 
    91     /* Draw the cube */
    92     glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    93     glEnableClientState(GL_COLOR_ARRAY);
    94     glVertexPointer(3, GL_FLOAT, 0, cube);
    95     glEnableClientState(GL_VERTEX_ARRAY);
    96     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
    97 
    98     glMatrixMode(GL_MODELVIEW);
    99     glRotatef(5.0, 1.0, 1.0, 1.0);
   100 }
   101 
   102 int
   103 main(int argc, char *argv[])
   104 {
   105     int fsaa, accel;
   106     int value;
   107     int i, done;
   108     SDL_DisplayMode mode;
   109     SDL_Event event;
   110     Uint32 then, now, frames;
   111     int status;
   112 
   113     /* Enable standard application logging */
   114     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   115 
   116     /* Initialize parameters */
   117     fsaa = 0;
   118     accel = 0;
   119 
   120     /* Initialize test framework */
   121     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   122     if (!state) {
   123         return 1;
   124     }
   125     for (i = 1; i < argc;) {
   126         int consumed;
   127 
   128         consumed = SDLTest_CommonArg(state, i);
   129         if (consumed == 0) {
   130             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   131                 ++fsaa;
   132                 consumed = 1;
   133             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   134                 ++accel;
   135                 consumed = 1;
   136             } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
   137                 i++;
   138                 if (!argv[i]) {
   139                     consumed = -1;
   140                 } else {
   141                     depth = SDL_atoi(argv[i]);
   142                     consumed = 1;
   143                 }
   144             } else {
   145                 consumed = -1;
   146             }
   147         }
   148         if (consumed < 0) {
   149             SDL_Log("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
   150                     SDLTest_CommonUsage(state));
   151             quit(1);
   152         }
   153         i += consumed;
   154     }
   155 
   156     /* Set OpenGL parameters */
   157     state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
   158     state->gl_red_size = 5;
   159     state->gl_green_size = 5;
   160     state->gl_blue_size = 5;
   161     state->gl_depth_size = depth;
   162     state->gl_major_version = 1;
   163     state->gl_minor_version = 1;
   164     if (fsaa) {
   165         state->gl_multisamplebuffers=1;
   166         state->gl_multisamplesamples=fsaa;
   167     }
   168     if (accel) {
   169         state->gl_accelerated=1;
   170     }
   171     if (!SDLTest_CommonInit(state)) {
   172         quit(2);
   173     }
   174 
   175     context = SDL_calloc(state->num_windows, sizeof(context));
   176     if (context == NULL) {
   177         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
   178         quit(2);
   179     }
   180 
   181     /* Create OpenGL ES contexts */
   182     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
   183 
   184     for (i = 0; i < state->num_windows; i++) {
   185         context[i] = SDL_GL_CreateContext(state->windows[i]);
   186         if (!context[i]) {
   187             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   188             quit(2);
   189         }
   190     }
   191 
   192     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   193         SDL_GL_SetSwapInterval(1);
   194     } else {
   195         SDL_GL_SetSwapInterval(0);
   196     }
   197 
   198     SDL_GetCurrentDisplayMode(0, &mode);
   199     SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
   200     SDL_Log("\n");
   201     SDL_Log("Vendor     : %s\n", glGetString(GL_VENDOR));
   202     SDL_Log("Renderer   : %s\n", glGetString(GL_RENDERER));
   203     SDL_Log("Version    : %s\n", glGetString(GL_VERSION));
   204     SDL_Log("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   205     SDL_Log("\n");
   206 
   207     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   208     if (!status) {
   209         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   210     } else {
   211         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n",
   212                 SDL_GetError());
   213     }
   214     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   215     if (!status) {
   216         SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   217     } else {
   218         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
   219                 SDL_GetError());
   220     }
   221     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   222     if (!status) {
   223         SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   224     } else {
   225         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
   226                 SDL_GetError());
   227     }
   228     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   229     if (!status) {
   230         SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
   231     } else {
   232         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
   233                 SDL_GetError());
   234     }
   235     if (fsaa) {
   236         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   237         if (!status) {
   238             SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   239         } else {
   240             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   241                     SDL_GetError());
   242         }
   243         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   244         if (!status) {
   245             SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   246                    value);
   247         } else {
   248             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   249                     SDL_GetError());
   250         }
   251     }
   252     if (accel) {
   253         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   254         if (!status) {
   255             SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   256         } else {
   257             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   258                     SDL_GetError());
   259         }
   260     }
   261 
   262     /* Set rendering settings for each context */
   263     for (i = 0; i < state->num_windows; ++i) {
   264         float aspectAdjust;
   265 
   266         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   267         if (status) {
   268             SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   269 
   270             /* Continue for next window */
   271             continue;
   272         }
   273 
   274         aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h);
   275         glViewport(0, 0, state->window_w, state->window_h);
   276         glMatrixMode(GL_PROJECTION);
   277         glLoadIdentity();
   278         glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
   279         glMatrixMode(GL_MODELVIEW);
   280         glLoadIdentity();
   281         glEnable(GL_DEPTH_TEST);
   282         glDepthFunc(GL_LESS);
   283         glShadeModel(GL_SMOOTH);
   284     }
   285 
   286     /* Main render loop */
   287     frames = 0;
   288     then = SDL_GetTicks();
   289     done = 0;
   290     while (!done) {
   291         /* Check for events */
   292         ++frames;
   293         while (SDL_PollEvent(&event)) {
   294             switch (event.type) {
   295             case SDL_WINDOWEVENT:
   296                 switch (event.window.event) {
   297                     case SDL_WINDOWEVENT_RESIZED:
   298                         for (i = 0; i < state->num_windows; ++i) {
   299                             if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
   300                                 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   301                                 if (status) {
   302                                     SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   303                                     break;
   304                                 }
   305                                 /* Change view port to the new window dimensions */
   306                                 glViewport(0, 0, event.window.data1, event.window.data2);
   307                                 /* Update window content */
   308                                 Render();
   309                                 SDL_GL_SwapWindow(state->windows[i]);
   310                                 break;
   311                             }
   312                         }
   313                         break;
   314                 }
   315             }
   316             SDLTest_CommonEvent(state, &event, &done);
   317         }
   318         for (i = 0; i < state->num_windows; ++i) {
   319             if (state->windows[i] == NULL)
   320                 continue;
   321             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   322             if (status) {
   323                 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   324 
   325                 /* Continue for next window */
   326                 continue;
   327             }
   328             Render();
   329             SDL_GL_SwapWindow(state->windows[i]);
   330         }
   331     }
   332 
   333     /* Print out some timing information */
   334     now = SDL_GetTicks();
   335     if (now > then) {
   336         SDL_Log("%2.2f frames per second\n",
   337                ((double) frames * 1000) / (now - then));
   338     }
   339     quit(0);
   340     return 0;
   341 }
   342 
   343 #else /* HAVE_OPENGLES */
   344 
   345 int
   346 main(int argc, char *argv[])
   347 {
   348     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL ES support on this system\n");
   349     return 1;
   350 }
   351 
   352 #endif /* HAVE_OPENGLES */
   353 
   354 /* vi: set ts=4 sw=4 expandtab: */