test/testgl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 17:21:00 -0500
changeset 6250 cce0ddcf6747
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Updated to SDL 2.0, and SDL 2.0 can now be installed coexisting with SDL 1.2
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
#include <stdlib.h>
slouken@0
    13
#include <stdio.h>
slouken@0
    14
#include <string.h>
slouken@0
    15
#include <math.h>
slouken@0
    16
slouken@0
    17
#include "SDL.h"
slouken@0
    18
slouken@1815
    19
#ifdef __MACOS__
slouken@1815
    20
#define HAVE_OPENGL
slouken@1815
    21
#endif
slouken@1815
    22
slouken@0
    23
#ifdef HAVE_OPENGL
slouken@234
    24
slouken@214
    25
#include "SDL_opengl.h"
slouken@0
    26
slouken@234
    27
/* Undefine this if you want a flat cube instead of a rainbow cube */
slouken@0
    28
#define SHADED_CUBE
slouken@0
    29
slouken@234
    30
/* Define this to be the name of the logo image to use with -logo */
slouken@234
    31
#define LOGO_FILE	"icon.bmp"
slouken@234
    32
slouken@492
    33
static SDL_Surface *global_image = NULL;
slouken@492
    34
static GLuint global_texture = 0;
slouken@933
    35
static GLuint cursor_texture = 0;
slouken@492
    36
slouken@234
    37
/**********************************************************************/
slouken@234
    38
slouken@1895
    39
void
slouken@1895
    40
HotKey_ToggleFullScreen(void)
slouken@0
    41
{
slouken@1895
    42
    SDL_Surface *screen;
slouken@0
    43
slouken@1895
    44
    screen = SDL_GetVideoSurface();
slouken@1895
    45
    if (SDL_WM_ToggleFullScreen(screen)) {
slouken@1895
    46
        printf("Toggled fullscreen mode - now %s\n",
slouken@1895
    47
               (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
slouken@1895
    48
    } else {
slouken@1895
    49
        printf("Unable to toggle fullscreen mode\n");
slouken@1895
    50
    }
slouken@0
    51
}
slouken@0
    52
slouken@1895
    53
void
slouken@1895
    54
HotKey_ToggleGrab(void)
slouken@0
    55
{
slouken@1895
    56
    SDL_GrabMode mode;
slouken@0
    57
slouken@1895
    58
    printf("Ctrl-G: toggling input grab!\n");
slouken@1895
    59
    mode = SDL_WM_GrabInput(SDL_GRAB_QUERY);
slouken@1895
    60
    if (mode == SDL_GRAB_ON) {
slouken@1895
    61
        printf("Grab was on\n");
slouken@1895
    62
    } else {
slouken@1895
    63
        printf("Grab was off\n");
slouken@1895
    64
    }
slouken@1895
    65
    mode = SDL_WM_GrabInput(!mode);
slouken@1895
    66
    if (mode == SDL_GRAB_ON) {
slouken@1895
    67
        printf("Grab is now on\n");
slouken@1895
    68
    } else {
slouken@1895
    69
        printf("Grab is now off\n");
slouken@1895
    70
    }
slouken@0
    71
}
slouken@0
    72
slouken@1895
    73
void
slouken@1895
    74
HotKey_Iconify(void)
slouken@0
    75
{
slouken@1895
    76
    printf("Ctrl-Z: iconifying window!\n");
slouken@1895
    77
    SDL_WM_IconifyWindow();
slouken@0
    78
}
slouken@0
    79
slouken@1895
    80
int
slouken@1895
    81
HandleEvent(SDL_Event * event)
slouken@0
    82
{
slouken@1895
    83
    int done;
slouken@0
    84
slouken@1895
    85
    done = 0;
slouken@1895
    86
    switch (event->type) {
slouken@1895
    87
    case SDL_ACTIVEEVENT:
slouken@1895
    88
        /* See what happened */
slouken@1895
    89
        printf("app %s ", event->active.gain ? "gained" : "lost");
slouken@1895
    90
        if (event->active.state & SDL_APPACTIVE) {
slouken@1895
    91
            printf("active ");
slouken@1895
    92
        } else if (event->active.state & SDL_APPMOUSEFOCUS) {
slouken@1895
    93
            printf("mouse ");
slouken@1895
    94
        } else if (event->active.state & SDL_APPINPUTFOCUS) {
slouken@1895
    95
            printf("input ");
slouken@1895
    96
        }
slouken@1895
    97
        printf("focus\n");
slouken@1895
    98
        break;
slouken@0
    99
slouken@1895
   100
slouken@1895
   101
    case SDL_KEYDOWN:
slouken@1895
   102
        if (event->key.keysym.sym == SDLK_ESCAPE) {
slouken@1895
   103
            done = 1;
slouken@1895
   104
        }
slouken@1895
   105
        if ((event->key.keysym.sym == SDLK_g) &&
slouken@1895
   106
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1895
   107
            HotKey_ToggleGrab();
slouken@1895
   108
        }
slouken@1895
   109
        if ((event->key.keysym.sym == SDLK_z) &&
slouken@1895
   110
            (event->key.keysym.mod & KMOD_CTRL)) {
slouken@1895
   111
            HotKey_Iconify();
slouken@1895
   112
        }
slouken@1895
   113
        if ((event->key.keysym.sym == SDLK_RETURN) &&
slouken@1895
   114
            (event->key.keysym.mod & KMOD_ALT)) {
slouken@1895
   115
            HotKey_ToggleFullScreen();
slouken@1895
   116
        }
slouken@1895
   117
        printf("key '%s' pressed\n", SDL_GetKeyName(event->key.keysym.sym));
slouken@1895
   118
        break;
slouken@1895
   119
    case SDL_QUIT:
slouken@1895
   120
        done = 1;
slouken@1895
   121
        break;
slouken@1895
   122
    }
slouken@1895
   123
    return (done);
slouken@0
   124
}
slouken@0
   125
slouken@1895
   126
void
slouken@1895
   127
SDL_GL_Enter2DMode()
slouken@233
   128
{
slouken@1895
   129
    SDL_Surface *screen = SDL_GetVideoSurface();
slouken@233
   130
slouken@1895
   131
    /* Note, there may be other things you need to change,
slouken@1895
   132
       depending on how you have your OpenGL state set up.
slouken@1895
   133
     */
slouken@1895
   134
    glPushAttrib(GL_ENABLE_BIT);
slouken@1895
   135
    glDisable(GL_DEPTH_TEST);
slouken@1895
   136
    glDisable(GL_CULL_FACE);
slouken@1895
   137
    glEnable(GL_TEXTURE_2D);
slouken@233
   138
slouken@1895
   139
    /* This allows alpha blending of 2D textures with the scene */
slouken@1895
   140
    glEnable(GL_BLEND);
slouken@1895
   141
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@243
   142
slouken@1895
   143
    glViewport(0, 0, screen->w, screen->h);
slouken@233
   144
slouken@1895
   145
    glMatrixMode(GL_PROJECTION);
slouken@1895
   146
    glPushMatrix();
slouken@1895
   147
    glLoadIdentity();
slouken@233
   148
slouken@1895
   149
    glOrtho(0.0, (GLdouble) screen->w, (GLdouble) screen->h, 0.0, 0.0, 1.0);
slouken@233
   150
slouken@1895
   151
    glMatrixMode(GL_MODELVIEW);
slouken@1895
   152
    glPushMatrix();
slouken@1895
   153
    glLoadIdentity();
slouken@233
   154
slouken@1895
   155
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
slouken@233
   156
}
slouken@233
   157
slouken@1895
   158
void
slouken@1895
   159
SDL_GL_Leave2DMode()
slouken@233
   160
{
slouken@1895
   161
    glMatrixMode(GL_MODELVIEW);
slouken@1895
   162
    glPopMatrix();
slouken@233
   163
slouken@1895
   164
    glMatrixMode(GL_PROJECTION);
slouken@1895
   165
    glPopMatrix();
slouken@233
   166
slouken@1895
   167
    glPopAttrib();
slouken@233
   168
}
slouken@233
   169
slouken@234
   170
/* Quick utility function for texture creation */
slouken@1895
   171
static int
slouken@1895
   172
power_of_two(int input)
slouken@234
   173
{
slouken@1895
   174
    int value = 1;
slouken@234
   175
slouken@1895
   176
    while (value < input) {
slouken@1895
   177
        value <<= 1;
slouken@1895
   178
    }
slouken@1895
   179
    return value;
slouken@234
   180
}
slouken@234
   181
slouken@1895
   182
GLuint
slouken@1895
   183
SDL_GL_LoadTexture(SDL_Surface * surface, GLfloat * texcoord)
slouken@234
   184
{
slouken@1895
   185
    GLuint texture;
slouken@1895
   186
    int w, h;
slouken@1895
   187
    SDL_Surface *image;
slouken@1895
   188
    SDL_Rect area;
slouken@1895
   189
    Uint32 saved_flags;
slouken@1895
   190
    Uint8 saved_alpha;
slouken@234
   191
slouken@1895
   192
    /* Use the surface width and height expanded to powers of 2 */
slouken@1895
   193
    w = power_of_two(surface->w);
slouken@1895
   194
    h = power_of_two(surface->h);
slouken@1895
   195
    texcoord[0] = 0.0f;         /* Min X */
slouken@1895
   196
    texcoord[1] = 0.0f;         /* Min Y */
slouken@1895
   197
    texcoord[2] = (GLfloat) surface->w / w;     /* Max X */
slouken@1895
   198
    texcoord[3] = (GLfloat) surface->h / h;     /* Max Y */
slouken@234
   199
slouken@1895
   200
    image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
slouken@1895
   201
#if SDL_BYTEORDER == SDL_LIL_ENDIAN     /* OpenGL RGBA masks */
slouken@1895
   202
                                 0x000000FF,
slouken@1895
   203
                                 0x0000FF00, 0x00FF0000, 0xFF000000
slouken@234
   204
#else
slouken@1895
   205
                                 0xFF000000,
slouken@1895
   206
                                 0x00FF0000, 0x0000FF00, 0x000000FF
slouken@234
   207
#endif
slouken@1895
   208
        );
slouken@1895
   209
    if (image == NULL) {
slouken@1895
   210
        return 0;
slouken@1895
   211
    }
slouken@234
   212
slouken@1895
   213
    /* Save the alpha blending attributes */
slouken@1895
   214
    saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@2267
   215
    SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
slouken@1895
   216
    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   217
        SDL_SetAlpha(surface, 0, 0);
slouken@1895
   218
    }
slouken@234
   219
slouken@1895
   220
    /* Copy the surface into the GL texture image */
slouken@1895
   221
    area.x = 0;
slouken@1895
   222
    area.y = 0;
slouken@1895
   223
    area.w = surface->w;
slouken@1895
   224
    area.h = surface->h;
slouken@1895
   225
    SDL_BlitSurface(surface, &area, image, &area);
slouken@234
   226
slouken@1895
   227
    /* Restore the alpha blending attributes */
slouken@1895
   228
    if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   229
        SDL_SetAlpha(surface, saved_flags, saved_alpha);
slouken@1895
   230
    }
slouken@234
   231
slouken@1895
   232
    /* Create an OpenGL texture for the image */
slouken@1895
   233
    glGenTextures(1, &texture);
slouken@1895
   234
    glBindTexture(GL_TEXTURE_2D, texture);
slouken@1895
   235
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
slouken@1895
   236
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
slouken@1895
   237
    glTexImage2D(GL_TEXTURE_2D,
slouken@1895
   238
                 0,
slouken@1895
   239
                 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
slouken@1895
   240
    SDL_FreeSurface(image);     /* No longer needed */
slouken@234
   241
slouken@1895
   242
    return texture;
slouken@234
   243
}
slouken@234
   244
slouken@1895
   245
void
slouken@1895
   246
DrawLogoCursor(void)
slouken@933
   247
{
slouken@1895
   248
    static GLfloat texMinX, texMinY;
slouken@1895
   249
    static GLfloat texMaxX, texMaxY;
slouken@1895
   250
    static int w, h;
slouken@1895
   251
    int x, y;
slouken@933
   252
slouken@1895
   253
    if (!cursor_texture) {
slouken@1895
   254
        SDL_Surface *image;
slouken@1895
   255
        GLfloat texcoord[4];
slouken@933
   256
slouken@1895
   257
        /* Load the image (could use SDL_image library here) */
slouken@1895
   258
        image = SDL_LoadBMP(LOGO_FILE);
slouken@1895
   259
        if (image == NULL) {
slouken@1895
   260
            return;
slouken@1895
   261
        }
slouken@1895
   262
        w = image->w;
slouken@1895
   263
        h = image->h;
slouken@933
   264
slouken@1895
   265
        /* Convert the image into an OpenGL texture */
slouken@1895
   266
        cursor_texture = SDL_GL_LoadTexture(image, texcoord);
slouken@933
   267
slouken@1895
   268
        /* Make texture coordinates easy to understand */
slouken@1895
   269
        texMinX = texcoord[0];
slouken@1895
   270
        texMinY = texcoord[1];
slouken@1895
   271
        texMaxX = texcoord[2];
slouken@1895
   272
        texMaxY = texcoord[3];
slouken@933
   273
slouken@1895
   274
        /* We don't need the original image anymore */
slouken@1895
   275
        SDL_FreeSurface(image);
slouken@933
   276
slouken@1895
   277
        /* Make sure that the texture conversion is okay */
slouken@1895
   278
        if (!cursor_texture) {
slouken@1895
   279
            return;
slouken@1895
   280
        }
slouken@1895
   281
    }
slouken@933
   282
slouken@1895
   283
    /* Move the image around */
icculus@3585
   284
    SDL_GetMouseState(&x, &y);
slouken@1895
   285
    x -= w / 2;
slouken@1895
   286
    y -= h / 2;
slouken@933
   287
slouken@1895
   288
    /* Show the image on the screen */
slouken@1895
   289
    SDL_GL_Enter2DMode();
slouken@1895
   290
    glBindTexture(GL_TEXTURE_2D, cursor_texture);
slouken@1895
   291
    glBegin(GL_TRIANGLE_STRIP);
slouken@1895
   292
    glTexCoord2f(texMinX, texMinY);
slouken@1895
   293
    glVertex2i(x, y);
slouken@1895
   294
    glTexCoord2f(texMaxX, texMinY);
slouken@1895
   295
    glVertex2i(x + w, y);
slouken@1895
   296
    glTexCoord2f(texMinX, texMaxY);
slouken@1895
   297
    glVertex2i(x, y + h);
slouken@1895
   298
    glTexCoord2f(texMaxX, texMaxY);
slouken@1895
   299
    glVertex2i(x + w, y + h);
slouken@1895
   300
    glEnd();
slouken@1895
   301
    SDL_GL_Leave2DMode();
slouken@933
   302
}
slouken@492
   303
slouken@1895
   304
void
slouken@1895
   305
DrawLogoTexture(void)
slouken@234
   306
{
slouken@1895
   307
    static GLfloat texMinX, texMinY;
slouken@1895
   308
    static GLfloat texMaxX, texMaxY;
slouken@1895
   309
    static int x = 0;
slouken@1895
   310
    static int y = 0;
slouken@1895
   311
    static int w, h;
slouken@1895
   312
    static int delta_x = 1;
slouken@1895
   313
    static int delta_y = 1;
slouken@234
   314
slouken@1895
   315
    SDL_Surface *screen = SDL_GetVideoSurface();
slouken@234
   316
slouken@1895
   317
    if (!global_texture) {
slouken@1895
   318
        SDL_Surface *image;
slouken@1895
   319
        GLfloat texcoord[4];
slouken@234
   320
slouken@1895
   321
        /* Load the image (could use SDL_image library here) */
slouken@1895
   322
        image = SDL_LoadBMP(LOGO_FILE);
slouken@1895
   323
        if (image == NULL) {
slouken@1895
   324
            return;
slouken@1895
   325
        }
slouken@1895
   326
        w = image->w;
slouken@1895
   327
        h = image->h;
slouken@234
   328
slouken@1895
   329
        /* Convert the image into an OpenGL texture */
slouken@1895
   330
        global_texture = SDL_GL_LoadTexture(image, texcoord);
slouken@234
   331
slouken@1895
   332
        /* Make texture coordinates easy to understand */
slouken@1895
   333
        texMinX = texcoord[0];
slouken@1895
   334
        texMinY = texcoord[1];
slouken@1895
   335
        texMaxX = texcoord[2];
slouken@1895
   336
        texMaxY = texcoord[3];
slouken@234
   337
slouken@1895
   338
        /* We don't need the original image anymore */
slouken@1895
   339
        SDL_FreeSurface(image);
slouken@234
   340
slouken@1895
   341
        /* Make sure that the texture conversion is okay */
slouken@1895
   342
        if (!global_texture) {
slouken@1895
   343
            return;
slouken@1895
   344
        }
slouken@1895
   345
    }
slouken@234
   346
slouken@1895
   347
    /* Move the image around */
slouken@1895
   348
    x += delta_x;
slouken@1895
   349
    if (x < 0) {
slouken@1895
   350
        x = 0;
slouken@1895
   351
        delta_x = -delta_x;
slouken@1895
   352
    } else if ((x + w) > screen->w) {
slouken@1895
   353
        x = screen->w - w;
slouken@1895
   354
        delta_x = -delta_x;
slouken@1895
   355
    }
slouken@1895
   356
    y += delta_y;
slouken@1895
   357
    if (y < 0) {
slouken@1895
   358
        y = 0;
slouken@1895
   359
        delta_y = -delta_y;
slouken@1895
   360
    } else if ((y + h) > screen->h) {
slouken@1895
   361
        y = screen->h - h;
slouken@1895
   362
        delta_y = -delta_y;
slouken@1895
   363
    }
slouken@234
   364
slouken@1895
   365
    /* Show the image on the screen */
slouken@1895
   366
    SDL_GL_Enter2DMode();
slouken@1895
   367
    glBindTexture(GL_TEXTURE_2D, global_texture);
slouken@1895
   368
    glBegin(GL_TRIANGLE_STRIP);
slouken@1895
   369
    glTexCoord2f(texMinX, texMinY);
slouken@1895
   370
    glVertex2i(x, y);
slouken@1895
   371
    glTexCoord2f(texMaxX, texMinY);
slouken@1895
   372
    glVertex2i(x + w, y);
slouken@1895
   373
    glTexCoord2f(texMinX, texMaxY);
slouken@1895
   374
    glVertex2i(x, y + h);
slouken@1895
   375
    glTexCoord2f(texMaxX, texMaxY);
slouken@1895
   376
    glVertex2i(x + w, y + h);
slouken@1895
   377
    glEnd();
slouken@1895
   378
    SDL_GL_Leave2DMode();
slouken@234
   379
}
slouken@234
   380
slouken@1895
   381
int
slouken@1895
   382
RunGLTest(int argc, char *argv[],
slouken@1895
   383
          int logo, int logocursor, int slowly, int bpp, float gamma,
slouken@3571
   384
          int noframe, int fsaa, int sync, int accel)
slouken@0
   385
{
slouken@1895
   386
    int i;
slouken@1895
   387
    int rgb_size[3];
slouken@1895
   388
    int w = 640;
slouken@1895
   389
    int h = 480;
slouken@1895
   390
    int done = 0;
slouken@1895
   391
    int frames;
slouken@1895
   392
    Uint32 start_time, this_time;
slouken@1895
   393
    float color[8][3] = { {1.0, 1.0, 0.0},
slouken@1895
   394
    {1.0, 0.0, 0.0},
slouken@1895
   395
    {0.0, 0.0, 0.0},
slouken@1895
   396
    {0.0, 1.0, 0.0},
slouken@1895
   397
    {0.0, 1.0, 1.0},
slouken@1895
   398
    {1.0, 1.0, 1.0},
slouken@1895
   399
    {1.0, 0.0, 1.0},
slouken@1895
   400
    {0.0, 0.0, 1.0}
slouken@1895
   401
    };
slouken@1895
   402
    float cube[8][3] = { {0.5, 0.5, -0.5},
slouken@1895
   403
    {0.5, -0.5, -0.5},
slouken@1895
   404
    {-0.5, -0.5, -0.5},
slouken@1895
   405
    {-0.5, 0.5, -0.5},
slouken@1895
   406
    {-0.5, 0.5, 0.5},
slouken@1895
   407
    {0.5, 0.5, 0.5},
slouken@1895
   408
    {0.5, -0.5, 0.5},
slouken@1895
   409
    {-0.5, -0.5, 0.5}
slouken@1895
   410
    };
slouken@1895
   411
    Uint32 video_flags;
slouken@1895
   412
    int value;
slouken@0
   413
slouken@1895
   414
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   415
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   416
        exit(1);
slouken@1895
   417
    }
slouken@0
   418
slouken@1895
   419
    /* See if we should detect the display depth */
slouken@1895
   420
    if (bpp == 0) {
slouken@1895
   421
        if (SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8) {
slouken@1895
   422
            bpp = 8;
slouken@1895
   423
        } else {
slouken@1895
   424
            bpp = 16;           /* More doesn't seem to work */
slouken@1895
   425
        }
slouken@1895
   426
    }
slouken@0
   427
slouken@1895
   428
    /* Set the flags we want to use for setting the video mode */
slouken@1895
   429
    video_flags = SDL_OPENGL;
slouken@1895
   430
    for (i = 1; argv[i]; ++i) {
slouken@1895
   431
        if (strcmp(argv[i], "-fullscreen") == 0) {
slouken@1895
   432
            video_flags |= SDL_FULLSCREEN;
slouken@1895
   433
        }
slouken@1895
   434
    }
slouken@234
   435
slouken@1895
   436
    if (noframe) {
slouken@1895
   437
        video_flags |= SDL_NOFRAME;
slouken@1895
   438
    }
slouken@234
   439
slouken@1895
   440
    /* Initialize the display */
slouken@1895
   441
    switch (bpp) {
slouken@1895
   442
    case 8:
slouken@1895
   443
        rgb_size[0] = 3;
slouken@1895
   444
        rgb_size[1] = 3;
slouken@1895
   445
        rgb_size[2] = 2;
slouken@1895
   446
        break;
slouken@1895
   447
    case 15:
slouken@1895
   448
    case 16:
slouken@1895
   449
        rgb_size[0] = 5;
slouken@1895
   450
        rgb_size[1] = 5;
slouken@1895
   451
        rgb_size[2] = 5;
slouken@1895
   452
        break;
slouken@1895
   453
    default:
slouken@1895
   454
        rgb_size[0] = 8;
slouken@1895
   455
        rgb_size[1] = 8;
slouken@1895
   456
        rgb_size[2] = 8;
slouken@1895
   457
        break;
slouken@1895
   458
    }
slouken@1895
   459
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rgb_size[0]);
slouken@1895
   460
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, rgb_size[1]);
slouken@1895
   461
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, rgb_size[2]);
slouken@1895
   462
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
slouken@1895
   463
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
slouken@1895
   464
    if (fsaa) {
slouken@1895
   465
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
slouken@1895
   466
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
slouken@1895
   467
    }
slouken@3571
   468
    if (accel >= 0) {
slouken@3571
   469
        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, accel);
slouken@3571
   470
    }
slouken@1895
   471
    if (SDL_SetVideoMode(w, h, bpp, video_flags) == NULL) {
slouken@1895
   472
        fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
slouken@1895
   473
        SDL_Quit();
slouken@1895
   474
        exit(1);
slouken@1895
   475
    }
slouken@1912
   476
    if (sync) {
slouken@1912
   477
        SDL_GL_SetSwapInterval(1);
slouken@1912
   478
    } else {
slouken@1912
   479
        SDL_GL_SetSwapInterval(0);
slouken@1912
   480
    }
slouken@0
   481
slouken@1895
   482
    printf("Screen BPP: %d\n", SDL_GetVideoSurface()->format->BitsPerPixel);
slouken@1895
   483
    printf("\n");
slouken@1895
   484
    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
slouken@1895
   485
    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
slouken@1895
   486
    printf("Version    : %s\n", glGetString(GL_VERSION));
slouken@1895
   487
    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
slouken@1895
   488
    printf("\n");
slouken@234
   489
slouken@1895
   490
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
slouken@1895
   491
    printf("SDL_GL_RED_SIZE: requested %d, got %d\n", rgb_size[0], value);
slouken@1895
   492
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
slouken@1895
   493
    printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", rgb_size[1], value);
slouken@1895
   494
    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
slouken@1895
   495
    printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", rgb_size[2], value);
slouken@1895
   496
    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
slouken@1895
   497
    printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", bpp, value);
slouken@1895
   498
    SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &value);
slouken@1895
   499
    printf("SDL_GL_DOUBLEBUFFER: requested 1, got %d\n", value);
slouken@1895
   500
    if (fsaa) {
slouken@1895
   501
        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
slouken@1895
   502
        printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
slouken@1895
   503
        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
slouken@1895
   504
        printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
slouken@1895
   505
               value);
slouken@1895
   506
    }
slouken@3571
   507
    if (accel >= 0) {
slouken@3571
   508
        SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
slouken@3571
   509
        printf("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
slouken@3571
   510
               value);
slouken@3571
   511
    }
slouken@1895
   512
    if (sync) {
slouken@1912
   513
        printf("Buffer swap interval: requested 1, got %d\n",
slouken@1912
   514
               SDL_GL_GetSwapInterval());
slouken@1895
   515
    }
slouken@1895
   516
slouken@1895
   517
    /* Set the window manager title bar */
slouken@1895
   518
    SDL_WM_SetCaption("SDL GL test", "testgl");
slouken@1895
   519
slouken@1895
   520
    /* Set the gamma for the window */
slouken@1895
   521
    if (gamma != 0.0) {
slouken@1895
   522
        SDL_SetGamma(gamma, gamma, gamma);
slouken@1895
   523
    }
slouken@1895
   524
slouken@1895
   525
    glViewport(0, 0, w, h);
slouken@1895
   526
    glMatrixMode(GL_PROJECTION);
slouken@1895
   527
    glLoadIdentity();
slouken@1895
   528
slouken@1895
   529
    glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
slouken@1895
   530
slouken@1895
   531
    glMatrixMode(GL_MODELVIEW);
slouken@1895
   532
    glLoadIdentity();
slouken@1895
   533
slouken@1895
   534
    glEnable(GL_DEPTH_TEST);
slouken@1895
   535
slouken@1895
   536
    glDepthFunc(GL_LESS);
slouken@1895
   537
slouken@1895
   538
    glShadeModel(GL_SMOOTH);
slouken@1895
   539
slouken@1895
   540
    /* Loop until done. */
slouken@1895
   541
    start_time = SDL_GetTicks();
slouken@1895
   542
    frames = 0;
slouken@1895
   543
    while (!done) {
slouken@1895
   544
        GLenum gl_error;
slouken@1895
   545
        char *sdl_error;
slouken@1895
   546
        SDL_Event event;
slouken@1895
   547
slouken@1895
   548
        /* Do our drawing, too. */
slouken@1895
   549
        glClearColor(0.0, 0.0, 0.0, 1.0);
slouken@1895
   550
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1895
   551
slouken@1895
   552
        glBegin(GL_QUADS);
slouken@1895
   553
slouken@1895
   554
#ifdef SHADED_CUBE
slouken@1895
   555
        glColor3fv(color[0]);
slouken@1895
   556
        glVertex3fv(cube[0]);
slouken@1895
   557
        glColor3fv(color[1]);
slouken@1895
   558
        glVertex3fv(cube[1]);
slouken@1895
   559
        glColor3fv(color[2]);
slouken@1895
   560
        glVertex3fv(cube[2]);
slouken@1895
   561
        glColor3fv(color[3]);
slouken@1895
   562
        glVertex3fv(cube[3]);
slouken@1895
   563
slouken@1895
   564
        glColor3fv(color[3]);
slouken@1895
   565
        glVertex3fv(cube[3]);
slouken@1895
   566
        glColor3fv(color[4]);
slouken@1895
   567
        glVertex3fv(cube[4]);
slouken@1895
   568
        glColor3fv(color[7]);
slouken@1895
   569
        glVertex3fv(cube[7]);
slouken@1895
   570
        glColor3fv(color[2]);
slouken@1895
   571
        glVertex3fv(cube[2]);
slouken@1895
   572
slouken@1895
   573
        glColor3fv(color[0]);
slouken@1895
   574
        glVertex3fv(cube[0]);
slouken@1895
   575
        glColor3fv(color[5]);
slouken@1895
   576
        glVertex3fv(cube[5]);
slouken@1895
   577
        glColor3fv(color[6]);
slouken@1895
   578
        glVertex3fv(cube[6]);
slouken@1895
   579
        glColor3fv(color[1]);
slouken@1895
   580
        glVertex3fv(cube[1]);
slouken@1895
   581
slouken@1895
   582
        glColor3fv(color[5]);
slouken@1895
   583
        glVertex3fv(cube[5]);
slouken@1895
   584
        glColor3fv(color[4]);
slouken@1895
   585
        glVertex3fv(cube[4]);
slouken@1895
   586
        glColor3fv(color[7]);
slouken@1895
   587
        glVertex3fv(cube[7]);
slouken@1895
   588
        glColor3fv(color[6]);
slouken@1895
   589
        glVertex3fv(cube[6]);
slouken@1895
   590
slouken@1895
   591
        glColor3fv(color[5]);
slouken@1895
   592
        glVertex3fv(cube[5]);
slouken@1895
   593
        glColor3fv(color[0]);
slouken@1895
   594
        glVertex3fv(cube[0]);
slouken@1895
   595
        glColor3fv(color[3]);
slouken@1895
   596
        glVertex3fv(cube[3]);
slouken@1895
   597
        glColor3fv(color[4]);
slouken@1895
   598
        glVertex3fv(cube[4]);
slouken@1895
   599
slouken@1895
   600
        glColor3fv(color[6]);
slouken@1895
   601
        glVertex3fv(cube[6]);
slouken@1895
   602
        glColor3fv(color[1]);
slouken@1895
   603
        glVertex3fv(cube[1]);
slouken@1895
   604
        glColor3fv(color[2]);
slouken@1895
   605
        glVertex3fv(cube[2]);
slouken@1895
   606
        glColor3fv(color[7]);
slouken@1895
   607
        glVertex3fv(cube[7]);
slouken@1895
   608
#else /* flat cube */
slouken@1895
   609
        glColor3f(1.0, 0.0, 0.0);
slouken@1895
   610
        glVertex3fv(cube[0]);
slouken@1895
   611
        glVertex3fv(cube[1]);
slouken@1895
   612
        glVertex3fv(cube[2]);
slouken@1895
   613
        glVertex3fv(cube[3]);
slouken@1895
   614
slouken@1895
   615
        glColor3f(0.0, 1.0, 0.0);
slouken@1895
   616
        glVertex3fv(cube[3]);
slouken@1895
   617
        glVertex3fv(cube[4]);
slouken@1895
   618
        glVertex3fv(cube[7]);
slouken@1895
   619
        glVertex3fv(cube[2]);
slouken@1895
   620
slouken@1895
   621
        glColor3f(0.0, 0.0, 1.0);
slouken@1895
   622
        glVertex3fv(cube[0]);
slouken@1895
   623
        glVertex3fv(cube[5]);
slouken@1895
   624
        glVertex3fv(cube[6]);
slouken@1895
   625
        glVertex3fv(cube[1]);
slouken@1895
   626
slouken@1895
   627
        glColor3f(0.0, 1.0, 1.0);
slouken@1895
   628
        glVertex3fv(cube[5]);
slouken@1895
   629
        glVertex3fv(cube[4]);
slouken@1895
   630
        glVertex3fv(cube[7]);
slouken@1895
   631
        glVertex3fv(cube[6]);
slouken@1895
   632
slouken@1895
   633
        glColor3f(1.0, 1.0, 0.0);
slouken@1895
   634
        glVertex3fv(cube[5]);
slouken@1895
   635
        glVertex3fv(cube[0]);
slouken@1895
   636
        glVertex3fv(cube[3]);
slouken@1895
   637
        glVertex3fv(cube[4]);
slouken@1895
   638
slouken@1895
   639
        glColor3f(1.0, 0.0, 1.0);
slouken@1895
   640
        glVertex3fv(cube[6]);
slouken@1895
   641
        glVertex3fv(cube[1]);
slouken@1895
   642
        glVertex3fv(cube[2]);
slouken@1895
   643
        glVertex3fv(cube[7]);
slouken@1895
   644
#endif /* SHADED_CUBE */
slouken@1895
   645
slouken@1895
   646
        glEnd();
slouken@1895
   647
slouken@1895
   648
        glMatrixMode(GL_MODELVIEW);
slouken@1895
   649
        glRotatef(5.0, 1.0, 1.0, 1.0);
slouken@1895
   650
slouken@1895
   651
        /* Draw 2D logo onto the 3D display */
slouken@1895
   652
        if (logo) {
slouken@1895
   653
            DrawLogoTexture();
slouken@1895
   654
        }
slouken@1895
   655
        if (logocursor) {
slouken@1895
   656
            DrawLogoCursor();
slouken@1895
   657
        }
slouken@1895
   658
slouken@1895
   659
        SDL_GL_SwapBuffers();
slouken@1895
   660
slouken@1895
   661
        /* Check for error conditions. */
slouken@1895
   662
        gl_error = glGetError();
slouken@1895
   663
slouken@1895
   664
        if (gl_error != GL_NO_ERROR) {
slouken@1895
   665
            fprintf(stderr, "testgl: OpenGL error: %d\n", gl_error);
slouken@1895
   666
        }
slouken@1895
   667
aschiffler@4884
   668
        sdl_error = (char *)SDL_GetError();
slouken@1895
   669
slouken@1895
   670
        if (sdl_error[0] != '\0') {
slouken@1895
   671
            fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
slouken@1895
   672
            SDL_ClearError();
slouken@1895
   673
        }
slouken@1895
   674
slouken@1895
   675
        /* Allow the user to see what's happening */
slouken@1895
   676
        if (slowly) {
slouken@1895
   677
            SDL_Delay(20);
slouken@1895
   678
        }
slouken@1895
   679
slouken@1895
   680
        /* Check if there's a pending event. */
slouken@2319
   681
        while (SDL_PollEvent(&event)) {
slouken@2319
   682
            done |= HandleEvent(&event);
slouken@1895
   683
        }
slouken@1895
   684
        ++frames;
slouken@1895
   685
    }
slouken@1895
   686
slouken@1895
   687
    /* Print out the frames per second */
slouken@1895
   688
    this_time = SDL_GetTicks();
slouken@1895
   689
    if (this_time != start_time) {
slouken@1895
   690
        printf("%2.2f FPS\n",
slouken@1895
   691
               ((float) frames / (this_time - start_time)) * 1000.0);
slouken@1895
   692
    }
slouken@1895
   693
slouken@1895
   694
    if (global_image) {
slouken@1895
   695
        SDL_FreeSurface(global_image);
slouken@1895
   696
        global_image = NULL;
slouken@1895
   697
    }
slouken@1895
   698
    if (global_texture) {
slouken@1895
   699
        glDeleteTextures(1, &global_texture);
slouken@1895
   700
        global_texture = 0;
slouken@1895
   701
    }
slouken@1895
   702
    if (cursor_texture) {
slouken@1895
   703
        glDeleteTextures(1, &cursor_texture);
slouken@1895
   704
        cursor_texture = 0;
slouken@1895
   705
    }
slouken@1895
   706
slouken@1895
   707
    /* Destroy our GL context, etc. */
slouken@1895
   708
    SDL_Quit();
slouken@1895
   709
    return (0);
slouken@0
   710
}
slouken@0
   711
slouken@1895
   712
int
slouken@1895
   713
main(int argc, char *argv[])
slouken@0
   714
{
slouken@1895
   715
    int i, logo, logocursor = 0;
slouken@1895
   716
    int numtests;
slouken@1895
   717
    int bpp = 0;
slouken@1895
   718
    int slowly;
slouken@1895
   719
    float gamma = 0.0;
slouken@1895
   720
    int noframe = 0;
slouken@1895
   721
    int fsaa = 0;
slouken@3571
   722
    int accel = -1;
slouken@1895
   723
    int sync = 0;
slouken@0
   724
slouken@1895
   725
    logo = 0;
slouken@1895
   726
    slowly = 0;
slouken@1895
   727
    numtests = 1;
slouken@1895
   728
    for (i = 1; argv[i]; ++i) {
slouken@1895
   729
        if (strcmp(argv[i], "-twice") == 0) {
slouken@1895
   730
            ++numtests;
slouken@320
   731
        }
slouken@1895
   732
        if (strcmp(argv[i], "-logo") == 0) {
slouken@1895
   733
            logo = 1;
slouken@1895
   734
        }
slouken@1895
   735
        if (strcmp(argv[i], "-logocursor") == 0) {
slouken@1895
   736
            logocursor = 1;
slouken@1895
   737
        }
slouken@1895
   738
        if (strcmp(argv[i], "-slow") == 0) {
slouken@1895
   739
            slowly = 1;
slouken@1895
   740
        }
slouken@1895
   741
        if (strcmp(argv[i], "-bpp") == 0) {
slouken@1895
   742
            bpp = atoi(argv[++i]);
slouken@1895
   743
        }
slouken@1895
   744
        if (strcmp(argv[i], "-gamma") == 0) {
slouken@1895
   745
            gamma = (float) atof(argv[++i]);
slouken@1895
   746
        }
slouken@1895
   747
        if (strcmp(argv[i], "-noframe") == 0) {
slouken@1895
   748
            noframe = 1;
slouken@1895
   749
        }
slouken@1895
   750
        if (strcmp(argv[i], "-fsaa") == 0) {
slouken@1895
   751
            ++fsaa;
slouken@1895
   752
        }
slouken@3571
   753
        if (strcmp(argv[i], "-accel") == 0) {
slouken@3571
   754
            accel = atoi(argv[++i]);
slouken@1895
   755
        }
slouken@1895
   756
        if (strcmp(argv[i], "-sync") == 0) {
slouken@1895
   757
            ++sync;
slouken@1895
   758
        }
slouken@1895
   759
        if (strncmp(argv[i], "-h", 2) == 0) {
slouken@1895
   760
            printf
slouken@3571
   761
                ("Usage: %s [-twice] [-logo] [-logocursor] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa] [-accel n] [-sync] [-fullscreen]\n",
slouken@1895
   762
                 argv[0]);
slouken@1895
   763
            exit(0);
slouken@1895
   764
        }
slouken@1895
   765
    }
slouken@1895
   766
    for (i = 0; i < numtests; ++i) {
slouken@1895
   767
        RunGLTest(argc, argv, logo, logocursor, slowly, bpp, gamma,
slouken@3571
   768
                  noframe, fsaa, sync, accel);
slouken@1895
   769
    }
slouken@1895
   770
    return 0;
slouken@0
   771
}
slouken@0
   772
slouken@0
   773
#else /* HAVE_OPENGL */
slouken@0
   774
slouken@1895
   775
int
slouken@1895
   776
main(int argc, char *argv[])
slouken@0
   777
{
slouken@1895
   778
    printf("No OpenGL support on this system\n");
slouken@1895
   779
    return 1;
slouken@0
   780
}
slouken@0
   781
slouken@0
   782
#endif /* HAVE_OPENGL */