test/testdyngles.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Mar 2011 22:06:12 -0800
changeset 5451 6535fdf7cf45
parent 5150 1435f8a6425c
child 5535 96594ac5fd1a
permissions -rw-r--r--
The msimg library isn't needed anymore.
slouken@2765
     1
/*
slouken@2765
     2
 * Small SDL example to demonstrate dynamically loading 
slouken@2765
     3
 * OpenGL lib and functions
slouken@2765
     4
 *
slouken@2765
     5
 * (FYI it was supposed to look like snow in the wind or something...)
slouken@2765
     6
 *
slouken@2765
     7
 * Compile with :
slouken@2765
     8
 * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
slouken@2765
     9
 *
slouken@2765
    10
 * You can specify a different OpenGL lib on the command line, i.e. :
slouken@2765
    11
 * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
slouken@2765
    12
 * or
slouken@2765
    13
 * ./testdyngl  /usr/lib/libGL.so.1.0.4496
slouken@2765
    14
 *
slouken@2765
    15
 */
slouken@2765
    16
slouken@2765
    17
#include <stdio.h>
slouken@2765
    18
#include <stdlib.h>
slouken@2765
    19
slouken@2765
    20
#include "SDL.h"
slouken@2765
    21
slouken@2765
    22
#ifdef __IPHONEOS__
slouken@2765
    23
#define HAVE_OPENGLES
slouken@2765
    24
#endif
slouken@2765
    25
slouken@2765
    26
#ifdef HAVE_OPENGLES
slouken@2765
    27
slouken@2765
    28
#include "SDL_opengles.h"
slouken@2765
    29
slouken@2765
    30
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@2765
    31
static void
slouken@2765
    32
quit(int rc)
slouken@2765
    33
{
slouken@2765
    34
    SDL_Quit();
slouken@2765
    35
    exit(rc);
slouken@2765
    36
}
slouken@2765
    37
slouken@2765
    38
void *
slouken@2765
    39
get_funcaddr(const char *p)
slouken@2765
    40
{
slouken@2765
    41
    void *f = SDL_GL_GetProcAddress(p);
slouken@2765
    42
    if (f) {
slouken@2765
    43
        return f;
slouken@2765
    44
    } else {
slouken@2765
    45
        printf("Unable to get function pointer for %s\n", p);
slouken@2765
    46
        quit(1);
slouken@2765
    47
    }
slouken@2765
    48
    return NULL;
slouken@2765
    49
}
slouken@2765
    50
slouken@2765
    51
typedef struct
slouken@2765
    52
{
slouken@2765
    53
    void (APIENTRY * glEnableClientState) (GLenum array);
lestat@3410
    54
    void (APIENTRY * glDisableClientState) (GLenum array);
slouken@2765
    55
    void (APIENTRY * glVertexPointer) (GLint size, GLenum type,
slouken@2765
    56
                                       GLsizei stride,
slouken@2765
    57
                                       const GLvoid * pointer);
slouken@2765
    58
    void (APIENTRY * glDrawArrays) (GLenum mode, GLint first, GLsizei count);
slouken@2765
    59
slouken@2765
    60
slouken@2765
    61
    void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    62
    void (APIENTRY * glClear) (GLbitfield);
slouken@2765
    63
    void (APIENTRY * glDisable) (GLenum);
slouken@2765
    64
    void (APIENTRY * glEnable) (GLenum);
slouken@3099
    65
    void (APIENTRY * glColor4f) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    66
    void (APIENTRY * glPointSize) (GLfloat);
slouken@2765
    67
    void (APIENTRY * glHint) (GLenum, GLenum);
slouken@2765
    68
    void (APIENTRY * glBlendFunc) (GLenum, GLenum);
slouken@2765
    69
    void (APIENTRY * glMatrixMode) (GLenum);
slouken@2765
    70
    void (APIENTRY * glLoadIdentity) ();
slouken@2765
    71
    void (APIENTRY * glOrthof) (GLfloat, GLfloat, GLfloat, GLfloat,
slouken@2765
    72
                                GLfloat, GLfloat);
slouken@2765
    73
    void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    74
    void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
slouken@2765
    75
    void (APIENTRY * glFogf) (GLenum, GLfloat);
slouken@2765
    76
}
slouken@2765
    77
glfuncs;
slouken@2765
    78
slouken@2765
    79
void
slouken@2765
    80
init_glfuncs(glfuncs * f)
slouken@2765
    81
{
slouken@2765
    82
    f->glEnableClientState = get_funcaddr("glEnableClientState");
lestat@3410
    83
    f->glDisableClientState = get_funcaddr("glDisableClientState");
slouken@2765
    84
    f->glVertexPointer = get_funcaddr("glVertexPointer");
slouken@2765
    85
    f->glDrawArrays = get_funcaddr("glDrawArrays");
slouken@2765
    86
    f->glClearColor = get_funcaddr("glClearColor");
slouken@2765
    87
    f->glClear = get_funcaddr("glClear");
slouken@2765
    88
    f->glDisable = get_funcaddr("glDisable");
slouken@2765
    89
    f->glEnable = get_funcaddr("glEnable");
slouken@3099
    90
    f->glColor4f = get_funcaddr("glColor4f");
slouken@2765
    91
    f->glPointSize = get_funcaddr("glPointSize");
slouken@2765
    92
    f->glHint = get_funcaddr("glHint");
slouken@2765
    93
    f->glBlendFunc = get_funcaddr("glBlendFunc");
slouken@2765
    94
    f->glMatrixMode = get_funcaddr("glMatrixMode");
slouken@2765
    95
    f->glLoadIdentity = get_funcaddr("glLoadIdentity");
slouken@2765
    96
    f->glOrthof = get_funcaddr("glOrthof");
slouken@2765
    97
    f->glRotatef = get_funcaddr("glRotatef");
slouken@2765
    98
    f->glViewport = get_funcaddr("glViewport");
slouken@2765
    99
    f->glFogf = get_funcaddr("glFogf");
slouken@2765
   100
}
slouken@2765
   101
slouken@2765
   102
#define NB_PIXELS 1000
slouken@2765
   103
slouken@2765
   104
int
slouken@2765
   105
main(int argc, char *argv[])
slouken@2765
   106
{
slouken@2765
   107
    glfuncs f;
slouken@2765
   108
    int i;
slouken@2765
   109
    SDL_Event event;
slouken@2765
   110
    int done = 0;
slouken@2765
   111
    GLfloat pixels[NB_PIXELS * 3];
slouken@2765
   112
    const char *gl_library = NULL;      /* Use the default GL library */
slouken@2765
   113
slouken@2765
   114
    int video_w, video_h;
slouken@2765
   115
slouken@2765
   116
    /* you may want to change these according to the platform */
slouken@2765
   117
    video_w = 320;
slouken@2765
   118
    video_h = 480;
slouken@2765
   119
slouken@2765
   120
    if (argv[1]) {
slouken@2765
   121
        gl_library = argv[1];
slouken@2765
   122
    }
slouken@2765
   123
slouken@2765
   124
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@2765
   125
        printf("Unable to init SDL : %s\n", SDL_GetError());
slouken@2765
   126
        return (1);
slouken@2765
   127
    }
slouken@2765
   128
slouken@2765
   129
    if (SDL_GL_LoadLibrary(gl_library) < 0) {
slouken@3087
   130
        printf("Unable to dynamically open GL ES lib : %s\n", SDL_GetError());
slouken@2765
   131
        quit(1);
slouken@2765
   132
    }
slouken@2765
   133
slouken@2765
   134
    if (SDL_SetVideoMode(video_h, video_w, 0, SDL_OPENGL) == NULL) {
slouken@2765
   135
        printf("Unable to open video mode : %s\n", SDL_GetError());
slouken@2765
   136
        quit(1);
slouken@2765
   137
    }
slouken@2765
   138
slouken@2765
   139
    /* Set the window manager title bar */
slouken@3087
   140
    SDL_WM_SetCaption("SDL Dynamic OpenGL ES Loading Test", "testdyngles");
slouken@2765
   141
slouken@2765
   142
    init_glfuncs(&f);
slouken@2765
   143
slouken@2765
   144
    for (i = 0; i < NB_PIXELS; i++) {
slouken@2765
   145
        pixels[3 * i] = rand() % 250 - 125;
slouken@2765
   146
        pixels[3 * i + 1] = rand() % 250 - 125;
slouken@2765
   147
        pixels[3 * i + 2] = rand() % 250 - 125;
slouken@2765
   148
    }
slouken@2765
   149
slouken@3139
   150
    f.glViewport(0, 0, video_w, video_h);
slouken@2765
   151
    f.glMatrixMode(GL_PROJECTION);
slouken@2765
   152
    f.glLoadIdentity();
slouken@2765
   153
    f.glOrthof(-100, 100, -100, 100, -500, 500);
slouken@2765
   154
slouken@2765
   155
    f.glMatrixMode(GL_MODELVIEW);
slouken@2765
   156
    f.glLoadIdentity();
slouken@2765
   157
slouken@2765
   158
    f.glEnable(GL_DEPTH_TEST);
slouken@2765
   159
    f.glDisable(GL_TEXTURE_2D);
slouken@2765
   160
    f.glEnable(GL_BLEND);
slouken@2765
   161
    f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2765
   162
slouken@2765
   163
    f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
slouken@2765
   164
    f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
   165
slouken@2765
   166
    f.glEnable(GL_POINT_SMOOTH);
slouken@2765
   167
    f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
lestat@3410
   168
    f.glPointSize(1.0f);
slouken@2765
   169
    f.glEnable(GL_FOG);
slouken@2765
   170
    f.glFogf(GL_FOG_START, -500);
slouken@2765
   171
    f.glFogf(GL_FOG_END, 500);
slouken@2765
   172
    f.glFogf(GL_FOG_DENSITY, 0.005);
slouken@2765
   173
lestat@3410
   174
    f.glVertexPointer(3, GL_FLOAT, 0, pixels);
lestat@3410
   175
    f.glEnableClientState(GL_VERTEX_ARRAY);
lestat@3410
   176
slouken@2765
   177
    do {
slouken@2765
   178
        f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
   179
slouken@2765
   180
        f.glRotatef(2.0, 1.0, 1.0, 1.0);
slouken@2765
   181
        f.glRotatef(1.0, 0.0, 1.0, 1.0);
slouken@2765
   182
slouken@3099
   183
        f.glColor4f(1.0, 1.0, 1.0, 1.0);
slouken@2765
   184
slouken@2765
   185
        f.glDrawArrays(GL_POINTS, 0, NB_PIXELS);
slouken@2765
   186
slouken@2765
   187
        SDL_GL_SwapBuffers();
slouken@2765
   188
slouken@2765
   189
        while (SDL_PollEvent(&event)) {
slouken@3099
   190
            if (event.type == SDL_QUIT)
slouken@3099
   191
                done = 1;
slouken@2765
   192
            if (event.type == SDL_KEYDOWN)
slouken@2765
   193
                done = 1;
slouken@2765
   194
        }
slouken@2765
   195
slouken@2765
   196
        SDL_Delay(20);
slouken@2765
   197
    }
slouken@2765
   198
    while (!done);
slouken@2765
   199
lestat@3410
   200
    f.glDisableClientState(GL_VERTEX_ARRAY);
lestat@3410
   201
slouken@2765
   202
    SDL_Quit();
slouken@2765
   203
    return 0;
slouken@2765
   204
}
slouken@2765
   205
slouken@2765
   206
#else /* HAVE_OPENGLES */
slouken@2765
   207
slouken@2765
   208
int
slouken@2765
   209
main(int argc, char *argv[])
slouken@2765
   210
{
slouken@3087
   211
    printf("No OpenGL ES support on this system\n");
slouken@2765
   212
    return 1;
slouken@2765
   213
}
slouken@2765
   214
slouken@2765
   215
#endif /* HAVE_OPENGLES */