test/testgles.c
author Mike Gorchak <lestat@i.com.ua>
Tue, 28 Apr 2009 04:38:51 +0000
changeset 3110 e6c21c14ce3a
parent 3099 82e60908fab1
child 3139 7f684f249ec9
permissions -rw-r--r--
Added OpenGL ES context creation for the each window which has been created (for --windows=N option).
     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     {
    27         for (i=0; i<state->num_windows; i++)
    28         {
    29             if (context[i]) {
    30                 SDL_GL_DeleteContext(context[i]);
    31             }
    32         }
    33 
    34         SDL_free(context);
    35     }
    36 
    37     CommonQuit(state);
    38     exit(rc);
    39 }
    40 
    41 static void
    42 Render()
    43 {
    44     static GLubyte color[8][4] = { {255, 0, 0, 0},
    45     {255, 0, 0, 255},
    46     {0, 255, 0, 255},
    47     {0, 255, 0, 255},
    48     {0, 255, 0, 255},
    49     {255, 255, 255, 255},
    50     {255, 0, 255, 255},
    51     {0, 0, 255, 255}
    52     };
    53     static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
    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     {-0.5f, -0.5f, 0.5f}
    61     };
    62     static GLubyte indices[36] = { 0, 3, 4,
    63         4, 5, 0,
    64         0, 5, 6,
    65         6, 1, 0,
    66         6, 7, 2,
    67         2, 1, 6,
    68         7, 4, 3,
    69         3, 2, 7,
    70         5, 4, 7,
    71         7, 6, 5,
    72         2, 3, 1,
    73         3, 0, 1
    74     };
    75 
    76 
    77     /* Do our drawing, too. */
    78     glClearColor(0.0, 0.0, 0.0, 1.0);
    79     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    80 
    81     /* Draw the cube */
    82     glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    83     glEnableClientState(GL_COLOR_ARRAY);
    84     glVertexPointer(3, GL_FLOAT, 0, cube);
    85     glEnableClientState(GL_VERTEX_ARRAY);
    86     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
    87 
    88     glMatrixMode(GL_MODELVIEW);
    89     glRotatef(5.0, 1.0, 1.0, 1.0);
    90 }
    91 
    92 int
    93 main(int argc, char *argv[])
    94 {
    95     int fsaa, accel;
    96     int value;
    97     int i, done;
    98     SDL_DisplayMode mode;
    99     SDL_Event event;
   100     Uint32 then, now, frames;
   101     int status;
   102 
   103     /* Initialize parameters */
   104     fsaa = 0;
   105     accel = 0;
   106 
   107     /* Initialize test framework */
   108     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   109     if (!state) {
   110         return 1;
   111     }
   112     for (i = 1; i < argc;) {
   113         int consumed;
   114 
   115         consumed = CommonArg(state, i);
   116         if (consumed == 0) {
   117             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   118                 ++fsaa;
   119                 consumed = 1;
   120             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   121                 ++accel;
   122                 consumed = 1;
   123             } else {
   124                 consumed = -1;
   125             }
   126         }
   127         if (consumed < 0) {
   128             fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
   129                     CommonUsage(state));
   130             quit(1);
   131         }
   132         i += consumed;
   133     }
   134 
   135     /* Set OpenGL parameters */
   136     state->window_flags |= SDL_WINDOW_OPENGL;
   137     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   138     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   139     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   140     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   141     if (fsaa) {
   142         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
   143         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
   144     }
   145     if (accel) {
   146         SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
   147     }
   148     if (!CommonInit(state)) {
   149         quit(2);
   150     }
   151 
   152     context=SDL_calloc(state->num_windows, sizeof(SDL_GLContext));
   153     if (context==NULL)
   154     {
   155        fprintf(stderr, "Out of memory!\n");
   156        quit(2);
   157     }
   158 
   159     /* Create OpenGL ES contexts */
   160     for (i=0; i<state->num_windows; i++)
   161     {
   162         context[i] = SDL_GL_CreateContext(state->windows[i]);
   163         if (!context[i]) {
   164             fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   165             quit(2);
   166         }
   167     }
   168 
   169     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   170         SDL_GL_SetSwapInterval(1);
   171     } else {
   172         SDL_GL_SetSwapInterval(0);
   173     }
   174 
   175     SDL_GetCurrentDisplayMode(&mode);
   176     printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
   177     printf("\n");
   178     printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   179     printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   180     printf("Version    : %s\n", glGetString(GL_VERSION));
   181     printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   182     printf("\n");
   183 
   184     status=SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   185     if (!status) {
   186         printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   187     } else {
   188         fprintf(stderr, "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
   189     }
   190     status=SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   191     if (!status) {
   192         printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   193     } else {
   194         fprintf(stderr, "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
   195     }
   196     status=SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   197     if (!status) {
   198         printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   199     } else {
   200         fprintf(stderr, "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
   201     }
   202     status=SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   203     if (!status) {
   204         printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   205     } else {
   206         fprintf(stderr, "Failed to get SDL_GL_DEPTH_SIZE: %s\n", 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", SDL_GetError());
   214         }
   215         status=SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   216         if (!status) {
   217             printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa, value);
   218         } else {
   219             fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n", SDL_GetError());
   220         }
   221     }
   222     if (accel) {
   223         status=SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   224         if (!status)
   225         {
   226             printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   227         } else {
   228             fprintf(stderr, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n", SDL_GetError());
   229         }
   230     }
   231 
   232     /* Set rendering settings for each context */
   233     for (i = 0; i < state->num_windows; ++i) {
   234         status=SDL_GL_MakeCurrent(state->windows[i], context[i]);
   235         if (status)
   236         {
   237             printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   238 
   239             /* Continue for next window */
   240             continue;
   241         }
   242 
   243         glMatrixMode(GL_PROJECTION);
   244         glLoadIdentity();
   245         glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   246         glMatrixMode(GL_MODELVIEW);
   247         glLoadIdentity();
   248         glEnable(GL_DEPTH_TEST);
   249         glDepthFunc(GL_LESS);
   250         glShadeModel(GL_SMOOTH);
   251     }
   252 
   253     /* Main render loop */
   254     frames = 0;
   255     then = SDL_GetTicks();
   256     done = 0;
   257     while (!done) {
   258         /* Check for events */
   259         ++frames;
   260         while (SDL_PollEvent(&event)) {
   261             CommonEvent(state, &event, &done);
   262         }
   263         for (i = 0; i < state->num_windows; ++i) {
   264             int w, h;
   265             status=SDL_GL_MakeCurrent(state->windows[i], context[i]);
   266             if (status)
   267             {
   268                printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   269 
   270                /* Continue for next window */
   271                continue;
   272             }
   273             SDL_GetWindowSize(state->windows[i], &w, &h);
   274             glViewport(0, 0, w, h);
   275             Render();
   276             SDL_GL_SwapWindow(state->windows[i]);
   277         }
   278     }
   279 
   280     /* Print out some timing information */
   281     now = SDL_GetTicks();
   282     if (now > then) {
   283         printf("%2.2f frames per second\n",
   284                ((double) frames * 1000) / (now - then));
   285     }
   286     quit(0);
   287     return 0;
   288 }
   289 
   290 #else /* HAVE_OPENGLES */
   291 
   292 int
   293 main(int argc, char *argv[])
   294 {
   295     printf("No OpenGL ES support on this system\n");
   296     return 1;
   297 }
   298 
   299 #endif /* HAVE_OPENGLES */