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