test/testdyngl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Feb 2004 00:21:06 +0000
changeset 847 0afe0e38e02c
parent 842 0eec9985767d
child 885 9f6ad2286011
permissions -rw-r--r--
*** empty log message ***
slouken@839
     1
/*
slouken@839
     2
 * Small SDL example to demonstrate dynamically loading 
slouken@839
     3
 * OpenGL lib and functions
slouken@839
     4
 *
slouken@839
     5
 * (FYI it was supposed to look like snow in the wind or something...)
slouken@839
     6
 *
slouken@839
     7
 * Compile with :
slouken@847
     8
 * gcc testdyngl.c `sdl-config --libs --cflags` -o testdyngl -DHAVE_OPENGL
slouken@839
     9
 *
slouken@839
    10
 * You can specify a different OpenGL lib on the command line, i.e. :
slouken@839
    11
 * ./testdyngl  /usr/X11R6/lib/libGL.so.1.2
slouken@839
    12
 * or
slouken@839
    13
 * ./testdyngl  /usr/lib/libGL.so.1.0.4496
slouken@839
    14
 *
slouken@839
    15
 */
slouken@839
    16
slouken@839
    17
#include <stdio.h>
slouken@839
    18
#include <stdlib.h>
slouken@839
    19
slouken@839
    20
#include "SDL.h"
slouken@839
    21
slouken@839
    22
#ifdef HAVE_OPENGL
slouken@839
    23
slouken@839
    24
#include "SDL_opengl.h"
slouken@839
    25
slouken@839
    26
void* get_funcaddr(const char* p)
slouken@839
    27
{
slouken@839
    28
	void* f=SDL_GL_GetProcAddress(p);
slouken@839
    29
	if (f)
slouken@839
    30
	{
slouken@839
    31
		return f;
slouken@839
    32
	}
slouken@839
    33
	else
slouken@839
    34
	{
slouken@839
    35
		printf("Unable to get function pointer for %s\n",p);
slouken@839
    36
		exit(1);
slouken@839
    37
	}
slouken@839
    38
}
slouken@839
    39
slouken@839
    40
typedef struct
slouken@839
    41
{
slouken@839
    42
	void(*glBegin)(GLenum);
slouken@839
    43
	void(*glEnd)();
slouken@839
    44
	void(*glVertex3f)(GLfloat, GLfloat, GLfloat);
slouken@839
    45
	void(*glClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
slouken@839
    46
	void(*glClear)(GLbitfield);
slouken@839
    47
	void(*glDisable)(GLenum);
slouken@839
    48
	void(*glEnable)(GLenum);
slouken@839
    49
	void(*glColor4ub)(GLubyte,GLubyte,GLubyte,GLubyte);
slouken@839
    50
	void(*glPointSize)(GLfloat);
slouken@839
    51
	void(*glHint)(GLenum,GLenum);
slouken@839
    52
	void(*glBlendFunc)(GLenum,GLenum);
slouken@839
    53
	void(*glMatrixMode)(GLenum);
slouken@839
    54
	void(*glLoadIdentity)();
slouken@839
    55
	void(*glOrtho)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble);
slouken@839
    56
	void(*glRotatef)(GLfloat,GLfloat,GLfloat,GLfloat);
slouken@839
    57
	void(*glViewport)(GLint,GLint,GLsizei,GLsizei);
slouken@839
    58
	void(*glFogf)(GLenum,GLfloat);
slouken@839
    59
}
slouken@839
    60
glfuncs;
slouken@839
    61
slouken@839
    62
void init_glfuncs(glfuncs* f)
slouken@839
    63
{
slouken@839
    64
	f->glBegin=get_funcaddr("glBegin");
slouken@839
    65
	f->glEnd=get_funcaddr("glEnd");
slouken@839
    66
	f->glVertex3f=get_funcaddr("glVertex3f");
slouken@839
    67
	f->glClearColor=get_funcaddr("glClearColor");
slouken@839
    68
	f->glClear=get_funcaddr("glClear");
slouken@839
    69
	f->glDisable=get_funcaddr("glDisable");
slouken@839
    70
	f->glEnable=get_funcaddr("glEnable");
slouken@839
    71
	f->glColor4ub=get_funcaddr("glColor4ub");
slouken@839
    72
	f->glPointSize=get_funcaddr("glPointSize");
slouken@839
    73
	f->glHint=get_funcaddr("glHint");
slouken@839
    74
	f->glBlendFunc=get_funcaddr("glBlendFunc");
slouken@839
    75
	f->glMatrixMode=get_funcaddr("glMatrixMode");
slouken@839
    76
	f->glLoadIdentity=get_funcaddr("glLoadIdentity");
slouken@839
    77
	f->glOrtho=get_funcaddr("glOrtho");
slouken@839
    78
	f->glRotatef=get_funcaddr("glRotatef");
slouken@839
    79
	f->glViewport=get_funcaddr("glViewport");
slouken@839
    80
	f->glFogf=get_funcaddr("glFogf");
slouken@839
    81
}
slouken@839
    82
slouken@839
    83
#define NB_PIXELS 1000
slouken@839
    84
slouken@839
    85
int main(int argc,char *argv[])
slouken@839
    86
{
slouken@839
    87
	glfuncs f;
slouken@839
    88
	int i;
slouken@839
    89
	SDL_Event event;
slouken@839
    90
	int done=0;
slouken@839
    91
	GLfloat pixels[NB_PIXELS*3];
slouken@842
    92
#ifdef _WIN32
slouken@842
    93
	char *gl_library = "OpenGL32.DLL";
slouken@842
    94
#else
slouken@842
    95
	char *gl_library = "libGL.so.1";
slouken@842
    96
#endif
slouken@839
    97
	
slouken@842
    98
	if (argv[1]) {
slouken@842
    99
		gl_library = argv[1];
slouken@839
   100
	}
slouken@839
   101
	
slouken@842
   102
	if (SDL_Init(SDL_INIT_VIDEO)<0)
slouken@839
   103
	{
slouken@839
   104
		printf("Unable to init SDL : %s\n",SDL_GetError());
slouken@839
   105
		exit(1);
slouken@839
   106
	}
slouken@839
   107
	
slouken@839
   108
	if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
slouken@839
   109
	{
slouken@839
   110
		printf("Unable to set GL attribute : %s\n",SDL_GetError());
slouken@839
   111
		exit(1);
slouken@839
   112
	}
slouken@839
   113
	
slouken@842
   114
	if (SDL_GL_LoadLibrary(gl_library)<0)
slouken@839
   115
	{
slouken@839
   116
		printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
slouken@839
   117
		exit(1);
slouken@839
   118
	}
slouken@839
   119
slouken@839
   120
	if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
slouken@839
   121
	{
slouken@839
   122
		printf("Unable to open video mode : %s\n",SDL_GetError());
slouken@839
   123
		exit(1);
slouken@839
   124
	}
slouken@839
   125
slouken@839
   126
	init_glfuncs(&f);
slouken@839
   127
slouken@839
   128
	for(i=0;i<NB_PIXELS;i++)
slouken@839
   129
	{
slouken@839
   130
		pixels[3*i]=rand()%250-125;
slouken@839
   131
		pixels[3*i+1]=rand()%250-125;
slouken@839
   132
		pixels[3*i+2]=rand()%250-125;
slouken@839
   133
	}
slouken@839
   134
	
slouken@839
   135
	f.glViewport(0,0,640,480);
slouken@839
   136
	
slouken@839
   137
	f.glMatrixMode(GL_PROJECTION);
slouken@839
   138
	f.glLoadIdentity();
slouken@839
   139
	f.glOrtho(-100,100,-100,100,-500,500);
slouken@839
   140
	
slouken@839
   141
	f.glMatrixMode(GL_MODELVIEW);
slouken@839
   142
	f.glLoadIdentity();
slouken@839
   143
	
slouken@839
   144
	f.glEnable(GL_DEPTH_TEST);
slouken@839
   145
	f.glDisable(GL_TEXTURE_2D);
slouken@839
   146
	f.glEnable(GL_BLEND);
slouken@839
   147
	f.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
slouken@839
   148
	
slouken@839
   149
	f.glClearColor(0.0f,0.0f,0.0f,0.0f);
slouken@839
   150
	f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
slouken@839
   151
	
slouken@839
   152
	f.glEnable(GL_POINT_SMOOTH);
slouken@839
   153
	f.glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
slouken@839
   154
	f.glPointSize(5.0f);
slouken@839
   155
	f.glEnable(GL_FOG);
slouken@839
   156
	f.glFogf(GL_FOG_START,-500);
slouken@839
   157
	f.glFogf(GL_FOG_END,500);
slouken@839
   158
	f.glFogf(GL_FOG_DENSITY,0.005);
slouken@839
   159
	
slouken@839
   160
	do
slouken@839
   161
	{
slouken@839
   162
		f.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
slouken@839
   163
		
slouken@839
   164
		f.glRotatef(2.0,1.0,1.0,1.0);
slouken@839
   165
		f.glRotatef(1.0,0.0,1.0,1.0);
slouken@839
   166
		
slouken@839
   167
		f.glColor4ub(255,255,255,255);
slouken@839
   168
		f.glBegin(GL_POINTS);
slouken@839
   169
		for(i=0;i<NB_PIXELS;i++)
slouken@839
   170
		{
slouken@839
   171
			f.glVertex3f(pixels[3*i],pixels[3*i+1],pixels[3*i+2]);
slouken@839
   172
		}
slouken@839
   173
		f.glEnd();
slouken@839
   174
		SDL_GL_SwapBuffers();
slouken@839
   175
slouken@839
   176
		while(SDL_PollEvent(&event))
slouken@839
   177
		{
slouken@839
   178
			if(event.type & SDL_KEYDOWN)
slouken@839
   179
				done=1;
slouken@839
   180
		}
slouken@839
   181
slouken@839
   182
		SDL_Delay(20);
slouken@839
   183
	}
slouken@839
   184
	while(!done);
slouken@839
   185
	
slouken@839
   186
	SDL_Quit();
slouken@839
   187
	return 0;
slouken@839
   188
}
slouken@839
   189
slouken@839
   190
#else /* HAVE_OPENGL */
slouken@839
   191
slouken@839
   192
int main(int argc, char *argv[])
slouken@839
   193
{
slouken@839
   194
	printf("No OpenGL support on this system\n");
slouken@839
   195
	return 1;
slouken@839
   196
}
slouken@839
   197
slouken@839
   198
#endif /* HAVE_OPENGL */