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