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