diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9caa9c6665..0bb16f39ee 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1298,6 +1298,7 @@ set (PCH_SOURCES utility/nodebuilder/nodebuild_utility.cpp utility/sc_man.cpp utility/stats.cpp + utility/atterm.cpp utility/cmdlib.cpp utility/colormatcher.cpp utility/configfile.cpp diff --git a/src/d_main.cpp b/src/d_main.cpp index f9db5fe0ed..12504081ca 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -102,6 +102,7 @@ #include "i_system.h" #include "g_cvars.h" #include "r_data/r_vanillatrans.h" +#include "atterm.h" EXTERN_CVAR(Bool, hud_althud) EXTERN_CVAR(Int, vr_mode) diff --git a/src/dobjtype.cpp b/src/dobjtype.cpp index c0c8e24323..d7ea265bc9 100644 --- a/src/dobjtype.cpp +++ b/src/dobjtype.cpp @@ -50,6 +50,7 @@ #include "types.h" #include "scriptutil.h" #include "i_system.h" +#include "atterm.h" // MACROS ------------------------------------------------------------------ diff --git a/src/doomtype.h b/src/doomtype.h index 47a636552f..b361bf77c4 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -302,6 +302,11 @@ inline float DEG2RAD(float deg) return deg * float(M_PI / 180.0); } +inline double DEG2RAD(double deg) +{ + return deg * (M_PI / 180.0); +} + inline float RAD2DEG(float deg) { return deg * float(180. / M_PI); diff --git a/src/g_statusbar/sbarinfo.cpp b/src/g_statusbar/sbarinfo.cpp index a2d1701fd8..f8929c9989 100644 --- a/src/g_statusbar/sbarinfo.cpp +++ b/src/g_statusbar/sbarinfo.cpp @@ -49,6 +49,7 @@ #include "vm.h" #include "i_system.h" #include "utf8.h" +#include "atterm.h" #define ARTIFLASH_OFFSET (statusBar->invBarOffset+6) enum diff --git a/src/gamedata/g_mapinfo.cpp b/src/gamedata/g_mapinfo.cpp index 9a0d04c9ac..42e06e2ba9 100644 --- a/src/gamedata/g_mapinfo.cpp +++ b/src/gamedata/g_mapinfo.cpp @@ -50,6 +50,7 @@ #include "g_levellocals.h" #include "events.h" #include "i_system.h" +#include "atterm.h" static TArray wadclusterinfos; TArray wadlevelinfos; diff --git a/src/i_net.cpp b/src/i_net.cpp index 4cbf1df23e..d2b17efb17 100644 --- a/src/i_net.cpp +++ b/src/i_net.cpp @@ -62,6 +62,7 @@ #include "st_start.h" #include "m_misc.h" #include "doomerrors.h" +#include "atterm.h" #include "i_net.h" diff --git a/src/m_misc.cpp b/src/m_misc.cpp index 42938fb809..cb980bf730 100644 --- a/src/m_misc.cpp +++ b/src/m_misc.cpp @@ -66,6 +66,7 @@ #include "gameconfigfile.h" #include "gstrings.h" +#include "atterm.h" FGameConfigFile *GameConfig; diff --git a/src/menu/menudef.cpp b/src/menu/menudef.cpp index 5eca78b010..a3bc152592 100644 --- a/src/menu/menudef.cpp +++ b/src/menu/menudef.cpp @@ -50,6 +50,7 @@ #include "gstrings.h" #include "teaminfo.h" #include "r_data/sprites.h" +#include "atterm.h" void ClearSaveGames(); diff --git a/src/p_setup.cpp b/src/p_setup.cpp index 2842587b40..7d92b794c6 100644 --- a/src/p_setup.cpp +++ b/src/p_setup.cpp @@ -75,6 +75,7 @@ #include "i_system.h" #include "v_video.h" #include "fragglescript/t_script.h" +#include "atterm.h" extern AActor *SpawnMapThing (int index, FMapThing *mthing, int position); diff --git a/src/posix/cocoa/i_joystick.cpp b/src/posix/cocoa/i_joystick.cpp index a34c8b03d2..189845c4aa 100644 --- a/src/posix/cocoa/i_joystick.cpp +++ b/src/posix/cocoa/i_joystick.cpp @@ -42,6 +42,7 @@ #include "m_joy.h" #include "templates.h" #include "v_text.h" +#include "atterm.h" EXTERN_CVAR(Bool, joy_axespolling) diff --git a/src/posix/cocoa/i_main.mm b/src/posix/cocoa/i_main.mm index 7f823b628c..859fe675de 100644 --- a/src/posix/cocoa/i_main.mm +++ b/src/posix/cocoa/i_main.mm @@ -33,6 +33,7 @@ #include "i_common.h" #include "s_sound.h" +#include "atterm.h" #include @@ -64,57 +65,6 @@ EXTERN_CVAR(Bool, vid_vsync ) namespace { -// The maximum number of functions that can be registered with atterm. -const size_t MAX_TERMS = 64; - -void (*TermFuncs[MAX_TERMS])(); -const char *TermNames[MAX_TERMS]; -size_t NumTerms; - -} // unnamed namespace - -// Expose this for i_main_except.cpp -void call_terms() -{ - while (NumTerms > 0) - { - TermFuncs[--NumTerms](); - } -} - - -void addterm(void (*func)(), const char *name) -{ - // Make sure this function wasn't already registered. - - for (size_t i = 0; i < NumTerms; ++i) - { - if (TermFuncs[i] == func) - { - return; - } - } - - if (NumTerms == MAX_TERMS) - { - func(); - I_FatalError("Too many exit functions registered."); - } - - TermNames[NumTerms] = name; - TermFuncs[NumTerms] = func; - - ++NumTerms; -} - -void popterm() -{ - if (NumTerms) - { - --NumTerms; - } -} - void Mac_I_FatalError(const char* const message) { diff --git a/src/posix/cocoa/i_main_except.cpp b/src/posix/cocoa/i_main_except.cpp index afb0ba7e41..e67914a0e3 100644 --- a/src/posix/cocoa/i_main_except.cpp +++ b/src/posix/cocoa/i_main_except.cpp @@ -36,9 +36,9 @@ #include "doomerrors.h" #include "vm.h" +#include "atterm.h" // Import some functions from i_main.mm -void call_terms(); void Mac_I_FatalError(const char* const message); void OriginalMainTry(int argc, char** argv); diff --git a/src/posix/cocoa/i_system.mm b/src/posix/cocoa/i_system.mm index 04a6455409..ab17268e4d 100644 --- a/src/posix/cocoa/i_system.mm +++ b/src/posix/cocoa/i_system.mm @@ -48,6 +48,7 @@ #include "v_text.h" #include "x86.h" #include "cmdlib.h" +#include "atterm.h" EXTERN_CVAR(String, language) diff --git a/src/posix/cocoa/i_video.mm b/src/posix/cocoa/i_video.mm index 36738055b2..8d6b203f20 100644 --- a/src/posix/cocoa/i_video.mm +++ b/src/posix/cocoa/i_video.mm @@ -54,6 +54,7 @@ #include "v_text.h" #include "version.h" #include "doomerrors.h" +#include "atterm.h" #include "gl/system/gl_framebuffer.h" #include "vulkan/system/vk_framebuffer.h" diff --git a/src/posix/i_system.h b/src/posix/i_system.h index e6cd95cf88..0ef217aa1a 100644 --- a/src/posix/i_system.h +++ b/src/posix/i_system.h @@ -89,10 +89,6 @@ void I_Quit (void); void I_Tactile (int on, int off, int total); -void addterm (void (*func)(void), const char *name); -#define atterm(t) addterm (t, #t) -void popterm (); - void I_DebugPrint (const char *cp); // Print a console string diff --git a/src/posix/sdl/hardware.cpp b/src/posix/sdl/hardware.cpp index e631fa3132..eb71e6e97b 100644 --- a/src/posix/sdl/hardware.cpp +++ b/src/posix/sdl/hardware.cpp @@ -43,6 +43,7 @@ #include "m_argv.h" #include "doomerrors.h" #include "swrenderer/r_swrenderer.h" +#include "atterm.h" IVideo *Video; diff --git a/src/posix/sdl/i_main.cpp b/src/posix/sdl/i_main.cpp index caddfcf870..5720942171 100644 --- a/src/posix/sdl/i_main.cpp +++ b/src/posix/sdl/i_main.cpp @@ -56,6 +56,7 @@ #include "doomerrors.h" #include "i_system.h" #include "g_game.h" +#include "atterm.h" // MACROS ------------------------------------------------------------------ @@ -89,47 +90,9 @@ FArgs *Args; // PRIVATE DATA DEFINITIONS ------------------------------------------------ -static void (*TermFuncs[MAX_TERMS]) (); -static const char *TermNames[MAX_TERMS]; -static int NumTerms; // CODE -------------------------------------------------------------------- -void addterm (void (*func) (), const char *name) -{ - // Make sure this function wasn't already registered. - for (int i = 0; i < NumTerms; ++i) - { - if (TermFuncs[i] == func) - { - return; - } - } - if (NumTerms == MAX_TERMS) - { - func (); - I_FatalError ( - "Too many exit functions registered.\n" - "Increase MAX_TERMS in i_main.cpp"); - } - TermNames[NumTerms] = name; - TermFuncs[NumTerms++] = func; -} - -void popterm () -{ - if (NumTerms) - NumTerms--; -} - -void call_terms () -{ - while (NumTerms > 0) - { -// printf ("term %d - %s\n", NumTerms, TermNames[NumTerms-1]); - TermFuncs[--NumTerms] (); - } -} static void NewFailure () { diff --git a/src/posix/sdl/i_system.cpp b/src/posix/sdl/i_system.cpp index 2b8fd91556..b59725e7bf 100644 --- a/src/posix/sdl/i_system.cpp +++ b/src/posix/sdl/i_system.cpp @@ -51,6 +51,7 @@ #include "d_net.h" #include "g_game.h" #include "c_dispatch.h" +#include "atterm.h" #include "gameconfigfile.h" diff --git a/src/posix/sdl/st_start.cpp b/src/posix/sdl/st_start.cpp index 38959f542d..fa8ebe209f 100644 --- a/src/posix/sdl/st_start.cpp +++ b/src/posix/sdl/st_start.cpp @@ -42,6 +42,7 @@ #include "doomdef.h" #include "i_system.h" #include "c_cvars.h" +#include "atterm.h" // MACROS ------------------------------------------------------------------ diff --git a/src/r_utility.cpp b/src/r_utility.cpp index c258d0df84..3bab7e0855 100644 --- a/src/r_utility.cpp +++ b/src/r_utility.cpp @@ -64,6 +64,7 @@ #include "actorinlines.h" #include "g_game.h" #include "i_system.h" +#include "atterm.h" // EXTERNAL DATA DECLARATIONS ---------------------------------------------- diff --git a/src/rendering/swrenderer/r_swcolormaps.cpp b/src/rendering/swrenderer/r_swcolormaps.cpp index 712107be8b..23dbf1281a 100644 --- a/src/rendering/swrenderer/r_swcolormaps.cpp +++ b/src/rendering/swrenderer/r_swcolormaps.cpp @@ -55,6 +55,7 @@ #include "templates.h" #include "r_utility.h" #include "r_renderer.h" +#include "atterm.h" #include FDynamicColormap NormalLight; diff --git a/src/s_advsound.cpp b/src/s_advsound.cpp index 054edd6fb4..b869473a2b 100644 --- a/src/s_advsound.cpp +++ b/src/s_advsound.cpp @@ -48,6 +48,7 @@ #include "r_data/sprites.h" #include "vm.h" #include "i_system.h" +#include "atterm.h" // MACROS ------------------------------------------------------------------ diff --git a/src/s_environment.cpp b/src/s_environment.cpp index 8bd9661c7d..cfc9e87b89 100644 --- a/src/s_environment.cpp +++ b/src/s_environment.cpp @@ -46,6 +46,7 @@ #include "vm.h" #include "dobject.h" #include "menu/menu.h" +#include "atterm.h" diff --git a/src/s_sound.cpp b/src/s_sound.cpp index 2198431721..762b1db07c 100644 --- a/src/s_sound.cpp +++ b/src/s_sound.cpp @@ -83,6 +83,7 @@ #include "g_levellocals.h" #include "vm.h" #include "g_game.h" +#include "atterm.h" // MACROS ------------------------------------------------------------------ diff --git a/src/utility/atterm.cpp b/src/utility/atterm.cpp new file mode 100644 index 0000000000..51270ec038 --- /dev/null +++ b/src/utility/atterm.cpp @@ -0,0 +1,97 @@ +/* +** attern.cpp +** Termination handling +** +**--------------------------------------------------------------------------- +** Copyright 1998-2007 Randy Heit +** Copyright 2019 Christoph Oelckers +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + + +#include +#include "tarray.h" +#include "atterm.h" + +static TArray> TermFuncs; + + +//========================================================================== +// +// atterm +// +// Our own atexit because atexit can be problematic under Linux, though I +// forget the circumstances that cause trouble. +// +//========================================================================== + +void addterm(void (*func)(), const char *name) +{ + // Make sure this function wasn't already registered. + + for (auto &term : TermFuncs) + { + if (term.first == func) + { + return; + } + } + TermFuncs.Push(std::make_pair(func, name)); +} + +//========================================================================== +// +// call_terms +// +//========================================================================== + +void call_terms() +{ + for(int i = TermFuncs.Size()-1; i >= 0; i--) + { + TermFuncs[i].first(); + } + TermFuncs.Clear(); +} + +//========================================================================== +// +// popterm +// +// Removes the most recently register atterm function. +// +//========================================================================== + +void popterm() +{ + if (TermFuncs.Size() > 0) + { + TermFuncs.Pop(); + } +} + diff --git a/src/utility/atterm.h b/src/utility/atterm.h new file mode 100644 index 0000000000..6c1a463c79 --- /dev/null +++ b/src/utility/atterm.h @@ -0,0 +1,6 @@ +#pragma once + +void addterm (void (*func)(void), const char *name); +#define atterm(t) addterm (t, #t) +void popterm (); +void call_terms(); diff --git a/src/v_video.cpp b/src/v_video.cpp index 9df3714779..645fc97192 100644 --- a/src/v_video.cpp +++ b/src/v_video.cpp @@ -69,6 +69,7 @@ #include "version.h" #include "g_levellocals.h" #include "am_map.h" +#include "atterm.h" EXTERN_CVAR(Int, menu_resolution_custom_width) EXTERN_CVAR(Int, menu_resolution_custom_height) diff --git a/src/win32/hardware.cpp b/src/win32/hardware.cpp index 0a5457eef4..2674d553ae 100644 --- a/src/win32/hardware.cpp +++ b/src/win32/hardware.cpp @@ -50,6 +50,7 @@ #include "doomerrors.h" #include "i_system.h" #include "swrenderer/r_swrenderer.h" +#include "atterm.h" EXTERN_CVAR(Int, vid_enablevulkan) diff --git a/src/win32/i_cd.cpp b/src/win32/i_cd.cpp index a127a13780..cd00fbcfc9 100644 --- a/src/win32/i_cd.cpp +++ b/src/win32/i_cd.cpp @@ -46,6 +46,7 @@ #include "i_cd.h" #include "helperthread.h" +#include "atterm.h" extern HWND Window; extern HINSTANCE g_hInst; diff --git a/src/win32/i_input.cpp b/src/win32/i_input.cpp index 7753566e9d..a6b2e39395 100644 --- a/src/win32/i_input.cpp +++ b/src/win32/i_input.cpp @@ -92,6 +92,7 @@ #include "doomerrors.h" #include "i_system.h" #include "g_levellocals.h" +#include "atterm.h" // Prototypes and declarations. #include "rawinput.h" diff --git a/src/win32/i_main.cpp b/src/win32/i_main.cpp index 4f42f7593b..0bfcc2e0c6 100644 --- a/src/win32/i_main.cpp +++ b/src/win32/i_main.cpp @@ -74,6 +74,7 @@ #include "vm.h" #include "i_system.h" #include "gstrings.h" +#include "atterm.h" #include "stats.h" #include "st_start.h" @@ -153,65 +154,9 @@ DYN_WIN32_SYM(SHGetKnownFolderPath); static const WCHAR WinClassName[] = WGAMENAME "MainWindow"; static HMODULE hwtsapi32; // handle to wtsapi32.dll -static void (*TermFuncs[MAX_TERMS])(void); -static int NumTerms; // CODE -------------------------------------------------------------------- -//========================================================================== -// -// atterm -// -// Our own atexit because atexit can be problematic under Linux, though I -// forget the circumstances that cause trouble. -// -//========================================================================== - -void atterm (void (*func)(void)) -{ - // Make sure this function wasn't already registered. - for (int i = 0; i < NumTerms; ++i) - { - if (TermFuncs[i] == func) - { - return; - } - } - if (NumTerms == MAX_TERMS) - { - func (); - I_FatalError ("Too many exit functions registered.\nIncrease MAX_TERMS in i_main.cpp"); - } - TermFuncs[NumTerms++] = func; -} - -//========================================================================== -// -// popterm -// -// Removes the most recently register atterm function. -// -//========================================================================== - -void popterm () -{ - if (NumTerms) - NumTerms--; -} - -//========================================================================== -// -// call_terms -// -//========================================================================== - -static void call_terms (void) -{ - while (NumTerms > 0) - { - TermFuncs[--NumTerms](); - } -} #ifdef _MSC_VER static int NewFailure (size_t size) diff --git a/src/win32/i_system.cpp b/src/win32/i_system.cpp index cc7727cd05..fc3ce169b2 100644 --- a/src/win32/i_system.cpp +++ b/src/win32/i_system.cpp @@ -84,6 +84,7 @@ #include "doomstat.h" #include "i_system.h" #include "textures/bitmap.h" +#include "atterm.h" #include "optwin32.h" diff --git a/src/win32/i_system.h b/src/win32/i_system.h index 5a5b663f98..4a28b7f91a 100644 --- a/src/win32/i_system.h +++ b/src/win32/i_system.h @@ -80,9 +80,6 @@ void I_Quit (void); void I_Tactile (int on, int off, int total); -void atterm (void (*func)(void)); -void popterm (); - // Set the mouse cursor. The texture must be 32x32. class FTexture; bool I_SetCursor(FTexture *cursor);