test/testgl2.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 01 Aug 2012 20:29:36 -0400
changeset 6382 64d54101773a
parent 5535 96594ac5fd1a
child 6785 c094c438c0a6
permissions -rw-r--r--
Add support for (GLX|WGL)_EXT_swap_control_tear.

This required a small public API change: SDL_GL_SetSwapInterval() now accepts
negative values, and SDL_GL_GetSwapInterval() doesn't report errors anymore
(if it can't work, it'll return zero as a reasonable default).

If you need to test for errors, such as a lack of swap_control_tear support,
check the results of SDL_GL_SetSwapInterval() when you set your desired
value.
     1 /*
     2   Copyright (C) 1997-2011 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 "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 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     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 = CommonCreateState(argv, SDL_INIT_VIDEO);
   190     if (!state) {
   191         return 1;
   192     }
   193     for (i = 1; i < argc;) {
   194         int consumed;
   195 
   196         consumed = CommonArg(state, i);
   197         if (consumed == 0) {
   198             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   199                 ++fsaa;
   200                 consumed = 1;
   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] [--accel n]\n", argv[0],
   210                     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 (!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("\n");
   259 
   260     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   261     if (!status) {
   262         printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   263     } else {
   264         printf("Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
   265     }
   266     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   267     if (!status) {
   268         printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   269     } else {
   270         printf("Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
   271     }
   272     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   273     if (!status) {
   274         printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   275     } else {
   276         printf("Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
   277     }
   278     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   279     if (!status) {
   280         printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   281     } else {
   282         printf("Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
   283     }
   284     if (fsaa) {
   285         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   286         if (!status) {
   287             printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   288         } else {
   289             printf("Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   290                    SDL_GetError());
   291         }
   292         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   293         if (!status) {
   294             printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   295                    value);
   296         } else {
   297             printf("Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   298                    SDL_GetError());
   299         }
   300     }
   301     if (accel >= 0) {
   302         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   303         if (!status) {
   304             printf("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
   305                    value);
   306         } else {
   307             printf("Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   308                    SDL_GetError());
   309         }
   310     }
   311 
   312     /* Set rendering settings */
   313     glMatrixMode(GL_PROJECTION);
   314     glLoadIdentity();
   315     glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   316     glMatrixMode(GL_MODELVIEW);
   317     glLoadIdentity();
   318     glEnable(GL_DEPTH_TEST);
   319     glDepthFunc(GL_LESS);
   320     glShadeModel(GL_SMOOTH);
   321 
   322     /* Main render loop */
   323     frames = 0;
   324     then = SDL_GetTicks();
   325     done = 0;
   326     while (!done) {
   327         /* Check for events */
   328         ++frames;
   329         while (SDL_PollEvent(&event)) {
   330             CommonEvent(state, &event, &done);
   331         }
   332         for (i = 0; i < state->num_windows; ++i) {
   333             int w, h;
   334             SDL_GL_MakeCurrent(state->windows[i], context);
   335             SDL_GetWindowSize(state->windows[i], &w, &h);
   336             glViewport(0, 0, w, h);
   337             Render();
   338             SDL_GL_SwapWindow(state->windows[i]);
   339         }
   340     }
   341 
   342     /* Print out some timing information */
   343     now = SDL_GetTicks();
   344     if (now > then) {
   345         printf("%2.2f frames per second\n",
   346                ((double) frames * 1000) / (now - then));
   347     }
   348     quit(0);
   349     return 0;
   350 }
   351 
   352 #else /* HAVE_OPENGL */
   353 
   354 int
   355 main(int argc, char *argv[])
   356 {
   357     printf("No OpenGL support on this system\n");
   358     return 1;
   359 }
   360 
   361 #endif /* HAVE_OPENGL */