test/testgl.c
author Bob Pendleton <bob@pendleton.com>
Wed, 20 Feb 2008 23:27:33 +0000
changeset 2318 0c653886cce7
parent 2267 c785543d1843
child 2319 5835745560bf
permissions -rw-r--r--
HandleEvent() returns done each time it is called. If done was set to 0 the program should quit, but because done is not checked until *all* events are handled a
following event can reset done to 1 and prevent the program from terminating when told to. I fixed the while loop that handles events to check for the state of done
after handling each event. That could leave some events unhandled when the program exits, but it ensures that the program will exit.
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@1895
   273
    SDL_GetMouseState(&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@1895
   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@1895
   457
    if (accel) {
slouken@1895
   458
        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
slouken@1895
   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@1895
   496
    if (accel) {
slouken@1895
   497
        SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
slouken@1895
   498
        printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
slouken@1895
   499
    }
slouken@1895
   500
    if (sync) {
slouken@1912
   501
        printf("Buffer swap interval: requested 1, got %d\n",
slouken@1912
   502
               SDL_GL_GetSwapInterval());
slouken@1895
   503
    }
slouken@1895
   504
slouken@1895
   505
    /* Set the window manager title bar */
slouken@1895
   506
    SDL_WM_SetCaption("SDL GL test", "testgl");
slouken@1895
   507
slouken@1895
   508
    /* Set the gamma for the window */
slouken@1895
   509
    if (gamma != 0.0) {
slouken@1895
   510
        SDL_SetGamma(gamma, gamma, gamma);
slouken@1895
   511
    }
slouken@1895
   512
slouken@1895
   513
    glViewport(0, 0, w, h);
slouken@1895
   514
    glMatrixMode(GL_PROJECTION);
slouken@1895
   515
    glLoadIdentity();
slouken@1895
   516
slouken@1895
   517
    glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
slouken@1895
   518
slouken@1895
   519
    glMatrixMode(GL_MODELVIEW);
slouken@1895
   520
    glLoadIdentity();
slouken@1895
   521
slouken@1895
   522
    glEnable(GL_DEPTH_TEST);
slouken@1895
   523
slouken@1895
   524
    glDepthFunc(GL_LESS);
slouken@1895
   525
slouken@1895
   526
    glShadeModel(GL_SMOOTH);
slouken@1895
   527
slouken@1895
   528
    /* Loop until done. */
slouken@1895
   529
    start_time = SDL_GetTicks();
slouken@1895
   530
    frames = 0;
slouken@1895
   531
    while (!done) {
slouken@1895
   532
        GLenum gl_error;
slouken@1895
   533
        char *sdl_error;
slouken@1895
   534
        SDL_Event event;
slouken@1895
   535
slouken@1895
   536
        /* Do our drawing, too. */
slouken@1895
   537
        glClearColor(0.0, 0.0, 0.0, 1.0);
slouken@1895
   538
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1895
   539
slouken@1895
   540
        glBegin(GL_QUADS);
slouken@1895
   541
slouken@1895
   542
#ifdef SHADED_CUBE
slouken@1895
   543
        glColor3fv(color[0]);
slouken@1895
   544
        glVertex3fv(cube[0]);
slouken@1895
   545
        glColor3fv(color[1]);
slouken@1895
   546
        glVertex3fv(cube[1]);
slouken@1895
   547
        glColor3fv(color[2]);
slouken@1895
   548
        glVertex3fv(cube[2]);
slouken@1895
   549
        glColor3fv(color[3]);
slouken@1895
   550
        glVertex3fv(cube[3]);
slouken@1895
   551
slouken@1895
   552
        glColor3fv(color[3]);
slouken@1895
   553
        glVertex3fv(cube[3]);
slouken@1895
   554
        glColor3fv(color[4]);
slouken@1895
   555
        glVertex3fv(cube[4]);
slouken@1895
   556
        glColor3fv(color[7]);
slouken@1895
   557
        glVertex3fv(cube[7]);
slouken@1895
   558
        glColor3fv(color[2]);
slouken@1895
   559
        glVertex3fv(cube[2]);
slouken@1895
   560
slouken@1895
   561
        glColor3fv(color[0]);
slouken@1895
   562
        glVertex3fv(cube[0]);
slouken@1895
   563
        glColor3fv(color[5]);
slouken@1895
   564
        glVertex3fv(cube[5]);
slouken@1895
   565
        glColor3fv(color[6]);
slouken@1895
   566
        glVertex3fv(cube[6]);
slouken@1895
   567
        glColor3fv(color[1]);
slouken@1895
   568
        glVertex3fv(cube[1]);
slouken@1895
   569
slouken@1895
   570
        glColor3fv(color[5]);
slouken@1895
   571
        glVertex3fv(cube[5]);
slouken@1895
   572
        glColor3fv(color[4]);
slouken@1895
   573
        glVertex3fv(cube[4]);
slouken@1895
   574
        glColor3fv(color[7]);
slouken@1895
   575
        glVertex3fv(cube[7]);
slouken@1895
   576
        glColor3fv(color[6]);
slouken@1895
   577
        glVertex3fv(cube[6]);
slouken@1895
   578
slouken@1895
   579
        glColor3fv(color[5]);
slouken@1895
   580
        glVertex3fv(cube[5]);
slouken@1895
   581
        glColor3fv(color[0]);
slouken@1895
   582
        glVertex3fv(cube[0]);
slouken@1895
   583
        glColor3fv(color[3]);
slouken@1895
   584
        glVertex3fv(cube[3]);
slouken@1895
   585
        glColor3fv(color[4]);
slouken@1895
   586
        glVertex3fv(cube[4]);
slouken@1895
   587
slouken@1895
   588
        glColor3fv(color[6]);
slouken@1895
   589
        glVertex3fv(cube[6]);
slouken@1895
   590
        glColor3fv(color[1]);
slouken@1895
   591
        glVertex3fv(cube[1]);
slouken@1895
   592
        glColor3fv(color[2]);
slouken@1895
   593
        glVertex3fv(cube[2]);
slouken@1895
   594
        glColor3fv(color[7]);
slouken@1895
   595
        glVertex3fv(cube[7]);
slouken@1895
   596
#else /* flat cube */
slouken@1895
   597
        glColor3f(1.0, 0.0, 0.0);
slouken@1895
   598
        glVertex3fv(cube[0]);
slouken@1895
   599
        glVertex3fv(cube[1]);
slouken@1895
   600
        glVertex3fv(cube[2]);
slouken@1895
   601
        glVertex3fv(cube[3]);
slouken@1895
   602
slouken@1895
   603
        glColor3f(0.0, 1.0, 0.0);
slouken@1895
   604
        glVertex3fv(cube[3]);
slouken@1895
   605
        glVertex3fv(cube[4]);
slouken@1895
   606
        glVertex3fv(cube[7]);
slouken@1895
   607
        glVertex3fv(cube[2]);
slouken@1895
   608
slouken@1895
   609
        glColor3f(0.0, 0.0, 1.0);
slouken@1895
   610
        glVertex3fv(cube[0]);
slouken@1895
   611
        glVertex3fv(cube[5]);
slouken@1895
   612
        glVertex3fv(cube[6]);
slouken@1895
   613
        glVertex3fv(cube[1]);
slouken@1895
   614
slouken@1895
   615
        glColor3f(0.0, 1.0, 1.0);
slouken@1895
   616
        glVertex3fv(cube[5]);
slouken@1895
   617
        glVertex3fv(cube[4]);
slouken@1895
   618
        glVertex3fv(cube[7]);
slouken@1895
   619
        glVertex3fv(cube[6]);
slouken@1895
   620
slouken@1895
   621
        glColor3f(1.0, 1.0, 0.0);
slouken@1895
   622
        glVertex3fv(cube[5]);
slouken@1895
   623
        glVertex3fv(cube[0]);
slouken@1895
   624
        glVertex3fv(cube[3]);
slouken@1895
   625
        glVertex3fv(cube[4]);
slouken@1895
   626
slouken@1895
   627
        glColor3f(1.0, 0.0, 1.0);
slouken@1895
   628
        glVertex3fv(cube[6]);
slouken@1895
   629
        glVertex3fv(cube[1]);
slouken@1895
   630
        glVertex3fv(cube[2]);
slouken@1895
   631
        glVertex3fv(cube[7]);
slouken@1895
   632
#endif /* SHADED_CUBE */
slouken@1895
   633
slouken@1895
   634
        glEnd();
slouken@1895
   635
slouken@1895
   636
        glMatrixMode(GL_MODELVIEW);
slouken@1895
   637
        glRotatef(5.0, 1.0, 1.0, 1.0);
slouken@1895
   638
slouken@1895
   639
        /* Draw 2D logo onto the 3D display */
slouken@1895
   640
        if (logo) {
slouken@1895
   641
            DrawLogoTexture();
slouken@1895
   642
        }
slouken@1895
   643
        if (logocursor) {
slouken@1895
   644
            DrawLogoCursor();
slouken@1895
   645
        }
slouken@1895
   646
slouken@1895
   647
        SDL_GL_SwapBuffers();
slouken@1895
   648
slouken@1895
   649
        /* Check for error conditions. */
slouken@1895
   650
        gl_error = glGetError();
slouken@1895
   651
slouken@1895
   652
        if (gl_error != GL_NO_ERROR) {
slouken@1895
   653
            fprintf(stderr, "testgl: OpenGL error: %d\n", gl_error);
slouken@1895
   654
        }
slouken@1895
   655
slouken@1895
   656
        sdl_error = SDL_GetError();
slouken@1895
   657
slouken@1895
   658
        if (sdl_error[0] != '\0') {
slouken@1895
   659
            fprintf(stderr, "testgl: SDL error '%s'\n", sdl_error);
slouken@1895
   660
            SDL_ClearError();
slouken@1895
   661
        }
slouken@1895
   662
slouken@1895
   663
        /* Allow the user to see what's happening */
slouken@1895
   664
        if (slowly) {
slouken@1895
   665
            SDL_Delay(20);
slouken@1895
   666
        }
slouken@1895
   667
slouken@1895
   668
        /* Check if there's a pending event. */
bob@2318
   669
        while (!done && SDL_PollEvent(&event)) {
slouken@1895
   670
            done = HandleEvent(&event);
slouken@1895
   671
        }
slouken@1895
   672
        ++frames;
slouken@1895
   673
    }
slouken@1895
   674
slouken@1895
   675
    /* Print out the frames per second */
slouken@1895
   676
    this_time = SDL_GetTicks();
slouken@1895
   677
    if (this_time != start_time) {
slouken@1895
   678
        printf("%2.2f FPS\n",
slouken@1895
   679
               ((float) frames / (this_time - start_time)) * 1000.0);
slouken@1895
   680
    }
slouken@1895
   681
slouken@1895
   682
    if (global_image) {
slouken@1895
   683
        SDL_FreeSurface(global_image);
slouken@1895
   684
        global_image = NULL;
slouken@1895
   685
    }
slouken@1895
   686
    if (global_texture) {
slouken@1895
   687
        glDeleteTextures(1, &global_texture);
slouken@1895
   688
        global_texture = 0;
slouken@1895
   689
    }
slouken@1895
   690
    if (cursor_texture) {
slouken@1895
   691
        glDeleteTextures(1, &cursor_texture);
slouken@1895
   692
        cursor_texture = 0;
slouken@1895
   693
    }
slouken@1895
   694
slouken@1895
   695
    /* Destroy our GL context, etc. */
slouken@1895
   696
    SDL_Quit();
slouken@1895
   697
    return (0);
slouken@0
   698
}
slouken@0
   699
slouken@1895
   700
int
slouken@1895
   701
main(int argc, char *argv[])
slouken@0
   702
{
slouken@1895
   703
    int i, logo, logocursor = 0;
slouken@1895
   704
    int numtests;
slouken@1895
   705
    int bpp = 0;
slouken@1895
   706
    int slowly;
slouken@1895
   707
    float gamma = 0.0;
slouken@1895
   708
    int noframe = 0;
slouken@1895
   709
    int fsaa = 0;
slouken@1895
   710
    int accel = 0;
slouken@1895
   711
    int sync = 0;
slouken@0
   712
slouken@1895
   713
    logo = 0;
slouken@1895
   714
    slowly = 0;
slouken@1895
   715
    numtests = 1;
slouken@1895
   716
    for (i = 1; argv[i]; ++i) {
slouken@1895
   717
        if (strcmp(argv[i], "-twice") == 0) {
slouken@1895
   718
            ++numtests;
slouken@320
   719
        }
slouken@1895
   720
        if (strcmp(argv[i], "-logo") == 0) {
slouken@1895
   721
            logo = 1;
slouken@1895
   722
        }
slouken@1895
   723
        if (strcmp(argv[i], "-logocursor") == 0) {
slouken@1895
   724
            logocursor = 1;
slouken@1895
   725
        }
slouken@1895
   726
        if (strcmp(argv[i], "-slow") == 0) {
slouken@1895
   727
            slowly = 1;
slouken@1895
   728
        }
slouken@1895
   729
        if (strcmp(argv[i], "-bpp") == 0) {
slouken@1895
   730
            bpp = atoi(argv[++i]);
slouken@1895
   731
        }
slouken@1895
   732
        if (strcmp(argv[i], "-gamma") == 0) {
slouken@1895
   733
            gamma = (float) atof(argv[++i]);
slouken@1895
   734
        }
slouken@1895
   735
        if (strcmp(argv[i], "-noframe") == 0) {
slouken@1895
   736
            noframe = 1;
slouken@1895
   737
        }
slouken@1895
   738
        if (strcmp(argv[i], "-fsaa") == 0) {
slouken@1895
   739
            ++fsaa;
slouken@1895
   740
        }
slouken@1895
   741
        if (strcmp(argv[i], "-accel") == 0) {
slouken@1895
   742
            ++accel;
slouken@1895
   743
        }
slouken@1895
   744
        if (strcmp(argv[i], "-sync") == 0) {
slouken@1895
   745
            ++sync;
slouken@1895
   746
        }
slouken@1895
   747
        if (strncmp(argv[i], "-h", 2) == 0) {
slouken@1895
   748
            printf
slouken@1895
   749
                ("Usage: %s [-twice] [-logo] [-logocursor] [-slow] [-bpp n] [-gamma n] [-noframe] [-fsaa] [-accel] [-sync] [-fullscreen]\n",
slouken@1895
   750
                 argv[0]);
slouken@1895
   751
            exit(0);
slouken@1895
   752
        }
slouken@1895
   753
    }
slouken@1895
   754
    for (i = 0; i < numtests; ++i) {
slouken@1895
   755
        RunGLTest(argc, argv, logo, logocursor, slowly, bpp, gamma,
slouken@1895
   756
                  noframe, fsaa, sync, accel);
slouken@1895
   757
    }
slouken@1895
   758
    return 0;
slouken@0
   759
}
slouken@0
   760
slouken@0
   761
#else /* HAVE_OPENGL */
slouken@0
   762
slouken@1895
   763
int
slouken@1895
   764
main(int argc, char *argv[])
slouken@0
   765
{
slouken@1895
   766
    printf("No OpenGL support on this system\n");
slouken@1895
   767
    return 1;
slouken@0
   768
}
slouken@0
   769
slouken@0
   770
#endif /* HAVE_OPENGL */