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