Added mlx_set_window_icon

This commit is contained in:
Daemo
2026-08-10 15:32:18 +02:00
parent 0491089463
commit 9800d2f78d
10 changed files with 77 additions and 9 deletions
+25 -1
View File
@@ -1,3 +1,4 @@
#include "Utils/Buffer.h"
#include "mlx.h"
#include <PreCompiled.h>
#include <Core/SDLManager.h>
@@ -57,7 +58,12 @@ namespace mlx
infos->window = SDL_CreateWindow(info->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, info->width, info->height, flags);
if(!infos->window)
FatalError("SDL: unable to open a new window; %", SDL_GetError());
infos->icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, Rmask(), Gmask(), Bmask(), Amask());
mlx_color* buffer = new mlx_color[logo_mlx_size];
std::memcpy(buffer, logo_mlx, logo_mlx_size);
infos->icon = SDL_CreateRGBSurfaceFrom(buffer, logo_mlx_width, logo_mlx_height, 32, logo_mlx_width * 4, Rmask(), Gmask(), Bmask(), Amask());
if(!infos->icon)
FatalError("SDL: unable to create a window icon; %", SDL_GetError());
SDL_SetWindowIcon(infos->window, infos->icon);
m_windows_registry.insert(infos);
@@ -75,7 +81,10 @@ namespace mlx
if(infos->window != nullptr)
SDL_DestroyWindow(infos->window);
if(infos->icon != nullptr)
{
delete[] (mlx_color*)infos->icon->pixels;
SDL_FreeSurface(infos->icon);
}
m_windows_registry.erase(infos);
delete infos;
@@ -153,6 +162,21 @@ namespace mlx
SDL_SetWindowTitle(static_cast<Internal::WindowInfos*>(window)->window, title.data());
}
void SDLManager::SetWindowIcon(Handle window, NonOwningPtr<Texture> texture) const noexcept
{
Internal::WindowInfos* infos = static_cast<Internal::WindowInfos*>(window);
if(infos->icon != nullptr)
{
delete[] (mlx_color*)infos->icon->pixels;
SDL_FreeSurface(infos->icon);
}
int width = texture->GetWidth(), height = texture->GetHeight();
infos->icon = SDL_CreateRGBSurfaceFrom(texture->GetBufferCopy(), width, height, 32, width * 4, Rmask(), Gmask(), Bmask(), Amask());
SDL_SetWindowIcon(infos->window, infos->icon);
}
void SDLManager::SetWindowFullscreen(Handle window, bool enable) const noexcept
{
SDL_SetWindowFullscreen(static_cast<Internal::WindowInfos*>(window)->window, (enable ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));