test/testgles.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2765 f55c87ae336b
child 3099 82e60908fab1
permissions -rw-r--r--
Added testresample.c
     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;
    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     if (context) {
    24         /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
    25         SDL_GL_DeleteContext(context);
    26     }
    27     CommonQuit(state);
    28     exit(rc);
    29 }
    30 
    31 static void
    32 Render()
    33 {
    34     static GLubyte color[8][4] = { {255, 0, 0, 0},
    35     {255, 0, 0, 255},
    36     {0, 255, 0, 255},
    37     {0, 255, 0, 255},
    38     {0, 255, 0, 255},
    39     {255, 255, 255, 255},
    40     {255, 0, 255, 255},
    41     {0, 0, 255, 255}
    42     };
    43     static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
    44     {0.5f, -0.5f, -0.5f},
    45     {-0.5f, -0.5f, -0.5f},
    46     {-0.5f, 0.5f, -0.5f},
    47     {-0.5f, 0.5f, 0.5f},
    48     {0.5f, 0.5f, 0.5f},
    49     {0.5f, -0.5f, 0.5f},
    50     {-0.5f, -0.5f, 0.5f}
    51     };
    52     static GLubyte indices[36] = { 0, 3, 4,
    53         4, 5, 0,
    54         0, 5, 6,
    55         6, 1, 0,
    56         6, 7, 2,
    57         2, 1, 6,
    58         7, 4, 3,
    59         3, 2, 7,
    60         5, 4, 7,
    61         7, 6, 5,
    62         2, 3, 1,
    63         3, 0, 1
    64     };
    65 
    66 
    67     /* Do our drawing, too. */
    68     glClearColor(0.0, 0.0, 0.0, 1.0);
    69     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    70 
    71     /* Draw the cube */
    72     glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    73     glEnableClientState(GL_COLOR_ARRAY);
    74     glVertexPointer(3, GL_FLOAT, 0, cube);
    75     glEnableClientState(GL_VERTEX_ARRAY);
    76     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
    77 
    78     glMatrixMode(GL_MODELVIEW);
    79     glRotatef(5.0, 1.0, 1.0, 1.0);
    80 }
    81 
    82 int
    83 main(int argc, char *argv[])
    84 {
    85     int fsaa, accel;
    86     int value;
    87     int i, done;
    88     SDL_DisplayMode mode;
    89     SDL_Event event;
    90     Uint32 then, now, frames;
    91 
    92     /* Initialize parameters */
    93     fsaa = 0;
    94     accel = 0;
    95 
    96     /* Initialize test framework */
    97     state = CommonCreateState(argv, SDL_INIT_VIDEO);
    98     if (!state) {
    99         return 1;
   100     }
   101     for (i = 1; i < argc;) {
   102         int consumed;
   103 
   104         consumed = CommonArg(state, i);
   105         if (consumed == 0) {
   106             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   107                 ++fsaa;
   108                 consumed = 1;
   109             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   110                 ++accel;
   111                 consumed = 1;
   112             } else {
   113                 consumed = -1;
   114             }
   115         }
   116         if (consumed < 0) {
   117             fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
   118                     CommonUsage(state));
   119             quit(1);
   120         }
   121         i += consumed;
   122     }
   123 
   124     /* Set OpenGL parameters */
   125     state->window_flags |= SDL_WINDOW_OPENGL;
   126     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   127     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   128     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   129     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   130     if (fsaa) {
   131         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
   132         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
   133     }
   134     if (accel) {
   135         SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
   136     }
   137     if (!CommonInit(state)) {
   138         quit(2);
   139     }
   140 
   141     /* Create OpenGL context */
   142     context = SDL_GL_CreateContext(state->windows[0]);
   143     if (!context) {
   144         fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   145         quit(2);
   146     }
   147 
   148     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   149         SDL_GL_SetSwapInterval(1);
   150     } else {
   151         SDL_GL_SetSwapInterval(0);
   152     }
   153 
   154     SDL_GetCurrentDisplayMode(&mode);
   155     printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
   156     printf("\n");
   157     printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   158     printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   159     printf("Version    : %s\n", glGetString(GL_VERSION));
   160     printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   161     printf("\n");
   162 
   163     SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   164     printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   165     SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   166     printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   167     SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   168     printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   169     SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   170     printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   171     if (fsaa) {
   172         SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   173         printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   174         SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   175         printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   176                value);
   177     }
   178     if (accel) {
   179         SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   180         printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   181     }
   182 
   183     /* Set rendering settings */
   184     glMatrixMode(GL_PROJECTION);
   185     glLoadIdentity();
   186     glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   187     glMatrixMode(GL_MODELVIEW);
   188     glLoadIdentity();
   189     glEnable(GL_DEPTH_TEST);
   190     glDepthFunc(GL_LESS);
   191     glShadeModel(GL_SMOOTH);
   192 
   193     /* Main render loop */
   194     frames = 0;
   195     then = SDL_GetTicks();
   196     done = 0;
   197     while (!done) {
   198         /* Check for events */
   199         ++frames;
   200         while (SDL_PollEvent(&event)) {
   201             CommonEvent(state, &event, &done);
   202         }
   203         for (i = 0; i < state->num_windows; ++i) {
   204             int w, h;
   205             SDL_GL_MakeCurrent(state->windows[i], context);
   206             SDL_GetWindowSize(state->windows[i], &w, &h);
   207             glViewport(0, 0, w, h);
   208             Render();
   209             SDL_GL_SwapWindow(state->windows[i]);
   210         }
   211     }
   212 
   213     /* Print out some timing information */
   214     now = SDL_GetTicks();
   215     if (now > then) {
   216         printf("%2.2f frames per second\n",
   217                ((double) frames * 1000) / (now - then));
   218     }
   219     quit(0);
   220     return 0;
   221 }
   222 
   223 #else /* HAVE_OPENGLES */
   224 
   225 int
   226 main(int argc, char *argv[])
   227 {
   228     printf("No OpenGL ES support on this system\n");
   229     return 1;
   230 }
   231 
   232 #endif /* HAVE_OPENGLES */