test/testdyngl.c
changeset 0 a6cb692b8939
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testdyngl.c	Sun Mar 03 21:55:23 2013 -0500
     1.3 @@ -0,0 +1,209 @@
     1.4 +/*
     1.5 + * Small SDL example to demonstrate dynamically loading 
     1.6 + * OpenGL lib and functions
     1.7 + *
     1.8 + * (FYI it was supposed to look like snow in the wind or something...)
     1.9 + *
    1.10 + * Compile with :
    1.11 + * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
    1.12 + *
    1.13 + * You can specify a different OpenGL lib on the command line, i.e. :
    1.14 + * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
    1.15 + * or
    1.16 + * ./testdyngl  /usr/lib/libGL.so.1.0.4496
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#include <stdio.h>
    1.21 +#include <stdlib.h>
    1.22 +
    1.23 +#include "SDL.h"
    1.24 +
    1.25 +#ifdef __MACOS__
    1.26 +#define HAVE_OPENGL
    1.27 +#endif
    1.28 +
    1.29 +#ifdef HAVE_OPENGL
    1.30 +
    1.31 +#include "SDL_opengl.h"
    1.32 +
    1.33 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.34 +static void quit(int rc)
    1.35 +{
    1.36 +	SDL_Quit();
    1.37 +	exit(rc);
    1.38 +}
    1.39 +
    1.40 +void* get_funcaddr(const char* p)
    1.41 +{
    1.42 +	void* f=SDL_GL_GetProcAddress(p);
    1.43 +	if (f)
    1.44 +	{
    1.45 +		return f;
    1.46 +	}
    1.47 +	else
    1.48 +	{
    1.49 +		printf("Unable to get function pointer for %s\n",p);
    1.50 +		quit(1);
    1.51 +	}
    1.52 +	return NULL;
    1.53 +}
    1.54 +
    1.55 +typedef struct
    1.56 +{
    1.57 +	void(APIENTRY*glBegin)(GLenum);
    1.58 +	void(APIENTRY*glEnd)();
    1.59 +	void(APIENTRY*glVertex3f)(GLfloat, GLfloat, GLfloat);
    1.60 +	void(APIENTRY*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
    1.61 +	void(APIENTRY*glClear)(GLbitfield);
    1.62 +	void(APIENTRY*glDisable)(GLenum);
    1.63 +	void(APIENTRY*glEnable)(GLenum);
    1.64 +	void(APIENTRY*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
    1.65 +	void(APIENTRY*glPointSize)(GLfloat);
    1.66 +	void(APIENTRY*glHint)(GLenum,GLenum);
    1.67 +	void(APIENTRY*glBlendFunc)(GLenum,GLenum);
    1.68 +	void(APIENTRY*glMatrixMode)(GLenum);
    1.69 +	void(APIENTRY*glLoadIdentity)();
    1.70 +	void(APIENTRY*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
    1.71 +	void(APIENTRY*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
    1.72 +	void(APIENTRY*glViewport)(GLint,GLint,GLsizei,GLsizei);
    1.73 +	void(APIENTRY*glFogf)(GLenum,GLfloat);
    1.74 +}
    1.75 +glfuncs;
    1.76 +
    1.77 +void init_glfuncs(glfuncs* f)
    1.78 +{
    1.79 +	f->glBegin=get_funcaddr("glBegin");
    1.80 +	f->glEnd=get_funcaddr("glEnd");
    1.81 +	f->glVertex3f=get_funcaddr("glVertex3f");
    1.82 +	f->glClearColor=get_funcaddr("glClearColor");
    1.83 +	f->glClear=get_funcaddr("glClear");
    1.84 +	f->glDisable=get_funcaddr("glDisable");
    1.85 +	f->glEnable=get_funcaddr("glEnable");
    1.86 +	f->glColor4ub=get_funcaddr("glColor4ub");
    1.87 +	f->glPointSize=get_funcaddr("glPointSize");
    1.88 +	f->glHint=get_funcaddr("glHint");
    1.89 +	f->glBlendFunc=get_funcaddr("glBlendFunc");
    1.90 +	f->glMatrixMode=get_funcaddr("glMatrixMode");
    1.91 +	f->glLoadIdentity=get_funcaddr("glLoadIdentity");
    1.92 +	f->glOrtho=get_funcaddr("glOrtho");
    1.93 +	f->glRotatef=get_funcaddr("glRotatef");
    1.94 +	f->glViewport=get_funcaddr("glViewport");
    1.95 +	f->glFogf=get_funcaddr("glFogf");
    1.96 +}
    1.97 +
    1.98 +#define NB_PIXELS 1000
    1.99 +
   1.100 +int main(int argc,char *argv[])
   1.101 +{
   1.102 +	glfuncs f;
   1.103 +	int i;
   1.104 +	SDL_Event event;
   1.105 +	int done=0;
   1.106 +	GLfloat pixels[NB_PIXELS*3];
   1.107 +	const char *gl_library = NULL; /* Use the default GL library */
   1.108 +
   1.109 +	if (argv[1]) {
   1.110 +		gl_library = argv[1];
   1.111 +	}
   1.112 +	
   1.113 +	if (SDL_Init(SDL_INIT_VIDEO)<0)
   1.114 +	{
   1.115 +		printf("Unable to init SDL : %s\n",SDL_GetError());
   1.116 +		return(1);
   1.117 +	}
   1.118 +
   1.119 +	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
   1.120 +	{
   1.121 +		printf("Unable to set GL attribute : %s\n",SDL_GetError());
   1.122 +		quit(1);
   1.123 +	}
   1.124 +	
   1.125 +	if (SDL_GL_LoadLibrary(gl_library)<0)
   1.126 +	{
   1.127 +		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
   1.128 +		quit(1);
   1.129 +	}
   1.130 +
   1.131 +	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
   1.132 +	{
   1.133 +		printf("Unable to open video mode : %s\n",SDL_GetError());
   1.134 +		quit(1);
   1.135 +	}
   1.136 +
   1.137 +	/* Set the window manager title bar */
   1.138 +	SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
   1.139 +
   1.140 +	init_glfuncs(&f);
   1.141 +
   1.142 +	for(i=0;i<NB_PIXELS;i++)
   1.143 +	{
   1.144 +		pixels[3*i]=rand()%250-125;
   1.145 +		pixels[3*i+1]=rand()%250-125;
   1.146 +		pixels[3*i+2]=rand()%250-125;
   1.147 +	}
   1.148 +	
   1.149 +	f.glViewport(0,0,640,480);
   1.150 +	
   1.151 +	f.glMatrixMode(GL_PROJECTION);
   1.152 +	f.glLoadIdentity();
   1.153 +	f.glOrtho(-100,100,-100,100,-500,500);
   1.154 +	
   1.155 +	f.glMatrixMode(GL_MODELVIEW);
   1.156 +	f.glLoadIdentity();
   1.157 +	
   1.158 +	f.glEnable(GL_DEPTH_TEST);
   1.159 +	f.glDisable(GL_TEXTURE_2D);
   1.160 +	f.glEnable(GL_BLEND);
   1.161 +	f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.162 +	
   1.163 +	f.glClearColor(0.0f,0.0f,0.0f,0.0f);
   1.164 +	f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   1.165 +	
   1.166 +	f.glEnable(GL_POINT_SMOOTH);
   1.167 +	f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
   1.168 +	f.glPointSize(5.0f);
   1.169 +	f.glEnable(GL_FOG);
   1.170 +	f.glFogf(GL_FOG_START,-500);
   1.171 +	f.glFogf(GL_FOG_END,500);
   1.172 +	f.glFogf(GL_FOG_DENSITY,0.005);
   1.173 +	
   1.174 +	do
   1.175 +	{
   1.176 +		f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   1.177 +		
   1.178 +		f.glRotatef(2.0,1.0,1.0,1.0);
   1.179 +		f.glRotatef(1.0,0.0,1.0,1.0);
   1.180 +		
   1.181 +		f.glColor4ub(255,255,255,255);
   1.182 +		f.glBegin(GL_POINTS);
   1.183 +		for(i=0;i<NB_PIXELS;i++)
   1.184 +		{
   1.185 +			f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
   1.186 +		}
   1.187 +		f.glEnd();
   1.188 +		SDL_GL_SwapBuffers();
   1.189 +
   1.190 +		while(SDL_PollEvent(&event))
   1.191 +		{
   1.192 +			if(event.type & SDL_KEYDOWN)
   1.193 +				done=1;
   1.194 +		}
   1.195 +
   1.196 +		SDL_Delay(20);
   1.197 +	}
   1.198 +	while(!done);
   1.199 +	
   1.200 +	SDL_Quit();
   1.201 +	return 0;
   1.202 +}
   1.203 +
   1.204 +#else /* HAVE_OPENGL */
   1.205 +
   1.206 +int main(int argc, char *argv[])
   1.207 +{
   1.208 +	printf("No OpenGL support on this system\n");
   1.209 +	return 1;
   1.210 +}
   1.211 +
   1.212 +#endif /* HAVE_OPENGL */