test/testgl2.c
changeset 1915 a228436a2404
parent 1914 051df511279c
child 1916 c773b0c0ac89
     1.1 --- a/test/testgl2.c	Tue Jul 18 07:49:51 2006 +0000
     1.2 +++ b/test/testgl2.c	Wed Jul 19 04:24:41 2006 +0000
     1.3 @@ -3,7 +3,7 @@
     1.4  #include <string.h>
     1.5  #include <math.h>
     1.6  
     1.7 -#include "SDL.h"
     1.8 +#include "common.h"
     1.9  
    1.10  #ifdef __MACOS__
    1.11  #define HAVE_OPENGL
    1.12 @@ -16,438 +16,195 @@
    1.13  /* Undefine this if you want a flat cube instead of a rainbow cube */
    1.14  #define SHADED_CUBE
    1.15  
    1.16 -/* Define this to be the name of the logo image to use with -logo */
    1.17 -#define LOGO_FILE	"icon.bmp"
    1.18 +static CommonState *state;
    1.19 +static SDL_GLContext context;
    1.20  
    1.21 -static SDL_Surface *global_image = NULL;
    1.22 -static GLuint global_texture = 0;
    1.23 -static GLuint cursor_texture = 0;
    1.24 -
    1.25 -/**********************************************************************/
    1.26 -
    1.27 -void
    1.28 -HotKey_ToggleFullScreen(void)
    1.29 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.30 +static void
    1.31 +quit(int rc)
    1.32  {
    1.33 -    SDL_Surface *screen;
    1.34 -
    1.35 -    screen = SDL_GetVideoSurface();
    1.36 -    if (SDL_WM_ToggleFullScreen(screen)) {
    1.37 -        printf("Toggled fullscreen mode - now %s\n",
    1.38 -               (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
    1.39 -    } else {
    1.40 -        printf("Unable to toggle fullscreen mode\n");
    1.41 +    if (context) {
    1.42 +        SDL_GL_MakeCurrent(0, NULL);
    1.43 +        SDL_GL_DeleteContext(context);
    1.44      }
    1.45 +    CommonQuit(state);
    1.46 +    exit(rc);
    1.47  }
    1.48  
    1.49 -void
    1.50 -HotKey_ToggleGrab(void)
    1.51 +static void
    1.52 +Render()
    1.53  {
    1.54 -    SDL_GrabMode mode;
    1.55 +    static float color[8][3] = {
    1.56 +        {1.0, 1.0, 0.0},
    1.57 +        {1.0, 0.0, 0.0},
    1.58 +        {0.0, 0.0, 0.0},
    1.59 +        {0.0, 1.0, 0.0},
    1.60 +        {0.0, 1.0, 1.0},
    1.61 +        {1.0, 1.0, 1.0},
    1.62 +        {1.0, 0.0, 1.0},
    1.63 +        {0.0, 0.0, 1.0}
    1.64 +    };
    1.65 +    static float cube[8][3] = {
    1.66 +        {0.5, 0.5, -0.5},
    1.67 +        {0.5, -0.5, -0.5},
    1.68 +        {-0.5, -0.5, -0.5},
    1.69 +        {-0.5, 0.5, -0.5},
    1.70 +        {-0.5, 0.5, 0.5},
    1.71 +        {0.5, 0.5, 0.5},
    1.72 +        {0.5, -0.5, 0.5},
    1.73 +        {-0.5, -0.5, 0.5}
    1.74 +    };
    1.75  
    1.76 -    printf("Ctrl-G: toggling input grab!\n");
    1.77 -    mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
    1.78 -    if (mode == SDL_GRAB_ON) {
    1.79 -        printf("Grab was on\n");
    1.80 -    } else {
    1.81 -        printf("Grab was off\n");
    1.82 -    }
    1.83 -    mode = SDL_WM_GrabInput(!mode);
    1.84 -    if (mode == SDL_GRAB_ON) {
    1.85 -        printf("Grab is now on\n");
    1.86 -    } else {
    1.87 -        printf("Grab is now off\n");
    1.88 -    }
    1.89 -}
    1.90 +    /* Do our drawing, too. */
    1.91 +    glClearColor(0.0, 0.0, 0.0, 1.0);
    1.92 +    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.93  
    1.94 -void
    1.95 -HotKey_Iconify(void)
    1.96 -{
    1.97 -    printf("Ctrl-Z: iconifying window!\n");
    1.98 -    SDL_WM_IconifyWindow();
    1.99 +    glBegin(GL_QUADS);
   1.100 +
   1.101 +#ifdef SHADED_CUBE
   1.102 +    glColor3fv(color[0]);
   1.103 +    glVertex3fv(cube[0]);
   1.104 +    glColor3fv(color[1]);
   1.105 +    glVertex3fv(cube[1]);
   1.106 +    glColor3fv(color[2]);
   1.107 +    glVertex3fv(cube[2]);
   1.108 +    glColor3fv(color[3]);
   1.109 +    glVertex3fv(cube[3]);
   1.110 +
   1.111 +    glColor3fv(color[3]);
   1.112 +    glVertex3fv(cube[3]);
   1.113 +    glColor3fv(color[4]);
   1.114 +    glVertex3fv(cube[4]);
   1.115 +    glColor3fv(color[7]);
   1.116 +    glVertex3fv(cube[7]);
   1.117 +    glColor3fv(color[2]);
   1.118 +    glVertex3fv(cube[2]);
   1.119 +
   1.120 +    glColor3fv(color[0]);
   1.121 +    glVertex3fv(cube[0]);
   1.122 +    glColor3fv(color[5]);
   1.123 +    glVertex3fv(cube[5]);
   1.124 +    glColor3fv(color[6]);
   1.125 +    glVertex3fv(cube[6]);
   1.126 +    glColor3fv(color[1]);
   1.127 +    glVertex3fv(cube[1]);
   1.128 +
   1.129 +    glColor3fv(color[5]);
   1.130 +    glVertex3fv(cube[5]);
   1.131 +    glColor3fv(color[4]);
   1.132 +    glVertex3fv(cube[4]);
   1.133 +    glColor3fv(color[7]);
   1.134 +    glVertex3fv(cube[7]);
   1.135 +    glColor3fv(color[6]);
   1.136 +    glVertex3fv(cube[6]);
   1.137 +
   1.138 +    glColor3fv(color[5]);
   1.139 +    glVertex3fv(cube[5]);
   1.140 +    glColor3fv(color[0]);
   1.141 +    glVertex3fv(cube[0]);
   1.142 +    glColor3fv(color[3]);
   1.143 +    glVertex3fv(cube[3]);
   1.144 +    glColor3fv(color[4]);
   1.145 +    glVertex3fv(cube[4]);
   1.146 +
   1.147 +    glColor3fv(color[6]);
   1.148 +    glVertex3fv(cube[6]);
   1.149 +    glColor3fv(color[1]);
   1.150 +    glVertex3fv(cube[1]);
   1.151 +    glColor3fv(color[2]);
   1.152 +    glVertex3fv(cube[2]);
   1.153 +    glColor3fv(color[7]);
   1.154 +    glVertex3fv(cube[7]);
   1.155 +#else /* flat cube */
   1.156 +    glColor3f(1.0, 0.0, 0.0);
   1.157 +    glVertex3fv(cube[0]);
   1.158 +    glVertex3fv(cube[1]);
   1.159 +    glVertex3fv(cube[2]);
   1.160 +    glVertex3fv(cube[3]);
   1.161 +
   1.162 +    glColor3f(0.0, 1.0, 0.0);
   1.163 +    glVertex3fv(cube[3]);
   1.164 +    glVertex3fv(cube[4]);
   1.165 +    glVertex3fv(cube[7]);
   1.166 +    glVertex3fv(cube[2]);
   1.167 +
   1.168 +    glColor3f(0.0, 0.0, 1.0);
   1.169 +    glVertex3fv(cube[0]);
   1.170 +    glVertex3fv(cube[5]);
   1.171 +    glVertex3fv(cube[6]);
   1.172 +    glVertex3fv(cube[1]);
   1.173 +
   1.174 +    glColor3f(0.0, 1.0, 1.0);
   1.175 +    glVertex3fv(cube[5]);
   1.176 +    glVertex3fv(cube[4]);
   1.177 +    glVertex3fv(cube[7]);
   1.178 +    glVertex3fv(cube[6]);
   1.179 +
   1.180 +    glColor3f(1.0, 1.0, 0.0);
   1.181 +    glVertex3fv(cube[5]);
   1.182 +    glVertex3fv(cube[0]);
   1.183 +    glVertex3fv(cube[3]);
   1.184 +    glVertex3fv(cube[4]);
   1.185 +
   1.186 +    glColor3f(1.0, 0.0, 1.0);
   1.187 +    glVertex3fv(cube[6]);
   1.188 +    glVertex3fv(cube[1]);
   1.189 +    glVertex3fv(cube[2]);
   1.190 +    glVertex3fv(cube[7]);
   1.191 +#endif /* SHADED_CUBE */
   1.192 +
   1.193 +    glEnd();
   1.194 +
   1.195 +    glMatrixMode(GL_MODELVIEW);
   1.196 +    glRotatef(5.0, 1.0, 1.0, 1.0);
   1.197  }
   1.198  
   1.199  int
   1.200 -HandleEvent(SDL_Event * event)
   1.201 +main(int argc, char *argv[])
   1.202  {
   1.203 -    int done;
   1.204 +    int fsaa, accel;
   1.205 +    int value;
   1.206 +    int i, done;
   1.207 +    SDL_Event event;
   1.208 +    Uint32 then, now, frames;
   1.209  
   1.210 -    done = 0;
   1.211 -    switch (event->type) {
   1.212 -    case SDL_ACTIVEEVENT:
   1.213 -        /* See what happened */
   1.214 -        printf("app %s ", event->active.gain ? "gained" : "lost");
   1.215 -        if (event->active.state & SDL_APPACTIVE) {
   1.216 -            printf("active ");
   1.217 -        } else if (event->active.state & SDL_APPMOUSEFOCUS) {
   1.218 -            printf("mouse ");
   1.219 -        } else if (event->active.state & SDL_APPINPUTFOCUS) {
   1.220 -            printf("input ");
   1.221 +    /* Initialize parameters */
   1.222 +    fsaa = 0;
   1.223 +    accel = 0;
   1.224 +
   1.225 +    /* Initialize test framework */
   1.226 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
   1.227 +    if (!state) {
   1.228 +        return 1;
   1.229 +    }
   1.230 +    for (i = 1; i < argc;) {
   1.231 +        int consumed;
   1.232 +
   1.233 +        consumed = CommonArg(state, i);
   1.234 +        if (consumed == 0) {
   1.235 +            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   1.236 +                ++fsaa;
   1.237 +                consumed = 1;
   1.238 +            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   1.239 +                ++accel;
   1.240 +                consumed = 1;
   1.241 +            } else {
   1.242 +                consumed = -1;
   1.243 +            }
   1.244          }
   1.245 -        printf("focus\n");
   1.246 -        break;
   1.247 -
   1.248 -
   1.249 -    case SDL_KEYDOWN:
   1.250 -        if (event->key.keysym.sym == SDLK_ESCAPE) {
   1.251 -            done = 1;
   1.252 +        if (consumed < 0) {
   1.253 +            fprintf(stderr, "Usage: %s [--fsaa] [--accel] %s", argv[0],
   1.254 +                    CommonUsage(state));
   1.255 +            quit(1);
   1.256          }
   1.257 -        if ((event->key.keysym.sym == SDLK_g) &&
   1.258 -            (event->key.keysym.mod & KMOD_CTRL)) {
   1.259 -            HotKey_ToggleGrab();
   1.260 -        }
   1.261 -        if ((event->key.keysym.sym == SDLK_z) &&
   1.262 -            (event->key.keysym.mod & KMOD_CTRL)) {
   1.263 -            HotKey_Iconify();
   1.264 -        }
   1.265 -        if ((event->key.keysym.sym == SDLK_RETURN) &&
   1.266 -            (event->key.keysym.mod & KMOD_ALT)) {
   1.267 -            HotKey_ToggleFullScreen();
   1.268 -        }
   1.269 -        printf("key '%s' pressed\n", SDL_GetKeyName(event->key.keysym.sym));
   1.270 -        break;
   1.271 -    case SDL_QUIT:
   1.272 -        done = 1;
   1.273 -        break;
   1.274 -    }
   1.275 -    return (done);
   1.276 -}
   1.277 -
   1.278 -void
   1.279 -SDL_GL_Enter2DMode()
   1.280 -{
   1.281 -    SDL_Surface *screen = SDL_GetVideoSurface();
   1.282 -
   1.283 -    /* Note, there may be other things you need to change,
   1.284 -       depending on how you have your OpenGL state set up.
   1.285 -     */
   1.286 -    glPushAttrib(GL_ENABLE_BIT);
   1.287 -    glDisable(GL_DEPTH_TEST);
   1.288 -    glDisable(GL_CULL_FACE);
   1.289 -    glEnable(GL_TEXTURE_2D);
   1.290 -
   1.291 -    /* This allows alpha blending of 2D textures with the scene */
   1.292 -    glEnable(GL_BLEND);
   1.293 -    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.294 -
   1.295 -    glViewport(0, 0, screen->w, screen->h);
   1.296 -
   1.297 -    glMatrixMode(GL_PROJECTION);
   1.298 -    glPushMatrix();
   1.299 -    glLoadIdentity();
   1.300 -
   1.301 -    glOrtho(0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
   1.302 -
   1.303 -    glMatrixMode(GL_MODELVIEW);
   1.304 -    glPushMatrix();
   1.305 -    glLoadIdentity();
   1.306 -
   1.307 -    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   1.308 -}
   1.309 -
   1.310 -void
   1.311 -SDL_GL_Leave2DMode()
   1.312 -{
   1.313 -    glMatrixMode(GL_MODELVIEW);
   1.314 -    glPopMatrix();
   1.315 -
   1.316 -    glMatrixMode(GL_PROJECTION);
   1.317 -    glPopMatrix();
   1.318 -
   1.319 -    glPopAttrib();
   1.320 -}
   1.321 -
   1.322 -/* Quick utility function for texture creation */
   1.323 -static int
   1.324 -power_of_two(int input)
   1.325 -{
   1.326 -    int value = 1;
   1.327 -
   1.328 -    while (value < input) {
   1.329 -        value <<= 1;
   1.330 -    }
   1.331 -    return value;
   1.332 -}
   1.333 -
   1.334 -GLuint
   1.335 -SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
   1.336 -{
   1.337 -    GLuint texture;
   1.338 -    int w, h;
   1.339 -    SDL_Surface *image;
   1.340 -    SDL_Rect area;
   1.341 -    Uint32 saved_flags;
   1.342 -    Uint8 saved_alpha;
   1.343 -
   1.344 -    /* Use the surface width and height expanded to powers of 2 */
   1.345 -    w = power_of_two(surface->w);
   1.346 -    h = power_of_two(surface->h);
   1.347 -    texcoord[0] = 0.0f;         /* Min X */
   1.348 -    texcoord[1] = 0.0f;         /* Min Y */
   1.349 -    texcoord[2] = (GLfloat) surface->w / w;     /* Max X */
   1.350 -    texcoord[3] = (GLfloat) surface->h / h;     /* Max Y */
   1.351 -
   1.352 -    image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
   1.353 -#if SDL_BYTEORDER == SDL_LIL_ENDIAN     /* OpenGL RGBA masks */
   1.354 -                                 0x000000FF,
   1.355 -                                 0x0000FF00, 0x00FF0000, 0xFF000000
   1.356 -#else
   1.357 -                                 0xFF000000,
   1.358 -                                 0x00FF0000, 0x0000FF00, 0x000000FF
   1.359 -#endif
   1.360 -        );
   1.361 -    if (image == NULL) {
   1.362 -        return 0;
   1.363 +        i += consumed;
   1.364      }
   1.365  
   1.366 -    /* Save the alpha blending attributes */
   1.367 -    saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.368 -    saved_alpha = surface->format->alpha;
   1.369 -    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   1.370 -        SDL_SetAlpha(surface, 0, 0);
   1.371 -    }
   1.372 -
   1.373 -    /* Copy the surface into the GL texture image */
   1.374 -    area.x = 0;
   1.375 -    area.y = 0;
   1.376 -    area.w = surface->w;
   1.377 -    area.h = surface->h;
   1.378 -    SDL_BlitSurface(surface, &area, image, &area);
   1.379 -
   1.380 -    /* Restore the alpha blending attributes */
   1.381 -    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
   1.382 -        SDL_SetAlpha(surface, saved_flags, saved_alpha);
   1.383 -    }
   1.384 -
   1.385 -    /* Create an OpenGL texture for the image */
   1.386 -    glGenTextures(1, &texture);
   1.387 -    glBindTexture(GL_TEXTURE_2D, texture);
   1.388 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
   1.389 -    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   1.390 -    glTexImage2D(GL_TEXTURE_2D,
   1.391 -                 0,
   1.392 -                 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
   1.393 -    SDL_FreeSurface(image);     /* No longer needed */
   1.394 -
   1.395 -    return texture;
   1.396 -}
   1.397 -
   1.398 -void
   1.399 -DrawLogoCursor(void)
   1.400 -{
   1.401 -    static GLfloat texMinX, texMinY;
   1.402 -    static GLfloat texMaxX, texMaxY;
   1.403 -    static int w, h;
   1.404 -    int x, y;
   1.405 -
   1.406 -    if (!cursor_texture) {
   1.407 -        SDL_Surface *image;
   1.408 -        GLfloat texcoord[4];
   1.409 -
   1.410 -        /* Load the image (could use SDL_image library here) */
   1.411 -        image = SDL_LoadBMP(LOGO_FILE);
   1.412 -        if (image == NULL) {
   1.413 -            return;
   1.414 -        }
   1.415 -        w = image->w;
   1.416 -        h = image->h;
   1.417 -
   1.418 -        /* Convert the image into an OpenGL texture */
   1.419 -        cursor_texture = SDL_GL_LoadTexture(image, texcoord);
   1.420 -
   1.421 -        /* Make texture coordinates easy to understand */
   1.422 -        texMinX = texcoord[0];
   1.423 -        texMinY = texcoord[1];
   1.424 -        texMaxX = texcoord[2];
   1.425 -        texMaxY = texcoord[3];
   1.426 -
   1.427 -        /* We don't need the original image anymore */
   1.428 -        SDL_FreeSurface(image);
   1.429 -
   1.430 -        /* Make sure that the texture conversion is okay */
   1.431 -        if (!cursor_texture) {
   1.432 -            return;
   1.433 -        }
   1.434 -    }
   1.435 -
   1.436 -    /* Move the image around */
   1.437 -    SDL_GetMouseState(&x, &y);
   1.438 -    x -= w / 2;
   1.439 -    y -= h / 2;
   1.440 -
   1.441 -    /* Show the image on the screen */
   1.442 -    SDL_GL_Enter2DMode();
   1.443 -    glBindTexture(GL_TEXTURE_2D, cursor_texture);
   1.444 -    glBegin(GL_TRIANGLE_STRIP);
   1.445 -    glTexCoord2f(texMinX, texMinY);
   1.446 -    glVertex2i(x, y);
   1.447 -    glTexCoord2f(texMaxX, texMinY);
   1.448 -    glVertex2i(x + w, y);
   1.449 -    glTexCoord2f(texMinX, texMaxY);
   1.450 -    glVertex2i(x, y + h);
   1.451 -    glTexCoord2f(texMaxX, texMaxY);
   1.452 -    glVertex2i(x + w, y + h);
   1.453 -    glEnd();
   1.454 -    SDL_GL_Leave2DMode();
   1.455 -}
   1.456 -
   1.457 -void
   1.458 -DrawLogoTexture(void)
   1.459 -{
   1.460 -    static GLfloat texMinX, texMinY;
   1.461 -    static GLfloat texMaxX, texMaxY;
   1.462 -    static int x = 0;
   1.463 -    static int y = 0;
   1.464 -    static int w, h;
   1.465 -    static int delta_x = 1;
   1.466 -    static int delta_y = 1;
   1.467 -
   1.468 -    SDL_Surface *screen = SDL_GetVideoSurface();
   1.469 -
   1.470 -    if (!global_texture) {
   1.471 -        SDL_Surface *image;
   1.472 -        GLfloat texcoord[4];
   1.473 -
   1.474 -        /* Load the image (could use SDL_image library here) */
   1.475 -        image = SDL_LoadBMP(LOGO_FILE);
   1.476 -        if (image == NULL) {
   1.477 -            return;
   1.478 -        }
   1.479 -        w = image->w;
   1.480 -        h = image->h;
   1.481 -
   1.482 -        /* Convert the image into an OpenGL texture */
   1.483 -        global_texture = SDL_GL_LoadTexture(image, texcoord);
   1.484 -
   1.485 -        /* Make texture coordinates easy to understand */
   1.486 -        texMinX = texcoord[0];
   1.487 -        texMinY = texcoord[1];
   1.488 -        texMaxX = texcoord[2];
   1.489 -        texMaxY = texcoord[3];
   1.490 -
   1.491 -        /* We don't need the original image anymore */
   1.492 -        SDL_FreeSurface(image);
   1.493 -
   1.494 -        /* Make sure that the texture conversion is okay */
   1.495 -        if (!global_texture) {
   1.496 -            return;
   1.497 -        }
   1.498 -    }
   1.499 -
   1.500 -    /* Move the image around */
   1.501 -    x += delta_x;
   1.502 -    if (x < 0) {
   1.503 -        x = 0;
   1.504 -        delta_x = -delta_x;
   1.505 -    } else if ((x + w) > screen->w) {
   1.506 -        x = screen->w - w;
   1.507 -        delta_x = -delta_x;
   1.508 -    }
   1.509 -    y += delta_y;
   1.510 -    if (y < 0) {
   1.511 -        y = 0;
   1.512 -        delta_y = -delta_y;
   1.513 -    } else if ((y + h) > screen->h) {
   1.514 -        y = screen->h - h;
   1.515 -        delta_y = -delta_y;
   1.516 -    }
   1.517 -
   1.518 -    /* Show the image on the screen */
   1.519 -    SDL_GL_Enter2DMode();
   1.520 -    glBindTexture(GL_TEXTURE_2D, global_texture);
   1.521 -    glBegin(GL_TRIANGLE_STRIP);
   1.522 -    glTexCoord2f(texMinX, texMinY);
   1.523 -    glVertex2i(x, y);
   1.524 -    glTexCoord2f(texMaxX, texMinY);
   1.525 -    glVertex2i(x + w, y);
   1.526 -    glTexCoord2f(texMinX, texMaxY);
   1.527 -    glVertex2i(x, y + h);
   1.528 -    glTexCoord2f(texMaxX, texMaxY);
   1.529 -    glVertex2i(x + w, y + h);
   1.530 -    glEnd();
   1.531 -    SDL_GL_Leave2DMode();
   1.532 -}
   1.533 -
   1.534 -int
   1.535 -RunGLTest(int argc, char *argv[],
   1.536 -          int logo, int logocursor, int slowly, int bpp, float gamma,
   1.537 -          int noframe, int fsaa, int sync, int accel)
   1.538 -{
   1.539 -    int i;
   1.540 -    int rgb_size[3];
   1.541 -    int w = 640;
   1.542 -    int h = 480;
   1.543 -    int done = 0;
   1.544 -    int frames;
   1.545 -    Uint32 start_time, this_time;
   1.546 -    float color[8][3] = { {1.0, 1.0, 0.0},
   1.547 -    {1.0, 0.0, 0.0},
   1.548 -    {0.0, 0.0, 0.0},
   1.549 -    {0.0, 1.0, 0.0},
   1.550 -    {0.0, 1.0, 1.0},
   1.551 -    {1.0, 1.0, 1.0},
   1.552 -    {1.0, 0.0, 1.0},
   1.553 -    {0.0, 0.0, 1.0}
   1.554 -    };
   1.555 -    float cube[8][3] = { {0.5, 0.5, -0.5},
   1.556 -    {0.5, -0.5, -0.5},
   1.557 -    {-0.5, -0.5, -0.5},
   1.558 -    {-0.5, 0.5, -0.5},
   1.559 -    {-0.5, 0.5, 0.5},
   1.560 -    {0.5, 0.5, 0.5},
   1.561 -    {0.5, -0.5, 0.5},
   1.562 -    {-0.5, -0.5, 0.5}
   1.563 -    };
   1.564 -    Uint32 video_flags;
   1.565 -    int value;
   1.566 -
   1.567 -    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.568 -        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.569 -        exit(1);
   1.570 -    }
   1.571 -
   1.572 -    /* See if we should detect the display depth */
   1.573 -    if (bpp == 0) {
   1.574 -        if (SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8) {
   1.575 -            bpp = 8;
   1.576 -        } else {
   1.577 -            bpp = 16;           /* More doesn't seem to work */
   1.578 -        }
   1.579 -    }
   1.580 -
   1.581 -    /* Set the flags we want to use for setting the video mode */
   1.582 -    video_flags = SDL_OPENGL;
   1.583 -    for (i = 1; argv[i]; ++i) {
   1.584 -        if (strcmp(argv[i], "-fullscreen") == 0) {
   1.585 -            video_flags |= SDL_FULLSCREEN;
   1.586 -        }
   1.587 -    }
   1.588 -
   1.589 -    if (noframe) {
   1.590 -        video_flags |= SDL_NOFRAME;
   1.591 -    }
   1.592 -
   1.593 -    /* Initialize the display */
   1.594 -    switch (bpp) {
   1.595 -    case 8:
   1.596 -        rgb_size[0] = 3;
   1.597 -        rgb_size[1] = 3;
   1.598 -        rgb_size[2] = 2;
   1.599 -        break;
   1.600 -    case 15:
   1.601 -    case 16:
   1.602 -        rgb_size[0] = 5;
   1.603 -        rgb_size[1] = 5;
   1.604 -        rgb_size[2] = 5;
   1.605 -        break;
   1.606 -    default:
   1.607 -        rgb_size[0] = 8;
   1.608 -        rgb_size[1] = 8;
   1.609 -        rgb_size[2] = 8;
   1.610 -        break;
   1.611 -    }
   1.612 -    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rgb_size[0]);
   1.613 -    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, rgb_size[1]);
   1.614 -    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, rgb_size[2]);
   1.615 +    /* Set OpenGL parameters */
   1.616 +    state->window_flags |= SDL_WINDOW_OPENGL;
   1.617 +    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   1.618 +    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   1.619 +    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   1.620      SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   1.621      SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   1.622      if (fsaa) {
   1.623 @@ -457,18 +214,29 @@
   1.624      if (accel) {
   1.625          SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
   1.626      }
   1.627 -    if (SDL_SetVideoMode(w, h, bpp, video_flags) == NULL) {
   1.628 -        fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
   1.629 -        SDL_Quit();
   1.630 -        exit(1);
   1.631 +    if (!CommonInit(state)) {
   1.632 +        quit(2);
   1.633      }
   1.634 -    if (sync) {
   1.635 +
   1.636 +    /* Create OpenGL context */
   1.637 +    context = SDL_GL_CreateContext(state->windows[0]);
   1.638 +    if (!context) {
   1.639 +        fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   1.640 +        quit(2);
   1.641 +    }
   1.642 +    if (SDL_GL_MakeCurrent(state->windows[0], context) < 0) {
   1.643 +        fprintf(stderr, "SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
   1.644 +        quit(2);
   1.645 +    }
   1.646 +
   1.647 +    if (state->render_flags & SDL_Renderer_PresentVSync) {
   1.648          SDL_GL_SetSwapInterval(1);
   1.649      } else {
   1.650          SDL_GL_SetSwapInterval(0);
   1.651      }
   1.652  
   1.653 -    printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel);
   1.654 +    printf("Screen BPP: %d\n",
   1.655 +           SDL_BITSPERPIXEL(SDL_GetCurrentDisplayMode()->format));
   1.656      printf("\n");
   1.657      printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   1.658      printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   1.659 @@ -476,286 +244,68 @@
   1.660      printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   1.661      printf("\n");
   1.662  
   1.663 -    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   1.664 -    printf("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
   1.665 -    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   1.666 -    printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
   1.667 -    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   1.668 -    printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
   1.669 -    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   1.670 -    printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
   1.671 -    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &value);
   1.672 +    SDL_GL_GetWindowAttribute(state->windows[0], SDL_GL_RED_SIZE, &value);
   1.673 +    printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   1.674 +    SDL_GL_GetWindowAttribute(state->windows[0], SDL_GL_GREEN_SIZE, &value);
   1.675 +    printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   1.676 +    SDL_GL_GetWindowAttribute(state->windows[0], SDL_GL_BLUE_SIZE, &value);
   1.677 +    printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   1.678 +    SDL_GL_GetWindowAttribute(state->windows[0], SDL_GL_DEPTH_SIZE, &value);
   1.679 +    printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   1.680 +    SDL_GL_GetWindowAttribute(state->windows[0], SDL_GL_DOUBLEBUFFER, &value);
   1.681      printf("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
   1.682      if (fsaa) {
   1.683 -        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   1.684 +        SDL_GL_GetWindowAttribute(state->windows[0],
   1.685 +                                  SDL_GL_MULTISAMPLEBUFFERS, &value);
   1.686          printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   1.687 -        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   1.688 +        SDL_GL_GetWindowAttribute(state->windows[0],
   1.689 +                                  SDL_GL_MULTISAMPLESAMPLES, &value);
   1.690          printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   1.691                 value);
   1.692      }
   1.693      if (accel) {
   1.694 -        SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   1.695 +        SDL_GL_GetWindowAttribute(state->windows[0],
   1.696 +                                  SDL_GL_ACCELERATED_VISUAL, &value);
   1.697          printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   1.698      }
   1.699 -    if (sync) {
   1.700 -        printf("Buffer swap interval: requested 1, got %d\n",
   1.701 -               SDL_GL_GetSwapInterval());
   1.702 +
   1.703 +    /* Set rendering settings */
   1.704 +    glMatrixMode(GL_PROJECTION);
   1.705 +    glLoadIdentity();
   1.706 +    glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   1.707 +    glMatrixMode(GL_MODELVIEW);
   1.708 +    glLoadIdentity();
   1.709 +    glEnable(GL_DEPTH_TEST);
   1.710 +    glDepthFunc(GL_LESS);
   1.711 +    glShadeModel(GL_SMOOTH);
   1.712 +
   1.713 +    /* Main render loop */
   1.714 +    frames = 0;
   1.715 +    then = SDL_GetTicks();
   1.716 +    done = 0;
   1.717 +    while (!done) {
   1.718 +        /* Check for events */
   1.719 +        ++frames;
   1.720 +        while (SDL_PollEvent(&event)) {
   1.721 +            CommonEvent(state, &event, &done);
   1.722 +        }
   1.723 +        for (i = 0; i < state->num_windows; ++i) {
   1.724 +            int w, h;
   1.725 +            SDL_GL_MakeCurrent(state->windows[i], context);
   1.726 +            SDL_GetWindowSize(state->windows[i], &w, &h);
   1.727 +            glViewport(0, 0, w, h);
   1.728 +            Render();
   1.729 +            SDL_GL_SwapWindow(state->windows[i]);
   1.730 +        }
   1.731      }
   1.732  
   1.733 -    /* Set the window manager title bar */
   1.734 -    SDL_WM_SetCaption("SDL GL test", "testgl");
   1.735 -
   1.736 -    /* Set the gamma for the window */
   1.737 -    if (gamma != 0.0) {
   1.738 -        SDL_SetGamma(gamma, gamma, gamma);
   1.739 +    /* Print out some timing information */
   1.740 +    now = SDL_GetTicks();
   1.741 +    if (now > then) {
   1.742 +        printf("%2.2f frames per second\n",
   1.743 +               ((double) frames * 1000) / (now - then));
   1.744      }
   1.745 -
   1.746 -    glViewport(0, 0, w, h);
   1.747 -    glMatrixMode(GL_PROJECTION);
   1.748 -    glLoadIdentity();
   1.749 -
   1.750 -    glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   1.751 -
   1.752 -    glMatrixMode(GL_MODELVIEW);
   1.753 -    glLoadIdentity();
   1.754 -
   1.755 -    glEnable(GL_DEPTH_TEST);
   1.756 -
   1.757 -    glDepthFunc(GL_LESS);
   1.758 -
   1.759 -    glShadeModel(GL_SMOOTH);
   1.760 -
   1.761 -    /* Loop until done. */
   1.762 -    start_time = SDL_GetTicks();
   1.763 -    frames = 0;
   1.764 -    while (!done) {
   1.765 -        GLenum gl_error;
   1.766 -        char *sdl_error;
   1.767 -        SDL_Event event;
   1.768 -
   1.769 -        /* Do our drawing, too. */
   1.770 -        glClearColor(0.0, 0.0, 0.0, 1.0);
   1.771 -        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.772 -
   1.773 -        glBegin(GL_QUADS);
   1.774 -
   1.775 -#ifdef SHADED_CUBE
   1.776 -        glColor3fv(color[0]);
   1.777 -        glVertex3fv(cube[0]);
   1.778 -        glColor3fv(color[1]);
   1.779 -        glVertex3fv(cube[1]);
   1.780 -        glColor3fv(color[2]);
   1.781 -        glVertex3fv(cube[2]);
   1.782 -        glColor3fv(color[3]);
   1.783 -        glVertex3fv(cube[3]);
   1.784 -
   1.785 -        glColor3fv(color[3]);
   1.786 -        glVertex3fv(cube[3]);
   1.787 -        glColor3fv(color[4]);
   1.788 -        glVertex3fv(cube[4]);
   1.789 -        glColor3fv(color[7]);
   1.790 -        glVertex3fv(cube[7]);
   1.791 -        glColor3fv(color[2]);
   1.792 -        glVertex3fv(cube[2]);
   1.793 -
   1.794 -        glColor3fv(color[0]);
   1.795 -        glVertex3fv(cube[0]);
   1.796 -        glColor3fv(color[5]);
   1.797 -        glVertex3fv(cube[5]);
   1.798 -        glColor3fv(color[6]);
   1.799 -        glVertex3fv(cube[6]);
   1.800 -        glColor3fv(color[1]);
   1.801 -        glVertex3fv(cube[1]);
   1.802 -
   1.803 -        glColor3fv(color[5]);
   1.804 -        glVertex3fv(cube[5]);
   1.805 -        glColor3fv(color[4]);
   1.806 -        glVertex3fv(cube[4]);
   1.807 -        glColor3fv(color[7]);
   1.808 -        glVertex3fv(cube[7]);
   1.809 -        glColor3fv(color[6]);
   1.810 -        glVertex3fv(cube[6]);
   1.811 -
   1.812 -        glColor3fv(color[5]);
   1.813 -        glVertex3fv(cube[5]);
   1.814 -        glColor3fv(color[0]);
   1.815 -        glVertex3fv(cube[0]);
   1.816 -        glColor3fv(color[3]);
   1.817 -        glVertex3fv(cube[3]);
   1.818 -        glColor3fv(color[4]);
   1.819 -        glVertex3fv(cube[4]);
   1.820 -
   1.821 -        glColor3fv(color[6]);
   1.822 -        glVertex3fv(cube[6]);
   1.823 -        glColor3fv(color[1]);
   1.824 -        glVertex3fv(cube[1]);
   1.825 -        glColor3fv(color[2]);
   1.826 -        glVertex3fv(cube[2]);
   1.827 -        glColor3fv(color[7]);
   1.828 -        glVertex3fv(cube[7]);
   1.829 -#else /* flat cube */
   1.830 -        glColor3f(1.0, 0.0, 0.0);
   1.831 -        glVertex3fv(cube[0]);
   1.832 -        glVertex3fv(cube[1]);
   1.833 -        glVertex3fv(cube[2]);
   1.834 -        glVertex3fv(cube[3]);
   1.835 -
   1.836 -        glColor3f(0.0, 1.0, 0.0);
   1.837 -        glVertex3fv(cube[3]);
   1.838 -        glVertex3fv(cube[4]);
   1.839 -        glVertex3fv(cube[7]);
   1.840 -        glVertex3fv(cube[2]);
   1.841 -
   1.842 -        glColor3f(0.0, 0.0, 1.0);
   1.843 -        glVertex3fv(cube[0]);
   1.844 -        glVertex3fv(cube[5]);
   1.845 -        glVertex3fv(cube[6]);
   1.846 -        glVertex3fv(cube[1]);
   1.847 -
   1.848 -        glColor3f(0.0, 1.0, 1.0);
   1.849 -        glVertex3fv(cube[5]);
   1.850 -        glVertex3fv(cube[4]);
   1.851 -        glVertex3fv(cube[7]);
   1.852 -        glVertex3fv(cube[6]);
   1.853 -
   1.854 -        glColor3f(1.0, 1.0, 0.0);
   1.855 -        glVertex3fv(cube[5]);
   1.856 -        glVertex3fv(cube[0]);
   1.857 -        glVertex3fv(cube[3]);
   1.858 -        glVertex3fv(cube[4]);
   1.859 -
   1.860 -        glColor3f(1.0, 0.0, 1.0);
   1.861 -        glVertex3fv(cube[6]);
   1.862 -        glVertex3fv(cube[1]);
   1.863 -        glVertex3fv(cube[2]);
   1.864 -        glVertex3fv(cube[7]);
   1.865 -#endif /* SHADED_CUBE */
   1.866 -
   1.867 -        glEnd();
   1.868 -
   1.869 -        glMatrixMode(GL_MODELVIEW);
   1.870 -        glRotatef(5.0, 1.0, 1.0, 1.0);
   1.871 -
   1.872 -        /* Draw 2D logo onto the 3D display */
   1.873 -        if (logo) {
   1.874 -            DrawLogoTexture();
   1.875 -        }
   1.876 -        if (logocursor) {
   1.877 -            DrawLogoCursor();
   1.878 -        }
   1.879 -
   1.880 -        SDL_GL_SwapBuffers();
   1.881 -
   1.882 -        /* Check for error conditions. */
   1.883 -        gl_error = glGetError();
   1.884 -
   1.885 -        if (gl_error != GL_NO_ERROR) {
   1.886 -            fprintf(stderr, "testgl: OpenGL error: %d\n", gl_error);
   1.887 -        }
   1.888 -
   1.889 -        sdl_error = SDL_GetError();
   1.890 -
   1.891 -        if (sdl_error[0] != '\0') {
   1.892 -            fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
   1.893 -            SDL_ClearError();
   1.894 -        }
   1.895 -
   1.896 -        /* Allow the user to see what's happening */
   1.897 -        if (slowly) {
   1.898 -            SDL_Delay(20);
   1.899 -        }
   1.900 -
   1.901 -        /* Check if there's a pending event. */
   1.902 -        while (SDL_PollEvent(&event)) {
   1.903 -            done = HandleEvent(&event);
   1.904 -        }
   1.905 -        ++frames;
   1.906 -    }
   1.907 -
   1.908 -    /* Print out the frames per second */
   1.909 -    this_time = SDL_GetTicks();
   1.910 -    if (this_time != start_time) {
   1.911 -        printf("%2.2f FPS\n",
   1.912 -               ((float) frames / (this_time - start_time)) * 1000.0);
   1.913 -    }
   1.914 -
   1.915 -    if (global_image) {
   1.916 -        SDL_FreeSurface(global_image);
   1.917 -        global_image = NULL;
   1.918 -    }
   1.919 -    if (global_texture) {
   1.920 -        glDeleteTextures(1, &global_texture);
   1.921 -        global_texture = 0;
   1.922 -    }
   1.923 -    if (cursor_texture) {
   1.924 -        glDeleteTextures(1, &cursor_texture);
   1.925 -        cursor_texture = 0;
   1.926 -    }
   1.927 -
   1.928 -    /* Destroy our GL context, etc. */
   1.929 -    SDL_Quit();
   1.930 -    return (0);
   1.931 -}
   1.932 -
   1.933 -int
   1.934 -main(int argc, char *argv[])
   1.935 -{
   1.936 -    int i, logo, logocursor = 0;
   1.937 -    int numtests;
   1.938 -    int bpp = 0;
   1.939 -    int slowly;
   1.940 -    float gamma = 0.0;
   1.941 -    int noframe = 0;
   1.942 -    int fsaa = 0;
   1.943 -    int accel = 0;
   1.944 -    int sync = 0;
   1.945 -
   1.946 -    logo = 0;
   1.947 -    slowly = 0;
   1.948 -    numtests = 1;
   1.949 -    for (i = 1; argv[i]; ++i) {
   1.950 -        if (strcmp(argv[i], "-twice") == 0) {
   1.951 -            ++numtests;
   1.952 -        }
   1.953 -        if (strcmp(argv[i], "-logo") == 0) {
   1.954 -            logo = 1;
   1.955 -        }
   1.956 -        if (strcmp(argv[i], "-logocursor") == 0) {
   1.957 -            logocursor = 1;
   1.958 -        }
   1.959 -        if (strcmp(argv[i], "-slow") == 0) {
   1.960 -            slowly = 1;
   1.961 -        }
   1.962 -        if (strcmp(argv[i], "-bpp") == 0) {
   1.963 -            bpp = atoi(argv[++i]);
   1.964 -        }
   1.965 -        if (strcmp(argv[i], "-gamma") == 0) {
   1.966 -            gamma = (float) atof(argv[++i]);
   1.967 -        }
   1.968 -        if (strcmp(argv[i], "-noframe") == 0) {
   1.969 -            noframe = 1;
   1.970 -        }
   1.971 -        if (strcmp(argv[i], "-fsaa") == 0) {
   1.972 -            ++fsaa;
   1.973 -        }
   1.974 -        if (strcmp(argv[i], "-accel") == 0) {
   1.975 -            ++accel;
   1.976 -        }
   1.977 -        if (strcmp(argv[i], "-sync") == 0) {
   1.978 -            ++sync;
   1.979 -        }
   1.980 -        if (strncmp(argv[i], "-h", 2) == 0) {
   1.981 -            printf
   1.982 -                ("Usage: %s [-twice] [-logo] [-logocursor] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa] [-accel] [-sync] [-fullscreen]\n",
   1.983 -                 argv[0]);
   1.984 -            exit(0);
   1.985 -        }
   1.986 -    }
   1.987 -    for (i = 0; i < numtests; ++i) {
   1.988 -        RunGLTest(argc, argv, logo, logocursor, slowly, bpp, gamma,
   1.989 -                  noframe, fsaa, sync, accel);
   1.990 -    }
   1.991 -    return 0;
   1.992 +    quit(0);
   1.993  }
   1.994  
   1.995  #else /* HAVE_OPENGL */