Revisit initialisation to drive it all from the FrameBase.

Signed-off-by: Andrea Odetti <mariofutire@gmail.com>
This commit is contained in:
Andrea Odetti 2021-03-27 18:47:40 +00:00
parent 8dc4c042a2
commit 11c44233b4
12 changed files with 51 additions and 51 deletions

View file

@ -1,5 +1,6 @@
#include "StdAfx.h" #include "StdAfx.h"
#include "frontends/common2/commonframe.h" #include "frontends/common2/commonframe.h"
#include "frontends/common2/utils.h"
#include "linux/resources.h" #include "linux/resources.h"
#include <sys/stat.h> #include <sys/stat.h>
@ -68,11 +69,17 @@ namespace common2
{ {
} }
void CommonFrame::Initialize()
{
InitialiseEmulator();
LinuxFrame::Initialize();
}
void CommonFrame::Destroy() void CommonFrame::Destroy()
{ {
LinuxFrame::Destroy(); LinuxFrame::Destroy();
myResource.clear(); myResource.clear();
DestroyEmulator();
} }
BYTE* CommonFrame::GetResource(WORD id, LPCSTR lpType, DWORD expectedSize) BYTE* CommonFrame::GetResource(WORD id, LPCSTR lpType, DWORD expectedSize)

View file

@ -12,6 +12,7 @@ namespace common2
public: public:
CommonFrame(); CommonFrame();
void Initialize() override;
void Destroy() override; void Destroy() override;
BYTE* GetResource(WORD id, LPCSTR lpType, DWORD expectedSize) override; BYTE* GetResource(WORD id, LPCSTR lpType, DWORD expectedSize) override;

View file

@ -59,7 +59,7 @@ namespace common2
} }
} }
Initialisation::Initialisation() void InitialiseEmulator()
{ {
#ifdef RIFF_SPKR #ifdef RIFF_SPKR
RiffInitWriteFile("/tmp/Spkr.wav", SPKR_SAMPLE_RATE, 1); RiffInitWriteFile("/tmp/Spkr.wav", SPKR_SAMPLE_RATE, 1);
@ -83,7 +83,6 @@ namespace common2
SpkrInitialize(); SpkrInitialize();
MemInitialize(); MemInitialize();
GetFrame().Initialize();
GetCardMgr().GetDisk2CardMgr().Reset(); GetCardMgr().GetDisk2CardMgr().Reset();
HD_Reset(); HD_Reset();
@ -92,7 +91,7 @@ namespace common2
DebugInitialize(); DebugInitialize();
} }
Initialisation::~Initialisation() void DestroyEmulator()
{ {
Snapshot_Shutdown(); Snapshot_Shutdown();
CMouseInterface* pMouseCard = GetCardMgr().GetMouseCard(); CMouseInterface* pMouseCard = GetCardMgr().GetMouseCard();
@ -101,7 +100,6 @@ namespace common2
pMouseCard->Reset(); pMouseCard->Reset();
} }
MemDestroy(); MemDestroy();
GetFrame().Destroy();
SpkrDestroy(); SpkrDestroy();
MB_Destroy(); MB_Destroy();

View file

@ -7,11 +7,8 @@ namespace common2
void setSnapshotFilename(const std::string & filename, const bool load); void setSnapshotFilename(const std::string & filename, const bool load);
class Initialisation // Do not call directly. Used in CommonFrame
{ void InitialiseEmulator();
public: void DestroyEmulator();
Initialisation();
~Initialisation();
};
} }

View file

@ -57,11 +57,10 @@ namespace ra2
Game::Game(const std::shared_ptr<RetroFrame> & frame) Game::Game(const std::shared_ptr<RetroFrame> & frame)
: myFrame(frame), mySpeed(true), myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1) : myFrame(frame), mySpeed(true), myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1)
{ {
SetFrame(myFrame);
LogInit(); LogInit();
InitialiseRetroRegistry(); InitialiseRetroRegistry();
SetFrame(myFrame);
myInit.reset(new common2::Initialisation); myFrame->Initialize();
switch (ourInputDevices[0]) switch (ourInputDevices[0])
{ {
@ -83,7 +82,7 @@ namespace ra2
Game::~Game() Game::~Game()
{ {
myInit.reset(); myFrame->Destroy();
SetFrame(std::shared_ptr<FrameBase>()); SetFrame(std::shared_ptr<FrameBase>());
Paddle::instance.reset(); Paddle::instance.reset();
Registry::instance.reset(); Registry::instance.reset();

View file

@ -6,11 +6,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace common2
{
class Initialisation;
}
namespace ra2 namespace ra2
{ {
@ -44,8 +39,6 @@ namespace ra2
std::vector<int> myButtonStates; std::vector<int> myButtonStates;
std::shared_ptr<common2::Initialisation> myInit;
bool checkButtonPressed(unsigned id); bool checkButtonPressed(unsigned id);
void keyboardEmulation(); void keyboardEmulation();

View file

@ -147,21 +147,17 @@ namespace
if (!run) if (!run)
return 1; return 1;
std::shared_ptr<na2::NFrame> 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) if (options.log)
{ {
LogInit(); LogInit();
} }
InitializeFileRegistry(options); InitializeFileRegistry(options);
g_nMemoryClearType = options.memclear; g_nMemoryClearType = options.memclear;
common2::Initialisation init; std::shared_ptr<na2::NFrame> frame(new na2::NFrame(options.paddleDeviceName));
Initialisation init(frame);
na2::SetCtrlCHandler(options.headless); na2::SetCtrlCHandler(options.headless);
applyOptions(options); applyOptions(options);

View file

@ -50,7 +50,7 @@ namespace na2
void NFrame::Initialize() void NFrame::Initialize()
{ {
LinuxFrame::Initialize(); CommonFrame::Initialize();
myTextFlashCounter = 0; myTextFlashCounter = 0;
myTextFlashState = 0; myTextFlashState = 0;
myAsciiArt.reset(new ASCIIArt()); myAsciiArt.reset(new ASCIIArt());
@ -60,7 +60,7 @@ namespace na2
void NFrame::Destroy() void NFrame::Destroy()
{ {
LinuxFrame::Destroy(); CommonFrame::Destroy();
myTextFlashCounter = 0; myTextFlashCounter = 0;
myTextFlashState = 0; myTextFlashState = 0;
myFrame.reset(); myFrame.reset();
@ -359,11 +359,6 @@ namespace na2
return true; return true;
} }
void NFrame::Cleanup()
{
GetFrame().Destroy();
}
int NFrame::FrameMessageBox(LPCSTR lpText, LPCSTR lpCaption, UINT uType) int NFrame::FrameMessageBox(LPCSTR lpText, LPCSTR lpCaption, UINT uType)
{ {
LogFileOutput("MessageBox:\n%s\n%s\n\n", lpCaption, lpText); LogFileOutput("MessageBox:\n%s\n%s\n\n", lpCaption, lpText);

View file

@ -33,8 +33,6 @@ namespace na2
void Init(int rows, int columns); void Init(int rows, int columns);
static void Cleanup();
private: private:
const std::string myPaddleDevice; const std::string myPaddleDevice;

View file

@ -90,6 +90,16 @@ void run_sdl(int argc, const char * argv [])
if (!run) if (!run)
return; return;
InitializeFileRegistry(options);
if (options.log)
{
LogInit();
}
g_nMemoryClearType = options.memclear;
Paddle::instance.reset(new sa2::Gamepad(0));
std::shared_ptr<sa2::SDLFrame> frame; std::shared_ptr<sa2::SDLFrame> frame;
if (options.imgui) if (options.imgui)
@ -106,20 +116,8 @@ void run_sdl(int argc, const char * argv [])
throw std::runtime_error(SDL_GetError()); 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); applyOptions(options);
const int fps = getRefreshRate(); 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). // this must happen BEFORE the SDL_Quit() as otherwise we have a double free (of the game controller).
Paddle::instance.reset(); Paddle::instance.reset();
SetFrame(std::shared_ptr<FrameBase>());
SDL_Quit(); SDL_Quit();
return exit; return exit;

View file

@ -32,3 +32,15 @@ Video& GetVideo()
static Video sg_Video; static Video sg_Video;
return sg_Video; return sg_Video;
} }
Initialisation::Initialisation(const std::shared_ptr<FrameBase> & frame)
{
SetFrame(frame);
frame->Initialize();
}
Initialisation::~Initialisation()
{
GetFrame().Destroy();
SetFrame(std::shared_ptr<FrameBase>());
}

View file

@ -5,3 +5,10 @@
class FrameBase; class FrameBase;
void SetFrame(const std::shared_ptr<FrameBase> & frame); void SetFrame(const std::shared_ptr<FrameBase> & frame);
class Initialisation
{
public:
Initialisation(const std::shared_ptr<FrameBase> & frame);
~Initialisation();
};