test/testgles.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 23 May 2009 22:41:08 +0000
changeset 3139 7f684f249ec9
parent 3110 e6c21c14ce3a
child 3194 c18c03927a77
permissions -rw-r--r--
indent
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@2765
     8
#ifdef __IPHONEOS__
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;
slouken@2765
    18
slouken@2765
    19
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@2765
    20
static void
slouken@2765
    21
quit(int rc)
slouken@2765
    22
{
lestat@3110
    23
    int i;
lestat@3110
    24
slouken@3139
    25
    if (context != NULL) {
slouken@3139
    26
        for (i = 0; i < state->num_windows; i++) {
lestat@3110
    27
            if (context[i]) {
lestat@3110
    28
                SDL_GL_DeleteContext(context[i]);
lestat@3110
    29
            }
lestat@3110
    30
        }
lestat@3110
    31
lestat@3110
    32
        SDL_free(context);
slouken@2765
    33
    }
lestat@3110
    34
slouken@2765
    35
    CommonQuit(state);
slouken@2765
    36
    exit(rc);
slouken@2765
    37
}
slouken@2765
    38
slouken@2765
    39
static void
slouken@2765
    40
Render()
slouken@2765
    41
{
slouken@2765
    42
    static GLubyte color[8][4] = { {255, 0, 0, 0},
slouken@2765
    43
    {255, 0, 0, 255},
slouken@2765
    44
    {0, 255, 0, 255},
slouken@2765
    45
    {0, 255, 0, 255},
slouken@2765
    46
    {0, 255, 0, 255},
slouken@2765
    47
    {255, 255, 255, 255},
slouken@2765
    48
    {255, 0, 255, 255},
slouken@2765
    49
    {0, 0, 255, 255}
slouken@2765
    50
    };
slouken@2765
    51
    static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
slouken@2765
    52
    {0.5f, -0.5f, -0.5f},
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
    };
slouken@2765
    60
    static GLubyte indices[36] = { 0, 3, 4,
slouken@2765
    61
        4, 5, 0,
slouken@2765
    62
        0, 5, 6,
slouken@2765
    63
        6, 1, 0,
slouken@2765
    64
        6, 7, 2,
slouken@2765
    65
        2, 1, 6,
slouken@2765
    66
        7, 4, 3,
slouken@2765
    67
        3, 2, 7,
slouken@2765
    68
        5, 4, 7,
slouken@2765
    69
        7, 6, 5,
slouken@2765
    70
        2, 3, 1,
slouken@2765
    71
        3, 0, 1
slouken@2765
    72
    };
slouken@2765
    73
slouken@2765
    74
slouken@2765
    75
    /* Do our drawing, too. */
slouken@2765
    76
    glClearColor(0.0, 0.0, 0.0, 1.0);
slouken@2765
    77
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
    78
slouken@2765
    79
    /* Draw the cube */
slouken@2765
    80
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
slouken@2765
    81
    glEnableClientState(GL_COLOR_ARRAY);
slouken@2765
    82
    glVertexPointer(3, GL_FLOAT, 0, cube);
slouken@2765
    83
    glEnableClientState(GL_VERTEX_ARRAY);
slouken@2765
    84
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
slouken@2765
    85
slouken@2765
    86
    glMatrixMode(GL_MODELVIEW);
slouken@2765
    87
    glRotatef(5.0, 1.0, 1.0, 1.0);
slouken@2765
    88
}
slouken@2765
    89
slouken@2765
    90
int
slouken@2765
    91
main(int argc, char *argv[])
slouken@2765
    92
{
slouken@2765
    93
    int fsaa, accel;
slouken@2765
    94
    int value;
slouken@2765
    95
    int i, done;
slouken@2765
    96
    SDL_DisplayMode mode;
slouken@2765
    97
    SDL_Event event;
slouken@2765
    98
    Uint32 then, now, frames;
slouken@3099
    99
    int status;
slouken@2765
   100
slouken@2765
   101
    /* Initialize parameters */
slouken@2765
   102
    fsaa = 0;
slouken@2765
   103
    accel = 0;
slouken@2765
   104
slouken@2765
   105
    /* Initialize test framework */
slouken@2765
   106
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2765
   107
    if (!state) {
slouken@2765
   108
        return 1;
slouken@2765
   109
    }
slouken@2765
   110
    for (i = 1; i < argc;) {
slouken@2765
   111
        int consumed;
slouken@2765
   112
slouken@2765
   113
        consumed = CommonArg(state, i);
slouken@2765
   114
        if (consumed == 0) {
slouken@2765
   115
            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
slouken@2765
   116
                ++fsaa;
slouken@2765
   117
                consumed = 1;
slouken@2765
   118
            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
slouken@2765
   119
                ++accel;
slouken@2765
   120
                consumed = 1;
slouken@2765
   121
            } else {
slouken@2765
   122
                consumed = -1;
slouken@2765
   123
            }
slouken@2765
   124
        }
slouken@2765
   125
        if (consumed < 0) {
slouken@2765
   126
            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
slouken@2765
   127
                    CommonUsage(state));
slouken@2765
   128
            quit(1);
slouken@2765
   129
        }
slouken@2765
   130
        i += consumed;
slouken@2765
   131
    }
slouken@2765
   132
slouken@2765
   133
    /* Set OpenGL parameters */
slouken@2765
   134
    state->window_flags |= SDL_WINDOW_OPENGL;
slouken@2765
   135
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
slouken@2765
   136
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
slouken@2765
   137
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
slouken@2765
   138
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
slouken@2765
   139
    if (fsaa) {
slouken@2765
   140
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
slouken@2765
   141
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
slouken@2765
   142
    }
slouken@2765
   143
    if (accel) {
slouken@2765
   144
        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
slouken@2765
   145
    }
slouken@2765
   146
    if (!CommonInit(state)) {
slouken@2765
   147
        quit(2);
slouken@2765
   148
    }
slouken@2765
   149
slouken@3139
   150
    context = SDL_calloc(state->num_windows, sizeof(SDL_GLContext));
slouken@3139
   151
    if (context == NULL) {
slouken@3139
   152
        fprintf(stderr, "Out of memory!\n");
slouken@3139
   153
        quit(2);
lestat@3110
   154
    }
lestat@3110
   155
lestat@3110
   156
    /* Create OpenGL ES contexts */
slouken@3139
   157
    for (i = 0; i < state->num_windows; i++) {
lestat@3110
   158
        context[i] = SDL_GL_CreateContext(state->windows[i]);
lestat@3110
   159
        if (!context[i]) {
lestat@3110
   160
            fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
lestat@3110
   161
            quit(2);
lestat@3110
   162
        }
slouken@2765
   163
    }
slouken@2765
   164
slouken@2765
   165
    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@2765
   166
        SDL_GL_SetSwapInterval(1);
slouken@2765
   167
    } else {
slouken@2765
   168
        SDL_GL_SetSwapInterval(0);
slouken@2765
   169
    }
slouken@2765
   170
slouken@2765
   171
    SDL_GetCurrentDisplayMode(&mode);
slouken@2765
   172
    printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
slouken@2765
   173
    printf("\n");
slouken@2765
   174
    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
slouken@2765
   175
    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
slouken@2765
   176
    printf("Version    : %s\n", glGetString(GL_VERSION));
slouken@2765
   177
    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
slouken@2765
   178
    printf("\n");
slouken@2765
   179
slouken@3139
   180
    status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
slouken@3099
   181
    if (!status) {
slouken@3099
   182
        printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   183
    } else {
slouken@3139
   184
        fprintf(stderr, "Failed to get SDL_GL_RED_SIZE: %s\n",
slouken@3139
   185
                SDL_GetError());
slouken@3099
   186
    }
slouken@3139
   187
    status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
slouken@3099
   188
    if (!status) {
slouken@3099
   189
        printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   190
    } else {
slouken@3139
   191
        fprintf(stderr, "Failed to get SDL_GL_GREEN_SIZE: %s\n",
slouken@3139
   192
                SDL_GetError());
slouken@3099
   193
    }
slouken@3139
   194
    status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
slouken@3099
   195
    if (!status) {
slouken@3099
   196
        printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
slouken@3099
   197
    } else {
slouken@3139
   198
        fprintf(stderr, "Failed to get SDL_GL_BLUE_SIZE: %s\n",
slouken@3139
   199
                SDL_GetError());
slouken@3099
   200
    }
slouken@3139
   201
    status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
slouken@3099
   202
    if (!status) {
slouken@3099
   203
        printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
slouken@3099
   204
    } else {
slouken@3139
   205
        fprintf(stderr, "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
slouken@3139
   206
                SDL_GetError());
slouken@3099
   207
    }
slouken@2765
   208
    if (fsaa) {
slouken@3139
   209
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
slouken@3099
   210
        if (!status) {
slouken@3099
   211
            printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
slouken@3099
   212
        } else {
slouken@3139
   213
            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
slouken@3139
   214
                    SDL_GetError());
slouken@3099
   215
        }
slouken@3139
   216
        status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
slouken@3099
   217
        if (!status) {
slouken@3139
   218
            printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
slouken@3139
   219
                   value);
slouken@3099
   220
        } else {
slouken@3139
   221
            fprintf(stderr, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
slouken@3139
   222
                    SDL_GetError());
slouken@3099
   223
        }
slouken@2765
   224
    }
slouken@2765
   225
    if (accel) {
slouken@3139
   226
        status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
slouken@3139
   227
        if (!status) {
slouken@3099
   228
            printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
slouken@3099
   229
        } else {
slouken@3139
   230
            fprintf(stderr, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
slouken@3139
   231
                    SDL_GetError());
slouken@3099
   232
        }
slouken@2765
   233
    }
slouken@2765
   234
lestat@3110
   235
    /* Set rendering settings for each context */
lestat@3110
   236
    for (i = 0; i < state->num_windows; ++i) {
slouken@3139
   237
        status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
slouken@3139
   238
        if (status) {
lestat@3110
   239
            printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
lestat@3110
   240
lestat@3110
   241
            /* Continue for next window */
lestat@3110
   242
            continue;
lestat@3110
   243
        }
lestat@3110
   244
lestat@3110
   245
        glMatrixMode(GL_PROJECTION);
lestat@3110
   246
        glLoadIdentity();
lestat@3110
   247
        glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
lestat@3110
   248
        glMatrixMode(GL_MODELVIEW);
lestat@3110
   249
        glLoadIdentity();
lestat@3110
   250
        glEnable(GL_DEPTH_TEST);
lestat@3110
   251
        glDepthFunc(GL_LESS);
lestat@3110
   252
        glShadeModel(GL_SMOOTH);
lestat@3110
   253
    }
slouken@2765
   254
slouken@2765
   255
    /* Main render loop */
slouken@2765
   256
    frames = 0;
slouken@2765
   257
    then = SDL_GetTicks();
slouken@2765
   258
    done = 0;
slouken@2765
   259
    while (!done) {
slouken@2765
   260
        /* Check for events */
slouken@2765
   261
        ++frames;
slouken@2765
   262
        while (SDL_PollEvent(&event)) {
slouken@2765
   263
            CommonEvent(state, &event, &done);
slouken@2765
   264
        }
slouken@2765
   265
        for (i = 0; i < state->num_windows; ++i) {
slouken@2765
   266
            int w, h;
slouken@3139
   267
            status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
slouken@3139
   268
            if (status) {
slouken@3139
   269
                printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
lestat@3110
   270
slouken@3139
   271
                /* Continue for next window */
slouken@3139
   272
                continue;
lestat@3110
   273
            }
slouken@2765
   274
            SDL_GetWindowSize(state->windows[i], &w, &h);
slouken@2765
   275
            glViewport(0, 0, w, h);
slouken@2765
   276
            Render();
slouken@2765
   277
            SDL_GL_SwapWindow(state->windows[i]);
slouken@2765
   278
        }
slouken@2765
   279
    }
slouken@2765
   280
slouken@2765
   281
    /* Print out some timing information */
slouken@2765
   282
    now = SDL_GetTicks();
slouken@2765
   283
    if (now > then) {
slouken@2765
   284
        printf("%2.2f frames per second\n",
slouken@2765
   285
               ((double) frames * 1000) / (now - then));
slouken@2765
   286
    }
slouken@2765
   287
    quit(0);
slouken@2765
   288
    return 0;
slouken@2765
   289
}
slouken@2765
   290
slouken@2765
   291
#else /* HAVE_OPENGLES */
slouken@2765
   292
slouken@2765
   293
int
slouken@2765
   294
main(int argc, char *argv[])
slouken@2765
   295
{
slouken@2765
   296
    printf("No OpenGL ES support on this system\n");
slouken@2765
   297
    return 1;
slouken@2765
   298
}
slouken@2765
   299
slouken@2765
   300
#endif /* HAVE_OPENGLES */