test/testdyngl.c
changeset 1895 c121d94672cb
parent 1815 871090feb7ad
child 2327 7b53a8401195
     1.1 --- a/test/testdyngl.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testdyngl.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -28,182 +28,177 @@
     1.4  #include "SDL_opengl.h"
     1.5  
     1.6  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     1.7 -static void quit(int rc)
     1.8 +static void
     1.9 +quit(int rc)
    1.10  {
    1.11 -	SDL_Quit();
    1.12 -	exit(rc);
    1.13 +    SDL_Quit();
    1.14 +    exit(rc);
    1.15  }
    1.16  
    1.17 -void* get_funcaddr(const char* p)
    1.18 +void *
    1.19 +get_funcaddr(const char *p)
    1.20  {
    1.21 -	void* f=SDL_GL_GetProcAddress(p);
    1.22 -	if (f)
    1.23 -	{
    1.24 -		return f;
    1.25 -	}
    1.26 -	else
    1.27 -	{
    1.28 -		printf("Unable to get function pointer for %s\n",p);
    1.29 -		quit(1);
    1.30 -	}
    1.31 -	return NULL;
    1.32 +    void *f = SDL_GL_GetProcAddress(p);
    1.33 +    if (f) {
    1.34 +        return f;
    1.35 +    } else {
    1.36 +        printf("Unable to get function pointer for %s\n", p);
    1.37 +        quit(1);
    1.38 +    }
    1.39 +    return NULL;
    1.40  }
    1.41  
    1.42  typedef struct
    1.43  {
    1.44 -	void(APIENTRY*glBegin)(GLenum);
    1.45 -	void(APIENTRY*glEnd)();
    1.46 -	void(APIENTRY*glVertex3f)(GLfloat, GLfloat, GLfloat);
    1.47 -	void(APIENTRY*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
    1.48 -	void(APIENTRY*glClear)(GLbitfield);
    1.49 -	void(APIENTRY*glDisable)(GLenum);
    1.50 -	void(APIENTRY*glEnable)(GLenum);
    1.51 -	void(APIENTRY*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
    1.52 -	void(APIENTRY*glPointSize)(GLfloat);
    1.53 -	void(APIENTRY*glHint)(GLenum,GLenum);
    1.54 -	void(APIENTRY*glBlendFunc)(GLenum,GLenum);
    1.55 -	void(APIENTRY*glMatrixMode)(GLenum);
    1.56 -	void(APIENTRY*glLoadIdentity)();
    1.57 -	void(APIENTRY*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
    1.58 -	void(APIENTRY*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
    1.59 -	void(APIENTRY*glViewport)(GLint,GLint,GLsizei,GLsizei);
    1.60 -	void(APIENTRY*glFogf)(GLenum,GLfloat);
    1.61 +    void (APIENTRY * glBegin) (GLenum);
    1.62 +    void (APIENTRY * glEnd) ();
    1.63 +    void (APIENTRY * glVertex3f) (GLfloat, GLfloat, GLfloat);
    1.64 +    void (APIENTRY * glClearColor) (GLfloat, GLfloat, GLfloat, GLfloat);
    1.65 +    void (APIENTRY * glClear) (GLbitfield);
    1.66 +    void (APIENTRY * glDisable) (GLenum);
    1.67 +    void (APIENTRY * glEnable) (GLenum);
    1.68 +    void (APIENTRY * glColor4ub) (GLubyte, GLubyte, GLubyte, GLubyte);
    1.69 +    void (APIENTRY * glPointSize) (GLfloat);
    1.70 +    void (APIENTRY * glHint) (GLenum, GLenum);
    1.71 +    void (APIENTRY * glBlendFunc) (GLenum, GLenum);
    1.72 +    void (APIENTRY * glMatrixMode) (GLenum);
    1.73 +    void (APIENTRY * glLoadIdentity) ();
    1.74 +    void (APIENTRY * glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble,
    1.75 +                               GLdouble, GLdouble);
    1.76 +    void (APIENTRY * glRotatef) (GLfloat, GLfloat, GLfloat, GLfloat);
    1.77 +    void (APIENTRY * glViewport) (GLint, GLint, GLsizei, GLsizei);
    1.78 +    void (APIENTRY * glFogf) (GLenum, GLfloat);
    1.79  }
    1.80  glfuncs;
    1.81  
    1.82 -void init_glfuncs(glfuncs* f)
    1.83 +void
    1.84 +init_glfuncs(glfuncs * f)
    1.85  {
    1.86 -	f->glBegin=get_funcaddr("glBegin");
    1.87 -	f->glEnd=get_funcaddr("glEnd");
    1.88 -	f->glVertex3f=get_funcaddr("glVertex3f");
    1.89 -	f->glClearColor=get_funcaddr("glClearColor");
    1.90 -	f->glClear=get_funcaddr("glClear");
    1.91 -	f->glDisable=get_funcaddr("glDisable");
    1.92 -	f->glEnable=get_funcaddr("glEnable");
    1.93 -	f->glColor4ub=get_funcaddr("glColor4ub");
    1.94 -	f->glPointSize=get_funcaddr("glPointSize");
    1.95 -	f->glHint=get_funcaddr("glHint");
    1.96 -	f->glBlendFunc=get_funcaddr("glBlendFunc");
    1.97 -	f->glMatrixMode=get_funcaddr("glMatrixMode");
    1.98 -	f->glLoadIdentity=get_funcaddr("glLoadIdentity");
    1.99 -	f->glOrtho=get_funcaddr("glOrtho");
   1.100 -	f->glRotatef=get_funcaddr("glRotatef");
   1.101 -	f->glViewport=get_funcaddr("glViewport");
   1.102 -	f->glFogf=get_funcaddr("glFogf");
   1.103 +    f->glBegin = get_funcaddr("glBegin");
   1.104 +    f->glEnd = get_funcaddr("glEnd");
   1.105 +    f->glVertex3f = get_funcaddr("glVertex3f");
   1.106 +    f->glClearColor = get_funcaddr("glClearColor");
   1.107 +    f->glClear = get_funcaddr("glClear");
   1.108 +    f->glDisable = get_funcaddr("glDisable");
   1.109 +    f->glEnable = get_funcaddr("glEnable");
   1.110 +    f->glColor4ub = get_funcaddr("glColor4ub");
   1.111 +    f->glPointSize = get_funcaddr("glPointSize");
   1.112 +    f->glHint = get_funcaddr("glHint");
   1.113 +    f->glBlendFunc = get_funcaddr("glBlendFunc");
   1.114 +    f->glMatrixMode = get_funcaddr("glMatrixMode");
   1.115 +    f->glLoadIdentity = get_funcaddr("glLoadIdentity");
   1.116 +    f->glOrtho = get_funcaddr("glOrtho");
   1.117 +    f->glRotatef = get_funcaddr("glRotatef");
   1.118 +    f->glViewport = get_funcaddr("glViewport");
   1.119 +    f->glFogf = get_funcaddr("glFogf");
   1.120  }
   1.121  
   1.122  #define NB_PIXELS 1000
   1.123  
   1.124 -int main(int argc,char *argv[])
   1.125 +int
   1.126 +main(int argc, char *argv[])
   1.127  {
   1.128 -	glfuncs f;
   1.129 -	int i;
   1.130 -	SDL_Event event;
   1.131 -	int done=0;
   1.132 -	GLfloat pixels[NB_PIXELS*3];
   1.133 -	const char *gl_library = NULL; /* Use the default GL library */
   1.134 +    glfuncs f;
   1.135 +    int i;
   1.136 +    SDL_Event event;
   1.137 +    int done = 0;
   1.138 +    GLfloat pixels[NB_PIXELS * 3];
   1.139 +    const char *gl_library = NULL;      /* Use the default GL library */
   1.140  
   1.141 -	if (argv[1]) {
   1.142 -		gl_library = argv[1];
   1.143 -	}
   1.144 -	
   1.145 -	if (SDL_Init(SDL_INIT_VIDEO)<0)
   1.146 -	{
   1.147 -		printf("Unable to init SDL : %s\n",SDL_GetError());
   1.148 -		return(1);
   1.149 -	}
   1.150 +    if (argv[1]) {
   1.151 +        gl_library = argv[1];
   1.152 +    }
   1.153  
   1.154 -	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
   1.155 -	{
   1.156 -		printf("Unable to set GL attribute : %s\n",SDL_GetError());
   1.157 -		quit(1);
   1.158 -	}
   1.159 -	
   1.160 -	if (SDL_GL_LoadLibrary(gl_library)<0)
   1.161 -	{
   1.162 -		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
   1.163 -		quit(1);
   1.164 -	}
   1.165 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.166 +        printf("Unable to init SDL : %s\n", SDL_GetError());
   1.167 +        return (1);
   1.168 +    }
   1.169  
   1.170 -	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
   1.171 -	{
   1.172 -		printf("Unable to open video mode : %s\n",SDL_GetError());
   1.173 -		quit(1);
   1.174 -	}
   1.175 +    if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) {
   1.176 +        printf("Unable to set GL attribute : %s\n", SDL_GetError());
   1.177 +        quit(1);
   1.178 +    }
   1.179  
   1.180 -	/* Set the window manager title bar */
   1.181 -	SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
   1.182 +    if (SDL_GL_LoadLibrary(gl_library) < 0) {
   1.183 +        printf("Unable to dynamically open GL lib : %s\n", SDL_GetError());
   1.184 +        quit(1);
   1.185 +    }
   1.186  
   1.187 -	init_glfuncs(&f);
   1.188 +    if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL) {
   1.189 +        printf("Unable to open video mode : %s\n", SDL_GetError());
   1.190 +        quit(1);
   1.191 +    }
   1.192  
   1.193 -	for(i=0;i<NB_PIXELS;i++)
   1.194 -	{
   1.195 -		pixels[3*i]=rand()%250-125;
   1.196 -		pixels[3*i+1]=rand()%250-125;
   1.197 -		pixels[3*i+2]=rand()%250-125;
   1.198 -	}
   1.199 -	
   1.200 -	f.glViewport(0,0,640,480);
   1.201 -	
   1.202 -	f.glMatrixMode(GL_PROJECTION);
   1.203 -	f.glLoadIdentity();
   1.204 -	f.glOrtho(-100,100,-100,100,-500,500);
   1.205 -	
   1.206 -	f.glMatrixMode(GL_MODELVIEW);
   1.207 -	f.glLoadIdentity();
   1.208 -	
   1.209 -	f.glEnable(GL_DEPTH_TEST);
   1.210 -	f.glDisable(GL_TEXTURE_2D);
   1.211 -	f.glEnable(GL_BLEND);
   1.212 -	f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.213 -	
   1.214 -	f.glClearColor(0.0f,0.0f,0.0f,0.0f);
   1.215 -	f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   1.216 -	
   1.217 -	f.glEnable(GL_POINT_SMOOTH);
   1.218 -	f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
   1.219 -	f.glPointSize(5.0f);
   1.220 -	f.glEnable(GL_FOG);
   1.221 -	f.glFogf(GL_FOG_START,-500);
   1.222 -	f.glFogf(GL_FOG_END,500);
   1.223 -	f.glFogf(GL_FOG_DENSITY,0.005);
   1.224 -	
   1.225 -	do
   1.226 -	{
   1.227 -		f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   1.228 -		
   1.229 -		f.glRotatef(2.0,1.0,1.0,1.0);
   1.230 -		f.glRotatef(1.0,0.0,1.0,1.0);
   1.231 -		
   1.232 -		f.glColor4ub(255,255,255,255);
   1.233 -		f.glBegin(GL_POINTS);
   1.234 -		for(i=0;i<NB_PIXELS;i++)
   1.235 -		{
   1.236 -			f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
   1.237 -		}
   1.238 -		f.glEnd();
   1.239 -		SDL_GL_SwapBuffers();
   1.240 +    /* Set the window manager title bar */
   1.241 +    SDL_WM_SetCaption("SDL Dynamic OpenGL Loading Test", "testdyngl");
   1.242  
   1.243 -		while(SDL_PollEvent(&event))
   1.244 -		{
   1.245 -			if(event.type & SDL_KEYDOWN)
   1.246 -				done=1;
   1.247 -		}
   1.248 +    init_glfuncs(&f);
   1.249  
   1.250 -		SDL_Delay(20);
   1.251 -	}
   1.252 -	while(!done);
   1.253 -	
   1.254 -	SDL_Quit();
   1.255 -	return 0;
   1.256 +    for (i = 0; i < NB_PIXELS; i++) {
   1.257 +        pixels[3 * i] = rand() % 250 - 125;
   1.258 +        pixels[3 * i + 1] = rand() % 250 - 125;
   1.259 +        pixels[3 * i + 2] = rand() % 250 - 125;
   1.260 +    }
   1.261 +
   1.262 +    f.glViewport(0, 0, 640, 480);
   1.263 +
   1.264 +    f.glMatrixMode(GL_PROJECTION);
   1.265 +    f.glLoadIdentity();
   1.266 +    f.glOrtho(-100, 100, -100, 100, -500, 500);
   1.267 +
   1.268 +    f.glMatrixMode(GL_MODELVIEW);
   1.269 +    f.glLoadIdentity();
   1.270 +
   1.271 +    f.glEnable(GL_DEPTH_TEST);
   1.272 +    f.glDisable(GL_TEXTURE_2D);
   1.273 +    f.glEnable(GL_BLEND);
   1.274 +    f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.275 +
   1.276 +    f.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   1.277 +    f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.278 +
   1.279 +    f.glEnable(GL_POINT_SMOOTH);
   1.280 +    f.glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
   1.281 +    f.glPointSize(5.0f);
   1.282 +    f.glEnable(GL_FOG);
   1.283 +    f.glFogf(GL_FOG_START, -500);
   1.284 +    f.glFogf(GL_FOG_END, 500);
   1.285 +    f.glFogf(GL_FOG_DENSITY, 0.005);
   1.286 +
   1.287 +    do {
   1.288 +        f.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.289 +
   1.290 +        f.glRotatef(2.0, 1.0, 1.0, 1.0);
   1.291 +        f.glRotatef(1.0, 0.0, 1.0, 1.0);
   1.292 +
   1.293 +        f.glColor4ub(255, 255, 255, 255);
   1.294 +        f.glBegin(GL_POINTS);
   1.295 +        for (i = 0; i < NB_PIXELS; i++) {
   1.296 +            f.glVertex3f(pixels[3 * i], pixels[3 * i + 1], pixels[3 * i + 2]);
   1.297 +        }
   1.298 +        f.glEnd();
   1.299 +        SDL_GL_SwapBuffers();
   1.300 +
   1.301 +        while (SDL_PollEvent(&event)) {
   1.302 +            if (event.type & SDL_KEYDOWN)
   1.303 +                done = 1;
   1.304 +        }
   1.305 +
   1.306 +        SDL_Delay(20);
   1.307 +    }
   1.308 +    while (!done);
   1.309 +
   1.310 +    SDL_Quit();
   1.311 +    return 0;
   1.312  }
   1.313  
   1.314  #else /* HAVE_OPENGL */
   1.315  
   1.316 -int main(int argc, char *argv[])
   1.317 +int
   1.318 +main(int argc, char *argv[])
   1.319  {
   1.320 -	printf("No OpenGL support on this system\n");
   1.321 -	return 1;
   1.322 +    printf("No OpenGL support on this system\n");
   1.323 +    return 1;
   1.324  }
   1.325  
   1.326  #endif /* HAVE_OPENGL */