Commit 91121c31 authored by Ryan C. Gordon's avatar Ryan C. Gordon

Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe

 if SDL is built with a non-cdecl calling convention, and it's just generally
 bad practice anyhow.

Now programs explicitly call SDL_Quit() where appropriate, wrap SDL_Quit() in
 a cdecl function where it can't be avoided, and rely on the parachute where
 a crash might have hit the atexit() before (these ARE test programs, after
 all!).

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401154
parent 004ec060
...@@ -10,6 +10,13 @@ ...@@ -10,6 +10,13 @@
#include "SDL.h" #include "SDL.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
static void print_modifiers(void) static void print_modifiers(void)
{ {
int mod; int mod;
...@@ -82,9 +89,8 @@ int main(int argc, char *argv[]) ...@@ -82,9 +89,8 @@ int main(int argc, char *argv[])
/* Initialize SDL */ /* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
videoflags = SDL_SWSURFACE; videoflags = SDL_SWSURFACE;
while( argc > 1 ) { while( argc > 1 ) {
...@@ -93,7 +99,7 @@ int main(int argc, char *argv[]) ...@@ -93,7 +99,7 @@ int main(int argc, char *argv[])
videoflags |= SDL_FULLSCREEN; videoflags |= SDL_FULLSCREEN;
} else { } else {
fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]); fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
exit(1); quit(1);
} }
} }
...@@ -101,7 +107,7 @@ int main(int argc, char *argv[]) ...@@ -101,7 +107,7 @@ int main(int argc, char *argv[])
if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) { if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
fprintf(stderr, "Couldn't set 640x480 video mode: %s\n", fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
SDL_GetError()); SDL_GetError());
exit(2); quit(2);
} }
/* Enable UNICODE translation for keyboard input */ /* Enable UNICODE translation for keyboard input */
...@@ -132,5 +138,7 @@ int main(int argc, char *argv[]) ...@@ -132,5 +138,7 @@ int main(int argc, char *argv[])
break; break;
} }
} }
SDL_Quit();
return(0); return(0);
} }
...@@ -19,6 +19,15 @@ struct { ...@@ -19,6 +19,15 @@ struct {
int soundpos; /* Current play position */ int soundpos; /* Current play position */
} wave; } wave;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
void fillerup(void *unused, Uint8 *stream, int len) void fillerup(void *unused, Uint8 *stream, int len)
{ {
Uint8 *waveptr; Uint8 *waveptr;
...@@ -54,13 +63,12 @@ int main(int argc, char *argv[]) ...@@ -54,13 +63,12 @@ int main(int argc, char *argv[])
/* Load the SDL library */ /* Load the SDL library */
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) { if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
if ( argv[1] == NULL ) { if ( argv[1] == NULL ) {
fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]); fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
exit(1); quit(1);
} }
/* Load the wave file into memory */ /* Load the wave file into memory */
...@@ -68,7 +76,7 @@ int main(int argc, char *argv[]) ...@@ -68,7 +76,7 @@ int main(int argc, char *argv[])
&wave.spec, &wave.sound, &wave.soundlen) == NULL ) { &wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n", fprintf(stderr, "Couldn't load %s: %s\n",
argv[1], SDL_GetError()); argv[1], SDL_GetError());
exit(1); quit(1);
} }
wave.spec.callback = fillerup; wave.spec.callback = fillerup;
...@@ -86,7 +94,7 @@ int main(int argc, char *argv[]) ...@@ -86,7 +94,7 @@ int main(int argc, char *argv[])
if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) { if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
SDL_FreeWAV(wave.sound); SDL_FreeWAV(wave.sound);
exit(2); quit(2);
} }
SDL_PauseAudio(0); SDL_PauseAudio(0);
...@@ -98,5 +106,6 @@ int main(int argc, char *argv[]) ...@@ -98,5 +106,6 @@ int main(int argc, char *argv[])
/* Clean up on signal */ /* Clean up on signal */
SDL_CloseAudio(); SDL_CloseAudio();
SDL_FreeWAV(wave.sound); SDL_FreeWAV(wave.sound);
SDL_Quit();
return(0); return(0);
} }
...@@ -12,6 +12,14 @@ ...@@ -12,6 +12,14 @@
#define FRAME_TICKS (1000/30) /* 30 frames/second */ #define FRAME_TICKS (1000/30) /* 30 frames/second */
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
/* Create a "light" -- a yellowish surface with variable alpha */ /* Create a "light" -- a yellowish surface with variable alpha */
SDL_Surface *CreateLight(SDL_Surface *screen, int radius) SDL_Surface *CreateLight(SDL_Surface *screen, int radius)
{ {
...@@ -292,9 +300,8 @@ int main(int argc, char *argv[]) ...@@ -292,9 +300,8 @@ int main(int argc, char *argv[])
/* Initialize SDL */ /* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
/* Alpha blending doesn't work well at 8-bit color */ /* Alpha blending doesn't work well at 8-bit color */
info = SDL_GetVideoInfo(); info = SDL_GetVideoInfo();
...@@ -327,7 +334,7 @@ int main(int argc, char *argv[]) ...@@ -327,7 +334,7 @@ int main(int argc, char *argv[])
fprintf(stderr, fprintf(stderr,
"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
argv[0]); argv[0]);
exit(1); quit(1);
} }
} }
...@@ -335,14 +342,14 @@ int main(int argc, char *argv[]) ...@@ -335,14 +342,14 @@ int main(int argc, char *argv[])
if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
video_bpp, SDL_GetError()); video_bpp, SDL_GetError());
exit(2); quit(2);
} }
/* Set the surface pixels and refresh! */ /* Set the surface pixels and refresh! */
if ( SDL_LockSurface(screen) < 0 ) { if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Couldn't lock the display surface: %s\n", fprintf(stderr, "Couldn't lock the display surface: %s\n",
SDL_GetError()); SDL_GetError());
exit(2); quit(2);
} }
buffer=(Uint8 *)screen->pixels; buffer=(Uint8 *)screen->pixels;
if (screen->format->BytesPerPixel!=2) { if (screen->format->BytesPerPixel!=2) {
...@@ -371,13 +378,13 @@ int main(int argc, char *argv[]) ...@@ -371,13 +378,13 @@ int main(int argc, char *argv[])
/* Create the light */ /* Create the light */
light = CreateLight(screen, 82); light = CreateLight(screen, 82);
if ( light == NULL ) { if ( light == NULL ) {
exit(1); quit(1);
} }
/* Load the sprite */ /* Load the sprite */
if ( LoadSprite(screen, "icon.bmp") < 0 ) { if ( LoadSprite(screen, "icon.bmp") < 0 ) {
SDL_FreeSurface(light); SDL_FreeSurface(light);
exit(1); quit(1);
} }
/* Print out information about our surfaces */ /* Print out information about our surfaces */
...@@ -492,5 +499,7 @@ fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks)); ...@@ -492,5 +499,7 @@ fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
printf("%d alpha blits, ~%4.4f ms per blit\n", printf("%d alpha blits, ~%4.4f ms per blit\n",
flashes, (float)flashtime/flashes); flashes, (float)flashtime/flashes);
} }
SDL_Quit();
return(0); return(0);
} }
...@@ -8,6 +8,13 @@ ...@@ -8,6 +8,13 @@
#include "SDL.h" #include "SDL.h"
#include "picture.xbm" #include "picture.xbm"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits) SDL_Surface *LoadXBM(SDL_Surface *screen, int w, int h, Uint8 *bits)
{ {
SDL_Surface *bitmap; SDL_Surface *bitmap;
...@@ -61,9 +68,8 @@ int main(int argc, char *argv[]) ...@@ -61,9 +68,8 @@ int main(int argc, char *argv[])
/* Initialize SDL */ /* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
video_bpp = 0; video_bpp = 0;
videoflags = SDL_SWSURFACE; videoflags = SDL_SWSURFACE;
...@@ -85,7 +91,7 @@ int main(int argc, char *argv[]) ...@@ -85,7 +91,7 @@ int main(int argc, char *argv[])
fprintf(stderr, fprintf(stderr,
"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n", "Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
argv[0]); argv[0]);
exit(1); quit(1);
} }
} }
...@@ -93,7 +99,7 @@ int main(int argc, char *argv[]) ...@@ -93,7 +99,7 @@ int main(int argc, char *argv[])
if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) { if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
video_bpp, SDL_GetError()); video_bpp, SDL_GetError());
exit(2); quit(2);
} }
if (video_bpp==8) { if (video_bpp==8) {
...@@ -110,7 +116,7 @@ int main(int argc, char *argv[]) ...@@ -110,7 +116,7 @@ int main(int argc, char *argv[])
if ( SDL_LockSurface(screen) < 0 ) { if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Couldn't lock the display surface: %s\n", fprintf(stderr, "Couldn't lock the display surface: %s\n",
SDL_GetError()); SDL_GetError());
exit(2); quit(2);
} }
buffer=(Uint8 *)screen->pixels; buffer=(Uint8 *)screen->pixels;
if (screen->format->BytesPerPixel!=2) { if (screen->format->BytesPerPixel!=2) {
...@@ -139,7 +145,7 @@ int main(int argc, char *argv[]) ...@@ -139,7 +145,7 @@ int main(int argc, char *argv[])
bitmap = LoadXBM(screen, picture_width, picture_height, bitmap = LoadXBM(screen, picture_width, picture_height,
(Uint8 *)picture_bits); (Uint8 *)picture_bits);
if ( bitmap == NULL ) { if ( bitmap == NULL ) {
exit(1); quit(1);
} }
/* Wait for a keystroke */ /* Wait for a keystroke */
...@@ -173,5 +179,6 @@ int main(int argc, char *argv[]) ...@@ -173,5 +179,6 @@ int main(int argc, char *argv[])
} }
} }
SDL_FreeSurface(bitmap); SDL_FreeSurface(bitmap);
SDL_Quit();
return(0); return(0);
} }
...@@ -7,6 +7,12 @@ ...@@ -7,6 +7,12 @@
#include "SDL.h" #include "SDL.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
static void PrintStatus(int driveindex, SDL_CD *cdrom) static void PrintStatus(int driveindex, SDL_CD *cdrom)
{ {
...@@ -92,14 +98,13 @@ int main(int argc, char *argv[]) ...@@ -92,14 +98,13 @@ int main(int argc, char *argv[])
/* Initialize SDL first */ /* Initialize SDL first */
if ( SDL_Init(SDL_INIT_CDROM) < 0 ) { if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
/* Find out how many CD-ROM drives are connected to the system */ /* Find out how many CD-ROM drives are connected to the system */
if ( SDL_CDNumDrives() == 0 ) { if ( SDL_CDNumDrives() == 0 ) {
printf("No CD-ROM devices detected\n"); printf("No CD-ROM devices detected\n");
exit(0); quit(0);
} }
printf("Drives available: %d\n", SDL_CDNumDrives()); printf("Drives available: %d\n", SDL_CDNumDrives());
for ( i=0; i<SDL_CDNumDrives(); ++i ) { for ( i=0; i<SDL_CDNumDrives(); ++i ) {
...@@ -116,7 +121,7 @@ int main(int argc, char *argv[]) ...@@ -116,7 +121,7 @@ int main(int argc, char *argv[])
if ( cdrom == NULL ) { if ( cdrom == NULL ) {
fprintf(stderr, "Couldn't open drive %d: %s\n", drive, fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
SDL_GetError()); SDL_GetError());
exit(2); quit(2);
} }
#ifdef TEST_NULLCD #ifdef TEST_NULLCD
cdrom = NULL; cdrom = NULL;
...@@ -192,11 +197,12 @@ int main(int argc, char *argv[]) ...@@ -192,11 +197,12 @@ int main(int argc, char *argv[])
} else { } else {
PrintUsage(argv[0]); PrintUsage(argv[0]);
SDL_CDClose(cdrom); SDL_CDClose(cdrom);
exit(1); quit(1);
} }
} }
PrintStatus(drive, cdrom); PrintStatus(drive, cdrom);
SDL_CDClose(cdrom); SDL_CDClose(cdrom);
SDL_Quit();
return(0); return(0);
} }
...@@ -23,6 +23,13 @@ ...@@ -23,6 +23,13 @@
#include "SDL_opengl.h" #include "SDL_opengl.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
void* get_funcaddr(const char* p) void* get_funcaddr(const char* p)
{ {
void* f=SDL_GL_GetProcAddress(p); void* f=SDL_GL_GetProcAddress(p);
...@@ -33,7 +40,7 @@ void* get_funcaddr(const char* p) ...@@ -33,7 +40,7 @@ void* get_funcaddr(const char* p)
else else
{ {
printf("Unable to get function pointer for %s\n",p); printf("Unable to get function pointer for %s\n",p);
exit(1); quit(1);
} }
} }
...@@ -104,27 +111,25 @@ int main(int argc,char *argv[]) ...@@ -104,27 +111,25 @@ int main(int argc,char *argv[])
if (SDL_Init(SDL_INIT_VIDEO)<0) if (SDL_Init(SDL_INIT_VIDEO)<0)
{ {
printf("Unable to init SDL : %s\n",SDL_GetError()); printf("Unable to init SDL : %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0) if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
{ {
printf("Unable to set GL attribute : %s\n",SDL_GetError()); printf("Unable to set GL attribute : %s\n",SDL_GetError());
exit(1); quit(1);
} }
if (SDL_GL_LoadLibrary(gl_library)<0) if (SDL_GL_LoadLibrary(gl_library)<0)
{ {
printf("Unable to dynamically open GL lib : %s\n",SDL_GetError()); printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
exit(1); quit(1);
} }
if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL) if (SDL_SetVideoMode(640,480,0,SDL_OPENGL)==NULL)
{ {
printf("Unable to open video mode : %s\n",SDL_GetError()); printf("Unable to open video mode : %s\n",SDL_GetError());
exit(1); quit(1);
} }
/* Set the window manager title bar */ /* Set the window manager title bar */
......
...@@ -10,6 +10,13 @@ ...@@ -10,6 +10,13 @@
static int alive = 0; static int alive = 0;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
int ThreadFunc(void *data) int ThreadFunc(void *data)
{ {
/* Set the child thread error string */ /* Set the child thread error string */
...@@ -30,9 +37,8 @@ int main(int argc, char *argv[]) ...@@ -30,9 +37,8 @@ int main(int argc, char *argv[])
/* Load the SDL library */ /* Load the SDL library */
if ( SDL_Init(0) < 0 ) { if ( SDL_Init(0) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
/* Set the error value for the main thread */ /* Set the error value for the main thread */
SDL_SetError("No worries"); SDL_SetError("No worries");
...@@ -41,7 +47,7 @@ int main(int argc, char *argv[]) ...@@ -41,7 +47,7 @@ int main(int argc, char *argv[])
thread = SDL_CreateThread(ThreadFunc, "#1"); thread = SDL_CreateThread(ThreadFunc, "#1");
if ( thread == NULL ) { if ( thread == NULL ) {
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
exit(1); quit(1);
} }
SDL_Delay(5*1000); SDL_Delay(5*1000);
printf("Waiting for thread #1\n"); printf("Waiting for thread #1\n");
...@@ -50,5 +56,6 @@ int main(int argc, char *argv[]) ...@@ -50,5 +56,6 @@ int main(int argc, char *argv[])
printf("Main thread error string: %s\n", SDL_GetError()); printf("Main thread error string: %s\n", SDL_GetError());
SDL_Quit();
return(0); return(0);
} }
...@@ -8,6 +8,13 @@ ...@@ -8,6 +8,13 @@
#include "SDL.h" #include "SDL.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
/* Turn a normal gamma value into an appropriate gamma ramp */ /* Turn a normal gamma value into an appropriate gamma ramp */
void CalculateGamma(double gamma, Uint16 *ramp) void CalculateGamma(double gamma, Uint16 *ramp)
{ {
...@@ -82,16 +89,15 @@ int main(int argc, char *argv[]) ...@@ -82,16 +89,15 @@ int main(int argc, char *argv[])
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError()); "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
/* Initialize the display, always use hardware palette */ /* Initialize the display, always use hardware palette */
screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE); screen = SDL_SetVideoMode(w, h, bpp, flags | SDL_HWPALETTE);
if ( screen == NULL ) { if ( screen == NULL ) {
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
w, h, SDL_GetError()); w, h, SDL_GetError());
exit(1); quit(1);
} }
/* Set the window manager title bar */ /* Set the window manager title bar */
...@@ -104,7 +110,7 @@ int main(int argc, char *argv[]) ...@@ -104,7 +110,7 @@ int main(int argc, char *argv[])
} }
if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) { if ( SDL_SetGamma(gamma, gamma, gamma) < 0 ) {
fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError()); fprintf(stderr, "Unable to set gamma: %s\n", SDL_GetError());
exit(1); quit(1);
} }
#if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */ #if 0 /* This isn't supported. Integrating the gamma ramps isn't exact */
...@@ -186,5 +192,6 @@ int main(int argc, char *argv[]) ...@@ -186,5 +192,6 @@ int main(int argc, char *argv[])
} }
SDL_Delay(1*1000); SDL_Delay(1*1000);
SDL_Quit();
return(0); return(0);
} }
...@@ -10,6 +10,13 @@ ...@@ -10,6 +10,13 @@
static int alive = 0; static int alive = 0;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
int ThreadFunc(void *data) int ThreadFunc(void *data)
{ {
printf("Started thread %s: My thread id is %u\n", printf("Started thread %s: My thread id is %u\n",
...@@ -27,7 +34,7 @@ static void killed(int sig) ...@@ -27,7 +34,7 @@ static void killed(int sig)
printf("Killed with SIGTERM, waiting 5 seconds to exit\n"); printf("Killed with SIGTERM, waiting 5 seconds to exit\n");
SDL_Delay(5*1000); SDL_Delay(5*1000);
alive = 0; alive = 0;
exit(0); quit(0);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
...@@ -37,15 +44,14 @@ int main(int argc, char *argv[]) ...@@ -37,15 +44,14 @@ int main(int argc, char *argv[])
/* Load the SDL library */ /* Load the SDL library */
if ( SDL_Init(0) < 0 ) { if ( SDL_Init(0) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
alive = 1; alive = 1;
thread = SDL_CreateThread(ThreadFunc, "#1"); thread = SDL_CreateThread(ThreadFunc, "#1");
if ( thread == NULL ) { if ( thread == NULL ) {
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
exit(1); quit(1);
} }
SDL_Delay(5*1000); SDL_Delay(5*1000);
printf("Waiting for thread #1\n"); printf("Waiting for thread #1\n");
...@@ -56,7 +62,7 @@ int main(int argc, char *argv[]) ...@@ -56,7 +62,7 @@ int main(int argc, char *argv[])
thread = SDL_CreateThread(ThreadFunc, "#2"); thread = SDL_CreateThread(ThreadFunc, "#2");
if ( thread == NULL ) { if ( thread == NULL ) {
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
exit(1); quit(1);
} }
SDL_Delay(5*1000); SDL_Delay(5*1000);
printf("Killing thread #2\n"); printf("Killing thread #2\n");
...@@ -67,9 +73,10 @@ int main(int argc, char *argv[]) ...@@ -67,9 +73,10 @@ int main(int argc, char *argv[])
thread = SDL_CreateThread(ThreadFunc, "#3"); thread = SDL_CreateThread(ThreadFunc, "#3");
if ( thread == NULL ) { if ( thread == NULL ) {
fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't create thread: %s\n", SDL_GetError());
exit(1); quit(1);
} }
raise(SIGTERM); raise(SIGTERM);
SDL_Quit(); /* Never reached */
return(0); /* Never reached */ return(0); /* Never reached */
} }
...@@ -15,6 +15,15 @@ static SDL_mutex *mutex = NULL; ...@@ -15,6 +15,15 @@ static SDL_mutex *mutex = NULL;
static Uint32 mainthread; static Uint32 mainthread;
static SDL_Thread *threads[6]; static SDL_Thread *threads[6];
/*
* SDL_Quit() shouldn't be used with atexit() directly because
* calling conventions may differ...
*/
static void SDL_Quit_Wrapper(void)
{
SDL_Quit();
}
void printid(void) void printid(void)
{ {
printf("Process %u: exiting\n", SDL_ThreadID()); printf("Process %u: exiting\n", SDL_ThreadID());
...@@ -68,7 +77,7 @@ int main(int argc, char *argv[]) ...@@ -68,7 +77,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "%s\n", SDL_GetError()); fprintf(stderr, "%s\n", SDL_GetError());
exit(1); exit(1);
} }
atexit(SDL_Quit); atexit(SDL_Quit_Wrapper);
if ( (mutex=SDL_CreateMutex()) == NULL ) { if ( (mutex=SDL_CreateMutex()) == NULL ) {
fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't create mutex: %s\n", SDL_GetError());
......
...@@ -21,6 +21,13 @@ int monochrome; ...@@ -21,6 +21,13 @@ int monochrome;
int luminance; int luminance;
int w, h; int w, h;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
/* NOTE: These RGB conversion functions are not intended for speed, /* NOTE: These RGB conversion functions are not intended for speed,
only as examples. only as examples.
*/ */
...@@ -347,7 +354,7 @@ int main(int argc, char **argv) ...@@ -347,7 +354,7 @@ int main(int argc, char **argv)
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -delay option requires an argument\n"); "The -delay option requires an argument\n");
exit(1); return(1);
} }
} else } else
if ( strcmp(argv[1], "-width") == 0 ) { if ( strcmp(argv[1], "-width") == 0 ) {
...@@ -357,7 +364,7 @@ int main(int argc, char **argv) ...@@ -357,7 +364,7 @@ int main(int argc, char **argv)
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -width option requires an argument\n"); "The -width option requires an argument\n");
exit(1); return(1);
} }
} else } else
if ( strcmp(argv[1], "-height") == 0 ) { if ( strcmp(argv[1], "-height") == 0 ) {
...@@ -367,7 +374,7 @@ int main(int argc, char **argv) ...@@ -367,7 +374,7 @@ int main(int argc, char **argv)
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -height option requires an argument\n"); "The -height option requires an argument\n");
exit(1); return(1);
} }
} else } else
if ( strcmp(argv[1], "-bpp") == 0 ) { if ( strcmp(argv[1], "-bpp") == 0 ) {
...@@ -378,7 +385,7 @@ int main(int argc, char **argv) ...@@ -378,7 +385,7 @@ int main(int argc, char **argv)
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -bpp option requires an argument\n"); "The -bpp option requires an argument\n");
exit(1); return(1);
} }
} else } else
if ( strcmp(argv[1], "-lum") == 0 ) { if ( strcmp(argv[1], "-lum") == 0 ) {
...@@ -389,7 +396,7 @@ int main(int argc, char **argv) ...@@ -389,7 +396,7 @@ int main(int argc, char **argv)
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -lum option requires an argument\n"); "The -lum option requires an argument\n");
exit(1); return(1);
} }
} else } else
if ( strcmp(argv[1], "-format") == 0 ) { if ( strcmp(argv[1], "-format") == 0 ) {
...@@ -407,14 +414,14 @@ int main(int argc, char **argv) ...@@ -407,14 +414,14 @@ int main(int argc, char **argv)
else else
{ {
fprintf(stderr, "The -format option %s is not recognized\n",argv[2]); fprintf(stderr, "The -format option %s is not recognized\n",argv[2]);
exit(1); return(1);
} }
argv += 2; argv += 2;
argc -= 2; argc -= 2;
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -format option requires an argument\n"); "The -format option requires an argument\n");
exit(1); return(1);
} }
} else } else
if ( strcmp(argv[1], "-hw") == 0 ) { if ( strcmp(argv[1], "-hw") == 0 ) {
...@@ -439,7 +446,7 @@ int main(int argc, char **argv) ...@@ -439,7 +446,7 @@ int main(int argc, char **argv)
} else } else
if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) { if (( strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) {
PrintUsage(argv0); PrintUsage(argv0);
exit(1); return(1);
} else } else
if ( strcmp(argv[1], "-fullscreen") == 0 ) { if ( strcmp(argv[1], "-fullscreen") == 0 ) {
video_flags |= SDL_FULLSCREEN; video_flags |= SDL_FULLSCREEN;
...@@ -451,16 +458,15 @@ int main(int argc, char **argv) ...@@ -451,16 +458,15 @@ int main(int argc, char **argv)
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError()); "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit); /* Clean up on exit */
/* Initialize the display */ /* Initialize the display */
screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
if ( screen == NULL ) { if ( screen == NULL ) {
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
w, h, desired_bpp, SDL_GetError()); w, h, desired_bpp, SDL_GetError());
exit(1); quit(1);
} }
printf("Set%s %dx%dx%d mode\n", printf("Set%s %dx%dx%d mode\n",
screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
...@@ -481,7 +487,7 @@ int main(int argc, char **argv) ...@@ -481,7 +487,7 @@ int main(int argc, char **argv)
if ( pic == NULL ) { if ( pic == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, fprintf(stderr, "Couldn't load %s: %s\n", bmpfile,
SDL_GetError()); SDL_GetError());
exit(1); quit(1);
} }
/* Convert the picture to 32bits, for easy conversion */ /* Convert the picture to 32bits, for easy conversion */
...@@ -518,7 +524,7 @@ int main(int argc, char **argv) ...@@ -518,7 +524,7 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n",
SDL_GetError()); SDL_GetError());
exit(1); quit(1);
} }
SDL_FreeSurface(pic); SDL_FreeSurface(pic);
pic=newsurf; pic=newsurf;
...@@ -528,7 +534,7 @@ int main(int argc, char **argv) ...@@ -528,7 +534,7 @@ int main(int argc, char **argv)
overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen); overlay = SDL_CreateYUVOverlay(pic->w, pic->h, overlay_format, screen);
if ( overlay == NULL ) { if ( overlay == NULL ) {
fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
exit(1); quit(1);
} }
printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
overlay->hw_overlay?"hardware":"software", overlay->hw_overlay?"hardware":"software",
...@@ -566,7 +572,7 @@ int main(int argc, char **argv) ...@@ -566,7 +572,7 @@ int main(int argc, char **argv)
break; break;
default: default:
printf("cannot convert RGB picture to obtained YUV format!\n"); printf("cannot convert RGB picture to obtained YUV format!\n");
exit(1); quit(1);
break; break;
} }
#ifdef BENCHMARK_SDL #ifdef BENCHMARK_SDL
...@@ -584,6 +590,7 @@ int main(int argc, char **argv) ...@@ -584,6 +590,7 @@ int main(int argc, char **argv)
printf("Time: %d milliseconds\n", now-then); printf("Time: %d milliseconds\n", now-then);
#endif #endif
SDL_Delay(delay*1000); SDL_Delay(delay*1000);
SDL_Quit();
return(0); return(0);
} }
...@@ -41,6 +41,14 @@ SDL_Color MooseColors[84]={ ...@@ -41,6 +41,14 @@ SDL_Color MooseColors[84]={
{222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173} {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
}; };
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
/* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */ /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
/* NOTE: These RGB conversion functions are not intended for speed, /* NOTE: These RGB conversion functions are not intended for speed,
...@@ -288,6 +296,12 @@ int main(int argc, char **argv) ...@@ -288,6 +296,12 @@ int main(int argc, char **argv)
int overlay_format=SDL_YUY2_OVERLAY; int overlay_format=SDL_YUY2_OVERLAY;
int scale=5; int scale=5;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 3;
}
while ( argc > 1 ) while ( argc > 1 )
{ {
if (strcmp(argv[1], "-fps")== 0) if (strcmp(argv[1], "-fps")== 0)
...@@ -298,12 +312,12 @@ int main(int argc, char **argv) ...@@ -298,12 +312,12 @@ int main(int argc, char **argv)
if (fps==0) if (fps==0)
{ {
fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
return -1; quit(10);
} }
if ((fps<0) || (fps>1000)) if ((fps<0) || (fps>1000))
{ {
fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n"); fprintf(stderr, "The -fps option must be in range from 1 to 1000, default is 12.\n");
return -1; quit(10);
} }
argv += 2; argv += 2;
argc -= 2; argc -= 2;
...@@ -311,7 +325,7 @@ int main(int argc, char **argv) ...@@ -311,7 +325,7 @@ int main(int argc, char **argv)
else else
{ {
fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
return -1; quit(10);
} }
} else } else
if (strcmp(argv[1], "-format") == 0) if (strcmp(argv[1], "-format") == 0)
...@@ -331,7 +345,7 @@ int main(int argc, char **argv) ...@@ -331,7 +345,7 @@ int main(int argc, char **argv)
else else
{ {
fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]); fprintf(stderr, "The -format option %s is not recognized, see help for info.\n", argv[2]);
return -1; quit(10);
} }
argv += 2; argv += 2;
argc -= 2; argc -= 2;
...@@ -339,7 +353,7 @@ int main(int argc, char **argv) ...@@ -339,7 +353,7 @@ int main(int argc, char **argv)
else else
{ {
fprintf(stderr, "The -format option requires an argument, default is YUY2.\n"); fprintf(stderr, "The -format option requires an argument, default is YUY2.\n");
return -1; quit(10);
} }
} else } else
if (strcmp(argv[1], "-scale") == 0) if (strcmp(argv[1], "-scale") == 0)
...@@ -350,12 +364,12 @@ int main(int argc, char **argv) ...@@ -350,12 +364,12 @@ int main(int argc, char **argv)
if (scale==0) if (scale==0)
{ {
fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n"); fprintf(stderr, "The -scale option requires an argument [from 1 to 50], default is 5.\n");
return -1; quit(10);
} }
if ((scale<0) || (scale>50)) if ((scale<0) || (scale>50))
{ {
fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n"); fprintf(stderr, "The -scale option must be in range from 1 to 50, default is 5.\n");
return -1; quit(10);
} }
argv += 2; argv += 2;
argc -= 2; argc -= 2;
...@@ -363,17 +377,17 @@ int main(int argc, char **argv) ...@@ -363,17 +377,17 @@ int main(int argc, char **argv)
else else
{ {
fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n"); fprintf(stderr, "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
return -1; quit(10);
} }
} else } else
if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0)) if ((strcmp(argv[1], "-help") == 0 ) || (strcmp(argv[1], "-h") == 0))
{ {
PrintUsage(argv[0]); PrintUsage(argv[0]);
return 0; quit(0);
} else } else
{ {
fprintf(stderr, "Unrecognized option: %s.\n", argv[1]); fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
return -1; quit(10);
} }
break; break;
} }
...@@ -383,7 +397,7 @@ int main(int argc, char **argv) ...@@ -383,7 +397,7 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, "Can't allocate memory for movie !\n"); fprintf(stderr, "Can't allocate memory for movie !\n");
free(RawMooseData); free(RawMooseData);
return 1; quit(1);
} }
/* load the trojan moose images */ /* load the trojan moose images */
...@@ -392,27 +406,19 @@ int main(int argc, char **argv) ...@@ -392,27 +406,19 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, "Can't find the file moose.dat !\n"); fprintf(stderr, "Can't find the file moose.dat !\n");
free(RawMooseData); free(RawMooseData);
return 2; quit(2);
} }
SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT); SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
SDL_RWclose(handle); SDL_RWclose(handle);
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
{
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
free(RawMooseData);
return 3;
}
atexit(SDL_Quit);
/* Set video mode */ /* Set video mode */
if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL ) if ( (screen=SDL_SetVideoMode(MOOSEPIC_W*scale, MOOSEPIC_H*scale, 0, SDL_RESIZABLE | SDL_SWSURFACE)) == NULL )
{ {
fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError()); fprintf(stderr, "Couldn't set video mode: %s\n", 0, SDL_GetError());
free(RawMooseData); free(RawMooseData);
return 4; quit(4);
} }
/* Set the window manager title bar */ /* Set the window manager title bar */
...@@ -426,7 +432,7 @@ int main(int argc, char **argv) ...@@ -426,7 +432,7 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError()); fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n", 0, SDL_GetError());
free(RawMooseData); free(RawMooseData);
return 5; quit(5);
} }
SDL_SetColors(MooseFrame[i], MooseColors, 0, 84); SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
...@@ -462,7 +468,7 @@ int main(int argc, char **argv) ...@@ -462,7 +468,7 @@ int main(int argc, char **argv)
if(!newsurf) if(!newsurf)
{ {
fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't convert picture to 32bits RGB: %s\n", SDL_GetError());
return 6; quit(6);
} }
SDL_FreeSurface(MooseFrame[i]); SDL_FreeSurface(MooseFrame[i]);
MooseFrame[i]=newsurf; MooseFrame[i]=newsurf;
...@@ -475,7 +481,7 @@ int main(int argc, char **argv) ...@@ -475,7 +481,7 @@ int main(int argc, char **argv)
if (!overlay) if (!overlay)
{ {
fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
return 7; quit(7);
} }
printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes, printf("Created %dx%dx%d %s %s overlay\n",overlay->w,overlay->h,overlay->planes,
...@@ -538,7 +544,7 @@ int main(int argc, char **argv) ...@@ -538,7 +544,7 @@ int main(int argc, char **argv)
{ {
SDL_FreeSurface(MooseFrame[i]); SDL_FreeSurface(MooseFrame[i]);
} }
return 0; quit(0);
} }
} }
...@@ -586,6 +592,7 @@ int main(int argc, char **argv) ...@@ -586,6 +592,7 @@ int main(int argc, char **argv)
SDL_Delay(1); SDL_Delay(1);
} }
SDL_Quit();
return 0; return 0;
} }
...@@ -54,10 +54,17 @@ static SDL_Color wavemap[] = { ...@@ -54,10 +54,17 @@ static SDL_Color wavemap[] = {
{0,39,172}, {0,28,152}, {0,17,132}, {0,7,114} {0,39,172}, {0,28,152}, {0,17,132}, {0,7,114}
}; };
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
static void sdlerr(char *when) static void sdlerr(char *when)
{ {
fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError()); fprintf(stderr, "SDL error: %s: %s\n", when, SDL_GetError());
exit(1); quit(1);
} }
/* create a background surface */ /* create a background surface */
...@@ -139,8 +146,6 @@ int main(int argc, char **argv) ...@@ -139,8 +146,6 @@ int main(int argc, char **argv)
if(SDL_Init(SDL_INIT_VIDEO) < 0) if(SDL_Init(SDL_INIT_VIDEO) < 0)
sdlerr("initialising SDL"); sdlerr("initialising SDL");
atexit(SDL_Quit);
while(--argc) { while(--argc) {
++argv; ++argv;
if(strcmp(*argv, "-hw") == 0) if(strcmp(*argv, "-hw") == 0)
...@@ -157,7 +162,7 @@ int main(int argc, char **argv) ...@@ -157,7 +162,7 @@ int main(int argc, char **argv)
fprintf(stderr, fprintf(stderr,
"usage: testpalette " "usage: testpalette "
" [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n"); " [-hw] [-fullscreen] [-nofade] [-gamma] [-gammaramp]\n");
return 1; quit(1);
} }
} }
...@@ -165,7 +170,7 @@ int main(int argc, char **argv) ...@@ -165,7 +170,7 @@ int main(int argc, char **argv)
if(!(screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE))) { if(!(screen = SDL_SetVideoMode(SCRW, SCRH, 8, vidflags | SDL_HWPALETTE))) {
fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n", fprintf(stderr, "error setting %dx%d 8bpp indexed mode: %s\n",
SCRW, SCRH, SDL_GetError()); SCRW, SCRH, SDL_GetError());
return 1; quit(1);
} }
if(!(boat[0] = SDL_LoadBMP("sail.bmp"))) if(!(boat[0] = SDL_LoadBMP("sail.bmp")))
...@@ -327,6 +332,8 @@ int main(int argc, char **argv) ...@@ -327,6 +332,8 @@ int main(int argc, char **argv)
printf("%d frames, %.2f fps\n", printf("%d frames, %.2f fps\n",
frames, 1000.0 * frames / (SDL_GetTicks() - start)); frames, 1000.0 * frames / (SDL_GetTicks() - start));
SDL_Quit();
return 0; return 0;
} }
...@@ -13,6 +13,13 @@ ...@@ -13,6 +13,13 @@
static SDL_sem *sem; static SDL_sem *sem;
int alive = 1; int alive = 1;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
int ThreadFunc(void *data) int ThreadFunc(void *data)
{ {
while ( alive ) { while ( alive ) {
...@@ -39,15 +46,14 @@ int main(int argc, char **argv) ...@@ -39,15 +46,14 @@ int main(int argc, char **argv)
if(argc < 2) { if(argc < 2) {
fprintf(stderr,"Usage: %s init_value\n", argv[0]); fprintf(stderr,"Usage: %s init_value\n", argv[0]);
exit(1); return(1);
} }
/* Load the SDL library */ /* Load the SDL library */
if ( SDL_Init(0) < 0 ) { if ( SDL_Init(0) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
signal(SIGTERM, killed); signal(SIGTERM, killed);
signal(SIGINT, killed); signal(SIGINT, killed);
...@@ -72,5 +78,6 @@ int main(int argc, char **argv) ...@@ -72,5 +78,6 @@ int main(int argc, char **argv)
printf("Finished waiting for threads\n"); printf("Finished waiting for threads\n");
SDL_DestroySemaphore(sem); SDL_DestroySemaphore(sem);
SDL_Quit();
return(0); return(0);
} }
...@@ -22,6 +22,13 @@ SDL_Rect *velocities; ...@@ -22,6 +22,13 @@ SDL_Rect *velocities;
int sprites_visible; int sprites_visible;
Uint16 sprite_w, sprite_h; Uint16 sprite_w, sprite_h;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
int LoadSprite(SDL_Surface *screen, char *file) int LoadSprite(SDL_Surface *screen, char *file)
{ {
SDL_Surface *temp; SDL_Surface *temp;
...@@ -159,9 +166,8 @@ int main(int argc, char *argv[]) ...@@ -159,9 +166,8 @@ int main(int argc, char *argv[])
/* Initialize SDL */ /* Initialize SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
numsprites = NUM_SPRITES; numsprites = NUM_SPRITES;
videoflags = SDL_SWSURFACE|SDL_ANYFORMAT; videoflags = SDL_SWSURFACE|SDL_ANYFORMAT;
...@@ -201,7 +207,7 @@ int main(int argc, char *argv[]) ...@@ -201,7 +207,7 @@ int main(int argc, char *argv[])
fprintf(stderr, fprintf(stderr,
"Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n", "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
argv[0]); argv[0]);
exit(1); quit(1);
} }
} }
...@@ -210,12 +216,12 @@ int main(int argc, char *argv[]) ...@@ -210,12 +216,12 @@ int main(int argc, char *argv[])
if ( ! screen ) { if ( ! screen ) {
fprintf(stderr, "Couldn't set %dx%d video mode: %s\n", fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
width, height, SDL_GetError()); width, height, SDL_GetError());
exit(2); quit(2);
} }
/* Load the sprite */ /* Load the sprite */
if ( LoadSprite(screen, "icon.bmp") < 0 ) { if ( LoadSprite(screen, "icon.bmp") < 0 ) {
exit(1); quit(1);
} }
/* Allocate memory for the sprite info */ /* Allocate memory for the sprite info */
...@@ -223,7 +229,7 @@ int main(int argc, char *argv[]) ...@@ -223,7 +229,7 @@ int main(int argc, char *argv[])
if ( mem == NULL ) { if ( mem == NULL ) {
SDL_FreeSurface(sprite); SDL_FreeSurface(sprite);
fprintf(stderr, "Out of memory!\n"); fprintf(stderr, "Out of memory!\n");
exit(2); quit(2);
} }
sprite_rects = (SDL_Rect *)mem; sprite_rects = (SDL_Rect *)mem;
positions = sprite_rects; positions = sprite_rects;
......
...@@ -12,6 +12,13 @@ ...@@ -12,6 +12,13 @@
static int ticks = 0; static int ticks = 0;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
static Uint32 ticktock(Uint32 interval) static Uint32 ticktock(Uint32 interval)
{ {
++ticks; ++ticks;
...@@ -31,9 +38,8 @@ int main(int argc, char *argv[]) ...@@ -31,9 +38,8 @@ int main(int argc, char *argv[])
if ( SDL_Init(SDL_INIT_TIMER) < 0 ) { if ( SDL_Init(SDL_INIT_TIMER) < 0 ) {
fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError()); fprintf(stderr, "Couldn't load SDL: %s\n", SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
/* Start the timer */ /* Start the timer */
desired = 0; desired = 0;
...@@ -83,5 +89,6 @@ int main(int argc, char *argv[]) ...@@ -83,5 +89,6 @@ int main(int argc, char *argv[])
SDL_RemoveTimer(t2); SDL_RemoveTimer(t2);
SDL_RemoveTimer(t3); SDL_RemoveTimer(t3);
SDL_Quit();
return(0); return(0);
} }
...@@ -11,6 +11,13 @@ ...@@ -11,6 +11,13 @@
#include "SDL.h" #include "SDL.h"
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
void DrawPict(SDL_Surface *screen, char *bmpfile, void DrawPict(SDL_Surface *screen, char *bmpfile,
int speedy, int flip, int nofade) int speedy, int flip, int nofade)
{ {
...@@ -245,6 +252,13 @@ int main(int argc, char *argv[]) ...@@ -245,6 +252,13 @@ int main(int argc, char *argv[])
h = 480; h = 480;
desired_bpp = 0; desired_bpp = 0;
video_flags = 0; video_flags = 0;
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
return(1);
}
while ( argc > 1 ) { while ( argc > 1 ) {
if ( strcmp(argv[1], "-speedy") == 0 ) { if ( strcmp(argv[1], "-speedy") == 0 ) {
speedy = 1; speedy = 1;
...@@ -264,7 +278,7 @@ int main(int argc, char *argv[]) ...@@ -264,7 +278,7 @@ int main(int argc, char *argv[])
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -delay option requires an argument\n"); "The -delay option requires an argument\n");
exit(1); quit(1);
} }
} else } else
if ( strcmp(argv[1], "-width") == 0 ) { if ( strcmp(argv[1], "-width") == 0 ) {
...@@ -274,7 +288,7 @@ int main(int argc, char *argv[]) ...@@ -274,7 +288,7 @@ int main(int argc, char *argv[])
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -width option requires an argument\n"); "The -width option requires an argument\n");
exit(1); quit(1);
} }
} else } else
if ( strcmp(argv[1], "-height") == 0 ) { if ( strcmp(argv[1], "-height") == 0 ) {
...@@ -284,7 +298,7 @@ int main(int argc, char *argv[]) ...@@ -284,7 +298,7 @@ int main(int argc, char *argv[])
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -height option requires an argument\n"); "The -height option requires an argument\n");
exit(1); quit(1);
} }
} else } else
if ( strcmp(argv[1], "-bpp") == 0 ) { if ( strcmp(argv[1], "-bpp") == 0 ) {
...@@ -295,7 +309,7 @@ int main(int argc, char *argv[]) ...@@ -295,7 +309,7 @@ int main(int argc, char *argv[])
} else { } else {
fprintf(stderr, fprintf(stderr,
"The -bpp option requires an argument\n"); "The -bpp option requires an argument\n");
exit(1); quit(1);
} }
} else } else
if ( strcmp(argv[1], "-warp") == 0 ) { if ( strcmp(argv[1], "-warp") == 0 ) {
...@@ -321,19 +335,12 @@ int main(int argc, char *argv[]) ...@@ -321,19 +335,12 @@ int main(int argc, char *argv[])
break; break;
} }
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit); /* Clean up on exit */
/* Initialize the display */ /* Initialize the display */
screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags); screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
if ( screen == NULL ) { if ( screen == NULL ) {
fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n", fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
w, h, desired_bpp, SDL_GetError()); w, h, desired_bpp, SDL_GetError());
exit(1); quit(1);
} }
printf("Set%s %dx%dx%d mode\n", printf("Set%s %dx%dx%d mode\n",
screen->flags & SDL_FULLSCREEN ? " fullscreen" : "", screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
...@@ -358,5 +365,6 @@ int main(int argc, char *argv[]) ...@@ -358,5 +365,6 @@ int main(int argc, char *argv[])
DrawPict(screen, argv[1], speedy, flip, nofade); DrawPict(screen, argv[1], speedy, flip, nofade);
#endif #endif
SDL_Delay(delay*1000); SDL_Delay(delay*1000);
SDL_Quit();
return(0); return(0);
} }
...@@ -13,6 +13,13 @@ static int visible = 1; ...@@ -13,6 +13,13 @@ static int visible = 1;
static Uint8 video_bpp; static Uint8 video_bpp;
static Uint32 video_flags; static Uint32 video_flags;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
int SetVideoMode(int w, int h) int SetVideoMode(int w, int h)
{ {
SDL_Surface *screen; SDL_Surface *screen;
...@@ -264,9 +271,8 @@ int main(int argc, char *argv[]) ...@@ -264,9 +271,8 @@ int main(int argc, char *argv[])
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError()); "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
/* Check command line arguments */ /* Check command line arguments */
w = 640; w = 640;
...@@ -333,7 +339,7 @@ int main(int argc, char *argv[]) ...@@ -333,7 +339,7 @@ int main(int argc, char *argv[])
/* Initialize the display */ /* Initialize the display */
if ( SetVideoMode(w, h) < 0 ) { if ( SetVideoMode(w, h) < 0 ) {
return(1); quit(1);
} }
/* Set an event filter that discards everything but QUIT */ /* Set an event filter that discards everything but QUIT */
...@@ -355,7 +361,7 @@ int main(int argc, char *argv[]) ...@@ -355,7 +361,7 @@ int main(int argc, char *argv[])
/* Fall through to the quit handler */ /* Fall through to the quit handler */
case SDL_QUIT: case SDL_QUIT:
printf("Bye bye..\n"); printf("Bye bye..\n");
return(0); quit(0);
default: default:
/* This should never happen */ /* This should never happen */
printf("Warning: Event %d wasn't filtered\n", printf("Warning: Event %d wasn't filtered\n",
...@@ -364,5 +370,6 @@ int main(int argc, char *argv[]) ...@@ -364,5 +370,6 @@ int main(int argc, char *argv[])
} }
} }
printf("SDL_WaitEvent() error: %s\n", SDL_GetError()); printf("SDL_WaitEvent() error: %s\n", SDL_GetError());
SDL_Quit();
return(255); return(255);
} }
...@@ -14,6 +14,13 @@ static int done = 0; ...@@ -14,6 +14,13 @@ static int done = 0;
/* Is the cursor visible? */ /* Is the cursor visible? */
static int visible = 1; static int visible = 1;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp) SDL_Surface *LoadIconSurface(char *file, Uint8 **maskp)
{ {
SDL_Surface *icon; SDL_Surface *icon;
...@@ -260,9 +267,8 @@ int main(int argc, char *argv[]) ...@@ -260,9 +267,8 @@ int main(int argc, char *argv[])
if ( SDL_Init(init_flags) < 0 ) { if ( SDL_Init(init_flags) < 0 ) {
fprintf(stderr, fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError()); "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
/* Set the icon -- this must be done before the first mode set */ /* Set the icon -- this must be done before the first mode set */
icon = LoadIconSurface("icon.bmp", &icon_mask); icon = LoadIconSurface("icon.bmp", &icon_mask);
...@@ -277,7 +283,7 @@ int main(int argc, char *argv[]) ...@@ -277,7 +283,7 @@ int main(int argc, char *argv[])
if ( screen == NULL ) { if ( screen == NULL ) {
fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n", fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
video_bpp, SDL_GetError()); video_bpp, SDL_GetError());
exit(1); quit(1);
} }
printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ? printf("Running in %s mode\n", screen->flags & SDL_FULLSCREEN ?
"fullscreen" : "windowed"); "fullscreen" : "windowed");
...@@ -302,7 +308,7 @@ int main(int argc, char *argv[]) ...@@ -302,7 +308,7 @@ int main(int argc, char *argv[])
if ( SDL_LockSurface(screen) < 0 ) { if ( SDL_LockSurface(screen) < 0 ) {
fprintf(stderr, "Couldn't lock display surface: %s\n", fprintf(stderr, "Couldn't lock display surface: %s\n",
SDL_GetError()); SDL_GetError());
exit(2); quit(2);
} }
buffer = (Uint8 *)screen->pixels; buffer = (Uint8 *)screen->pixels;
for ( i=0; i<screen->h; ++i ) { for ( i=0; i<screen->h; ++i ) {
...@@ -326,5 +332,6 @@ int main(int argc, char *argv[]) ...@@ -326,5 +332,6 @@ int main(int argc, char *argv[])
} }
SDL_WaitThread(mouse_thread, NULL); SDL_WaitThread(mouse_thread, NULL);
SDL_WaitThread(keybd_thread, NULL); SDL_WaitThread(keybd_thread, NULL);
SDL_Quit();
return(0); return(0);
} }
...@@ -13,6 +13,13 @@ ...@@ -13,6 +13,13 @@
static char volatile time_for_threads_to_die[NUMTHREADS]; static char volatile time_for_threads_to_die[NUMTHREADS];
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void quit(int rc)
{
SDL_Quit();
exit(rc);
}
int SubThreadFunc(void *data) { int SubThreadFunc(void *data) {
while(! *(int volatile *)data) { while(! *(int volatile *)data) {
; /*SDL_Delay(10); /* do nothing */ ; /*SDL_Delay(10); /* do nothing */
...@@ -57,10 +64,8 @@ int main(int argc, char *argv[]) ...@@ -57,10 +64,8 @@ int main(int argc, char *argv[])
/* Load the SDL library */ /* Load the SDL library */
if ( SDL_Init(0) < 0 ) { if ( SDL_Init(0) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1); return(1);
} }
atexit(SDL_Quit);
signal(SIGSEGV, SIG_DFL); signal(SIGSEGV, SIG_DFL);
for(i = 0; i < NUMTHREADS; i++) { for(i = 0; i < NUMTHREADS; i++) {
...@@ -70,7 +75,7 @@ int main(int argc, char *argv[]) ...@@ -70,7 +75,7 @@ int main(int argc, char *argv[])
if ( threads[i] == NULL ) { if ( threads[i] == NULL ) {
fprintf(stderr, fprintf(stderr,
"Couldn't create thread: %s\n", SDL_GetError()); "Couldn't create thread: %s\n", SDL_GetError());
exit(1); quit(1);
} }
} }
...@@ -81,5 +86,6 @@ int main(int argc, char *argv[]) ...@@ -81,5 +86,6 @@ int main(int argc, char *argv[])
for(i = NUMTHREADS-1; i >= 0; --i) { for(i = NUMTHREADS-1; i >= 0; --i) {
SDL_WaitThread(threads[i], NULL); SDL_WaitThread(threads[i], NULL);
} }
return(0); /* Never reached */ SDL_Quit();
return(0);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment