From 49102e395c585f9d311e92895548c781da5492b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Thu, 19 Jan 2023 17:29:47 +0100 Subject: [PATCH] Move platform-specific code out of entrypoint TUs --- src/PowderToyFontEditor.cpp | 23 ++-------------- src/PowderToySDL.cpp | 52 +++++-------------------------------- src/WindowIcon.cpp | 15 +++++++++++ src/WindowIcon.h | 4 +++ src/common/Platform.cpp | 11 ++++++++ src/common/Platform.h | 2 ++ src/meson.build | 5 ++++ 7 files changed, 45 insertions(+), 67 deletions(-) create mode 100644 src/WindowIcon.cpp create mode 100644 src/WindowIcon.h diff --git a/src/PowderToyFontEditor.cpp b/src/PowderToyFontEditor.cpp index 9ae824aff..f813e21df 100644 --- a/src/PowderToyFontEditor.cpp +++ b/src/PowderToyFontEditor.cpp @@ -1,28 +1,16 @@ #include "Config.h" #include #include -#ifdef WIN -#include -#endif #include -#if defined(LIN) -# include "icon_exe.png.h" -#endif #include -#ifndef WIN -#include -#endif -#ifdef MACOSX -# include -# include -#endif #include #include "Format.h" #include "Misc.h" +#include "WindowIcon.h" #include "graphics/Graphics.h" #include "client/SaveInfo.h" @@ -130,14 +118,7 @@ int SDLOpen() } #ifdef LIN - std::vector imageData; - int imgw, imgh; - if (PngDataToPixels(imageData, imgw, imgh, reinterpret_cast(icon_exe_png), icon_exe_png_size, false)) - { - SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(&imageData[0], imgw, imgh, 32, imgw * sizeof(pixel), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); - SDL_SetWindowIcon(sdl_window, icon); - SDL_FreeSurface(icon); - } + WindowIcon(sdl_window); #endif return 0; diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 6b7ba8624..15351c434 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -6,30 +6,18 @@ #include #include #include -#ifdef WIN -# include -# include -#endif #include #include -#if defined(LIN) -# include "icon_exe.png.h" -#endif #include #include -#ifndef WIN -# include -#endif -#ifdef MACOSX -# include -#endif #include #include #include "Format.h" #include "X86KillDenormals.h" +#include "WindowIcon.h" #include "Misc.h" #include "prefs/GlobalPrefs.h" @@ -190,14 +178,7 @@ void SDLOpen() } #ifdef LIN - std::vector imageData; - int imgw, imgh; - if (PngDataToPixels(imageData, imgw, imgh, reinterpret_cast(icon_exe_png), icon_exe_png_size, false)) - { - SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(&imageData[0], imgw, imgh, 32, imgw * sizeof(pixel), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); - SDL_SetWindowIcon(sdl_window, icon); - SDL_FreeSurface(icon); - } + WindowIcon(sdl_window); #endif } @@ -617,16 +598,11 @@ static std::unique_ptr explicitSingletons; int main(int argc, char * argv[]) { + Platform::SetupCrt(); atexit([]() { explicitSingletons.reset(); }); explicitSingletons = std::make_unique(); -#ifdef WIN - if constexpr (DEBUG) - { - _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); - } -#endif currentWidth = WINDOWW; currentHeight = WINDOWH; @@ -683,12 +659,7 @@ int main(int argc, char * argv[]) auto ddirArg = arguments["ddir"]; if (ddirArg.has_value()) { -#ifdef WIN - int failure = _chdir(ddirArg.value().c_str()); -#else - int failure = chdir(ddirArg.value().c_str()); -#endif - if (!failure) + if (Platform::ChangeDir(ddirArg.value())) Platform::sharedCwd = Platform::GetCwd(); else perror("failed to chdir to requested ddir"); @@ -696,22 +667,11 @@ int main(int argc, char * argv[]) else { char *ddir = SDL_GetPrefPath(NULL, APPDATA); -#ifdef WIN - struct _stat s; - if (_stat("powder.pref", &s) != 0) -#else - struct stat s; - if (stat("powder.pref", &s) != 0) -#endif + if (!Platform::FileExists("powder.pref")) { if (ddir) { -#ifdef WIN - int failure = _chdir(ddir); -#else - int failure = chdir(ddir); -#endif - if (failure) + if (!Platform::ChangeDir(ddir)) { perror("failed to chdir to default ddir"); SDL_free(ddir); diff --git a/src/WindowIcon.cpp b/src/WindowIcon.cpp new file mode 100644 index 000000000..2b2365e89 --- /dev/null +++ b/src/WindowIcon.cpp @@ -0,0 +1,15 @@ +#include "WindowIcon.h" +#include "graphics/Graphics.h" +#include "icon_exe.png.h" + +void WindowIcon(SDL_Window *window) +{ + std::vector imageData; + int imgw, imgh; + if (PngDataToPixels(imageData, imgw, imgh, reinterpret_cast(icon_exe_png), icon_exe_png_size, false)) + { + SDL_Surface *icon = SDL_CreateRGBSurfaceFrom(&imageData[0], imgw, imgh, 32, imgw * sizeof(pixel), 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); + SDL_SetWindowIcon(window, icon); + SDL_FreeSurface(icon); + } +} diff --git a/src/WindowIcon.h b/src/WindowIcon.h new file mode 100644 index 000000000..0ffce34ad --- /dev/null +++ b/src/WindowIcon.h @@ -0,0 +1,4 @@ +#pragma once +#include + +void WindowIcon(SDL_Window *window); diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index d5c683f6c..78d84bebf 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -16,6 +16,7 @@ # include # include # include +# include #else # include # include @@ -729,4 +730,14 @@ void UpdateCleanup() UpdateFinish(); #endif } + +void SetupCrt() +{ +#ifdef WIN + if constexpr (DEBUG) + { + _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); + } +#endif +} } diff --git a/src/common/Platform.h b/src/common/Platform.h index f42ac2f03..9dd717805 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -53,4 +53,6 @@ namespace Platform bool UpdateStart(const std::vector &data); bool UpdateFinish(); void UpdateCleanup(); + + void SetupCrt(); } diff --git a/src/meson.build b/src/meson.build index 2583a4c77..3e433fcc4 100644 --- a/src/meson.build +++ b/src/meson.build @@ -29,6 +29,11 @@ common_files = files( 'Probability.cpp', ) +if host_platform == 'linux' + powder_files += files('WindowIcon.cpp') + font_files += files('WindowIcon.cpp') +endif + subdir('bson') subdir('bzip2') subdir('client')