test/testgles.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 10:26:38 -0800
changeset 6785 c094c438c0a6
parent 5535 96594ac5fd1a
child 7517 965d57022c01
permissions -rw-r--r--
Switched the test code over to use the common functions in the test library.
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@2765
    12
#include <stdlib.h>
slouken@2765
    13
#include <stdio.h>
slouken@2765
    14
#include <string.h>
slouken@2765
    15
#include <math.h>
slouken@2765
    16
slouken@6785
    17
#include "SDL_test_common.h"
slouken@2765
    18
slouken@4961
    19
#if defined(__IPHONEOS__) || defined(__ANDROID__)
slouken@2765
    20
#define HAVE_OPENGLES
slouken@2765
    21
#endif
slouken@2765
    22
slouken@2765
    23
#ifdef HAVE_OPENGLES
slouken@2765
    24
slouken@2765
    25
#include "SDL_opengles.h"
slouken@2765
    26
slouken@6785
    27
static SDLTest_CommonState *state;
slouken@3139
    28
static SDL_GLContext *context = NULL;
lestat@3408
    29
static int depth = 16;
slouken@2765
    30
slouken@2765
    31
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@2765
    32
static void
slouken@2765
    33
quit(int rc)
slouken@2765
    34
{
lestat@3110
    35
    int i;
lestat@3110
    36
slouken@3139
    37
    if (context != NULL) {
slouken@3139
    38
        for (i = 0; i < state->num_windows; i++) {
lestat@3110
    39
            if (context[i]) {
lestat@3110
    40
                SDL_GL_DeleteContext(context[i]);
lestat@3110
    41
            }
lestat@3110
    42
        }
lestat@3110
    43
lestat@3110
    44
        SDL_free(context);
slouken@2765
    45
    }
lestat@3110
    46
slouken@6785
    47
    SDLTest_CommonQuit(state);
slouken@2765
    48
    exit(rc);
slouken@2765
    49
}
slouken@2765
    50
slouken@2765
    51
static void
slouken@2765
    52
Render()
slouken@2765
    53
{
slouken@2765
    54
    static GLubyte color[8][4] = { {255, 0, 0, 0},
slouken@2765
    55
    {255, 0, 0, 255},
slouken@2765
    56
    {0, 255, 0, 255},
slouken@2765
    57
    {0, 255, 0, 255},
slouken@2765
    58
    {0, 255, 0, 255},
slouken@2765
    59
    {255, 255, 255, 255},
slouken@2765
    60
    {255, 0, 255, 255},
slouken@2765
    61
    {0, 0, 255, 255}
slouken@2765
    62
    };
slouken@2765
    63
    static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
slouken@2765
    64
    {0.5f, -0.5f, -0.5f},
slouken@2765
    65
    {-0.5f, -0.5f, -0.5f},
slouken@2765
    66
    {-0.5f, 0.5f, -0.5f},
slouken@2765
    67
    {-0.5f, 0.5f, 0.5f},
slouken@2765
    68
    {0.5f, 0.5f, 0.5f},
slouken@2765
    69
    {0.5f, -0.5f, 0.5f},
slouken@2765
    70
    {-0.5f, -0.5f, 0.5f}
slouken@2765
    71
    };
slouken@2765
    72
    static GLubyte indices[36] = { 0, 3, 4,
slouken@2765
    73
        4, 5, 0,
slouken@2765
    74
        0, 5, 6,
slouken@2765
    75
        6, 1, 0,
slouken@2765
    76
        6, 7, 2,
slouken@2765
    77
        2, 1, 6,
slouken@2765
    78
        7, 4, 3,
slouken@2765
    79
        3, 2, 7,
slouken@2765
    80
        5, 4, 7,
slouken@2765
    81
        7, 6, 5,
slouken@2765
    82
        2, 3, 1,
slouken@2765
    83
        3, 0, 1
slouken@2765
    84
    };
slouken@2765
    85
slouken@2765
    86
slouken@2765
    87
    /* Do our drawing, too. */
slouken@2765
    88
    glClearColor(0.0, 0.0, 0.0, 1.0);
slouken@2765
    89
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
    90
slouken@2765
    91
    /* Draw the cube */
slouken@2765
    92
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
slouken@2765
    93
    glEnableClientState(GL_COLOR_ARRAY);
slouken@2765
    94
    glVertexPointer(3, GL_FLOAT, 0, cube);
slouken@2765
    95
    glEnableClientState(GL_VERTEX_ARRAY);
slouken@2765
    96
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
slouken@2765
    97
slouken@2765
    98
    glMatrixMode(GL_MODELVIEW);
slouken@2765
    99
    glRotatef(5.0, 1.0, 1.0, 1.0);
slouken@2765
   100
}
slouken@2765
   101
slouken@2765
   102
int
slouken@2765
   103
main(int argc, char *argv[])
slouken@2765
   104
{
slouken@2765
   105
    int fsaa, accel;
slouken@2765
   106
    int value;
slouken@2765
   107
    int i, done;
slouken@2765
   108
    SDL_DisplayMode mode;
slouken@2765
   109
    SDL_Event event;
slouken@2765
   110
    Uint32 then, now, frames;
slouken@3099
   111
    int status;
slouken@2765
   112
slouken@2765
   113
    /* Initialize parameters */
slouken@2765
   114
    fsaa = 0;
slouken@2765
   115
    accel = 0;
slouken@2765
   116
slouken@2765
   117
    /* Initialize test framework */
slouken@6785
   118
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2765
   119
    if (!state) {
slouken@2765
   120
        return 1;
slouken@2765
   121
    }
slouken@2765
   122
    for (i = 1; i < argc;) {
slouken@2765
   123
        int consumed;
slouken@2765
   124
slouken@6785
   125
        consumed = SDLTest_CommonArg(state, i);
slouken@2765
   126
        if (consumed == 0) {
slouken@2765
   127
            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
slouken@2765
   128
                ++fsaa;
slouken@2765
   129
                consumed = 1;
slouken@2765
   130
            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
slouken@2765
   131
                ++accel;
slouken@2765
   132
                consumed = 1;
lestat@3408
   133
            } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
lestat@3408
   134
                i++;
lestat@3408
   135
                if (!argv[i]) {
lestat@3408
   136
                    consumed = -1;
lestat@3408
   137
                } else {
lestat@3408
   138
                    depth = SDL_atoi(argv[i]);
lestat@3408
   139
                    consumed = 1;
lestat@3408
   140
                }
slouken@2765
   141
            } else {
slouken@2765
   142
                consumed = -1;
slouken@2765
   143
            }
slouken@2765
   144
        }
slouken@2765
   145
        if (consumed < 0) {
lestat@3408
   146
            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
slouken@6785
   147
                    SDLTest_CommonUsage(state));
slouken@2765
   148
            quit(1);
slouken@2765
   149
        }
slouken@2765
   150
        i += consumed;
slouken@2765
   151
    }
slouken@2765
   152
slouken@2765
   153
    /* Set OpenGL parameters */
icculus@5520
   154
    state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
lestat@3408
   155
    state->gl_red_size = 5;
lestat@3408
   156
    state->gl_green_size = 5;
lestat@3408
   157
    state->gl_blue_size = 5;
lestat@3408
   158
    state->gl_depth_size = depth;
slouken@2765
   159
    if (fsaa) {
lestat@3408
   160
        state->gl_multisamplebuffers=1;
lestat@3408
   161
        state->gl_multisamplesamples=fsaa;
slouken@2765
   162
    }
slouken@2765
   163
    if (accel) {
lestat@3408
   164
        state->gl_accelerated=1;
slouken@2765
   165
    }
slouken@6785
   166
    if (!SDLTest_CommonInit(state)) {
slouken@2765
   167
        quit(2);
slouken@2765
   168
    }
slouken@2765
   169
lestat@3398
   170
    context = SDL_calloc(state->num_windows, sizeof(context));
slouken@3139
   171
    if (context == NULL) {
slouken@3139
   172
        fprintf(stderr, "Out of memory!\n");
slouken@3139
   173
        quit(2);
lestat@3110
   174
    }
lestat@3110
   175
lestat@3110
   176
    /* Create OpenGL ES contexts */
slouken@3139
   177
    for (i = 0; i < state->num_windows; i++) {
lestat@3110
   178
        context[i] = SDL_GL_CreateContext(state->windows[i]);
lestat@3110
   179
        if (!context[i]) {
lestat@3110
   180
            fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
lestat@3110
   181
            quit(2);
lestat@3110
   182
        }
slouken@2765
   183
    }
slouken@2765
   184
slouken@2765
   185
    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@2765
   186
        SDL_GL_SetSwapInterval(1);
slouken@2765
   187
    } else {
slouken@2765
   188
        SDL_GL_SetSwapInterval(0);
slouken@2765
   189
    }
slouken@2765
   190
slouken@5363
   191
    SDL_GetCurrentDisplayMode(0, &mode);
lestat@3408
   192
    printf("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
slouken@2765
   193
    printf("\n");
slouken@2765
   194
    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
slouken@2765
   195
    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
slouken@2765
   196
    printf("Version    : %s\n", glGetString(GL_VERSION));
slouken@2765
   197
    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
slouken@2765
   198
    printf("\n");
slouken@2765
   199
slouken@3139
   200
    status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
slouken@3099
   201
    if (!status) {
slouken@3099
   202
        printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   203
    } else {
slouken@3139
   204
        fprintf(stderr, "Failed to get SDL_GL_RED_SIZE: %s\n",
slouken@3139
   205
                SDL_GetError());
slouken@3099
   206
    }
slouken@3139
   207
    status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
slouken@3099
   208
    if (!status) {
slouken@3099
   209
        printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   210
    } else {
slouken@3139
   211
        fprintf(stderr, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
slouken@3139
   212
                SDL_GetError());
slouken@3099
   213
    }
slouken@3139
   214
    status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
slouken@3099
   215
    if (!status) {
slouken@3099
   216
        printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   217
    } else {
slouken@3139
   218
        fprintf(stderr, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
slouken@3139
   219
                SDL_GetError());
slouken@3099
   220
    }
slouken@3139
   221
    status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
slouken@3099
   222
    if (!status) {
lestat@3408
   223
        printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
slouken@3099
   224
    } else {
slouken@3139
   225
        fprintf(stderr, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
slouken@3139
   226
                SDL_GetError());
slouken@3099
   227
    }
slouken@2765
   228
    if (fsaa) {
slouken@3139
   229
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
slouken@3099
   230
        if (!status) {
slouken@3099
   231
            printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
slouken@3099
   232
        } else {
slouken@3139
   233
            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
slouken@3139
   234
                    SDL_GetError());
slouken@3099
   235
        }
slouken@3139
   236
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
slouken@3099
   237
        if (!status) {
slouken@3139
   238
            printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
slouken@3139
   239
                   value);
slouken@3099
   240
        } else {
slouken@3139
   241
            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
slouken@3139
   242
                    SDL_GetError());
slouken@3099
   243
        }
slouken@2765
   244
    }
slouken@2765
   245
    if (accel) {
slouken@3139
   246
        status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
slouken@3139
   247
        if (!status) {
slouken@3099
   248
            printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
slouken@3099
   249
        } else {
slouken@3139
   250
            fprintf(stderr, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
slouken@3139
   251
                    SDL_GetError());
slouken@3099
   252
        }
slouken@2765
   253
    }
slouken@2765
   254
lestat@3110
   255
    /* Set rendering settings for each context */
lestat@3110
   256
    for (i = 0; i < state->num_windows; ++i) {
slouken@5363
   257
        float aspectAdjust;
slouken@5363
   258
slouken@3139
   259
        status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
slouken@3139
   260
        if (status) {
lestat@3110
   261
            printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
lestat@3110
   262
lestat@3110
   263
            /* Continue for next window */
lestat@3110
   264
            continue;
lestat@3110
   265
        }
lestat@3110
   266
slouken@5363
   267
        aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h);
lestat@3397
   268
        glViewport(0, 0, state->window_w, state->window_h);
lestat@3110
   269
        glMatrixMode(GL_PROJECTION);
lestat@3110
   270
        glLoadIdentity();
slouken@5363
   271
        glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0);
lestat@3110
   272
        glMatrixMode(GL_MODELVIEW);
lestat@3110
   273
        glLoadIdentity();
lestat@3110
   274
        glEnable(GL_DEPTH_TEST);
lestat@3110
   275
        glDepthFunc(GL_LESS);
lestat@3110
   276
        glShadeModel(GL_SMOOTH);
lestat@3110
   277
    }
slouken@2765
   278
slouken@2765
   279
    /* Main render loop */
slouken@2765
   280
    frames = 0;
slouken@2765
   281
    then = SDL_GetTicks();
slouken@2765
   282
    done = 0;
slouken@2765
   283
    while (!done) {
slouken@2765
   284
        /* Check for events */
slouken@2765
   285
        ++frames;
slouken@2765
   286
        while (SDL_PollEvent(&event)) {
lestat@3194
   287
            switch (event.type) {
lestat@3194
   288
            case SDL_WINDOWEVENT:
lestat@3194
   289
                switch (event.window.event) {
lestat@3194
   290
                    case SDL_WINDOWEVENT_RESIZED:
lestat@3194
   291
                        for (i = 0; i < state->num_windows; ++i) {
slouken@5363
   292
                            if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
lestat@3194
   293
                                status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
lestat@3194
   294
                                if (status) {
lestat@3194
   295
                                    printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
lestat@3194
   296
                                    break;
lestat@3194
   297
                                }
lestat@3194
   298
                                /* Change view port to the new window dimensions */
lestat@3194
   299
                                glViewport(0, 0, event.window.data1, event.window.data2);
lestat@3194
   300
                                /* Update window content */
lestat@3194
   301
                                Render();
lestat@3194
   302
                                SDL_GL_SwapWindow(state->windows[i]);
lestat@3194
   303
                                break;
lestat@3194
   304
                            }
lestat@3194
   305
                        }
lestat@3194
   306
                        break;
lestat@3194
   307
                }
lestat@3194
   308
            }
slouken@6785
   309
            SDLTest_CommonEvent(state, &event, &done);
slouken@2765
   310
        }
slouken@2765
   311
        for (i = 0; i < state->num_windows; ++i) {
slouken@3139
   312
            status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
slouken@3139
   313
            if (status) {
slouken@3139
   314
                printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
lestat@3110
   315
slouken@3139
   316
                /* Continue for next window */
slouken@3139
   317
                continue;
lestat@3110
   318
            }
slouken@2765
   319
            Render();
slouken@2765
   320
            SDL_GL_SwapWindow(state->windows[i]);
slouken@2765
   321
        }
slouken@2765
   322
    }
slouken@2765
   323
slouken@2765
   324
    /* Print out some timing information */
slouken@2765
   325
    now = SDL_GetTicks();
slouken@2765
   326
    if (now > then) {
slouken@2765
   327
        printf("%2.2f frames per second\n",
slouken@2765
   328
               ((double) frames * 1000) / (now - then));
slouken@2765
   329
    }
slouken@2765
   330
    quit(0);
slouken@2765
   331
    return 0;
slouken@2765
   332
}
slouken@2765
   333
slouken@2765
   334
#else /* HAVE_OPENGLES */
slouken@2765
   335
slouken@2765
   336
int
slouken@2765
   337
main(int argc, char *argv[])
slouken@2765
   338
{
slouken@2765
   339
    printf("No OpenGL ES support on this system\n");
slouken@2765
   340
    return 1;
slouken@2765
   341
}
slouken@2765
   342
slouken@2765
   343
#endif /* HAVE_OPENGLES */
slouken@5363
   344
slouken@5363
   345
/* vi: set ts=4 sw=4 expandtab: */