test/testdyngl.c
changeset 839 7b255f0290da
child 842 0eec9985767d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testdyngl.c	Tue Feb 17 18:16:49 2004 +0000
     1.3 @@ -0,0 +1,196 @@
     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
    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 HAVE_OPENGL
    1.26 +
    1.27 +#include "SDL_opengl.h"
    1.28 +
    1.29 +void* get_funcaddr(const char* p)
    1.30 +{
    1.31 +	void* f=SDL_GL_GetProcAddress(p);
    1.32 +	if (f)
    1.33 +	{
    1.34 +		return f;
    1.35 +	}
    1.36 +	else
    1.37 +	{
    1.38 +		printf("Unable to get function pointer for %s\n",p);
    1.39 +		exit(1);
    1.40 +	}
    1.41 +}
    1.42 +
    1.43 +typedef struct
    1.44 +{
    1.45 +	void(*glBegin)(GLenum);
    1.46 +	void(*glEnd)();
    1.47 +	void(*glVertex3f)(GLfloat, GLfloat, GLfloat);
    1.48 +	void(*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
    1.49 +	void(*glClear)(GLbitfield);
    1.50 +	void(*glDisable)(GLenum);
    1.51 +	void(*glEnable)(GLenum);
    1.52 +	void(*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
    1.53 +	void(*glPointSize)(GLfloat);
    1.54 +	void(*glHint)(GLenum,GLenum);
    1.55 +	void(*glBlendFunc)(GLenum,GLenum);
    1.56 +	void(*glMatrixMode)(GLenum);
    1.57 +	void(*glLoadIdentity)();
    1.58 +	void(*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
    1.59 +	void(*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
    1.60 +	void(*glViewport)(GLint,GLint,GLsizei,GLsizei);
    1.61 +	void(*glFogf)(GLenum,GLfloat);
    1.62 +}
    1.63 +glfuncs;
    1.64 +
    1.65 +void init_glfuncs(glfuncs* f)
    1.66 +{
    1.67 +	f->glBegin=get_funcaddr("glBegin");
    1.68 +	f->glEnd=get_funcaddr("glEnd");
    1.69 +	f->glVertex3f=get_funcaddr("glVertex3f");
    1.70 +	f->glClearColor=get_funcaddr("glClearColor");
    1.71 +	f->glClear=get_funcaddr("glClear");
    1.72 +	f->glDisable=get_funcaddr("glDisable");
    1.73 +	f->glEnable=get_funcaddr("glEnable");
    1.74 +	f->glColor4ub=get_funcaddr("glColor4ub");
    1.75 +	f->glPointSize=get_funcaddr("glPointSize");
    1.76 +	f->glHint=get_funcaddr("glHint");
    1.77 +	f->glBlendFunc=get_funcaddr("glBlendFunc");
    1.78 +	f->glMatrixMode=get_funcaddr("glMatrixMode");
    1.79 +	f->glLoadIdentity=get_funcaddr("glLoadIdentity");
    1.80 +	f->glOrtho=get_funcaddr("glOrtho");
    1.81 +	f->glRotatef=get_funcaddr("glRotatef");
    1.82 +	f->glViewport=get_funcaddr("glViewport");
    1.83 +	f->glFogf=get_funcaddr("glFogf");
    1.84 +}
    1.85 +
    1.86 +#define NB_PIXELS 1000
    1.87 +
    1.88 +int main(int argc,char *argv[])
    1.89 +{
    1.90 +	glfuncs f;
    1.91 +	int i;
    1.92 +	SDL_Event event;
    1.93 +	int done=0;
    1.94 +	GLfloat pixels[NB_PIXELS*3];
    1.95 +	char default_gl_lib[]="/usr/lib/libGL.so";
    1.96 +	char* current_gl_lib=default_gl_lib;
    1.97 +	
    1.98 +	if (argc==2)
    1.99 +	{
   1.100 +		current_gl_lib=argv[1];
   1.101 +	}
   1.102 +	
   1.103 +	if (SDL_Init(SDL_INIT_EVERYTHING)<0)
   1.104 +	{
   1.105 +		printf("Unable to init SDL : %s\n",SDL_GetError());
   1.106 +		exit(1);
   1.107 +	}
   1.108 +	
   1.109 +	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
   1.110 +	{
   1.111 +		printf("Unable to set GL attribute : %s\n",SDL_GetError());
   1.112 +		exit(1);
   1.113 +	}
   1.114 +	
   1.115 +	if (SDL_GL_LoadLibrary(current_gl_lib)<0)
   1.116 +	{
   1.117 +		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
   1.118 +		exit(1);
   1.119 +	}
   1.120 +
   1.121 +	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
   1.122 +	{
   1.123 +		printf("Unable to open video mode : %s\n",SDL_GetError());
   1.124 +		exit(1);
   1.125 +	}
   1.126 +
   1.127 +	init_glfuncs(&f);
   1.128 +
   1.129 +	for(i=0;i<NB_PIXELS;i++)
   1.130 +	{
   1.131 +		pixels[3*i]=rand()%250-125;
   1.132 +		pixels[3*i+1]=rand()%250-125;
   1.133 +		pixels[3*i+2]=rand()%250-125;
   1.134 +	}
   1.135 +	
   1.136 +	f.glViewport(0,0,640,480);
   1.137 +	
   1.138 +	f.glMatrixMode(GL_PROJECTION);
   1.139 +	f.glLoadIdentity();
   1.140 +	f.glOrtho(-100,100,-100,100,-500,500);
   1.141 +	
   1.142 +	f.glMatrixMode(GL_MODELVIEW);
   1.143 +	f.glLoadIdentity();
   1.144 +	
   1.145 +	f.glEnable(GL_DEPTH_TEST);
   1.146 +	f.glDisable(GL_TEXTURE_2D);
   1.147 +	f.glEnable(GL_BLEND);
   1.148 +	f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.149 +	
   1.150 +	f.glClearColor(0.0f,0.0f,0.0f,0.0f);
   1.151 +	f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   1.152 +	
   1.153 +	f.glEnable(GL_POINT_SMOOTH);
   1.154 +	f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
   1.155 +	f.glPointSize(5.0f);
   1.156 +	f.glEnable(GL_FOG);
   1.157 +	f.glFogf(GL_FOG_START,-500);
   1.158 +	f.glFogf(GL_FOG_END,500);
   1.159 +	f.glFogf(GL_FOG_DENSITY,0.005);
   1.160 +	
   1.161 +	do
   1.162 +	{
   1.163 +		f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   1.164 +		
   1.165 +		f.glRotatef(2.0,1.0,1.0,1.0);
   1.166 +		f.glRotatef(1.0,0.0,1.0,1.0);
   1.167 +		
   1.168 +		f.glColor4ub(255,255,255,255);
   1.169 +		f.glBegin(GL_POINTS);
   1.170 +		for(i=0;i<NB_PIXELS;i++)
   1.171 +		{
   1.172 +			f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
   1.173 +		}
   1.174 +		f.glEnd();
   1.175 +		SDL_GL_SwapBuffers();
   1.176 +
   1.177 +		while(SDL_PollEvent(&event))
   1.178 +		{
   1.179 +			if(event.type & SDL_KEYDOWN)
   1.180 +				done=1;
   1.181 +		}
   1.182 +
   1.183 +		SDL_Delay(20);
   1.184 +	}
   1.185 +	while(!done);
   1.186 +	
   1.187 +	SDL_Quit();
   1.188 +	return 0;
   1.189 +}
   1.190 +
   1.191 +#else /* HAVE_OPENGL */
   1.192 +
   1.193 +int main(int argc, char *argv[])
   1.194 +{
   1.195 +	printf("No OpenGL support on this system\n");
   1.196 +	return 1;
   1.197 +}
   1.198 +
   1.199 +#endif /* HAVE_OPENGL */