test/testgles.c
branchgsoc2008_iphone
changeset 2430 166821fa1591
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testgles.c	Fri Aug 15 00:54:21 2008 +0000
     1.3 @@ -0,0 +1,231 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <stdio.h>
     1.6 +#include <string.h>
     1.7 +#include <math.h>
     1.8 +
     1.9 +#include "common.h"
    1.10 +
    1.11 +#ifdef __IPHONEOS__
    1.12 +#define HAVE_OPENGLES
    1.13 +#endif
    1.14 +
    1.15 +#ifdef HAVE_OPENGLES
    1.16 +
    1.17 +#include "SDL_opengles.h"
    1.18 +
    1.19 +static CommonState *state;
    1.20 +static SDL_GLContext context;
    1.21 +
    1.22 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.23 +static void
    1.24 +quit(int rc)
    1.25 +{
    1.26 +    if (context) {
    1.27 +        /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
    1.28 +        SDL_GL_DeleteContext(context);
    1.29 +    }
    1.30 +    CommonQuit(state);
    1.31 +    exit(rc);
    1.32 +}
    1.33 +
    1.34 +static void
    1.35 +Render()
    1.36 +{
    1.37 +	static GLubyte color[8][4] = { {255, 0, 0, 0},
    1.38 +		{ 255,	0, 0, 255},
    1.39 +		{	0, 255, 0, 255},
    1.40 +		{	0, 255, 0, 255},
    1.41 +		{	0, 255, 0, 255},
    1.42 +		{ 255, 255, 255, 255},
    1.43 +		{ 255,	0, 255, 255},
    1.44 +		{	0,	0, 255, 255}
    1.45 +    };
    1.46 +    static GLfloat cube[8][3] = { {0.5, 0.5, -0.5},
    1.47 +		{0.5f, -0.5f, -0.5f},
    1.48 +		{-0.5f, -0.5f, -0.5f},
    1.49 +		{-0.5f, 0.5f, -0.5f},
    1.50 +		{-0.5f, 0.5f, 0.5f},
    1.51 +		{0.5f, 0.5f, 0.5f},
    1.52 +		{0.5f, -0.5f, 0.5f},
    1.53 +		{-0.5f, -0.5f, 0.5f}
    1.54 +    };
    1.55 +	static GLubyte indices[36] = { 0, 3, 4,\
    1.56 +								   4, 5, 0,\
    1.57 +								   0, 5, 6,\
    1.58 +								   6, 1, 0,\
    1.59 +								   6, 7, 2,\
    1.60 +								   2, 1, 6,\
    1.61 +								   7, 4, 3,\
    1.62 +								   3, 2, 7,\
    1.63 +								   5, 4, 7,\
    1.64 +								   7, 6, 5,\
    1.65 +								   2, 3, 1,\
    1.66 +								   3, 0, 1 };
    1.67 +	
    1.68 +	
    1.69 +    /* Do our drawing, too. */
    1.70 +    glClearColor(0.0, 0.0, 0.0, 1.0);
    1.71 +    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.72 +		
    1.73 +	/* Draw the cube */
    1.74 +	glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);
    1.75 +	glEnableClientState(GL_COLOR_ARRAY);
    1.76 +	glVertexPointer(3, GL_FLOAT, 0, cube);
    1.77 +	glEnableClientState(GL_VERTEX_ARRAY);
    1.78 +	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices);
    1.79 +	
    1.80 +    glMatrixMode(GL_MODELVIEW);
    1.81 +    glRotatef(5.0, 1.0, 1.0, 1.0);
    1.82 +}
    1.83 +
    1.84 +int
    1.85 +main(int argc, char *argv[])
    1.86 +{
    1.87 +    int fsaa, accel;
    1.88 +    int value;
    1.89 +    int i, done;
    1.90 +    SDL_DisplayMode mode;
    1.91 +    SDL_Event event;
    1.92 +    Uint32 then, now, frames;
    1.93 +	
    1.94 +    /* Initialize parameters */
    1.95 +    fsaa = 0;
    1.96 +    accel = 0;
    1.97 +	
    1.98 +    /* Initialize test framework */
    1.99 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
   1.100 +    if (!state) {
   1.101 +        return 1;
   1.102 +    }
   1.103 +    for (i = 1; i < argc;) {
   1.104 +        int consumed;
   1.105 +		
   1.106 +        consumed = CommonArg(state, i);
   1.107 +        if (consumed == 0) {
   1.108 +            if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
   1.109 +                ++fsaa;
   1.110 +                consumed = 1;
   1.111 +            } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
   1.112 +                ++accel;
   1.113 +                consumed = 1;
   1.114 +            } else {
   1.115 +                consumed = -1;
   1.116 +            }
   1.117 +        }
   1.118 +        if (consumed < 0) {
   1.119 +            fprintf(stderr, "Usage: %s %s [--fsaa] [--accel]\n", argv[0],
   1.120 +                    CommonUsage(state));
   1.121 +            quit(1);
   1.122 +        }
   1.123 +        i += consumed;
   1.124 +    }
   1.125 +	
   1.126 +    /* Set OpenGL parameters */
   1.127 +    state->window_flags |= SDL_WINDOW_OPENGL;
   1.128 +    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
   1.129 +    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   1.130 +    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
   1.131 +    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
   1.132 +    if (fsaa) {
   1.133 +        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
   1.134 +        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, fsaa);
   1.135 +    }
   1.136 +    if (accel) {
   1.137 +        SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
   1.138 +    }
   1.139 +    if (!CommonInit(state)) {
   1.140 +        quit(2);
   1.141 +    }
   1.142 +	
   1.143 +    /* Create OpenGL context */
   1.144 +    context = SDL_GL_CreateContext(state->windows[0]);
   1.145 +    if (!context) {
   1.146 +        fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
   1.147 +        quit(2);
   1.148 +    }
   1.149 +	
   1.150 +    if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
   1.151 +        SDL_GL_SetSwapInterval(1);
   1.152 +    } else {
   1.153 +        SDL_GL_SetSwapInterval(0);
   1.154 +    }
   1.155 +	
   1.156 +    SDL_GetCurrentDisplayMode(&mode);
   1.157 +    printf("Screen BPP: %d\n", SDL_BITSPERPIXEL(mode.format));
   1.158 +    printf("\n");
   1.159 +    printf("Vendor     : %s\n", glGetString(GL_VENDOR));
   1.160 +    printf("Renderer   : %s\n", glGetString(GL_RENDERER));
   1.161 +    printf("Version    : %s\n", glGetString(GL_VERSION));
   1.162 +    printf("Extensions : %s\n", glGetString(GL_EXTENSIONS));
   1.163 +    printf("\n");
   1.164 +	
   1.165 +    SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
   1.166 +    printf("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
   1.167 +    SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
   1.168 +    printf("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
   1.169 +    SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
   1.170 +    printf("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
   1.171 +    SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
   1.172 +    printf("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", 16, value);
   1.173 +    if (fsaa) {
   1.174 +        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
   1.175 +        printf("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
   1.176 +        SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
   1.177 +        printf("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
   1.178 +               value);
   1.179 +    }
   1.180 +    if (accel) {
   1.181 +        SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
   1.182 +        printf("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
   1.183 +    }
   1.184 +	
   1.185 +    /* Set rendering settings */
   1.186 +    glMatrixMode(GL_PROJECTION);
   1.187 +    glLoadIdentity();
   1.188 +    glOrthof(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
   1.189 +    glMatrixMode(GL_MODELVIEW);
   1.190 +    glLoadIdentity();
   1.191 +    glEnable(GL_DEPTH_TEST);
   1.192 +    glDepthFunc(GL_LESS);
   1.193 +    glShadeModel(GL_SMOOTH);
   1.194 +	
   1.195 +    /* Main render loop */
   1.196 +    frames = 0;
   1.197 +    then = SDL_GetTicks();
   1.198 +    done = 0;
   1.199 +    while (!done) {
   1.200 +        /* Check for events */
   1.201 +        ++frames;
   1.202 +        while (SDL_PollEvent(&event)) {
   1.203 +            CommonEvent(state, &event, &done);
   1.204 +        }
   1.205 +        for (i = 0; i < state->num_windows; ++i) {
   1.206 +            int w, h;
   1.207 +            SDL_GL_MakeCurrent(state->windows[i], context);
   1.208 +            SDL_GetWindowSize(state->windows[i], &w, &h);
   1.209 +            glViewport(0, 0, w, h);
   1.210 +            Render();
   1.211 +            SDL_GL_SwapWindow(state->windows[i]);
   1.212 +        }
   1.213 +    }
   1.214 +	
   1.215 +    /* Print out some timing information */
   1.216 +    now = SDL_GetTicks();
   1.217 +    if (now > then) {
   1.218 +        printf("%2.2f frames per second\n",
   1.219 +               ((double) frames * 1000) / (now - then));
   1.220 +    }
   1.221 +    quit(0);
   1.222 +    return 0;
   1.223 +}
   1.224 +
   1.225 +#else /* HAVE_OPENGLES */
   1.226 +
   1.227 +int
   1.228 +main(int argc, char *argv[])
   1.229 +{
   1.230 +    printf("No OpenGL ES support on this system\n");
   1.231 +    return 1;
   1.232 +}
   1.233 +
   1.234 +#endif /* HAVE_OPENGLES */