test/testgl2.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5244 762e40fb8e28
child 6382 64d54101773a
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     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         SDL_GL_SetSwapInterval(1);
   244     } else {
   245         SDL_GL_SetSwapInterval(0);
   246     }
   247 
   248     SDL_GetCurrentDisplayMode(0, &mode);
   249     printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
   250     printf("\n");
   251     printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   252     printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   253     printf("Version    : %s\n", glGetString(GL_VERSION));
   254     printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   255     printf("\n");
   256 
   257     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   258     if (!status) {
   259         printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   260     } else {
   261         printf("Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
   262     }
   263     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   264     if (!status) {
   265         printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   266     } else {
   267         printf("Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
   268     }
   269     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   270     if (!status) {
   271         printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   272     } else {
   273         printf("Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
   274     }
   275     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   276     if (!status) {
   277         printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   278     } else {
   279         printf("Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
   280     }
   281     if (fsaa) {
   282         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   283         if (!status) {
   284             printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   285         } else {
   286             printf("Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   287                    SDL_GetError());
   288         }
   289         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   290         if (!status) {
   291             printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   292                    value);
   293         } else {
   294             printf("Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   295                    SDL_GetError());
   296         }
   297     }
   298     if (accel >= 0) {
   299         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   300         if (!status) {
   301             printf("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
   302                    value);
   303         } else {
   304             printf("Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   305                    SDL_GetError());
   306         }
   307     }
   308 
   309     /* Set rendering settings */
   310     glMatrixMode(GL_PROJECTION);
   311     glLoadIdentity();
   312     glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   313     glMatrixMode(GL_MODELVIEW);
   314     glLoadIdentity();
   315     glEnable(GL_DEPTH_TEST);
   316     glDepthFunc(GL_LESS);
   317     glShadeModel(GL_SMOOTH);
   318 
   319     /* Main render loop */
   320     frames = 0;
   321     then = SDL_GetTicks();
   322     done = 0;
   323     while (!done) {
   324         /* Check for events */
   325         ++frames;
   326         while (SDL_PollEvent(&event)) {
   327             CommonEvent(state, &event, &done);
   328         }
   329         for (i = 0; i < state->num_windows; ++i) {
   330             int w, h;
   331             SDL_GL_MakeCurrent(state->windows[i], context);
   332             SDL_GetWindowSize(state->windows[i], &w, &h);
   333             glViewport(0, 0, w, h);
   334             Render();
   335             SDL_GL_SwapWindow(state->windows[i]);
   336         }
   337     }
   338 
   339     /* Print out some timing information */
   340     now = SDL_GetTicks();
   341     if (now > then) {
   342         printf("%2.2f frames per second\n",
   343                ((double) frames * 1000) / (now - then));
   344     }
   345     quit(0);
   346     return 0;
   347 }
   348 
   349 #else /* HAVE_OPENGL */
   350 
   351 int
   352 main(int argc, char *argv[])
   353 {
   354     printf("No OpenGL support on this system\n");
   355     return 1;
   356 }
   357 
   358 #endif /* HAVE_OPENGL */