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