free up memory

This commit is contained in:
Reonu 2024-04-23 00:59:18 +01:00
parent 22cd474be6
commit 677b4594e3

View file

@ -59,15 +59,15 @@ ultramodern::gfx_callbacks_t::gfx_data_t create_gfx() {
} }
#if defined(__linux__) #if defined(__linux__)
SDL_Surface *LoadImage(char* filename) bool SetImageAsIcon(char* filename, SDL_Window* window)
{ {
// Read data // Read data
int width, height, bytesPerPixel; int width, height, bytesPerPixel;
void* data = stbi_load(filename, &width, &height, &bytesPerPixel, 0); void* data = stbi_load(filename, &width, &height, &bytesPerPixel, 4);
// Calculate pitch // Calculate pitch
int pitch; int pitch;
pitch = width * bytesPerPixel; pitch = width * 4;
pitch = (pitch + 3) & ~3; pitch = (pitch + 3) & ~3;
// Setup relevance bitmask // Setup relevance bitmask
@ -77,23 +77,31 @@ SDL_Surface *LoadImage(char* filename)
Rmask = 0x000000FF; Rmask = 0x000000FF;
Gmask = 0x0000FF00; Gmask = 0x0000FF00;
Bmask = 0x00FF0000; Bmask = 0x00FF0000;
Amask = (bytesPerPixel == 4) ? 0xFF000000 : 0; Amask = 0xFF000000;
#else #else
int s = (bytesPerPixel == 4) ? 0 : 8; Rmask = 0xFF000000;
Rmask = 0xFF000000 >> s; Gmask = 0x00FF0000;
Gmask = 0x00FF0000 >> s; Bmask = 0x0000FF00;
Bmask = 0x0000FF00 >> s; Amask = 0x000000FF;
Amask = 0x000000FF >> s;
#endif #endif
SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(data, width, height, bytesPerPixel*8, pitch, Rmask, Gmask, SDL_Surface* surface;
Bmask, Amask); if (data != NULL) {
if (!surface) surface = SDL_CreateRGBSurfaceFrom(data, width, height, 32, pitch, Rmask, Gmask,
{ Bmask, Amask);
// NOTE: Should free stbi_load 'data' variable here }
return NULL;
} if (surface == NULL) {
return surface; if (data != NULL) {
stbi_image_free(data);
}
return false;
} else {
SDL_SetWindowIcon(window,surface);
SDL_FreeSurface(surface);
stbi_image_free(data);
return true;
}
} }
#endif #endif
@ -102,8 +110,7 @@ SDL_Window* window;
ultramodern::WindowHandle create_window(ultramodern::gfx_callbacks_t::gfx_data_t) { ultramodern::WindowHandle create_window(ultramodern::gfx_callbacks_t::gfx_data_t) {
window = SDL_CreateWindow("Zelda 64: Recompiled", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1600, 960, SDL_WINDOW_RESIZABLE ); window = SDL_CreateWindow("Zelda 64: Recompiled", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1600, 960, SDL_WINDOW_RESIZABLE );
#if defined(__linux__) #if defined(__linux__)
SDL_Surface* icon = LoadImage("icons/512linux.png"); SetImageAsIcon("icons/512linux.png",window);
SDL_SetWindowIcon(window, icon);
#endif #endif
if (window == nullptr) { if (window == nullptr) {