test/testgl2.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 10 Feb 2011 12:14:37 -0800
changeset 5244 762e40fb8e28
parent 3571 19691cebb866
child 5535 96594ac5fd1a
permissions -rw-r--r--
Be explicit about what display you're querying. The default display is 0.
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <math.h>
     5 
     6 #include "common.h"
     7 
     8 #ifdef __MACOS__
     9 #define HAVE_OPENGL
    10 #endif
    11 
    12 #ifdef HAVE_OPENGL
    13 
    14 #include "SDL_opengl.h"
    15 
    16 /* Undefine this if you want a flat cube instead of a rainbow cube */
    17 #define SHADED_CUBE
    18 
    19 static CommonState *state;
    20 static SDL_GLContext context;
    21 
    22 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    23 static void
    24 quit(int rc)
    25 {
    26     if (context) {
    27         /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
    28         SDL_GL_DeleteContext(context);
    29     }
    30     CommonQuit(state);
    31     exit(rc);
    32 }
    33 
    34 static void
    35 Render()
    36 {
    37     static float color[8][3] = {
    38         {1.0, 1.0, 0.0},
    39         {1.0, 0.0, 0.0},
    40         {0.0, 0.0, 0.0},
    41         {0.0, 1.0, 0.0},
    42         {0.0, 1.0, 1.0},
    43         {1.0, 1.0, 1.0},
    44         {1.0, 0.0, 1.0},
    45         {0.0, 0.0, 1.0}
    46     };
    47     static float cube[8][3] = {
    48         {0.5, 0.5, -0.5},
    49         {0.5, -0.5, -0.5},
    50         {-0.5, -0.5, -0.5},
    51         {-0.5, 0.5, -0.5},
    52         {-0.5, 0.5, 0.5},
    53         {0.5, 0.5, 0.5},
    54         {0.5, -0.5, 0.5},
    55         {-0.5, -0.5, 0.5}
    56     };
    57 
    58     /* Do our drawing, too. */
    59     glClearColor(0.0, 0.0, 0.0, 1.0);
    60     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    61 
    62     glBegin(GL_QUADS);
    63 
    64 #ifdef SHADED_CUBE
    65     glColor3fv(color[0]);
    66     glVertex3fv(cube[0]);
    67     glColor3fv(color[1]);
    68     glVertex3fv(cube[1]);
    69     glColor3fv(color[2]);
    70     glVertex3fv(cube[2]);
    71     glColor3fv(color[3]);
    72     glVertex3fv(cube[3]);
    73 
    74     glColor3fv(color[3]);
    75     glVertex3fv(cube[3]);
    76     glColor3fv(color[4]);
    77     glVertex3fv(cube[4]);
    78     glColor3fv(color[7]);
    79     glVertex3fv(cube[7]);
    80     glColor3fv(color[2]);
    81     glVertex3fv(cube[2]);
    82 
    83     glColor3fv(color[0]);
    84     glVertex3fv(cube[0]);
    85     glColor3fv(color[5]);
    86     glVertex3fv(cube[5]);
    87     glColor3fv(color[6]);
    88     glVertex3fv(cube[6]);
    89     glColor3fv(color[1]);
    90     glVertex3fv(cube[1]);
    91 
    92     glColor3fv(color[5]);
    93     glVertex3fv(cube[5]);
    94     glColor3fv(color[4]);
    95     glVertex3fv(cube[4]);
    96     glColor3fv(color[7]);
    97     glVertex3fv(cube[7]);
    98     glColor3fv(color[6]);
    99     glVertex3fv(cube[6]);
   100 
   101     glColor3fv(color[5]);
   102     glVertex3fv(cube[5]);
   103     glColor3fv(color[0]);
   104     glVertex3fv(cube[0]);
   105     glColor3fv(color[3]);
   106     glVertex3fv(cube[3]);
   107     glColor3fv(color[4]);
   108     glVertex3fv(cube[4]);
   109 
   110     glColor3fv(color[6]);
   111     glVertex3fv(cube[6]);
   112     glColor3fv(color[1]);
   113     glVertex3fv(cube[1]);
   114     glColor3fv(color[2]);
   115     glVertex3fv(cube[2]);
   116     glColor3fv(color[7]);
   117     glVertex3fv(cube[7]);
   118 #else /* flat cube */
   119     glColor3f(1.0, 0.0, 0.0);
   120     glVertex3fv(cube[0]);
   121     glVertex3fv(cube[1]);
   122     glVertex3fv(cube[2]);
   123     glVertex3fv(cube[3]);
   124 
   125     glColor3f(0.0, 1.0, 0.0);
   126     glVertex3fv(cube[3]);
   127     glVertex3fv(cube[4]);
   128     glVertex3fv(cube[7]);
   129     glVertex3fv(cube[2]);
   130 
   131     glColor3f(0.0, 0.0, 1.0);
   132     glVertex3fv(cube[0]);
   133     glVertex3fv(cube[5]);
   134     glVertex3fv(cube[6]);
   135     glVertex3fv(cube[1]);
   136 
   137     glColor3f(0.0, 1.0, 1.0);
   138     glVertex3fv(cube[5]);
   139     glVertex3fv(cube[4]);
   140     glVertex3fv(cube[7]);
   141     glVertex3fv(cube[6]);
   142 
   143     glColor3f(1.0, 1.0, 0.0);
   144     glVertex3fv(cube[5]);
   145     glVertex3fv(cube[0]);
   146     glVertex3fv(cube[3]);
   147     glVertex3fv(cube[4]);
   148 
   149     glColor3f(1.0, 0.0, 1.0);
   150     glVertex3fv(cube[6]);
   151     glVertex3fv(cube[1]);
   152     glVertex3fv(cube[2]);
   153     glVertex3fv(cube[7]);
   154 #endif /* SHADED_CUBE */
   155 
   156     glEnd();
   157 
   158     glMatrixMode(GL_MODELVIEW);
   159     glRotatef(5.0, 1.0, 1.0, 1.0);
   160 }
   161 
   162 int
   163 main(int argc, char *argv[])
   164 {
   165     int fsaa, accel;
   166     int value;
   167     int i, done;
   168     SDL_DisplayMode mode;
   169     SDL_Event event;
   170     Uint32 then, now, frames;
   171     int status;
   172 
   173     /* Initialize parameters */
   174     fsaa = 0;
   175     accel = -1;
   176 
   177     /* Initialize test framework */
   178     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   179     if (!state) {
   180         return 1;
   181     }
   182     for (i = 1; i < argc;) {
   183         int consumed;
   184 
   185         consumed = CommonArg(state, i);
   186         if (consumed == 0) {
   187             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   188                 ++fsaa;
   189                 consumed = 1;
   190             } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i+1 < argc) {
   191                 accel = atoi(argv[i+1]);
   192                 consumed = 2;
   193             } else {
   194                 consumed = -1;
   195             }
   196         }
   197         if (consumed < 0) {
   198             fprintf(stderr, "Usage: %s %s [--fsaa] [--accel n]\n", argv[0],
   199                     CommonUsage(state));
   200             quit(1);
   201         }
   202         i += consumed;
   203     }
   204 
   205     /* Set OpenGL parameters */
   206     state->window_flags |= SDL_WINDOW_OPENGL;
   207     state->gl_red_size = 5;
   208     state->gl_green_size = 5;
   209     state->gl_blue_size = 5;
   210     state->gl_depth_size = 16;
   211     state->gl_double_buffer = 1;
   212     if (fsaa) {
   213         state->gl_multisamplebuffers = 1;
   214         state->gl_multisamplesamples = fsaa;
   215     }
   216     if (accel >= 0) {
   217         state->gl_accelerated = accel;
   218     }
   219 
   220     if (!CommonInit(state)) {
   221         quit(2);
   222     }
   223 
   224     /* Create OpenGL context */
   225     context = SDL_GL_CreateContext(state->windows[0]);
   226     if (!context) {
   227         fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   228         quit(2);
   229     }
   230 
   231     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   232         SDL_GL_SetSwapInterval(1);
   233     } else {
   234         SDL_GL_SetSwapInterval(0);
   235     }
   236 
   237     SDL_GetCurrentDisplayMode(0, &mode);
   238     printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
   239     printf("\n");
   240     printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   241     printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   242     printf("Version    : %s\n", glGetString(GL_VERSION));
   243     printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   244     printf("\n");
   245 
   246     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   247     if (!status) {
   248         printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   249     } else {
   250         printf("Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
   251     }
   252     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   253     if (!status) {
   254         printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   255     } else {
   256         printf("Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
   257     }
   258     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   259     if (!status) {
   260         printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   261     } else {
   262         printf("Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
   263     }
   264     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   265     if (!status) {
   266         printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   267     } else {
   268         printf("Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
   269     }
   270     if (fsaa) {
   271         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   272         if (!status) {
   273             printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   274         } else {
   275             printf("Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   276                    SDL_GetError());
   277         }
   278         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   279         if (!status) {
   280             printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   281                    value);
   282         } else {
   283             printf("Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   284                    SDL_GetError());
   285         }
   286     }
   287     if (accel >= 0) {
   288         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   289         if (!status) {
   290             printf("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
   291                    value);
   292         } else {
   293             printf("Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   294                    SDL_GetError());
   295         }
   296     }
   297 
   298     /* Set rendering settings */
   299     glMatrixMode(GL_PROJECTION);
   300     glLoadIdentity();
   301     glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   302     glMatrixMode(GL_MODELVIEW);
   303     glLoadIdentity();
   304     glEnable(GL_DEPTH_TEST);
   305     glDepthFunc(GL_LESS);
   306     glShadeModel(GL_SMOOTH);
   307 
   308     /* Main render loop */
   309     frames = 0;
   310     then = SDL_GetTicks();
   311     done = 0;
   312     while (!done) {
   313         /* Check for events */
   314         ++frames;
   315         while (SDL_PollEvent(&event)) {
   316             CommonEvent(state, &event, &done);
   317         }
   318         for (i = 0; i < state->num_windows; ++i) {
   319             int w, h;
   320             SDL_GL_MakeCurrent(state->windows[i], context);
   321             SDL_GetWindowSize(state->windows[i], &w, &h);
   322             glViewport(0, 0, w, h);
   323             Render();
   324             SDL_GL_SwapWindow(state->windows[i]);
   325         }
   326     }
   327 
   328     /* Print out some timing information */
   329     now = SDL_GetTicks();
   330     if (now > then) {
   331         printf("%2.2f frames per second\n",
   332                ((double) frames * 1000) / (now - then));
   333     }
   334     quit(0);
   335     return 0;
   336 }
   337 
   338 #else /* HAVE_OPENGL */
   339 
   340 int
   341 main(int argc, char *argv[])
   342 {
   343     printf("No OpenGL support on this system\n");
   344     return 1;
   345 }
   346 
   347 #endif /* HAVE_OPENGL */