diff --git a/source/frontends/common2/fileregistry.cpp b/source/frontends/common2/fileregistry.cpp index f040cb78..6a70fbb0 100644 --- a/source/frontends/common2/fileregistry.cpp +++ b/source/frontends/common2/fileregistry.cpp @@ -60,7 +60,6 @@ namespace } else { - mySaveOnExit = false; LogFileOutput("Registry: configuration file '%s' not found\n", filename.c_str()); } } @@ -69,7 +68,14 @@ namespace { if (mySaveOnExit) { - boost::property_tree::ini_parser::write_ini(myFilename, myINI); + try + { + boost::property_tree::ini_parser::write_ini(myFilename, myINI); + } + catch(const std::exception& e) + { + LogFileOutput("Registry: cannot save settings to '%s': %s\n", myFilename.c_str(), e.what()); + } } } @@ -104,7 +110,7 @@ namespace common2 } } - void InitializeFileRegistry(const EmulatorOptions & options) + std::shared_ptr CreateFileRegistry(const EmulatorOptions & options) { const std::string homeDir = getHomeDir(); @@ -125,7 +131,7 @@ namespace common2 std::shared_ptr config(new Configuration(filename, saveOnExit)); config->addExtraOptions(options.registryOptions); - Registry::instance = config; + return config; } } diff --git a/source/frontends/common2/fileregistry.h b/source/frontends/common2/fileregistry.h index 4d51a665..4fc73bde 100644 --- a/source/frontends/common2/fileregistry.h +++ b/source/frontends/common2/fileregistry.h @@ -1,6 +1,9 @@ #pragma once #include +#include + +class Registry; namespace common2 { @@ -8,6 +11,6 @@ namespace common2 struct EmulatorOptions; std::string GetConfigFile(const std::string & filename); - void InitializeFileRegistry(const EmulatorOptions & options); + std::shared_ptr CreateFileRegistry(const EmulatorOptions & options); } diff --git a/source/frontends/common2/utils.cpp b/source/frontends/common2/utils.cpp index cb35072f..b64e6e4f 100644 --- a/source/frontends/common2/utils.cpp +++ b/source/frontends/common2/utils.cpp @@ -123,9 +123,9 @@ namespace common2 HD_Destroy(); PrintDestroy(); CpuDestroy(); + DebugDestroy(); GetCardMgr().GetDisk2CardMgr().Destroy(); - LogDone(); RiffFinishWriteFile(); } diff --git a/source/frontends/libretro/game.cpp b/source/frontends/libretro/game.cpp index 3f880525..b512d0dd 100644 --- a/source/frontends/libretro/game.cpp +++ b/source/frontends/libretro/game.cpp @@ -54,11 +54,13 @@ namespace ra2 unsigned Game::ourInputDevices[MAX_PADS] = {RETRO_DEVICE_NONE}; - Game::Game(const std::shared_ptr & frame) - : myFrame(frame), mySpeed(true), myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1) + Game::Game() + : myLogger(true) + , myFrame(new ra2::RetroFrame()) + , mySpeed(true) + , myButtonStates(RETRO_DEVICE_ID_JOYPAD_R3 + 1) { - LogInit(); - InitialiseRetroRegistry(); + Registry::instance = CreateRetroRegistry(); SetFrame(myFrame); myFrame->Initialize(); diff --git a/source/frontends/libretro/game.h b/source/frontends/libretro/game.h index 386f7915..7f831cee 100644 --- a/source/frontends/libretro/game.h +++ b/source/frontends/libretro/game.h @@ -3,6 +3,8 @@ #include "frontends/common2/speed.h" #include "frontends/libretro/environment.h" +#include "linux/context.h" + #include #include @@ -14,7 +16,7 @@ namespace ra2 class Game { public: - Game(const std::shared_ptr & frame); + Game(); ~Game(); bool loadGame(const std::string & path); @@ -33,6 +35,7 @@ namespace ra2 static retro_usec_t ourFrameTime; private: + const Logger myLogger; const std::shared_ptr myFrame; common2::Speed mySpeed; // fixed speed diff --git a/source/frontends/libretro/libretro.cpp b/source/frontends/libretro/libretro.cpp index 1d6e3d59..5a2fb8f1 100644 --- a/source/frontends/libretro/libretro.cpp +++ b/source/frontends/libretro/libretro.cpp @@ -14,7 +14,6 @@ #include "frontends/libretro/environment.h" #include "frontends/libretro/rdirectsound.h" #include "frontends/libretro/retroregistry.h" -#include "frontends/libretro/retroframe.h" namespace { @@ -180,8 +179,7 @@ bool retro_load_game(const retro_game_info *info) try { - std::shared_ptr frame(new ra2::RetroFrame()); - std::unique_ptr game(new ra2::Game(frame)); + std::unique_ptr game(new ra2::Game()); const std::string snapshotEnding = ".aws.yaml"; const std::string gamePath = info->path; diff --git a/source/frontends/libretro/retroregistry.cpp b/source/frontends/libretro/retroregistry.cpp index da3f989d..30b65fd9 100644 --- a/source/frontends/libretro/retroregistry.cpp +++ b/source/frontends/libretro/retroregistry.cpp @@ -133,7 +133,7 @@ namespace ra2 environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, retroVariables.data()); } - void InitialiseRetroRegistry() + std::shared_ptr CreateRetroRegistry() { const auto registry = std::make_shared(); @@ -158,7 +158,7 @@ namespace ra2 } } - Registry::instance = registry; + return registry; } } diff --git a/source/frontends/libretro/retroregistry.h b/source/frontends/libretro/retroregistry.h index 1e21d09d..64580295 100644 --- a/source/frontends/libretro/retroregistry.h +++ b/source/frontends/libretro/retroregistry.h @@ -1,9 +1,13 @@ #pragma once +#include + +class Registry; + namespace ra2 { void SetupRetroVariables(); - void InitialiseRetroRegistry(); + std::shared_ptr CreateRetroRegistry(); } diff --git a/source/frontends/ncurses/main.cpp b/source/frontends/ncurses/main.cpp index 8fdbf253..53a1c59b 100644 --- a/source/frontends/ncurses/main.cpp +++ b/source/frontends/ncurses/main.cpp @@ -24,6 +24,7 @@ #include "frontends/common2/utils.h" #include "frontends/ncurses/world.h" #include "frontends/ncurses/nframe.h" +#include "frontends/ncurses/evdevpaddle.h" namespace { @@ -132,7 +133,6 @@ namespace void EnterMessageLoop(const common2::EmulatorOptions & options, const std::shared_ptr & frame) { - LogFileTimeUntilFirstKeyReadReset(); while (ContinueExecution(options, frame)) { } @@ -146,16 +146,14 @@ namespace if (!run) return 1; - if (options.log) - { - LogInit(); - } - - InitializeFileRegistry(options); + const Logger logger(options.log); + const std::shared_ptr registry = CreateFileRegistry(options); g_nMemoryClearType = options.memclear; - std::shared_ptr frame(new na2::NFrame(options.paddleDeviceName)); - Initialisation init(frame); + const std::shared_ptr paddle(new na2::EvDevPaddle(options.paddleDeviceName)); + + const std::shared_ptr frame(new na2::NFrame(paddle)); + const Initialisation init(registry, frame, paddle); na2::SetCtrlCHandler(options.headless); applyOptions(options); diff --git a/source/frontends/ncurses/nframe.cpp b/source/frontends/ncurses/nframe.cpp index e3f0f24f..c2231a9f 100644 --- a/source/frontends/ncurses/nframe.cpp +++ b/source/frontends/ncurses/nframe.cpp @@ -3,7 +3,6 @@ #include "frontends/ncurses/colors.h" #include "frontends/ncurses/asciiart.h" #include "frontends/ncurses/evdevpaddle.h" - #include "Interface.h" #include "Memory.h" #include "Log.h" @@ -39,8 +38,8 @@ namespace na2 std::shared_ptr colors; }; - NFrame::NFrame(const std::string & paddleDevice) - : myPaddleDevice(paddleDevice) + NFrame::NFrame(const std::shared_ptr & paddle) + : myPaddle(paddle) , myRows(-1) , myColumns(-1) { @@ -54,8 +53,6 @@ namespace na2 myTextFlashCounter = 0; myTextFlashState = 0; myAsciiArt.reset(new ASCIIArt()); - myPaddle.reset(new EvDevPaddle(myPaddleDevice)); - Paddle::instance = myPaddle; } void NFrame::Destroy() @@ -67,9 +64,6 @@ namespace na2 myStatus.reset(); myAsciiArt.reset(); - myPaddle.reset(); - Paddle::instance.reset(); - myNCurses.reset(); } diff --git a/source/frontends/ncurses/nframe.h b/source/frontends/ncurses/nframe.h index eab965b8..fdfb912c 100644 --- a/source/frontends/ncurses/nframe.h +++ b/source/frontends/ncurses/nframe.h @@ -16,7 +16,7 @@ namespace na2 class NFrame : public common2::CommonFrame { public: - NFrame(const std::string & paddleDevice); + NFrame(const std::shared_ptr & paddle); WINDOW * GetWindow(); WINDOW * GetStatus(); @@ -35,7 +35,8 @@ namespace na2 private: - const std::string myPaddleDevice; + const std::shared_ptr myPaddle; + int myRows; int myColumns; int myTextFlashCounter; @@ -44,7 +45,6 @@ namespace na2 std::shared_ptr myFrame; std::shared_ptr myStatus; std::shared_ptr myAsciiArt; - std::shared_ptr myPaddle; std::shared_ptr myNCurses; LPBYTE myTextBank1; // Aux diff --git a/source/frontends/sdl/main.cpp b/source/frontends/sdl/main.cpp index 86e9847d..cb5d5607 100644 --- a/source/frontends/sdl/main.cpp +++ b/source/frontends/sdl/main.cpp @@ -90,18 +90,11 @@ void run_sdl(int argc, const char * argv []) if (!run) return; - InitializeFileRegistry(options); - - if (options.log) - { - LogInit(); - } - + const Logger logger(options.log); + const std::shared_ptr registry = CreateFileRegistry(options); g_nMemoryClearType = options.memclear; - Paddle::instance.reset(new sa2::Gamepad(0)); std::shared_ptr frame; - if (options.imgui) { frame.reset(new sa2::SDLImGuiFrame(options)); @@ -111,13 +104,14 @@ void run_sdl(int argc, const char * argv []) frame.reset(new sa2::SDLRendererFrame(options)); } + std::shared_ptr paddle(new sa2::Gamepad(0)); + const Initialisation init(registry, frame, paddle); + if (SDL_GL_SetSwapInterval(options.glSwapInterval)) { throw std::runtime_error(SDL_GetError()); } - Initialisation init(frame); - applyOptions(options); const int fps = getRefreshRate(); @@ -230,8 +224,6 @@ int main(int argc, const char * argv []) std::cerr << e.what() << std::endl; } - // this must happen BEFORE the SDL_Quit() as otherwise we have a double free (of the game controller). - Paddle::instance.reset(); SDL_Quit(); return exit; diff --git a/source/linux/context.cpp b/source/linux/context.cpp index a9c5ab17..00ad45aa 100644 --- a/source/linux/context.cpp +++ b/source/linux/context.cpp @@ -1,10 +1,13 @@ #include "StdAfx.h" #include "linux/context.h" +#include "linux/linuxframe.h" +#include "linux/registry.h" +#include "linux/paddle.h" +#include "linux/duplicates/PropertySheet.h" #include "Interface.h" -#include "linux/duplicates/PropertySheet.h" -#include "linux/linuxframe.h" +#include "Log.h" namespace { @@ -33,9 +36,16 @@ Video& GetVideo() return sg_Video; } -Initialisation::Initialisation(const std::shared_ptr & frame) +Initialisation::Initialisation( + const std::shared_ptr & registry, + const std::shared_ptr & frame, + const std::shared_ptr & paddle + ) { + Registry::instance = registry; SetFrame(frame); + Paddle::instance = paddle; + frame->Initialize(); } @@ -43,4 +53,20 @@ Initialisation::~Initialisation() { GetFrame().Destroy(); SetFrame(std::shared_ptr()); + + Paddle::instance.reset(); + Registry::instance.reset(); +} + +Logger::Logger(const bool log) +{ + if (log) + { + LogInit(); + } +} + +Logger::~Logger() +{ + LogDone(); } diff --git a/source/linux/context.h b/source/linux/context.h index 61ad6cd2..7bcac22f 100644 --- a/source/linux/context.h +++ b/source/linux/context.h @@ -3,12 +3,27 @@ #include class FrameBase; +class Paddle; +class Registry; void SetFrame(const std::shared_ptr & frame); +// RAII around Frame Registry and Paddle class Initialisation { public: - Initialisation(const std::shared_ptr & frame); + Initialisation( + const std::shared_ptr & registry, + const std::shared_ptr & frame, + const std::shared_ptr & paddle + ); ~Initialisation(); }; + +// RAII around LogInit / LogDone. +class Logger +{ +public: + Logger(const bool log); + ~Logger(); +}; diff --git a/source/linux/linuxframe.cpp b/source/linux/linuxframe.cpp index 980d8d26..cdbc9cf7 100644 --- a/source/linux/linuxframe.cpp +++ b/source/linux/linuxframe.cpp @@ -2,6 +2,7 @@ #include "linux/linuxframe.h" #include "Interface.h" #include "Log.h" +#include "Core.h" void LinuxFrame::FrameDrawDiskLEDS() { @@ -54,6 +55,7 @@ void LinuxFrame::Initialize() const size_t numberOfBytes = sizeof(bgra_t) * numberOfPixels; myFramebuffer.resize(numberOfBytes); video.Initialize(myFramebuffer.data()); + LogFileTimeUntilFirstKeyReadReset(); } void LinuxFrame::Destroy() diff --git a/source/linux/windows/time.cpp b/source/linux/windows/time.cpp index 397a1f59..acadbf59 100644 --- a/source/linux/windows/time.cpp +++ b/source/linux/windows/time.cpp @@ -22,10 +22,10 @@ DWORD timeGetTime() /// Returns the number of ticks since an undefined time (usually system startup). DWORD GetTickCount() { - struct timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - const uint64_t ticks = (uint64_t)(ts.tv_nsec / 1000000) + ((uint64_t)ts.tv_sec * 1000ull); - return ticks; + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + const uint64_t ticks = (uint64_t)(ts.tv_nsec / 1000000) + ((uint64_t)ts.tv_sec * 1000ull); + return ticks; } void GetLocalTime(SYSTEMTIME *t) @@ -48,18 +48,18 @@ void GetLocalTime(SYSTEMTIME *t) t->wYear = local->tm_year; } -int GetDateFormat(LCID Locale, DWORD dwFlags, CONST SYSTEMTIME *lpDate, LPCSTR lpFormat, LPSTR lpDateStr, int cchDate) +int GetDateFormat(LCID /* Locale */, DWORD /* dwFlags */, CONST SYSTEMTIME * /* lpDate */, LPCSTR /* lpFormat */, LPSTR lpDateStr, int cchDate) { std::time_t t = std::time(nullptr); std::tm tm = *std::localtime(&t); std::ostringstream ss; - ss << std::put_time(&tm, "%D"); + ss << std::put_time(&tm, "%F"); const std::string str = ss.str(); strncpy(lpDateStr, str.c_str(), cchDate); return cchDate; // not 100% sure, but it is never used } -int GetTimeFormat(LCID Locale, DWORD dwFlags, CONST SYSTEMTIME *lpTime, LPCSTR lpFormat, LPSTR lpTimeStr, int cchTime) +int GetTimeFormat(LCID /* Locale */, DWORD /* dwFlags */, CONST SYSTEMTIME * /* lpTime */, LPCSTR /* lpFormat */, LPSTR lpTimeStr, int cchTime) { std::time_t t = std::time(nullptr); std::tm tm = *std::localtime(&t);