From 11c44233b47d72a72109d19c728bb5089786fe69 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Sat, 27 Mar 2021 18:47:40 +0000 Subject: [PATCH] Revisit initialisation to drive it all from the FrameBase. Signed-off-by: Andrea Odetti --- source/frontends/common2/commonframe.cpp | 7 +++++++ source/frontends/common2/commonframe.h | 1 + source/frontends/common2/utils.cpp | 6 ++---- source/frontends/common2/utils.h | 9 +++------ source/frontends/libretro/game.cpp | 7 +++---- source/frontends/libretro/game.h | 7 ------- source/frontends/ncurses/main.cpp | 10 +++------- source/frontends/ncurses/nframe.cpp | 9 ++------- source/frontends/ncurses/nframe.h | 2 -- source/frontends/sdl/main.cpp | 25 +++++++++++------------- source/linux/context.cpp | 12 ++++++++++++ source/linux/context.h | 7 +++++++ 12 files changed, 51 insertions(+), 51 deletions(-) diff --git a/source/frontends/common2/commonframe.cpp b/source/frontends/common2/commonframe.cpp index 1abddd09..9dc79e7f 100644 --- a/source/frontends/common2/commonframe.cpp +++ b/source/frontends/common2/commonframe.cpp @@ -1,5 +1,6 @@ #include "StdAfx.h" #include "frontends/common2/commonframe.h" +#include "frontends/common2/utils.h" #include "linux/resources.h" #include @@ -68,11 +69,17 @@ namespace common2 { } + void CommonFrame::Initialize() + { + InitialiseEmulator(); + LinuxFrame::Initialize(); + } void CommonFrame::Destroy() { LinuxFrame::Destroy(); myResource.clear(); + DestroyEmulator(); } BYTE* CommonFrame::GetResource(WORD id, LPCSTR lpType, DWORD expectedSize) diff --git a/source/frontends/common2/commonframe.h b/source/frontends/common2/commonframe.h index 67d30cba..810a944b 100644 --- a/source/frontends/common2/commonframe.h +++ b/source/frontends/common2/commonframe.h @@ -12,6 +12,7 @@ namespace common2 public: CommonFrame(); + void Initialize() override; void Destroy() override; BYTE* GetResource(WORD id, LPCSTR lpType, DWORD expectedSize) override; diff --git a/source/frontends/common2/utils.cpp b/source/frontends/common2/utils.cpp index e4d6f848..deabcfec 100644 --- a/source/frontends/common2/utils.cpp +++ b/source/frontends/common2/utils.cpp @@ -59,7 +59,7 @@ namespace common2 } } - Initialisation::Initialisation() + void InitialiseEmulator() { #ifdef RIFF_SPKR RiffInitWriteFile("/tmp/Spkr.wav", SPKR_SAMPLE_RATE, 1); @@ -83,7 +83,6 @@ namespace common2 SpkrInitialize(); MemInitialize(); - GetFrame().Initialize(); GetCardMgr().GetDisk2CardMgr().Reset(); HD_Reset(); @@ -92,7 +91,7 @@ namespace common2 DebugInitialize(); } - Initialisation::~Initialisation() + void DestroyEmulator() { Snapshot_Shutdown(); CMouseInterface* pMouseCard = GetCardMgr().GetMouseCard(); @@ -101,7 +100,6 @@ namespace common2 pMouseCard->Reset(); } MemDestroy(); - GetFrame().Destroy(); SpkrDestroy(); MB_Destroy(); diff --git a/source/frontends/common2/utils.h b/source/frontends/common2/utils.h index a4d121dd..9cf3d42c 100644 --- a/source/frontends/common2/utils.h +++ b/source/frontends/common2/utils.h @@ -7,11 +7,8 @@ namespace common2 void setSnapshotFilename(const std::string & filename, const bool load); - class Initialisation - { - public: - Initialisation(); - ~Initialisation(); - }; + // Do not call directly. Used in CommonFrame + void InitialiseEmulator(); + void DestroyEmulator(); } diff --git a/source/frontends/libretro/game.cpp b/source/frontends/libretro/game.cpp index 39ce9571..63672a92 100644 --- a/source/frontends/libretro/game.cpp +++ b/source/frontends/libretro/game.cpp @@ -57,11 +57,10 @@ namespace ra2 Game::Game(const std::shared_ptr & frame) : myFrame(frame), mySpeed(true), myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1) { - SetFrame(myFrame); LogInit(); InitialiseRetroRegistry(); - - myInit.reset(new common2::Initialisation); + SetFrame(myFrame); + myFrame->Initialize(); switch (ourInputDevices[0]) { @@ -83,7 +82,7 @@ namespace ra2 Game::~Game() { - myInit.reset(); + myFrame->Destroy(); SetFrame(std::shared_ptr()); Paddle::instance.reset(); Registry::instance.reset(); diff --git a/source/frontends/libretro/game.h b/source/frontends/libretro/game.h index 5ec7629f..386f7915 100644 --- a/source/frontends/libretro/game.h +++ b/source/frontends/libretro/game.h @@ -6,11 +6,6 @@ #include #include -namespace common2 -{ - class Initialisation; -} - namespace ra2 { @@ -44,8 +39,6 @@ namespace ra2 std::vector myButtonStates; - std::shared_ptr myInit; - bool checkButtonPressed(unsigned id); void keyboardEmulation(); diff --git a/source/frontends/ncurses/main.cpp b/source/frontends/ncurses/main.cpp index 75c2c194..1e5be06f 100644 --- a/source/frontends/ncurses/main.cpp +++ b/source/frontends/ncurses/main.cpp @@ -147,21 +147,17 @@ namespace if (!run) return 1; - std::shared_ptr frame(new na2::NFrame(options.paddleDeviceName)); - SetFrame(frame); - // does not seem to be a problem calling endwin() multiple times - std::atexit(na2::NFrame::Cleanup); - if (options.log) { LogInit(); } InitializeFileRegistry(options); - g_nMemoryClearType = options.memclear; - common2::Initialisation init; + std::shared_ptr frame(new na2::NFrame(options.paddleDeviceName)); + Initialisation init(frame); + na2::SetCtrlCHandler(options.headless); applyOptions(options); diff --git a/source/frontends/ncurses/nframe.cpp b/source/frontends/ncurses/nframe.cpp index 2441cf03..8ec1900a 100644 --- a/source/frontends/ncurses/nframe.cpp +++ b/source/frontends/ncurses/nframe.cpp @@ -50,7 +50,7 @@ namespace na2 void NFrame::Initialize() { - LinuxFrame::Initialize(); + CommonFrame::Initialize(); myTextFlashCounter = 0; myTextFlashState = 0; myAsciiArt.reset(new ASCIIArt()); @@ -60,7 +60,7 @@ namespace na2 void NFrame::Destroy() { - LinuxFrame::Destroy(); + CommonFrame::Destroy(); myTextFlashCounter = 0; myTextFlashState = 0; myFrame.reset(); @@ -359,11 +359,6 @@ namespace na2 return true; } - void NFrame::Cleanup() - { - GetFrame().Destroy(); - } - int NFrame::FrameMessageBox(LPCSTR lpText, LPCSTR lpCaption, UINT uType) { LogFileOutput("MessageBox:\n%s\n%s\n\n", lpCaption, lpText); diff --git a/source/frontends/ncurses/nframe.h b/source/frontends/ncurses/nframe.h index d3e26e9b..eab965b8 100644 --- a/source/frontends/ncurses/nframe.h +++ b/source/frontends/ncurses/nframe.h @@ -33,8 +33,6 @@ namespace na2 void Init(int rows, int columns); - static void Cleanup(); - private: const std::string myPaddleDevice; diff --git a/source/frontends/sdl/main.cpp b/source/frontends/sdl/main.cpp index 20816e29..41443048 100644 --- a/source/frontends/sdl/main.cpp +++ b/source/frontends/sdl/main.cpp @@ -90,6 +90,16 @@ void run_sdl(int argc, const char * argv []) if (!run) return; + InitializeFileRegistry(options); + + if (options.log) + { + LogInit(); + } + + g_nMemoryClearType = options.memclear; + Paddle::instance.reset(new sa2::Gamepad(0)); + std::shared_ptr frame; if (options.imgui) @@ -106,20 +116,8 @@ void run_sdl(int argc, const char * argv []) throw std::runtime_error(SDL_GetError()); } - SetFrame(frame); + Initialisation init(frame); - if (options.log) - { - LogInit(); - } - - InitializeFileRegistry(options); - - Paddle::instance.reset(new sa2::Gamepad(0)); - - g_nMemoryClearType = options.memclear; - - common2::Initialisation init; applyOptions(options); const int fps = getRefreshRate(); @@ -233,7 +231,6 @@ int main(int argc, const char * argv []) // this must happen BEFORE the SDL_Quit() as otherwise we have a double free (of the game controller). Paddle::instance.reset(); - SetFrame(std::shared_ptr()); SDL_Quit(); return exit; diff --git a/source/linux/context.cpp b/source/linux/context.cpp index d1fbf9fd..a9c5ab17 100644 --- a/source/linux/context.cpp +++ b/source/linux/context.cpp @@ -32,3 +32,15 @@ Video& GetVideo() static Video sg_Video; return sg_Video; } + +Initialisation::Initialisation(const std::shared_ptr & frame) +{ + SetFrame(frame); + frame->Initialize(); +} + +Initialisation::~Initialisation() +{ + GetFrame().Destroy(); + SetFrame(std::shared_ptr()); +} diff --git a/source/linux/context.h b/source/linux/context.h index 427b3ace..61ad6cd2 100644 --- a/source/linux/context.h +++ b/source/linux/context.h @@ -5,3 +5,10 @@ class FrameBase; void SetFrame(const std::shared_ptr & frame); + +class Initialisation +{ +public: + Initialisation(const std::shared_ptr & frame); + ~Initialisation(); +};