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
+3 -1
View File
@@ -177,6 +177,8 @@ int main(void)
mlx.logo_png = mlx_new_image_from_file(mlx.mlx, "42_logo.png", &dummy, &dummy); mlx.logo_png = mlx_new_image_from_file(mlx.mlx, "42_logo.png", &dummy, &dummy);
mlx.logo_jpg = mlx_new_image_from_file(mlx.mlx, "42_logo.jpg", &dummy, &dummy); mlx.logo_jpg = mlx_new_image_from_file(mlx.mlx, "42_logo.jpg", &dummy, &dummy);
mlx_set_window_icon(mlx.mlx, mlx.win, mlx.logo_png);
mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, (mlx_color){ .rgba = 0xFF00FFFF }); mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, (mlx_color){ .rgba = 0xFF00FFFF });
mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo_png, 0, 0); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo_png, 0, 0);
@@ -193,7 +195,7 @@ int main(void)
mlx_destroy_image(mlx.mlx, mlx.logo_bmp); mlx_destroy_image(mlx.mlx, mlx.logo_bmp);
mlx_destroy_image(mlx.mlx, mlx.img); mlx_destroy_image(mlx.mlx, mlx.img);
mlx_destroy_window(mlx.mlx, mlx.win); mlx_destroy_window(mlx.mlx, mlx.win);
mlx_destroy_context(mlx.mlx); mlx_destroy_context(mlx.mlx);
return 0; return 0;
+14 -5
View File
@@ -148,7 +148,7 @@ MLX_API void mlx_set_window_position(mlx_context mlx, mlx_window win, int x, int
* @brief Sets window size * @brief Sets window size
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to resize
* @param width New width * @param width New width
* @param height New height * @param height New height
*/ */
@@ -158,16 +158,25 @@ MLX_API void mlx_set_window_size(mlx_context mlx, mlx_window win, int width, int
* @brief Sets window title * @brief Sets window title
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to modify
* @param title New title * @param title New title
*/ */
MLX_API void mlx_set_window_title(mlx_context mlx, mlx_window win, const char* title); MLX_API void mlx_set_window_title(mlx_context mlx, mlx_window win, const char* title);
/**
* @brief Sets window icon
*
* @param mlx Internal MLX application
* @param win Internal window to modify
* @param image New icon image
*/
MLX_API void mlx_set_window_icon(mlx_context mlx, mlx_window win, mlx_image image);
/** /**
* @brief Enables/Disables window fullscreen mode * @brief Enables/Disables window fullscreen mode
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to modify
* @param enable Switch or not to fullscreen * @param enable Switch or not to fullscreen
*/ */
MLX_API void mlx_set_window_fullscreen(mlx_context mlx, mlx_window win, bool enable); MLX_API void mlx_set_window_fullscreen(mlx_context mlx, mlx_window win, bool enable);
@@ -176,7 +185,7 @@ MLX_API void mlx_set_window_fullscreen(mlx_context mlx, mlx_window win, bool ena
* @brief Gets window position * @brief Gets window position
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to get from
* @param x Pointers to get position of the window * @param x Pointers to get position of the window
* @param y Pointers to get position of the window * @param y Pointers to get position of the window
*/ */
@@ -186,7 +195,7 @@ MLX_API void mlx_get_window_position(mlx_context mlx, mlx_window win, int* x, in
* @brief Gets window size * @brief Gets window size
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to get from
* @param x Pointers to get size of the window * @param x Pointers to get size of the window
* @param y Pointers to get size of the window * @param y Pointers to get size of the window
*/ */
+1 -1
View File
@@ -70,7 +70,7 @@ namespace mlx
} }
if (info->title == nullptr) if (info->title == nullptr)
{ {
mlx::Error("invalid window title (NULL)"); Error("invalid window title (NULL)");
return nullptr; return nullptr;
} }
+2
View File
@@ -3,6 +3,7 @@
#include <mlx.h> #include <mlx.h>
#include <Maths/Vec2.h> #include <Maths/Vec2.h>
#include <Renderer/Image.h>
namespace mlx namespace mlx
{ {
@@ -26,6 +27,7 @@ namespace mlx
void SetWindowPosition(Handle window, int x, int y) const noexcept; void SetWindowPosition(Handle window, int x, int y) const noexcept;
void SetWindowSize(Handle window, int x, int y) const noexcept; void SetWindowSize(Handle window, int x, int y) const noexcept;
void SetWindowTitle(Handle window, std::string_view title) const noexcept; void SetWindowTitle(Handle window, std::string_view title) const noexcept;
void SetWindowIcon(Handle window, NonOwningPtr<Texture> texture) const noexcept;
void SetWindowFullscreen(Handle window, bool enable) const noexcept; void SetWindowFullscreen(Handle window, bool enable) const noexcept;
void SetWindowMaxSize(Handle window, int x, int y) const noexcept; void SetWindowMaxSize(Handle window, int x, int y) const noexcept;
void SetWindowMinSize(Handle window, int x, int y) const noexcept; void SetWindowMinSize(Handle window, int x, int y) const noexcept;
+2
View File
@@ -4,6 +4,7 @@
#include <mlx.h> #include <mlx.h>
#include <Maths/Vec2.h> #include <Maths/Vec2.h>
#include <Core/SDLManager.h> #include <Core/SDLManager.h>
#include <Renderer/Image.h>
namespace mlx namespace mlx
{ {
@@ -24,6 +25,7 @@ namespace mlx
MLX_FORCEINLINE void SetPosition(int x, int y) { SDLManager::Get().SetWindowPosition(p_window, x, y); } MLX_FORCEINLINE void SetPosition(int x, int y) { SDLManager::Get().SetWindowPosition(p_window, x, y); }
MLX_FORCEINLINE void SetSize(int x, int y) { SDLManager::Get().SetWindowSize(p_window, x, y); m_width = x; m_height = y; } MLX_FORCEINLINE void SetSize(int x, int y) { SDLManager::Get().SetWindowSize(p_window, x, y); m_width = x; m_height = y; }
MLX_FORCEINLINE void SetTitle(std::string title) { SDLManager::Get().SetWindowTitle(p_window, title); m_name = std::move(title); } MLX_FORCEINLINE void SetTitle(std::string title) { SDLManager::Get().SetWindowTitle(p_window, title); m_name = std::move(title); }
MLX_FORCEINLINE void SetIcon(NonOwningPtr<Texture> texture) {SDLManager::Get().SetWindowIcon(p_window, texture); }
MLX_FORCEINLINE void SetFullscreen(bool enable) { SDLManager::Get().SetWindowFullscreen(p_window, enable); } MLX_FORCEINLINE void SetFullscreen(bool enable) { SDLManager::Get().SetWindowFullscreen(p_window, enable); }
MLX_FORCEINLINE void SetMaxSize(int x, int y) { SDLManager::Get().SetWindowMaxSize(p_window, x, y); } MLX_FORCEINLINE void SetMaxSize(int x, int y) { SDLManager::Get().SetWindowMaxSize(p_window, x, y); }
MLX_FORCEINLINE void SetMinSize(int x, int y) { SDLManager::Get().SetWindowMinSize(p_window, x, y); } MLX_FORCEINLINE void SetMinSize(int x, int y) { SDLManager::Get().SetWindowMinSize(p_window, x, y); }
+2
View File
@@ -94,6 +94,8 @@ namespace mlx
void Swap(Texture& texture) noexcept; void Swap(Texture& texture) noexcept;
mlx_color* GetBufferCopy() noexcept;
// If a valid cmd buffer is passed, this function takes ownership and makes it invalid after // If a valid cmd buffer is passed, this function takes ownership and makes it invalid after
void SyncCPUBuffer(VkCommandBuffer cmd = VK_NULL_HANDLE); void SyncCPUBuffer(VkCommandBuffer cmd = VK_NULL_HANDLE);
void Update(VkCommandBuffer cmd); void Update(VkCommandBuffer cmd);
+2
View File
@@ -1,6 +1,8 @@
#ifndef __MLX_CPU_BUFFER__ #ifndef __MLX_CPU_BUFFER__
#define __MLX_CPU_BUFFER__ #define __MLX_CPU_BUFFER__
#include <PreCompiled.h>
namespace mlx namespace mlx
{ {
class CPUBuffer class CPUBuffer
+13 -1
View File
@@ -90,18 +90,30 @@ extern "C"
void mlx_set_window_title(mlx_context mlx, mlx_window win, const char* title) void mlx_set_window_title(mlx_context mlx, mlx_window win, const char* title)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
if (title == nullptr) if (title == nullptr)
{ {
mlx::Error("invalid window title (NULL)"); mlx::Error("invalid window title (NULL)");
return; return;
} }
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win); mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs && !gs->HasWindow()) if(!gs && !gs->HasWindow())
return; return;
gs->GetWindow()->SetTitle(title); gs->GetWindow()->SetTitle(title);
} }
void mlx_set_window_icon(mlx_context mlx, mlx_window win, mlx_image image)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs && !gs->HasWindow())
return;
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
gs->GetWindow()->SetIcon(texture);
}
void mlx_set_window_fullscreen(mlx_context mlx, mlx_window win, bool enable) void mlx_set_window_fullscreen(mlx_context mlx, mlx_window win, bool enable)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
+25 -1
View File
@@ -1,3 +1,4 @@
#include "Utils/Buffer.h"
#include "mlx.h" #include "mlx.h"
#include <PreCompiled.h> #include <PreCompiled.h>
#include <Core/SDLManager.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); infos->window = SDL_CreateWindow(info->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, info->width, info->height, flags);
if(!infos->window) if(!infos->window)
FatalError("SDL: unable to open a new window; %", SDL_GetError()); 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); SDL_SetWindowIcon(infos->window, infos->icon);
m_windows_registry.insert(infos); m_windows_registry.insert(infos);
@@ -75,7 +81,10 @@ namespace mlx
if(infos->window != nullptr) if(infos->window != nullptr)
SDL_DestroyWindow(infos->window); SDL_DestroyWindow(infos->window);
if(infos->icon != nullptr) if(infos->icon != nullptr)
{
delete[] (mlx_color*)infos->icon->pixels;
SDL_FreeSurface(infos->icon); SDL_FreeSurface(infos->icon);
}
m_windows_registry.erase(infos); m_windows_registry.erase(infos);
delete infos; delete infos;
@@ -153,6 +162,21 @@ namespace mlx
SDL_SetWindowTitle(static_cast<Internal::WindowInfos*>(window)->window, title.data()); 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 void SDLManager::SetWindowFullscreen(Handle window, bool enable) const noexcept
{ {
SDL_SetWindowFullscreen(static_cast<Internal::WindowInfos*>(window)->window, (enable ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0)); SDL_SetWindowFullscreen(static_cast<Internal::WindowInfos*>(window)->window, (enable ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
+13
View File
@@ -1,3 +1,5 @@
#include "Utils/Buffer.h"
#include "mlx.h"
#include <PreCompiled.h> #include <PreCompiled.h>
#include <Renderer/Image.h> #include <Renderer/Image.h>
#include <Maths/Vec4.h> #include <Maths/Vec4.h>
@@ -349,6 +351,17 @@ namespace mlx
m_has_been_modified = false; m_has_been_modified = false;
} }
mlx_color* Texture::GetBufferCopy() noexcept
{
MLX_PROFILE_FUNCTION();
if(!m_staging_buffer.has_value())
OpenCPUBuffer();
mlx_color* dst = new mlx_color[m_width * m_height];
std::memcpy(dst, m_staging_buffer->GetMap<mlx_color*>(), m_width * m_height * sizeof(mlx_color));
return dst;
}
void Texture::OpenCPUBuffer() void Texture::OpenCPUBuffer()
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();