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