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