test/testgles.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Apr 2013 00:26:17 -0700
changeset 7110 2a98852fd58d
parent 6785 c094c438c0a6
child 7517 965d57022c01
permissions -rw-r--r--
Fixed bug 1582 - Allow disabling SDL_VIDEO_DRIVER_WINDOWS

Marcus von Appen

Trying to build SDL 2.x (HG) on Win32 platforms with either VS.NET or MinGW requires one to have the video subsystem and SDL_VIDEO_DRIVER_WINDOWS flag enabled due to the helper window creation routines.

The attached patch changes the helper window creation behaviour, so that one can build SDL2 without the video subsystem or Windows video drivers on Win32 platforms.
     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 "SDL_test_common.h"
    18 
    19 #if defined(__IPHONEOS__) || defined(__ANDROID__)
    20 #define HAVE_OPENGLES
    21 #endif
    22 
    23 #ifdef HAVE_OPENGLES
    24 
    25 #include "SDL_opengles.h"
    26 
    27 static SDLTest_CommonState *state;
    28 static SDL_GLContext *context = NULL;
    29 static int depth = 16;
    30 
    31 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    32 static void
    33 quit(int rc)
    34 {
    35     int i;
    36 
    37     if (context != NULL) {
    38         for (i = 0; i < state->num_windows; i++) {
    39             if (context[i]) {
    40                 SDL_GL_DeleteContext(context[i]);
    41             }
    42         }
    43 
    44         SDL_free(context);
    45     }
    46 
    47     SDLTest_CommonQuit(state);
    48     exit(rc);
    49 }
    50 
    51 static void
    52 Render()
    53 {
    54     static GLubyte color[8][4] = { {255, 0, 0, 0},
    55     {255, 0, 0, 255},
    56     {0, 255, 0, 255},
    57     {0, 255, 0, 255},
    58     {0, 255, 0, 255},
    59     {255, 255, 255, 255},
    60     {255, 0, 255, 255},
    61     {0, 0, 255, 255}
    62     };
    63     static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
    64     {0.5f, -0.5f, -0.5f},
    65     {-0.5f, -0.5f, -0.5f},
    66     {-0.5f, 0.5f, -0.5f},
    67     {-0.5f, 0.5f, 0.5f},
    68     {0.5f, 0.5f, 0.5f},
    69     {0.5f, -0.5f, 0.5f},
    70     {-0.5f, -0.5f, 0.5f}
    71     };
    72     static GLubyte indices[36] = { 0, 3, 4,
    73         4, 5, 0,
    74         0, 5, 6,
    75         6, 1, 0,
    76         6, 7, 2,
    77         2, 1, 6,
    78         7, 4, 3,
    79         3, 2, 7,
    80         5, 4, 7,
    81         7, 6, 5,
    82         2, 3, 1,
    83         3, 0, 1
    84     };
    85 
    86 
    87     /* Do our drawing, too. */
    88     glClearColor(0.0, 0.0, 0.0, 1.0);
    89     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    90 
    91     /* Draw the cube */
    92     glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    93     glEnableClientState(GL_COLOR_ARRAY);
    94     glVertexPointer(3, GL_FLOAT, 0, cube);
    95     glEnableClientState(GL_VERTEX_ARRAY);
    96     glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
    97 
    98     glMatrixMode(GL_MODELVIEW);
    99     glRotatef(5.0, 1.0, 1.0, 1.0);
   100 }
   101 
   102 int
   103 main(int argc, char *argv[])
   104 {
   105     int fsaa, accel;
   106     int value;
   107     int i, done;
   108     SDL_DisplayMode mode;
   109     SDL_Event event;
   110     Uint32 then, now, frames;
   111     int status;
   112 
   113     /* Initialize parameters */
   114     fsaa = 0;
   115     accel = 0;
   116 
   117     /* Initialize test framework */
   118     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   119     if (!state) {
   120         return 1;
   121     }
   122     for (i = 1; i < argc;) {
   123         int consumed;
   124 
   125         consumed = SDLTest_CommonArg(state, i);
   126         if (consumed == 0) {
   127             if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   128                 ++fsaa;
   129                 consumed = 1;
   130             } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   131                 ++accel;
   132                 consumed = 1;
   133             } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
   134                 i++;
   135                 if (!argv[i]) {
   136                     consumed = -1;
   137                 } else {
   138                     depth = SDL_atoi(argv[i]);
   139                     consumed = 1;
   140                 }
   141             } else {
   142                 consumed = -1;
   143             }
   144         }
   145         if (consumed < 0) {
   146             fprintf(stderr, "Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
   147                     SDLTest_CommonUsage(state));
   148             quit(1);
   149         }
   150         i += consumed;
   151     }
   152 
   153     /* Set OpenGL parameters */
   154     state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
   155     state->gl_red_size = 5;
   156     state->gl_green_size = 5;
   157     state->gl_blue_size = 5;
   158     state->gl_depth_size = depth;
   159     if (fsaa) {
   160         state->gl_multisamplebuffers=1;
   161         state->gl_multisamplesamples=fsaa;
   162     }
   163     if (accel) {
   164         state->gl_accelerated=1;
   165     }
   166     if (!SDLTest_CommonInit(state)) {
   167         quit(2);
   168     }
   169 
   170     context = SDL_calloc(state->num_windows, sizeof(context));
   171     if (context == NULL) {
   172         fprintf(stderr, "Out of memory!\n");
   173         quit(2);
   174     }
   175 
   176     /* Create OpenGL ES contexts */
   177     for (i = 0; i < state->num_windows; i++) {
   178         context[i] = SDL_GL_CreateContext(state->windows[i]);
   179         if (!context[i]) {
   180             fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   181             quit(2);
   182         }
   183     }
   184 
   185     if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   186         SDL_GL_SetSwapInterval(1);
   187     } else {
   188         SDL_GL_SetSwapInterval(0);
   189     }
   190 
   191     SDL_GetCurrentDisplayMode(0, &mode);
   192     printf("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
   193     printf("\n");
   194     printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   195     printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   196     printf("Version    : %s\n", glGetString(GL_VERSION));
   197     printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   198     printf("\n");
   199 
   200     status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   201     if (!status) {
   202         printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   203     } else {
   204         fprintf(stderr, "Failed to get SDL_GL_RED_SIZE: %s\n",
   205                 SDL_GetError());
   206     }
   207     status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   208     if (!status) {
   209         printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   210     } else {
   211         fprintf(stderr, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
   212                 SDL_GetError());
   213     }
   214     status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   215     if (!status) {
   216         printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   217     } else {
   218         fprintf(stderr, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
   219                 SDL_GetError());
   220     }
   221     status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   222     if (!status) {
   223         printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
   224     } else {
   225         fprintf(stderr, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
   226                 SDL_GetError());
   227     }
   228     if (fsaa) {
   229         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   230         if (!status) {
   231             printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   232         } else {
   233             fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
   234                     SDL_GetError());
   235         }
   236         status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   237         if (!status) {
   238             printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   239                    value);
   240         } else {
   241             fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
   242                     SDL_GetError());
   243         }
   244     }
   245     if (accel) {
   246         status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   247         if (!status) {
   248             printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   249         } else {
   250             fprintf(stderr, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
   251                     SDL_GetError());
   252         }
   253     }
   254 
   255     /* Set rendering settings for each context */
   256     for (i = 0; i < state->num_windows; ++i) {
   257         float aspectAdjust;
   258 
   259         status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   260         if (status) {
   261             printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   262 
   263             /* Continue for next window */
   264             continue;
   265         }
   266 
   267         aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h);
   268         glViewport(0, 0, state->window_w, state->window_h);
   269         glMatrixMode(GL_PROJECTION);
   270         glLoadIdentity();
   271         glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
   272         glMatrixMode(GL_MODELVIEW);
   273         glLoadIdentity();
   274         glEnable(GL_DEPTH_TEST);
   275         glDepthFunc(GL_LESS);
   276         glShadeModel(GL_SMOOTH);
   277     }
   278 
   279     /* Main render loop */
   280     frames = 0;
   281     then = SDL_GetTicks();
   282     done = 0;
   283     while (!done) {
   284         /* Check for events */
   285         ++frames;
   286         while (SDL_PollEvent(&event)) {
   287             switch (event.type) {
   288             case SDL_WINDOWEVENT:
   289                 switch (event.window.event) {
   290                     case SDL_WINDOWEVENT_RESIZED:
   291                         for (i = 0; i < state->num_windows; ++i) {
   292                             if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
   293                                 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   294                                 if (status) {
   295                                     printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   296                                     break;
   297                                 }
   298                                 /* Change view port to the new window dimensions */
   299                                 glViewport(0, 0, event.window.data1, event.window.data2);
   300                                 /* Update window content */
   301                                 Render();
   302                                 SDL_GL_SwapWindow(state->windows[i]);
   303                                 break;
   304                             }
   305                         }
   306                         break;
   307                 }
   308             }
   309             SDLTest_CommonEvent(state, &event, &done);
   310         }
   311         for (i = 0; i < state->num_windows; ++i) {
   312             status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
   313             if (status) {
   314                 printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   315 
   316                 /* Continue for next window */
   317                 continue;
   318             }
   319             Render();
   320             SDL_GL_SwapWindow(state->windows[i]);
   321         }
   322     }
   323 
   324     /* Print out some timing information */
   325     now = SDL_GetTicks();
   326     if (now > then) {
   327         printf("%2.2f frames per second\n",
   328                ((double) frames * 1000) / (now - then));
   329     }
   330     quit(0);
   331     return 0;
   332 }
   333 
   334 #else /* HAVE_OPENGLES */
   335 
   336 int
   337 main(int argc, char *argv[])
   338 {
   339     printf("No OpenGL ES support on this system\n");
   340     return 1;
   341 }
   342 
   343 #endif /* HAVE_OPENGLES */
   344 
   345 /* vi: set ts=4 sw=4 expandtab: */