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