From dd24343141e0b5690ed1cd9183247d068367c2d6 Mon Sep 17 00:00:00 2001 From: Misa Date: Sat, 25 Dec 2021 01:27:33 -0800 Subject: [PATCH] Use `LoadImage` in `LoadIcon` This de-duplicates the code, simplifying the codebase and reducing the number of code paths that needs to be maintained. It also adds robustness checks to LoadIcon that weren't there before (checking that loading the file succeeded and that decoding the file also succeeded). Now, you might think that loading the image with alpha will change things in some way. But actually, I tested it, and I'm pretty sure it doesn't. Since my window manager, i3, doesn't display icons, I've had to resort to this hacky multi-liner ( https://unix.stackexchange.com/a/48866 ) to dump the icon to a PAM file. I don't know what a PAM file is and all my various attempts to convert it into something readable failed. But what I did instead was just grab the icon of the game before this commit (on 2.3, just to be extra sure), and `diff`ed it with the grabbed icon now, and they end up being the exact same file. So there's literally no difference. The only other consideration is that LoadImage needs to be exported, since it's implemented in GraphicsResources.cpp. I just opted to forward-declare it right before LoadIcon in Screen.cpp, since it's really the only other time it's used. No need to create a new header file for it or anything. --- desktop_version/src/GraphicsResources.cpp | 3 +- desktop_version/src/Screen.cpp | 45 ++++++++--------------- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/desktop_version/src/GraphicsResources.cpp b/desktop_version/src/GraphicsResources.cpp index a6455fcf..743f9f39 100644 --- a/desktop_version/src/GraphicsResources.cpp +++ b/desktop_version/src/GraphicsResources.cpp @@ -16,7 +16,8 @@ extern "C" extern const char* lodepng_error_text(unsigned code); } -static SDL_Surface* LoadImage(const char *filename) +/* Don't declare `static`, this is used elsewhere */ +SDL_Surface* LoadImage(const char *filename) { //Temporary storage for the image that's loaded SDL_Surface* loadedImage = NULL; diff --git a/desktop_version/src/Screen.cpp b/desktop_version/src/Screen.cpp index 57b18645..d84c7f71 100644 --- a/desktop_version/src/Screen.cpp +++ b/desktop_version/src/Screen.cpp @@ -8,18 +8,6 @@ #include "GraphicsUtil.h" #include "Vlogging.h" -// Used to create the window icon -extern "C" -{ - extern unsigned lodepng_decode24( - unsigned char** out, - unsigned* w, - unsigned* h, - const unsigned char* in, - size_t insize - ); -} - void ScreenSettings_default(struct ScreenSettings* _this) { _this->windowWidth = 320; @@ -120,29 +108,26 @@ void Screen::GetSettings(struct ScreenSettings* settings) settings->badSignal = badSignalEffect; } +#ifdef __APPLE__ +/* Apple doesn't like icons anymore... */ void Screen::LoadIcon(void) { -#ifndef __APPLE__ - unsigned char *fileIn; - size_t length; - unsigned char *data; - unsigned int width, height; - FILESYSTEM_loadAssetToMemory("VVVVVV.png", &fileIn, &length, false); - lodepng_decode24(&data, &width, &height, fileIn, length); - FILESYSTEM_freeMemory(&fileIn); - SDL_Surface *icon = SDL_CreateRGBSurfaceWithFormatFrom( - data, - width, - height, - 24, - width * 3, - SDL_PIXELFORMAT_RGB24 - ); + +} +#else +SDL_Surface* LoadImage(const char* filename); + +void Screen::LoadIcon(void) +{ + SDL_Surface* icon = LoadImage("VVVVVV.png"); + if (icon == NULL) + { + return; + } SDL_SetWindowIcon(m_window, icon); SDL_FreeSurface(icon); - SDL_free(data); -#endif /* __APPLE__ */ } +#endif /* __APPLE__ */ void Screen::ResizeScreen(int x, int y) {