test/testgl2.c
author David Ludwig <dludwig@pobox.com>
Wed, 25 Dec 2013 21:39:48 -0500
changeset 8563 c0e68f3b6bbb
parent 7787 e6f3e8fc96ea
child 8062 4fc5f66d63cc
permissions -rw-r--r--
WinRT: compiled the d3d11 renderer's shaders into SDL itself

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