test/testgl2.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 01 Aug 2012 20:29:36 -0400
changeset 6382 64d54101773a
parent 5535 96594ac5fd1a
child 6785 c094c438c0a6
permissions -rw-r--r--
Add support for (GLX|WGL)_EXT_swap_control_tear.

This required a small public API change: SDL_GL_SetSwapInterval() now accepts
negative values, and SDL_GL_GetSwapInterval() doesn't report errors anymore
(if it can't work, it'll return zero as a reasonable default).

If you need to test for errors, such as a lack of swap_control_tear support,
check the results of SDL_GL_SetSwapInterval() when you set your desired
value.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@1914
    12
#include <stdlib.h>
slouken@1914
    13
#include <stdio.h>
slouken@1914
    14
#include <string.h>
slouken@1914
    15
#include <math.h>
slouken@1914
    16
slouken@1915
    17
#include "common.h"
slouken@1914
    18
slouken@1914
    19
#ifdef __MACOS__
slouken@1914
    20
#define HAVE_OPENGL
slouken@1914
    21
#endif
slouken@1914
    22
slouken@1914
    23
#ifdef HAVE_OPENGL
slouken@1914
    24
slouken@1914
    25
#include "SDL_opengl.h"
slouken@1914
    26
slouken@1914
    27
/* Undefine this if you want a flat cube instead of a rainbow cube */
slouken@1914
    28
#define SHADED_CUBE
slouken@1914
    29
slouken@1915
    30
static CommonState *state;
slouken@1915
    31
static SDL_GLContext context;
slouken@1914
    32
slouken@1915
    33
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1915
    34
static void
slouken@1915
    35
quit(int rc)
slouken@1914
    36
{
slouken@1915
    37
    if (context) {
bob@2328
    38
        /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@1915
    39
        SDL_GL_DeleteContext(context);
slouken@1914
    40
    }
slouken@1915
    41
    CommonQuit(state);
slouken@1915
    42
    exit(rc);
slouken@1914
    43
}
slouken@1914
    44
slouken@1915
    45
static void
slouken@1915
    46
Render()
slouken@1914
    47
{
slouken@1915
    48
    static float color[8][3] = {
slouken@1915
    49
        {1.0, 1.0, 0.0},
slouken@1915
    50
        {1.0, 0.0, 0.0},
slouken@1915
    51
        {0.0, 0.0, 0.0},
slouken@1915
    52
        {0.0, 1.0, 0.0},
slouken@1915
    53
        {0.0, 1.0, 1.0},
slouken@1915
    54
        {1.0, 1.0, 1.0},
slouken@1915
    55
        {1.0, 0.0, 1.0},
slouken@1915
    56
        {0.0, 0.0, 1.0}
slouken@1915
    57
    };
slouken@1915
    58
    static float cube[8][3] = {
slouken@1915
    59
        {0.5, 0.5, -0.5},
slouken@1915
    60
        {0.5, -0.5, -0.5},
slouken@1915
    61
        {-0.5, -0.5, -0.5},
slouken@1915
    62
        {-0.5, 0.5, -0.5},
slouken@1915
    63
        {-0.5, 0.5, 0.5},
slouken@1915
    64
        {0.5, 0.5, 0.5},
slouken@1915
    65
        {0.5, -0.5, 0.5},
slouken@1915
    66
        {-0.5, -0.5, 0.5}
slouken@1915
    67
    };
slouken@1914
    68
slouken@1915
    69
    /* Do our drawing, too. */
slouken@1915
    70
    glClearColor(0.0, 0.0, 0.0, 1.0);
slouken@1915
    71
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@1914
    72
slouken@1915
    73
    glBegin(GL_QUADS);
slouken@1915
    74
slouken@1915
    75
#ifdef SHADED_CUBE
slouken@1915
    76
    glColor3fv(color[0]);
slouken@1915
    77
    glVertex3fv(cube[0]);
slouken@1915
    78
    glColor3fv(color[1]);
slouken@1915
    79
    glVertex3fv(cube[1]);
slouken@1915
    80
    glColor3fv(color[2]);
slouken@1915
    81
    glVertex3fv(cube[2]);
slouken@1915
    82
    glColor3fv(color[3]);
slouken@1915
    83
    glVertex3fv(cube[3]);
slouken@1915
    84
slouken@1915
    85
    glColor3fv(color[3]);
slouken@1915
    86
    glVertex3fv(cube[3]);
slouken@1915
    87
    glColor3fv(color[4]);
slouken@1915
    88
    glVertex3fv(cube[4]);
slouken@1915
    89
    glColor3fv(color[7]);
slouken@1915
    90
    glVertex3fv(cube[7]);
slouken@1915
    91
    glColor3fv(color[2]);
slouken@1915
    92
    glVertex3fv(cube[2]);
slouken@1915
    93
slouken@1915
    94
    glColor3fv(color[0]);
slouken@1915
    95
    glVertex3fv(cube[0]);
slouken@1915
    96
    glColor3fv(color[5]);
slouken@1915
    97
    glVertex3fv(cube[5]);
slouken@1915
    98
    glColor3fv(color[6]);
slouken@1915
    99
    glVertex3fv(cube[6]);
slouken@1915
   100
    glColor3fv(color[1]);
slouken@1915
   101
    glVertex3fv(cube[1]);
slouken@1915
   102
slouken@1915
   103
    glColor3fv(color[5]);
slouken@1915
   104
    glVertex3fv(cube[5]);
slouken@1915
   105
    glColor3fv(color[4]);
slouken@1915
   106
    glVertex3fv(cube[4]);
slouken@1915
   107
    glColor3fv(color[7]);
slouken@1915
   108
    glVertex3fv(cube[7]);
slouken@1915
   109
    glColor3fv(color[6]);
slouken@1915
   110
    glVertex3fv(cube[6]);
slouken@1915
   111
slouken@1915
   112
    glColor3fv(color[5]);
slouken@1915
   113
    glVertex3fv(cube[5]);
slouken@1915
   114
    glColor3fv(color[0]);
slouken@1915
   115
    glVertex3fv(cube[0]);
slouken@1915
   116
    glColor3fv(color[3]);
slouken@1915
   117
    glVertex3fv(cube[3]);
slouken@1915
   118
    glColor3fv(color[4]);
slouken@1915
   119
    glVertex3fv(cube[4]);
slouken@1915
   120
slouken@1915
   121
    glColor3fv(color[6]);
slouken@1915
   122
    glVertex3fv(cube[6]);
slouken@1915
   123
    glColor3fv(color[1]);
slouken@1915
   124
    glVertex3fv(cube[1]);
slouken@1915
   125
    glColor3fv(color[2]);
slouken@1915
   126
    glVertex3fv(cube[2]);
slouken@1915
   127
    glColor3fv(color[7]);
slouken@1915
   128
    glVertex3fv(cube[7]);
slouken@1915
   129
#else /* flat cube */
slouken@1915
   130
    glColor3f(1.0, 0.0, 0.0);
slouken@1915
   131
    glVertex3fv(cube[0]);
slouken@1915
   132
    glVertex3fv(cube[1]);
slouken@1915
   133
    glVertex3fv(cube[2]);
slouken@1915
   134
    glVertex3fv(cube[3]);
slouken@1915
   135
slouken@1915
   136
    glColor3f(0.0, 1.0, 0.0);
slouken@1915
   137
    glVertex3fv(cube[3]);
slouken@1915
   138
    glVertex3fv(cube[4]);
slouken@1915
   139
    glVertex3fv(cube[7]);
slouken@1915
   140
    glVertex3fv(cube[2]);
slouken@1915
   141
slouken@1915
   142
    glColor3f(0.0, 0.0, 1.0);
slouken@1915
   143
    glVertex3fv(cube[0]);
slouken@1915
   144
    glVertex3fv(cube[5]);
slouken@1915
   145
    glVertex3fv(cube[6]);
slouken@1915
   146
    glVertex3fv(cube[1]);
slouken@1915
   147
slouken@1915
   148
    glColor3f(0.0, 1.0, 1.0);
slouken@1915
   149
    glVertex3fv(cube[5]);
slouken@1915
   150
    glVertex3fv(cube[4]);
slouken@1915
   151
    glVertex3fv(cube[7]);
slouken@1915
   152
    glVertex3fv(cube[6]);
slouken@1915
   153
slouken@1915
   154
    glColor3f(1.0, 1.0, 0.0);
slouken@1915
   155
    glVertex3fv(cube[5]);
slouken@1915
   156
    glVertex3fv(cube[0]);
slouken@1915
   157
    glVertex3fv(cube[3]);
slouken@1915
   158
    glVertex3fv(cube[4]);
slouken@1915
   159
slouken@1915
   160
    glColor3f(1.0, 0.0, 1.0);
slouken@1915
   161
    glVertex3fv(cube[6]);
slouken@1915
   162
    glVertex3fv(cube[1]);
slouken@1915
   163
    glVertex3fv(cube[2]);
slouken@1915
   164
    glVertex3fv(cube[7]);
slouken@1915
   165
#endif /* SHADED_CUBE */
slouken@1915
   166
slouken@1915
   167
    glEnd();
slouken@1915
   168
slouken@1915
   169
    glMatrixMode(GL_MODELVIEW);
slouken@1915
   170
    glRotatef(5.0, 1.0, 1.0, 1.0);
slouken@1914
   171
}
slouken@1914
   172
slouken@1914
   173
int
slouken@1915
   174
main(int argc, char *argv[])
slouken@1914
   175
{
slouken@3571
   176
    int fsaa, accel;
slouken@1915
   177
    int value;
slouken@1915
   178
    int i, done;
slouken@1967
   179
    SDL_DisplayMode mode;
slouken@1915
   180
    SDL_Event event;
slouken@1915
   181
    Uint32 then, now, frames;
slouken@3099
   182
    int status;
slouken@1914
   183
slouken@1915
   184
    /* Initialize parameters */
slouken@1915
   185
    fsaa = 0;
slouken@3571
   186
    accel = -1;
slouken@1915
   187
slouken@1915
   188
    /* Initialize test framework */
slouken@1915
   189
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1915
   190
    if (!state) {
slouken@1915
   191
        return 1;
slouken@1915
   192
    }
slouken@1915
   193
    for (i = 1; i < argc;) {
slouken@1915
   194
        int consumed;
slouken@1915
   195
slouken@1915
   196
        consumed = CommonArg(state, i);
slouken@1915
   197
        if (consumed == 0) {
slouken@1915
   198
            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
slouken@1915
   199
                ++fsaa;
slouken@1915
   200
                consumed = 1;
slouken@3571
   201
            } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i+1 < argc) {
slouken@3571
   202
                accel = atoi(argv[i+1]);
slouken@3571
   203
                consumed = 2;
slouken@1915
   204
            } else {
slouken@1915
   205
                consumed = -1;
slouken@1915
   206
            }
slouken@1914
   207
        }
slouken@1915
   208
        if (consumed < 0) {
slouken@3571
   209
            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel n]\n", argv[0],
slouken@1915
   210
                    CommonUsage(state));
slouken@1915
   211
            quit(1);
slouken@1914
   212
        }
slouken@1915
   213
        i += consumed;
slouken@1914
   214
    }
slouken@1914
   215
slouken@1915
   216
    /* Set OpenGL parameters */
slouken@1915
   217
    state->window_flags |= SDL_WINDOW_OPENGL;
lestat@3408
   218
    state->gl_red_size = 5;
lestat@3408
   219
    state->gl_green_size = 5;
lestat@3408
   220
    state->gl_blue_size = 5;
lestat@3408
   221
    state->gl_depth_size = 16;
slouken@3418
   222
    state->gl_double_buffer = 1;
slouken@1914
   223
    if (fsaa) {
lestat@3408
   224
        state->gl_multisamplebuffers = 1;
lestat@3408
   225
        state->gl_multisamplesamples = fsaa;
slouken@1914
   226
    }
slouken@3571
   227
    if (accel >= 0) {
slouken@3571
   228
        state->gl_accelerated = accel;
slouken@3571
   229
    }
lestat@3408
   230
slouken@1915
   231
    if (!CommonInit(state)) {
slouken@1915
   232
        quit(2);
slouken@1914
   233
    }
slouken@1915
   234
slouken@1915
   235
    /* Create OpenGL context */
slouken@1915
   236
    context = SDL_GL_CreateContext(state->windows[0]);
slouken@1915
   237
    if (!context) {
slouken@1915
   238
        fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
slouken@1915
   239
        quit(2);
slouken@1915
   240
    }
slouken@1915
   241
slouken@1965
   242
    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
icculus@6382
   243
        /* try late-swap-tearing first. If not supported, try normal vsync. */
icculus@6382
   244
        if (SDL_GL_SetSwapInterval(-1) == -1) {
icculus@6382
   245
            SDL_GL_SetSwapInterval(1);
icculus@6382
   246
        }
slouken@1914
   247
    } else {
icculus@6382
   248
        SDL_GL_SetSwapInterval(0);  /* disable vsync. */
slouken@1914
   249
    }
slouken@1914
   250
slouken@5244
   251
    SDL_GetCurrentDisplayMode(0, &mode);
icculus@6382
   252
    printf("Screen BPP    : %d\n", SDL_BITSPERPIXEL(mode.format));
icculus@6382
   253
    printf("Swap Interval : %d\n", SDL_GL_GetSwapInterval());
slouken@1914
   254
    printf("\n");
icculus@6382
   255
    printf("Vendor        : %s\n", glGetString(GL_VENDOR));
icculus@6382
   256
    printf("Renderer      : %s\n", glGetString(GL_RENDERER));
icculus@6382
   257
    printf("Version       : %s\n", glGetString(GL_VERSION));
slouken@1914
   258
    printf("\n");
slouken@1914
   259
slouken@3139
   260
    status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
slouken@3099
   261
    if (!status) {
slouken@3099
   262
        printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   263
    } else {
slouken@3099
   264
        printf("Failed to get SDL_GL_RED_SIZE: %s\n", SDL_GetError());
slouken@3099
   265
    }
slouken@3139
   266
    status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
slouken@3099
   267
    if (!status) {
slouken@3099
   268
        printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   269
    } else {
slouken@3099
   270
        printf("Failed to get SDL_GL_GREEN_SIZE: %s\n", SDL_GetError());
slouken@3099
   271
    }
slouken@3139
   272
    status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
slouken@3099
   273
    if (!status) {
slouken@3099
   274
        printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   275
    } else {
slouken@3099
   276
        printf("Failed to get SDL_GL_BLUE_SIZE: %s\n", SDL_GetError());
slouken@3099
   277
    }
slouken@3139
   278
    status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
slouken@3099
   279
    if (!status) {
slouken@3099
   280
        printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
slouken@3099
   281
    } else {
slouken@3099
   282
        printf("Failed to get SDL_GL_DEPTH_SIZE: %s\n", SDL_GetError());
slouken@3099
   283
    }
slouken@1914
   284
    if (fsaa) {
slouken@3139
   285
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
slouken@3099
   286
        if (!status) {
slouken@3099
   287
            printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
slouken@3099
   288
        } else {
slouken@3139
   289
            printf("Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
slouken@3139
   290
                   SDL_GetError());
slouken@3099
   291
        }
slouken@3139
   292
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
slouken@3099
   293
        if (!status) {
slouken@3139
   294
            printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
slouken@3139
   295
                   value);
slouken@3099
   296
        } else {
slouken@3139
   297
            printf("Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
slouken@3139
   298
                   SDL_GetError());
slouken@3099
   299
        }
slouken@1914
   300
    }
slouken@3571
   301
    if (accel >= 0) {
slouken@3571
   302
        status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
slouken@3571
   303
        if (!status) {
slouken@3571
   304
            printf("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d\n", accel,
slouken@3571
   305
                   value);
slouken@3571
   306
        } else {
slouken@3571
   307
            printf("Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
slouken@3571
   308
                   SDL_GetError());
slouken@3571
   309
        }
slouken@1914
   310
    }
slouken@1915
   311
slouken@1915
   312
    /* Set rendering settings */
slouken@1915
   313
    glMatrixMode(GL_PROJECTION);
slouken@1915
   314
    glLoadIdentity();
slouken@1915
   315
    glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
slouken@1915
   316
    glMatrixMode(GL_MODELVIEW);
slouken@1915
   317
    glLoadIdentity();
slouken@1915
   318
    glEnable(GL_DEPTH_TEST);
slouken@1915
   319
    glDepthFunc(GL_LESS);
slouken@1915
   320
    glShadeModel(GL_SMOOTH);
slouken@1915
   321
slouken@1915
   322
    /* Main render loop */
slouken@1915
   323
    frames = 0;
slouken@1915
   324
    then = SDL_GetTicks();
slouken@1915
   325
    done = 0;
slouken@1915
   326
    while (!done) {
slouken@1915
   327
        /* Check for events */
slouken@1915
   328
        ++frames;
slouken@1915
   329
        while (SDL_PollEvent(&event)) {
slouken@1915
   330
            CommonEvent(state, &event, &done);
slouken@1915
   331
        }
slouken@1915
   332
        for (i = 0; i < state->num_windows; ++i) {
slouken@1915
   333
            int w, h;
slouken@1915
   334
            SDL_GL_MakeCurrent(state->windows[i], context);
slouken@1915
   335
            SDL_GetWindowSize(state->windows[i], &w, &h);
slouken@1915
   336
            glViewport(0, 0, w, h);
slouken@1915
   337
            Render();
slouken@1915
   338
            SDL_GL_SwapWindow(state->windows[i]);
slouken@1915
   339
        }
slouken@1914
   340
    }
slouken@1914
   341
slouken@1915
   342
    /* Print out some timing information */
slouken@1915
   343
    now = SDL_GetTicks();
slouken@1915
   344
    if (now > then) {
slouken@1915
   345
        printf("%2.2f frames per second\n",
slouken@1915
   346
               ((double) frames * 1000) / (now - then));
slouken@1914
   347
    }
slouken@1915
   348
    quit(0);
slouken@2232
   349
    return 0;
slouken@1914
   350
}
slouken@1914
   351
slouken@1914
   352
#else /* HAVE_OPENGL */
slouken@1914
   353
slouken@1914
   354
int
slouken@1914
   355
main(int argc, char *argv[])
slouken@1914
   356
{
slouken@1914
   357
    printf("No OpenGL support on this system\n");
slouken@1914
   358
    return 1;
slouken@1914
   359
}
slouken@1914
   360
slouken@1914
   361
#endif /* HAVE_OPENGL */