test/testdyngles.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Oct 2009 20:44:04 +0000
changeset 3342 3da232c5980a
parent 3139 7f684f249ec9
child 3410 f4bc25567731
permissions -rw-r--r--
Split acinclude.m4 into its constituent parts for easy upgrading
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 * glBegin) (GLenum);
slouken@2765
    54
    //void (APIENTRY * glEnd) ();
slouken@2765
    55
    //void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
slouken@2765
    56
slouken@2765
    57
    void (APIENTRY * glEnableClientState) (GLenum array);
slouken@2765
    58
    void (APIENTRY * glVertexPointer) (GLint size, GLenum type,
slouken@2765
    59
                                       GLsizei stride,
slouken@2765
    60
                                       const GLvoid * pointer);
slouken@2765
    61
    void (APIENTRY * glDrawArrays) (GLenum mode, GLint first, GLsizei count);
slouken@2765
    62
slouken@2765
    63
slouken@2765
    64
    void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    65
    void (APIENTRY * glClear) (GLbitfield);
slouken@2765
    66
    void (APIENTRY * glDisable) (GLenum);
slouken@2765
    67
    void (APIENTRY * glEnable) (GLenum);
slouken@3099
    68
    void (APIENTRY * glColor4f) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    69
    void (APIENTRY * glPointSize) (GLfloat);
slouken@2765
    70
    void (APIENTRY * glHint) (GLenum, GLenum);
slouken@2765
    71
    void (APIENTRY * glBlendFunc) (GLenum, GLenum);
slouken@2765
    72
    void (APIENTRY * glMatrixMode) (GLenum);
slouken@2765
    73
    void (APIENTRY * glLoadIdentity) ();
slouken@2765
    74
    void (APIENTRY * glOrthof) (GLfloat, GLfloat, GLfloat, GLfloat,
slouken@2765
    75
                                GLfloat, GLfloat);
slouken@2765
    76
    void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
slouken@2765
    77
    void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
slouken@2765
    78
    void (APIENTRY * glFogf) (GLenum, GLfloat);
slouken@2765
    79
}
slouken@2765
    80
glfuncs;
slouken@2765
    81
slouken@2765
    82
void
slouken@2765
    83
init_glfuncs(glfuncs * f)
slouken@2765
    84
{
slouken@2765
    85
    f->glEnableClientState = get_funcaddr("glEnableClientState");
slouken@2765
    86
    f->glVertexPointer = get_funcaddr("glVertexPointer");
slouken@2765
    87
    f->glDrawArrays = get_funcaddr("glDrawArrays");
slouken@2765
    88
    f->glClearColor = get_funcaddr("glClearColor");
slouken@2765
    89
    f->glClear = get_funcaddr("glClear");
slouken@2765
    90
    f->glDisable = get_funcaddr("glDisable");
slouken@2765
    91
    f->glEnable = get_funcaddr("glEnable");
slouken@3099
    92
    f->glColor4f = get_funcaddr("glColor4f");
slouken@2765
    93
    f->glPointSize = get_funcaddr("glPointSize");
slouken@2765
    94
    f->glHint = get_funcaddr("glHint");
slouken@2765
    95
    f->glBlendFunc = get_funcaddr("glBlendFunc");
slouken@2765
    96
    f->glMatrixMode = get_funcaddr("glMatrixMode");
slouken@2765
    97
    f->glLoadIdentity = get_funcaddr("glLoadIdentity");
slouken@2765
    98
    f->glOrthof = get_funcaddr("glOrthof");
slouken@2765
    99
    f->glRotatef = get_funcaddr("glRotatef");
slouken@2765
   100
    f->glViewport = get_funcaddr("glViewport");
slouken@2765
   101
    f->glFogf = get_funcaddr("glFogf");
slouken@2765
   102
}
slouken@2765
   103
slouken@2765
   104
#define NB_PIXELS 1000
slouken@2765
   105
slouken@2765
   106
int
slouken@2765
   107
main(int argc, char *argv[])
slouken@2765
   108
{
slouken@2765
   109
    glfuncs f;
slouken@2765
   110
    int i;
slouken@2765
   111
    SDL_Event event;
slouken@2765
   112
    int done = 0;
slouken@2765
   113
    GLfloat pixels[NB_PIXELS * 3];
slouken@2765
   114
    const char *gl_library = NULL;      /* Use the default GL library */
slouken@2765
   115
slouken@2765
   116
    int video_w, video_h;
slouken@2765
   117
slouken@2765
   118
    /* you may want to change these according to the platform */
slouken@2765
   119
    video_w = 320;
slouken@2765
   120
    video_h = 480;
slouken@3139
   121
#ifdef __QNXNTO__
slouken@3139
   122
    video_h = 640;
slouken@3139
   123
    video_w = 480;
slouken@3139
   124
#endif /* __QNXNTO__ */
slouken@2765
   125
slouken@2765
   126
    if (argv[1]) {
slouken@2765
   127
        gl_library = argv[1];
slouken@2765
   128
    }
slouken@2765
   129
slouken@2765
   130
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@2765
   131
        printf("Unable to init SDL : %s\n", SDL_GetError());
slouken@2765
   132
        return (1);
slouken@2765
   133
    }
slouken@2765
   134
slouken@2765
   135
    if (SDL_GL_LoadLibrary(gl_library) < 0) {
slouken@3087
   136
        printf("Unable to dynamically open GL ES lib : %s\n", SDL_GetError());
slouken@2765
   137
        quit(1);
slouken@2765
   138
    }
slouken@2765
   139
slouken@2765
   140
    if (SDL_SetVideoMode(video_h, video_w, 0, SDL_OPENGL) == NULL) {
slouken@2765
   141
        printf("Unable to open video mode : %s\n", SDL_GetError());
slouken@2765
   142
        quit(1);
slouken@2765
   143
    }
slouken@2765
   144
slouken@2765
   145
    /* Set the window manager title bar */
slouken@3087
   146
    SDL_WM_SetCaption("SDL Dynamic OpenGL ES Loading Test", "testdyngles");
slouken@2765
   147
slouken@2765
   148
    init_glfuncs(&f);
slouken@2765
   149
slouken@2765
   150
    for (i = 0; i < NB_PIXELS; i++) {
slouken@2765
   151
        pixels[3 * i] = rand() % 250 - 125;
slouken@2765
   152
        pixels[3 * i + 1] = rand() % 250 - 125;
slouken@2765
   153
        pixels[3 * i + 2] = rand() % 250 - 125;
slouken@2765
   154
    }
slouken@2765
   155
slouken@3139
   156
#ifdef __QNXNTO__
slouken@3139
   157
    f.glViewport(0, 0, video_h, video_w);
slouken@3139
   158
#else
slouken@3139
   159
    f.glViewport(0, 0, video_w, video_h);
slouken@3139
   160
#endif /* __QNXNTO__ */
slouken@2765
   161
slouken@2765
   162
    f.glMatrixMode(GL_PROJECTION);
slouken@2765
   163
    f.glLoadIdentity();
slouken@2765
   164
    f.glOrthof(-100, 100, -100, 100, -500, 500);
slouken@2765
   165
slouken@2765
   166
    f.glMatrixMode(GL_MODELVIEW);
slouken@2765
   167
    f.glLoadIdentity();
slouken@2765
   168
slouken@2765
   169
    f.glEnable(GL_DEPTH_TEST);
slouken@2765
   170
    f.glDisable(GL_TEXTURE_2D);
slouken@2765
   171
    f.glEnable(GL_BLEND);
slouken@2765
   172
    f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@2765
   173
slouken@2765
   174
    f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
slouken@2765
   175
    f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
   176
slouken@2765
   177
    f.glEnable(GL_POINT_SMOOTH);
slouken@2765
   178
    f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
slouken@2765
   179
    f.glPointSize(5.0f);
slouken@2765
   180
    f.glEnable(GL_FOG);
slouken@2765
   181
    f.glFogf(GL_FOG_START, -500);
slouken@2765
   182
    f.glFogf(GL_FOG_END, 500);
slouken@2765
   183
    f.glFogf(GL_FOG_DENSITY, 0.005);
slouken@2765
   184
slouken@2765
   185
    do {
slouken@2765
   186
        f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
slouken@2765
   187
slouken@2765
   188
        f.glRotatef(2.0, 1.0, 1.0, 1.0);
slouken@2765
   189
        f.glRotatef(1.0, 0.0, 1.0, 1.0);
slouken@2765
   190
slouken@3099
   191
        f.glColor4f(1.0, 1.0, 1.0, 1.0);
slouken@2765
   192
slouken@2765
   193
        f.glEnableClientState(GL_VERTEX_ARRAY);
slouken@2765
   194
        f.glVertexPointer(3, GL_FLOAT, 0, pixels);
slouken@2765
   195
        f.glDrawArrays(GL_POINTS, 0, NB_PIXELS);
slouken@2765
   196
slouken@2765
   197
        SDL_GL_SwapBuffers();
slouken@2765
   198
slouken@2765
   199
        while (SDL_PollEvent(&event)) {
slouken@3099
   200
            if (event.type == SDL_QUIT)
slouken@3099
   201
                done = 1;
slouken@2765
   202
            if (event.type == SDL_KEYDOWN)
slouken@2765
   203
                done = 1;
slouken@2765
   204
        }
slouken@2765
   205
slouken@2765
   206
        SDL_Delay(20);
slouken@2765
   207
    }
slouken@2765
   208
    while (!done);
slouken@2765
   209
slouken@2765
   210
    SDL_Quit();
slouken@2765
   211
    return 0;
slouken@2765
   212
}
slouken@2765
   213
slouken@2765
   214
#else /* HAVE_OPENGLES */
slouken@2765
   215
slouken@2765
   216
int
slouken@2765
   217
main(int argc, char *argv[])
slouken@2765
   218
{
slouken@3087
   219
    printf("No OpenGL ES support on this system\n");
slouken@2765
   220
    return 1;
slouken@2765
   221
}
slouken@2765
   222
slouken@2765
   223
#endif /* HAVE_OPENGLES */