test/testgl2.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 14 Aug 2013 23:30:10 -0700
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 7746 6a05d7352575
permissions -rw-r--r--
Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
     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     /* Enable standard application logging */
   185     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   186 
   187     /* Initialize parameters */
   188     fsaa = 0;
   189     accel = -1;
   190 
   191     /* Initialize test framework */
   192     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   193     if (!state) {
   194         return 1;
   195     }
   196     for (i = 1; i < argc;) {
   197         int consumed;
   198 
   199         consumed = SDLTest_CommonArg(state, i);
   200         if (consumed == 0) {
   201             if (SDL_strcasecmp(argv[i], "--fsaa") == 0 && i+1 < argc) {
   202                 fsaa = atoi(argv[i+1]);
   203                 consumed = 2;
   204             } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i+1 < argc) {
   205                 accel = atoi(argv[i+1]);
   206                 consumed = 2;
   207             } else {
   208                 consumed = -1;
   209             }
   210         }
   211         if (consumed < 0) {
   212             SDL_Log("Usage: %s %s [--fsaa n] [--accel n]\n", argv[0],
   213                     SDLTest_CommonUsage(state));
   214             quit(1);
   215         }
   216         i += consumed;
   217     }
   218 
   219     /* Set OpenGL parameters */
   220     state->window_flags |= SDL_WINDOW_OPENGL;
   221     state->gl_red_size = 5;
   222     state->gl_green_size = 5;
   223     state->gl_blue_size = 5;
   224     state->gl_depth_size = 16;
   225     state->gl_double_buffer = 1;
   226     if (fsaa) {
   227         state->gl_multisamplebuffers = 1;
   228         state->gl_multisamplesamples = fsaa;
   229     }
   230     if (accel >= 0) {
   231         state->gl_accelerated = accel;
   232     }
   233 
   234     if (!SDLTest_CommonInit(state)) {
   235         quit(2);
   236     }
   237 
   238     /* Create OpenGL context */
   239     context = SDL_GL_CreateContext(state->windows[0]);
   240     if (!context) {
   241         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   242         quit(2);
   243     }
   244 
   245     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   246         /* try late-swap-tearing first. If not supported, try normal vsync. */
   247         if (SDL_GL_SetSwapInterval(-1) == -1) {
   248             SDL_GL_SetSwapInterval(1);
   249         }
   250     } else {
   251         SDL_GL_SetSwapInterval(0);  /* disable vsync. */
   252     }
   253 
   254     SDL_GetCurrentDisplayMode(0, &mode);
   255     SDL_Log("Screen BPP    : %d\n", SDL_BITSPERPIXEL(mode.format));
   256     SDL_Log("Swap Interval : %d\n", SDL_GL_GetSwapInterval());
   257     SDL_Log("\n");
   258     SDL_Log("Vendor        : %s\n", glGetString(GL_VENDOR));
   259     SDL_Log("Renderer      : %s\n", glGetString(GL_RENDERER));
   260     SDL_Log("Version       : %s\n", glGetString(GL_VERSION));
   261     SDL_Log("Extensions    : %s\n", glGetString(GL_EXTENSIONS));
   262     SDL_Log("\n");
   263 
   264     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   265     if (!status) {
   266         SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   267     } else {
   268         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
   269     }
   270     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   271     if (!status) {
   272         SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   273     } else {
   274         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
   275     }
   276     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   277     if (!status) {
   278         SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   279     } else {
   280         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
   281     }
   282     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   283     if (!status) {
   284         SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   285     } else {
   286         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
   287     }
   288     if (fsaa) {
   289         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   290         if (!status) {
   291             SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   292         } else {
   293             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   294                    SDL_GetError());
   295         }
   296         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   297         if (!status) {
   298             SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   299                    value);
   300         } else {
   301             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   302                    SDL_GetError());
   303         }
   304     }
   305     if (accel >= 0) {
   306         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   307         if (!status) {
   308             SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
   309                    value);
   310         } else {
   311 			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   312                    SDL_GetError());
   313         }
   314     }
   315 
   316     /* Set rendering settings */
   317     glMatrixMode(GL_PROJECTION);
   318     glLoadIdentity();
   319     glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   320     glMatrixMode(GL_MODELVIEW);
   321     glLoadIdentity();
   322     glEnable(GL_DEPTH_TEST);
   323     glDepthFunc(GL_LESS);
   324     glShadeModel(GL_SMOOTH);
   325 
   326     /* Main render loop */
   327     frames = 0;
   328     then = SDL_GetTicks();
   329     done = 0;
   330     while (!done) {
   331         /* Check for events */
   332         ++frames;
   333         while (SDL_PollEvent(&event)) {
   334             SDLTest_CommonEvent(state, &event, &done);
   335         }
   336         for (i = 0; i < state->num_windows; ++i) {
   337             int w, h;
   338             SDL_GL_MakeCurrent(state->windows[i], context);
   339             SDL_GetWindowSize(state->windows[i], &w, &h);
   340             glViewport(0, 0, w, h);
   341             Render();
   342             SDL_GL_SwapWindow(state->windows[i]);
   343         }
   344     }
   345 
   346     /* Print out some timing information */
   347     now = SDL_GetTicks();
   348     if (now > then) {
   349         SDL_Log("%2.2f frames per second\n",
   350                ((double) frames * 1000) / (now - then));
   351     }
   352     quit(0);
   353     return 0;
   354 }
   355 
   356 #else /* HAVE_OPENGL */
   357 
   358 int
   359 main(int argc, char *argv[])
   360 {
   361     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system\n");
   362     return 1;
   363 }
   364 
   365 #endif /* HAVE_OPENGL */