test/testgles.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2765 f55c87ae336b
child 3099 82e60908fab1
permissions -rw-r--r--
Added testresample.c
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@2765
    17
static SDL_GLContext context;
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
{
slouken@2765
    23
    if (context) {
slouken@2765
    24
        /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
slouken@2765
    25
        SDL_GL_DeleteContext(context);
slouken@2765
    26
    }
slouken@2765
    27
    CommonQuit(state);
slouken@2765
    28
    exit(rc);
slouken@2765
    29
}
slouken@2765
    30
slouken@2765
    31
static void
slouken@2765
    32
Render()
slouken@2765
    33
{
slouken@2765
    34
    static GLubyte color[8][4] = { {255, 0, 0, 0},
slouken@2765
    35
    {255, 0, 0, 255},
slouken@2765
    36
    {0, 255, 0, 255},
slouken@2765
    37
    {0, 255, 0, 255},
slouken@2765
    38
    {0, 255, 0, 255},
slouken@2765
    39
    {255, 255, 255, 255},
slouken@2765
    40
    {255, 0, 255, 255},
slouken@2765
    41
    {0, 0, 255, 255}
slouken@2765
    42
    };
slouken@2765
    43
    static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
slouken@2765
    44
    {0.5f, -0.5f, -0.5f},
slouken@2765
    45
    {-0.5f, -0.5f, -0.5f},
slouken@2765
    46
    {-0.5f, 0.5f, -0.5f},
slouken@2765
    47
    {-0.5f, 0.5f, 0.5f},
slouken@2765
    48
    {0.5f, 0.5f, 0.5f},
slouken@2765
    49
    {0.5f, -0.5f, 0.5f},
slouken@2765
    50
    {-0.5f, -0.5f, 0.5f}
slouken@2765
    51
    };
slouken@2765
    52
    static GLubyte indices[36] = { 0, 3, 4,
slouken@2765
    53
        4, 5, 0,
slouken@2765
    54
        0, 5, 6,
slouken@2765
    55
        6, 1, 0,
slouken@2765
    56
        6, 7, 2,
slouken@2765
    57
        2, 1, 6,
slouken@2765
    58
        7, 4, 3,
slouken@2765
    59
        3, 2, 7,
slouken@2765
    60
        5, 4, 7,
slouken@2765
    61
        7, 6, 5,
slouken@2765
    62
        2, 3, 1,
slouken@2765
    63
        3, 0, 1
slouken@2765
    64
    };
slouken@2765
    65
slouken@2765
    66
slouken@2765
    67
    /* Do our drawing, too. */
slouken@2765
    68
    glClearColor(0.0, 0.0, 0.0, 1.0);
slouken@2765
    69
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
    70
slouken@2765
    71
    /* Draw the cube */
slouken@2765
    72
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
slouken@2765
    73
    glEnableClientState(GL_COLOR_ARRAY);
slouken@2765
    74
    glVertexPointer(3, GL_FLOAT, 0, cube);
slouken@2765
    75
    glEnableClientState(GL_VERTEX_ARRAY);
slouken@2765
    76
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
slouken@2765
    77
slouken@2765
    78
    glMatrixMode(GL_MODELVIEW);
slouken@2765
    79
    glRotatef(5.0, 1.0, 1.0, 1.0);
slouken@2765
    80
}
slouken@2765
    81
slouken@2765
    82
int
slouken@2765
    83
main(int argc, char *argv[])
slouken@2765
    84
{
slouken@2765
    85
    int fsaa, accel;
slouken@2765
    86
    int value;
slouken@2765
    87
    int i, done;
slouken@2765
    88
    SDL_DisplayMode mode;
slouken@2765
    89
    SDL_Event event;
slouken@2765
    90
    Uint32 then, now, frames;
slouken@2765
    91
slouken@2765
    92
    /* Initialize parameters */
slouken@2765
    93
    fsaa = 0;
slouken@2765
    94
    accel = 0;
slouken@2765
    95
slouken@2765
    96
    /* Initialize test framework */
slouken@2765
    97
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2765
    98
    if (!state) {
slouken@2765
    99
        return 1;
slouken@2765
   100
    }
slouken@2765
   101
    for (i = 1; i < argc;) {
slouken@2765
   102
        int consumed;
slouken@2765
   103
slouken@2765
   104
        consumed = CommonArg(state, i);
slouken@2765
   105
        if (consumed == 0) {
slouken@2765
   106
            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
slouken@2765
   107
                ++fsaa;
slouken@2765
   108
                consumed = 1;
slouken@2765
   109
            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
slouken@2765
   110
                ++accel;
slouken@2765
   111
                consumed = 1;
slouken@2765
   112
            } else {
slouken@2765
   113
                consumed = -1;
slouken@2765
   114
            }
slouken@2765
   115
        }
slouken@2765
   116
        if (consumed < 0) {
slouken@2765
   117
            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
slouken@2765
   118
                    CommonUsage(state));
slouken@2765
   119
            quit(1);
slouken@2765
   120
        }
slouken@2765
   121
        i += consumed;
slouken@2765
   122
    }
slouken@2765
   123
slouken@2765
   124
    /* Set OpenGL parameters */
slouken@2765
   125
    state->window_flags |= SDL_WINDOW_OPENGL;
slouken@2765
   126
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
slouken@2765
   127
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
slouken@2765
   128
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
slouken@2765
   129
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
slouken@2765
   130
    if (fsaa) {
slouken@2765
   131
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
slouken@2765
   132
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
slouken@2765
   133
    }
slouken@2765
   134
    if (accel) {
slouken@2765
   135
        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
slouken@2765
   136
    }
slouken@2765
   137
    if (!CommonInit(state)) {
slouken@2765
   138
        quit(2);
slouken@2765
   139
    }
slouken@2765
   140
slouken@2765
   141
    /* Create OpenGL context */
slouken@2765
   142
    context = SDL_GL_CreateContext(state->windows[0]);
slouken@2765
   143
    if (!context) {
slouken@2765
   144
        fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
slouken@2765
   145
        quit(2);
slouken@2765
   146
    }
slouken@2765
   147
slouken@2765
   148
    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@2765
   149
        SDL_GL_SetSwapInterval(1);
slouken@2765
   150
    } else {
slouken@2765
   151
        SDL_GL_SetSwapInterval(0);
slouken@2765
   152
    }
slouken@2765
   153
slouken@2765
   154
    SDL_GetCurrentDisplayMode(&mode);
slouken@2765
   155
    printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
slouken@2765
   156
    printf("\n");
slouken@2765
   157
    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
slouken@2765
   158
    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
slouken@2765
   159
    printf("Version    : %s\n", glGetString(GL_VERSION));
slouken@2765
   160
    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
slouken@2765
   161
    printf("\n");
slouken@2765
   162
slouken@2765
   163
    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
slouken@2765
   164
    printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
slouken@2765
   165
    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
slouken@2765
   166
    printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
slouken@2765
   167
    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
slouken@2765
   168
    printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
slouken@2765
   169
    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
slouken@2765
   170
    printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
slouken@2765
   171
    if (fsaa) {
slouken@2765
   172
        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
slouken@2765
   173
        printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
slouken@2765
   174
        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
slouken@2765
   175
        printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
slouken@2765
   176
               value);
slouken@2765
   177
    }
slouken@2765
   178
    if (accel) {
slouken@2765
   179
        SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
slouken@2765
   180
        printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
slouken@2765
   181
    }
slouken@2765
   182
slouken@2765
   183
    /* Set rendering settings */
slouken@2765
   184
    glMatrixMode(GL_PROJECTION);
slouken@2765
   185
    glLoadIdentity();
slouken@2765
   186
    glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
slouken@2765
   187
    glMatrixMode(GL_MODELVIEW);
slouken@2765
   188
    glLoadIdentity();
slouken@2765
   189
    glEnable(GL_DEPTH_TEST);
slouken@2765
   190
    glDepthFunc(GL_LESS);
slouken@2765
   191
    glShadeModel(GL_SMOOTH);
slouken@2765
   192
slouken@2765
   193
    /* Main render loop */
slouken@2765
   194
    frames = 0;
slouken@2765
   195
    then = SDL_GetTicks();
slouken@2765
   196
    done = 0;
slouken@2765
   197
    while (!done) {
slouken@2765
   198
        /* Check for events */
slouken@2765
   199
        ++frames;
slouken@2765
   200
        while (SDL_PollEvent(&event)) {
slouken@2765
   201
            CommonEvent(state, &event, &done);
slouken@2765
   202
        }
slouken@2765
   203
        for (i = 0; i < state->num_windows; ++i) {
slouken@2765
   204
            int w, h;
slouken@2765
   205
            SDL_GL_MakeCurrent(state->windows[i], context);
slouken@2765
   206
            SDL_GetWindowSize(state->windows[i], &w, &h);
slouken@2765
   207
            glViewport(0, 0, w, h);
slouken@2765
   208
            Render();
slouken@2765
   209
            SDL_GL_SwapWindow(state->windows[i]);
slouken@2765
   210
        }
slouken@2765
   211
    }
slouken@2765
   212
slouken@2765
   213
    /* Print out some timing information */
slouken@2765
   214
    now = SDL_GetTicks();
slouken@2765
   215
    if (now > then) {
slouken@2765
   216
        printf("%2.2f frames per second\n",
slouken@2765
   217
               ((double) frames * 1000) / (now - then));
slouken@2765
   218
    }
slouken@2765
   219
    quit(0);
slouken@2765
   220
    return 0;
slouken@2765
   221
}
slouken@2765
   222
slouken@2765
   223
#else /* HAVE_OPENGLES */
slouken@2765
   224
slouken@2765
   225
int
slouken@2765
   226
main(int argc, char *argv[])
slouken@2765
   227
{
slouken@2765
   228
    printf("No OpenGL ES support on this system\n");
slouken@2765
   229
    return 1;
slouken@2765
   230
}
slouken@2765
   231
slouken@2765
   232
#endif /* HAVE_OPENGLES */