Browse Source

Remove atexit() calls and implement a diablo_deinit() function instea… (#694)

* Remove atexit() calls and implement a diablo_deinit() function instead that cleans up every subsystem if it has been init before.
pull/702/head
Manuel Alfayate Corchete 6 years ago committed by GitHub
parent
commit
03662be548
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      Source/appfat.cpp
  2. 44
      Source/diablo.cpp
  3. 4
      Source/diablo.h
  4. 4
      Source/init.cpp
  5. 2
      Source/init.h
  6. 2
      Source/movie.cpp
  7. 2
      SourceX/DiabloUI/diabloui.cpp
  8. 10
      SourceX/DiabloUI/fonts.cpp
  9. 1
      SourceX/DiabloUI/fonts.h
  10. 6
      SourceX/display.cpp
  11. 3
      SourceX/dx.cpp

2
Source/appfat.cpp

@ -27,7 +27,7 @@ void app_fatal(const char *pszFmt, ...)
va_end(va);
exit(1);
diablo_quit(1);
}
void MsgBox(const char *pszFmt, va_list va)

44
Source/diablo.cpp

@ -80,6 +80,12 @@ char *spszMsgTbl[4] = {
/** INI files variable names for quick message keys */
char *spszMsgHotKeyTbl[4] = { "F9", "F10", "F11", "F12" };
/** To know if these things have been done when we get to the diablo_deinit() function */
BOOL was_archives_init = false;
BOOL was_ui_init = false;
BOOL was_snd_init = false;
BOOL was_sfx_init = false;
void FreeGameMem()
{
music_stop();
@ -270,13 +276,13 @@ void diablo_init()
SFileEnableDirectAccess(TRUE);
init_archives();
atexit(init_cleanup);
was_archives_init = true;
UiInitialize();
#ifdef SPAWN
UiSetSpawned(TRUE);
#endif
atexit(UiDestroy);
was_ui_init = true;
ReadOnlyTest();
@ -285,9 +291,10 @@ void diablo_init()
diablo_init_screen();
snd_init(NULL);
atexit(sound_cleanup);
was_snd_init = true;
sound_init();
atexit(effects_cleanup_sfx);
was_sfx_init = true;
}
void diablo_splash()
@ -305,12 +312,37 @@ void diablo_splash()
UiTitleDialog();
}
void diablo_deinit()
{
if (was_sfx_init)
effects_cleanup_sfx();
if (was_snd_init)
sound_cleanup();
if (was_ui_init)
UiDestroy();
if (was_archives_init)
init_cleanup();
if (was_window_init)
dx_cleanup(); // Cleanup SDL surfaces stuff, so we have to do it before SDL_Quit().
if (was_fonts_init)
FontsCleanup();
if (SDL_WasInit(SDL_INIT_EVERYTHING & ~SDL_INIT_HAPTIC))
SDL_Quit();
}
void diablo_quit(int exitStatus)
{
diablo_deinit();
exit(exitStatus);
}
int DiabloMain(int argc, char **argv)
{
diablo_parse_flags(argc, argv);
diablo_init();
diablo_splash();
mainmenu_loop();
diablo_deinit();
return 0;
}
@ -341,7 +373,7 @@ static void print_help_and_exit()
printf(" %-20s %-30s\n", "-t <##>", "Set current quest level");
#endif
printf("\nReport bugs at https://github.com/diasurgical/devilutionX/\n");
exit(0);
diablo_quit(0);
}
void diablo_parse_flags(int argc, char **argv)
@ -351,7 +383,7 @@ void diablo_parse_flags(int argc, char **argv)
print_help_and_exit();
} else if (strcasecmp("--version", argv[i]) == 0) {
printf("%s v%s\n", PROJECT_NAME, PROJECT_VERSION);
exit(0);
diablo_quit(0);
} else if (strcasecmp("--data-dir", argv[i]) == 0) {
basePath = argv[++i];
#ifdef _WIN32

4
Source/diablo.h

@ -26,6 +26,9 @@ extern int DebugMonsters[10];
extern BOOLEAN cineflag;
extern int force_redraw;
extern BOOL visiondebug;
/* These are defined in fonts.h */
extern BOOL was_fonts_init;
extern void FontsCleanup();
/** unused */
extern BOOL scrollflag;
extern BOOL light4flag;
@ -48,6 +51,7 @@ int DiabloMain(int argc, char **argv);
void diablo_parse_flags(int argc, char **argv);
void diablo_init_screen();
void diablo_reload_process(HINSTANCE hInstance);
void diablo_quit(int exitStatus);
BOOL PressEscKey();
LRESULT DisableInputWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT GM_Game(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

4
Source/init.cpp

@ -52,7 +52,7 @@ void init_create_window()
if (!SpawnWindow(PROJECT_NAME, SCREEN_WIDTH, SCREEN_HEIGHT))
app_fatal("Unable to create main window");
dx_init(NULL);
atexit(dx_cleanup);
was_window_init = true;
gbActive = true;
gpBufStart = &gpBuffer[BUFFER_WIDTH * SCREEN_Y];
gpBufEnd = (BYTE *)(BUFFER_WIDTH * (SCREEN_HEIGHT + SCREEN_Y));
@ -132,7 +132,7 @@ LRESULT MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
case WM_QUERYNEWPALETTE:
return 1;
case WM_QUERYENDSESSION:
exit(0);
diablo_quit(0);
}
return 0;

2
Source/init.h

@ -27,6 +27,8 @@ LRESULT MainWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
void init_activate_window(HWND hWnd, BOOL bActive);
WNDPROC SetWindowProc(WNDPROC NewProc);
extern BOOL was_window_init; /** defined in dx.cpp */
/* rdata */
/* data */

2
Source/movie.cpp

@ -36,7 +36,7 @@ void play_movie(char *pszMovie, BOOL user_can_close)
break;
case WM_QUIT:
SVidPlayEnd(video_stream);
exit(0);
diablo_quit(0);
break;
}
}

2
SourceX/DiabloUI/diabloui.cpp

@ -328,7 +328,7 @@ void UiHandleEvents(SDL_Event *event)
}
if (event->type == SDL_QUIT)
exit(0);
diablo_quit(0);
#ifndef USE_SDL1
if (event->type == SDL_JOYDEVICEADDED || event->type == SDL_JOYDEVICEREMOVED) {

10
SourceX/DiabloUI/fonts.cpp

@ -5,6 +5,8 @@ namespace dvl {
TTF_Font *font = nullptr;
BYTE *FontTables[4];
Art ArtFonts[4][2];
/** This is so we know ttf has been init when we get to the diablo_deinit() function */
BOOL was_fonts_init = false;
namespace {
@ -53,9 +55,9 @@ void LoadTtfFont() {
if (!TTF_WasInit()) {
if (TTF_Init() == -1) {
SDL_Log("TTF_Init: %s", TTF_GetError());
exit(1);
diablo_quit(1);
}
atexit(TTF_Quit);
was_fonts_init = true;
}
font = TTF_OpenFont(TTF_FONT_PATH, 17);
@ -74,4 +76,8 @@ void UnloadTtfFont() {
font = nullptr;
}
void FontsCleanup() {
TTF_Quit();
}
} // namespace dvl

1
SourceX/DiabloUI/fonts.h

@ -33,5 +33,6 @@ void UnloadArtFonts();
void LoadTtfFont();
void UnloadTtfFont();
void FontsCleanup();
} // namespace dvl

6
SourceX/display.cpp

@ -19,7 +19,9 @@
namespace dvl {
extern SDL_Surface *renderer_texture_surface; // defined in dx.cpp
extern BOOL was_window_init; /** defined in dx.cpp */
extern SDL_Surface *renderer_texture_surface; /** defined in dx.cpp */
#ifdef USE_SDL1
void SetVideoMode(int width, int height, int bpp, std::uint32_t flags) {
@ -51,8 +53,6 @@ bool SpawnWindow(const char *lpWindowName, int nWidth, int nHeight)
ErrSdl();
}
atexit(SDL_Quit);
#ifdef USE_SDL1
SDL_EnableUNICODE(1);
#endif

3
SourceX/dx.cpp

@ -32,6 +32,9 @@ SDL_Surface *renderer_texture_surface = nullptr;
/** 8-bit surface wrapper around #gpBuffer */
SDL_Surface *pal_surface;
/** To know if surfaces have been initialized or not */
BOOL was_window_init = false;
static void dx_create_back_buffer()
{
pal_surface = SDL_CreateRGBSurfaceWithFormat(0, BUFFER_WIDTH, BUFFER_HEIGHT, 8, SDL_PIXELFORMAT_INDEX8);

Loading…
Cancel
Save