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