From 3a41ade9fab0556d0d025c0b0e81834436a4f2e8 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Mon, 8 Sep 2008 17:55:12 +0000 Subject: Remove i_system.c dependency on doom/ code and add a generic I_AtExit() API for scheduling functions to call on quit. Subversion-branch: /branches/raven-branch Subversion-revision: 1216 --- src/doom/d_main.c | 5 +++++ src/doom/d_net.c | 4 ++++ src/doom/s_sound.c | 2 ++ src/i_system.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++------- src/i_system.h | 6 ++++++ src/i_video.c | 4 ++++ 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/doom/d_main.c b/src/doom/d_main.c index a7fed952..c84783bc 100644 --- a/src/doom/d_main.c +++ b/src/doom/d_main.c @@ -950,6 +950,9 @@ void D_DoomMain (void) printf (DEH_String("M_LoadDefaults: Load system defaults.\n")); M_LoadDefaults (); // load before initing other systems + // Save configuration at exit. + I_AtExit(M_SaveDefaults, false); + printf (DEH_String("W_Init: Init WADfiles.\n")); D_AddFile(iwadfile); @@ -1193,6 +1196,8 @@ void D_DoomMain (void) } + I_AtExit((atexit_func_t) G_CheckDemoStatus, true); + // Generate the WAD hash table. Speed things up a bit. W_GenerateHashTable(); diff --git a/src/doom/d_net.c b/src/doom/d_net.c index 4e87a813..8f75fd06 100644 --- a/src/doom/d_net.c +++ b/src/doom/d_net.c @@ -237,6 +237,10 @@ void D_CheckNetGame (void) int i; int num_players; + // Call D_QuitNetGame on exit + + I_AtExit(D_QuitNetGame, true); + // default values for single player consoleplayer = 0; diff --git a/src/doom/s_sound.c b/src/doom/s_sound.c index 964ed27f..fa236970 100644 --- a/src/doom/s_sound.c +++ b/src/doom/s_sound.c @@ -147,6 +147,8 @@ void S_Init(int sfxVolume, int musicVolume) { S_sfx[i].lumpnum = S_sfx[i].usefulness = -1; } + + I_AtExit(S_Shutdown, true); } void S_Shutdown(void) diff --git a/src/i_system.c b/src/i_system.c index c3b3211e..a895a3d2 100644 --- a/src/i_system.c +++ b/src/i_system.c @@ -40,28 +40,45 @@ #include "deh_str.h" #include "doomtype.h" -#include "doomstat.h" #include "m_argv.h" #include "m_config.h" #include "m_misc.h" #include "i_joystick.h" #include "i_timer.h" #include "i_video.h" -#include "s_sound.h" - -#include "d_net.h" -#include "g_game.h" #include "i_system.h" #include "txt_main.h" - #include "w_wad.h" #include "z_zone.h" int mb_used = 16; int show_endoom = 1; +typedef struct atexit_listentry_s atexit_listentry_t; + +struct atexit_listentry_s +{ + atexit_func_t func; + boolean run_on_error; + atexit_listentry_t *next; +}; + +static atexit_listentry_t *exit_funcs = NULL; + +void I_AtExit(atexit_func_t func, boolean run_on_error) +{ + atexit_listentry_t *entry; + + entry = malloc(sizeof(*entry)); + + entry->func = func; + entry->run_on_error = run_on_error; + entry->next = exit_funcs; + exit_funcs = entry; +} + // Tactile feedback function, probably used for the Logitech Cyberman void I_Tactile(int on, int off, int total) @@ -178,6 +195,19 @@ void I_Endoom(void) void I_Quit (void) { + atexit_listentry_t *entry; + + // Run through all exit functions + + entry = exit_funcs; + + while (entry != NULL) + { + entry->func(); + entry = entry->next; + } + +/* D_QuitNetGame (); G_CheckDemoStatus(); S_Shutdown(); @@ -188,8 +218,9 @@ void I_Quit (void) } I_ShutdownGraphics(); + */ - if (show_endoom && !testcontrols && !screensaver_mode) + if (show_endoom && !screensaver_mode && !M_CheckParm("-testcontrols")) { I_Endoom(); } @@ -211,7 +242,8 @@ static boolean already_quitting = false; void I_Error (char *error, ...) { - va_list argptr; + va_list argptr; + atexit_listentry_t *entry; if (already_quitting) { @@ -233,6 +265,19 @@ void I_Error (char *error, ...) // Shutdown. Here might be other errors. + entry = exit_funcs; + + while (entry != NULL) + { + if (entry->run_on_error) + { + entry->func(); + } + + entry = entry->next; + } + + /* if (demorecording) { G_CheckDemoStatus(); @@ -241,6 +286,7 @@ void I_Error (char *error, ...) D_QuitNetGame (); I_ShutdownGraphics(); S_Shutdown(); + */ #ifdef _WIN32 // On Windows, pop up a dialog box with the error message. diff --git a/src/i_system.h b/src/i_system.h index a5e06a50..3f114fd0 100644 --- a/src/i_system.h +++ b/src/i_system.h @@ -32,6 +32,7 @@ #include "d_event.h" +typedef void (*atexit_func_t)(void); // Called by DoomMain. void I_Init (void); @@ -86,6 +87,11 @@ void I_Tactile (int on, int off, int total); void I_Error (char *error, ...); +// Schedule a function to be called when the program exits. +// If run_if_error is true, the function is called if the exit +// is due to an error (I_Error) + +void I_AtExit(atexit_func_t func, boolean run_if_error); #endif diff --git a/src/i_video.c b/src/i_video.c index 8b7f6892..25d6e4be 100644 --- a/src/i_video.c +++ b/src/i_video.c @@ -1638,5 +1638,9 @@ void I_InitGraphics(void) } initialised = true; + + // Call I_ShutdownGraphics on quit + + I_AtExit(I_ShutdownGraphics, true); } -- cgit v1.2.3