Move platform-specific code out of entrypoint TUs

This commit is contained in:
Tamás Bálint Misius 2023-01-19 17:29:47 +01:00
parent c8ca016494
commit 49102e395c
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
7 changed files with 45 additions and 67 deletions

View File

@ -1,28 +1,16 @@
#include "Config.h" #include "Config.h"
#include <ctime> #include <ctime>
#include <climits> #include <climits>
#ifdef WIN
#include <direct.h>
#endif
#include <iostream> #include <iostream>
#if defined(LIN)
# include "icon_exe.png.h"
#endif
#include <stdexcept> #include <stdexcept>
#ifndef WIN
#include <unistd.h>
#endif
#ifdef MACOSX
# include <mach-o/dyld.h>
# include <ApplicationServices/ApplicationServices.h>
#endif
#include <SDL.h> #include <SDL.h>
#include "Format.h" #include "Format.h"
#include "Misc.h" #include "Misc.h"
#include "WindowIcon.h"
#include "graphics/Graphics.h" #include "graphics/Graphics.h"
#include "client/SaveInfo.h" #include "client/SaveInfo.h"
@ -130,14 +118,7 @@ int SDLOpen()
} }
#ifdef LIN #ifdef LIN
std::vector<pixel> imageData; WindowIcon(sdl_window);
int imgw, imgh;
if (PngDataToPixels(imageData, imgw, imgh, reinterpret_cast<const char *>(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);
}
#endif #endif
return 0; return 0;

View File

@ -6,30 +6,18 @@
#include <ctime> #include <ctime>
#include <climits> #include <climits>
#include <cstdint> #include <cstdint>
#ifdef WIN
# include <direct.h>
# include <crtdbg.h>
#endif
#include <SDL.h> #include <SDL.h>
#include <iostream> #include <iostream>
#if defined(LIN)
# include "icon_exe.png.h"
#endif
#include <csignal> #include <csignal>
#include <stdexcept> #include <stdexcept>
#ifndef WIN
# include <unistd.h>
#endif
#ifdef MACOSX
# include <CoreServices/CoreServices.h>
#endif
#include <sys/stat.h> #include <sys/stat.h>
#include <SDL.h> #include <SDL.h>
#include "Format.h" #include "Format.h"
#include "X86KillDenormals.h" #include "X86KillDenormals.h"
#include "WindowIcon.h"
#include "Misc.h" #include "Misc.h"
#include "prefs/GlobalPrefs.h" #include "prefs/GlobalPrefs.h"
@ -190,14 +178,7 @@ void SDLOpen()
} }
#ifdef LIN #ifdef LIN
std::vector<pixel> imageData; WindowIcon(sdl_window);
int imgw, imgh;
if (PngDataToPixels(imageData, imgw, imgh, reinterpret_cast<const char *>(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);
}
#endif #endif
} }
@ -617,16 +598,11 @@ static std::unique_ptr<ExplicitSingletons> explicitSingletons;
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
Platform::SetupCrt();
atexit([]() { atexit([]() {
explicitSingletons.reset(); explicitSingletons.reset();
}); });
explicitSingletons = std::make_unique<ExplicitSingletons>(); explicitSingletons = std::make_unique<ExplicitSingletons>();
#ifdef WIN
if constexpr (DEBUG)
{
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
}
#endif
currentWidth = WINDOWW; currentWidth = WINDOWW;
currentHeight = WINDOWH; currentHeight = WINDOWH;
@ -683,12 +659,7 @@ int main(int argc, char * argv[])
auto ddirArg = arguments["ddir"]; auto ddirArg = arguments["ddir"];
if (ddirArg.has_value()) if (ddirArg.has_value())
{ {
#ifdef WIN if (Platform::ChangeDir(ddirArg.value()))
int failure = _chdir(ddirArg.value().c_str());
#else
int failure = chdir(ddirArg.value().c_str());
#endif
if (!failure)
Platform::sharedCwd = Platform::GetCwd(); Platform::sharedCwd = Platform::GetCwd();
else else
perror("failed to chdir to requested ddir"); perror("failed to chdir to requested ddir");
@ -696,22 +667,11 @@ int main(int argc, char * argv[])
else else
{ {
char *ddir = SDL_GetPrefPath(NULL, APPDATA); char *ddir = SDL_GetPrefPath(NULL, APPDATA);
#ifdef WIN if (!Platform::FileExists("powder.pref"))
struct _stat s;
if (_stat("powder.pref", &s) != 0)
#else
struct stat s;
if (stat("powder.pref", &s) != 0)
#endif
{ {
if (ddir) if (ddir)
{ {
#ifdef WIN if (!Platform::ChangeDir(ddir))
int failure = _chdir(ddir);
#else
int failure = chdir(ddir);
#endif
if (failure)
{ {
perror("failed to chdir to default ddir"); perror("failed to chdir to default ddir");
SDL_free(ddir); SDL_free(ddir);

15
src/WindowIcon.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "WindowIcon.h"
#include "graphics/Graphics.h"
#include "icon_exe.png.h"
void WindowIcon(SDL_Window *window)
{
std::vector<pixel> imageData;
int imgw, imgh;
if (PngDataToPixels(imageData, imgw, imgh, reinterpret_cast<const char *>(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);
}
}

4
src/WindowIcon.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#include <SDL.h>
void WindowIcon(SDL_Window *window);

View File

@ -16,6 +16,7 @@
# include <shlwapi.h> # include <shlwapi.h>
# include <shellapi.h> # include <shellapi.h>
# include <windows.h> # include <windows.h>
# include <crtdbg.h>
#else #else
# include <unistd.h> # include <unistd.h>
# include <ctime> # include <ctime>
@ -729,4 +730,14 @@ void UpdateCleanup()
UpdateFinish(); UpdateFinish();
#endif #endif
} }
void SetupCrt()
{
#ifdef WIN
if constexpr (DEBUG)
{
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
}
#endif
}
} }

View File

@ -53,4 +53,6 @@ namespace Platform
bool UpdateStart(const std::vector<char> &data); bool UpdateStart(const std::vector<char> &data);
bool UpdateFinish(); bool UpdateFinish();
void UpdateCleanup(); void UpdateCleanup();
void SetupCrt();
} }

View File

@ -29,6 +29,11 @@ common_files = files(
'Probability.cpp', 'Probability.cpp',
) )
if host_platform == 'linux'
powder_files += files('WindowIcon.cpp')
font_files += files('WindowIcon.cpp')
endif
subdir('bson') subdir('bson')
subdir('bzip2') subdir('bzip2')
subdir('client') subdir('client')