diff --git a/example/main.c b/example/main.c index f09d680..a06fc0e 100644 --- a/example/main.c +++ b/example/main.c @@ -16,7 +16,7 @@ typedef struct #define CIRCLE_RADIUS 50 #define CIRCLE_DIAMETER (CIRCLE_RADIUS + CIRCLE_RADIUS) -static int pixels_circle[CIRCLE_DIAMETER * CIRCLE_DIAMETER] = { 0 }; +static mlx_color pixels_circle[CIRCLE_DIAMETER * CIRCLE_DIAMETER] = { 0 }; int update(void* param) { @@ -25,7 +25,7 @@ int update(void* param) if(i > 200) { - mlx_clear_window(mlx->mlx, mlx->win, 0x334D4DFF); + mlx_clear_window(mlx->mlx, mlx->win, (mlx_color){ .rgba = 0x334D4DFF }); mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40, 0.5f, 0.5f, i); } @@ -34,18 +34,19 @@ int update(void* param) else mlx_set_font_scale(mlx->mlx, "default", 6.f); - mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFF2066FF, "this text should be hidden"); + mlx_string_put(mlx->mlx, mlx->win, 160, 120, (mlx_color){ .rgba = 0xFF2066FF }, "this text should be hidden"); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_png, 100, 100); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60); mlx_set_font(mlx->mlx, "default"); - mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); + mlx_string_put(mlx->mlx, mlx->win, 20, 50, (mlx_color){ .rgba = 0xFFFFFFFF }, "that's a text"); - for(int j = 0, color = 0; j < 400; j++) + uint32_t color = 0; + for(int j = 0; j < 400; j++) { - mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0x0000FFFF + (color << 24)); - mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0x0000FFFF); + mlx_pixel_put(mlx->mlx, mlx->win, j, j, (mlx_color){ .rgba = 0x0000FFFF + (color << 24) }); + mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, (mlx_color){ .rgba = 0x0000FFFF }); color += (color < 255); } @@ -54,7 +55,7 @@ int update(void* param) else mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, fabs(sin(i / 100.0f)), fabs(cos(i / 100.0f) * 2.0f), 0.0f); mlx_set_font_scale(mlx->mlx, "default", 8.f); - mlx_string_put(mlx->mlx, mlx->win, 210, 175, 0xFFAF2BFF, "hidden"); + mlx_string_put(mlx->mlx, mlx->win, 210, 175, (mlx_color){ .rgba = 0xFFAF2BFF }, "hidden"); mlx_pixel_put_region(mlx->mlx, mlx->win, 200, 170, CIRCLE_DIAMETER, CIRCLE_DIAMETER, pixels_circle); @@ -64,7 +65,6 @@ int update(void* param) mlx_image create_image(mlx_t* mlx) { - unsigned char pixel[4]; mlx_image img = mlx_new_image(mlx->mlx, 100, 100); for(int i = 0, j = 0, k = 0; i < (100 * 100) * 4; i += 4, j++) { @@ -75,11 +75,13 @@ mlx_image create_image(mlx_t* mlx) } if(i < 10000 || i > 20000) { - pixel[0] = 0x99; - pixel[1] = i; - pixel[2] = j; - pixel[3] = k; - mlx_set_image_pixel(mlx->mlx, img, j, k, *((int*)pixel)); + mlx_color pixel = { + .r = (uint8_t)k, + .g = (uint8_t)j, + .b = (uint8_t)i, + .a = 0x99 + }; + mlx_set_image_pixel(mlx->mlx, img, j, k, pixel); } } return img; @@ -104,7 +106,7 @@ int key_hook(int key, void* param) mlx_mouse_hide(mlx->mlx); break; case 6 : // (C)lear - mlx_clear_window(mlx->mlx, mlx->win, 0x334D4DFF); + mlx_clear_window(mlx->mlx, mlx->win, (mlx_color){ .rgba = 0x334D4DFF }); break; case 79 : // RIGHT KEY mlx_mouse_move(mlx->mlx, mlx->win, x + 10, y); @@ -144,7 +146,7 @@ int main(void) for(int k = 0; k < CIRCLE_DIAMETER; k++, i++) { if((CIRCLE_RADIUS - j) * (CIRCLE_RADIUS - j) + (CIRCLE_RADIUS - k) * (CIRCLE_RADIUS - k) < CIRCLE_RADIUS * CIRCLE_RADIUS) - pixels_circle[i] = 0xA10000FF + ((j * k * i) << 8); + pixels_circle[i] = (mlx_color){ .rgba = 0xA10000FF + ((j * k * i) << 8) }; } } @@ -169,13 +171,13 @@ int main(void) mlx.logo_bmp = mlx_new_image_from_file(mlx.mlx, "42_logo.bmp", &dummy, &dummy); mlx.logo_jpg = mlx_new_image_from_file(mlx.mlx, "42_logo.jpg", &dummy, &dummy); - mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 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, 10, 190); mlx.img = create_image(&mlx); mlx_set_font_scale(mlx.mlx, "font.ttf", 16.f); - mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0x0020FFFF, "that text will disappear"); + mlx_string_put(mlx.mlx, mlx.win, 20, 20, (mlx_color){ .rgba = 0x0020FFFF }, "that text will disappear"); mlx_loop_hook(mlx.mlx, update, &mlx); mlx_loop(mlx.mlx); diff --git a/includes/mlx.h b/includes/mlx.h index e39da89..21fc75a 100644 --- a/includes/mlx.h +++ b/includes/mlx.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ -/* Updated: 2024/12/15 13:59:00 by maldavid ### ########.fr */ +/* Updated: 2024/12/16 23:11:59 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,21 @@ MLX_DEFINE_HANDLE(mlx_window); */ MLX_DEFINE_HANDLE(mlx_image); +/** + * @brief Union representing RGBA color with access to each part as bytes + */ +typedef union mlx_color +{ + struct + { + uint8_t a; + uint8_t b; + uint8_t g; + uint8_t r; + }; + uint32_t rgba; +} mlx_color; + /* MLX backend related functions */ @@ -167,7 +182,7 @@ MLX_API void mlx_get_window_size(mlx_context mlx, mlx_window win, int* x, int* y * @param mlx Internal MLX application * @param win Internal window */ -MLX_API void mlx_clear_window(mlx_context mlx, mlx_window win, int color); +MLX_API void mlx_clear_window(mlx_context mlx, mlx_window win, mlx_color color); /** * @brief Get the size of the screen the given window is on @@ -283,7 +298,7 @@ MLX_API void mlx_on_event(mlx_context mlx, mlx_window win, mlx_event_type event, * @param y Y coordinate * @param color Color of the pixel (coded on 4 bytes in an int, 0xRRGGBBAA) */ -MLX_API void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, int color); +MLX_API void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, mlx_color color); @@ -339,7 +354,7 @@ MLX_API void mlx_destroy_image(mlx_context mlx, mlx_image image); * ~ make IMAGES_OPTIMIZED=false * ``` */ -MLX_API int mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y); +MLX_API mlx_color mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y); /** * @brief Set image pixel data @@ -358,7 +373,7 @@ MLX_API int mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y); * ~ make IMAGES_OPTIMIZED=false * ``` */ -MLX_API void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, int color); +MLX_API void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, mlx_color color); /** * @brief Put image to the given window @@ -386,7 +401,7 @@ MLX_API void mlx_put_image_to_window(mlx_context mlx, mlx_window win, mlx_image * @param color Color of the pixel (coded on 4 bytes in an int, 0xAARRGGBB) * @param str Text to put */ -MLX_API void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str); +MLX_API void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, mlx_color color, char* str); /** * @brief Loads a font to be used by `mlx_string_put` diff --git a/includes/mlx_extended.h b/includes/mlx_extended.h index 942fdd1..a67206f 100644 --- a/includes/mlx_extended.h +++ b/includes/mlx_extended.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/14 16:17:10 by maldavid #+# #+# */ -/* Updated: 2024/12/16 15:06:43 by maldavid ### ########.fr */ +/* Updated: 2024/12/16 20:33:50 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,7 +85,7 @@ MLX_API void mlx_restore_window(mlx_context mlx, mlx_window win); * @param y Y coordinate * @param pixels Array of pixels (coded on 4 bytes in an int, 0xRRGGBBAA) */ -MLX_API void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, int* pixels, size_t pixels_size); +MLX_API void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, mlx_color* pixels, size_t pixels_size); /** * @brief Put a region of pixels in the window @@ -101,7 +101,7 @@ MLX_API void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, * Note: it is responsability of the user to make sure the size of `pixels` is * big enough for the given region. */ -MLX_API void mlx_pixel_put_region(mlx_context mlx, mlx_window win, int x, int y, int w, int h, int* pixels); +MLX_API void mlx_pixel_put_region(mlx_context mlx, mlx_window win, int x, int y, int w, int h, mlx_color* pixels); @@ -130,7 +130,7 @@ MLX_API void mlx_pixel_put_region(mlx_context mlx, mlx_window win, int x, int y, * ~ make IMAGES_OPTIMIZED=false * ``` */ -MLX_API void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* dst); +MLX_API void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, mlx_color* dst); /** * @brief Set image region @@ -154,7 +154,7 @@ MLX_API void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y * ~ make IMAGES_OPTIMIZED=false * ``` */ -MLX_API void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* pixels); +MLX_API void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, mlx_color* pixels); /** * @brief Transform and put image to the given window diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h index 381061e..06a221a 100644 --- a/runtime/Includes/Core/Graphics.h +++ b/runtime/Includes/Core/Graphics.h @@ -22,12 +22,12 @@ namespace mlx void Render() noexcept; - inline void ResetRenderData(int color) noexcept; + inline void ResetRenderData(mlx_color color) noexcept; - inline void PixelPut(int x, int y, int color) noexcept; - inline void PixelPutArray(int x, int y, int* pixels, std::size_t pixels_size) noexcept; - inline void PixelPutRegion(int x, int y, int w, int h, int* pixels) noexcept; - inline void StringPut(int x, int y, int, std::string str); + inline void PixelPut(int x, int y, mlx_color color) noexcept; + inline void PixelPutArray(int x, int y, mlx_color* color, std::size_t pixels_size) noexcept; + inline void PixelPutRegion(int x, int y, int w, int h, mlx_color* color) noexcept; + inline void StringPut(int x, int y, mlx_color color, std::string str); inline void TexturePut(NonOwningPtr texture, int x, int y, float scale_x, float scale_y, float angle); inline void TryEraseSpritesInScene(NonOwningPtr texture) noexcept; diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index eee2785..1416b9a 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -3,14 +3,14 @@ namespace mlx { - void GraphicsSupport::ResetRenderData(int color) noexcept + void GraphicsSupport::ResetRenderData(mlx_color color) noexcept { MLX_PROFILE_FUNCTION(); Vec4f vec_color = { - static_cast((color & 0xFF000000) >> 24) / 255.0f, - static_cast((color & 0x00FF0000) >> 16) / 255.0f, - static_cast((color & 0x0000FF00) >> 8) / 255.0f, - static_cast((color & 0x000000FF)) / 255.0f, + static_cast(color.r) / 255.0f, + static_cast(color.g) / 255.0f, + static_cast(color.b) / 255.0f, + static_cast(color.a) / 255.0f }; p_scene->ResetScene(std::move(vec_color)); m_put_pixel_manager.ResetRenderData(); @@ -18,7 +18,7 @@ namespace mlx m_pixelput_called = false; } - void GraphicsSupport::PixelPut(int x, int y, int color) noexcept + void GraphicsSupport::PixelPut(int x, int y, mlx_color color) noexcept { MLX_PROFILE_FUNCTION(); NonOwningPtr texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color); @@ -30,7 +30,7 @@ namespace mlx } } - void GraphicsSupport::PixelPutArray(int x, int y, int* pixels, std::size_t pixels_size) noexcept + void GraphicsSupport::PixelPutArray(int x, int y, mlx_color* pixels, std::size_t pixels_size) noexcept { MLX_PROFILE_FUNCTION(); NonOwningPtr texture = m_put_pixel_manager.DrawPixelsArray(x, y, m_draw_layer, pixels, pixels_size); @@ -42,7 +42,7 @@ namespace mlx } } - void GraphicsSupport::PixelPutRegion(int x, int y, int w, int h, int* pixels) noexcept + void GraphicsSupport::PixelPutRegion(int x, int y, int w, int h, mlx_color* pixels) noexcept { MLX_PROFILE_FUNCTION(); NonOwningPtr texture = m_put_pixel_manager.DrawPixelsRegion(x, y, w, h, m_draw_layer, pixels); @@ -54,17 +54,17 @@ namespace mlx } } - void GraphicsSupport::StringPut(int x, int y, int color, std::string str) + void GraphicsSupport::StringPut(int x, int y, mlx_color color, std::string str) { MLX_PROFILE_FUNCTION(); if(str.empty()) return; Vec4f vec_color = { - static_cast((color & 0xFF000000) >> 24) / 255.0f, - static_cast((color & 0x00FF0000) >> 16) / 255.0f, - static_cast((color & 0x0000FF00) >> 8) / 255.0f, - static_cast((color & 0x000000FF)) / 255.0f, + static_cast(color.r) / 255.0f, + static_cast(color.g) / 255.0f, + static_cast(color.b) / 255.0f, + static_cast(color.a) / 255.0f, }; NonOwningPtr text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast(x), static_cast(y) }, vec_color); diff --git a/runtime/Includes/Graphics/PutPixelManager.h b/runtime/Includes/Graphics/PutPixelManager.h index 8283759..8d1626c 100644 --- a/runtime/Includes/Graphics/PutPixelManager.h +++ b/runtime/Includes/Graphics/PutPixelManager.h @@ -11,9 +11,9 @@ namespace mlx PutPixelManager(NonOwningPtr renderer) : p_renderer(renderer) {} // Returns a valid pointer when a new texture has been created - NonOwningPtr DrawPixel(int x, int y, std::uint64_t draw_layer, int color); - NonOwningPtr DrawPixelsArray(int x, int y, std::uint64_t draw_layer, int* pixels, std::size_t pixels_size); - NonOwningPtr DrawPixelsRegion(int x, int y, int w, int h, std::uint64_t draw_layer, int* pixels); + NonOwningPtr DrawPixel(int x, int y, std::uint64_t draw_layer, mlx_color color); + NonOwningPtr DrawPixelsArray(int x, int y, std::uint64_t draw_layer, mlx_color* pixels, std::size_t pixels_size); + NonOwningPtr DrawPixelsRegion(int x, int y, int w, int h, std::uint64_t draw_layer, mlx_color* pixels); void ResetRenderData(); ~PutPixelManager() = default; diff --git a/runtime/Includes/Renderer/Image.h b/runtime/Includes/Renderer/Image.h index f5a70f4..5833f0a 100644 --- a/runtime/Includes/Renderer/Image.h +++ b/runtime/Includes/Renderer/Image.h @@ -83,11 +83,11 @@ namespace mlx void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name); void Destroy() noexcept override; - void SetPixel(int x, int y, int color) noexcept; - void SetRegion(int x, int y, int w, int h, int* pixels) noexcept; - void SetLinearRegion(int x, int y, std::size_t len, int* pixels) noexcept; - int GetPixel(int x, int y) noexcept; - void GetRegion(int x, int y, int w, int h, int* dst) noexcept; + void SetPixel(int x, int y, mlx_color color) noexcept; + void SetRegion(int x, int y, int w, int h, mlx_color* color) noexcept; + void SetLinearRegion(int x, int y, std::size_t len, mlx_color* color) noexcept; + mlx_color GetPixel(int x, int y) noexcept; + void GetRegion(int x, int y, int w, int h, mlx_color* dst) noexcept; void Clear(VkCommandBuffer cmd, Vec4f color) override; void Update(VkCommandBuffer cmd); @@ -98,7 +98,7 @@ namespace mlx void OpenCPUBuffer(); private: - std::vector m_cpu_buffer; + std::vector m_cpu_buffer; std::optional m_staging_buffer; bool m_has_been_modified = false; }; diff --git a/runtime/Includes/Utils/Bits.h b/runtime/Includes/Utils/Bits.h index 0b6ecf2..4d7df89 100644 --- a/runtime/Includes/Utils/Bits.h +++ b/runtime/Includes/Utils/Bits.h @@ -11,6 +11,38 @@ namespace mlx std::ranges::reverse(value_representation); return std::bit_cast(value_representation); } + + constexpr std::uint32_t Rmask() noexcept + { + if constexpr(std::endian::native == std::endian::big) + return 0xFF000000; + else + return 0x000000FF; + } + + constexpr std::uint32_t Gmask() noexcept + { + if constexpr(std::endian::native == std::endian::big) + return 0x00FF0000; + else + return 0x0000FF00; + } + + constexpr std::uint32_t Bmask() noexcept + { + if constexpr(std::endian::native == std::endian::big) + return 0x0000FF00; + else + return 0x00FF0000; + } + + constexpr std::uint32_t Amask() noexcept + { + if constexpr(std::endian::native == std::endian::big) + return 0x000000FF; + else + return 0xFF000000; + } } #endif diff --git a/runtime/Sources/Core/Bridge.cpp b/runtime/Sources/Core/Bridge.cpp index 10e8a41..eff5d65 100644 --- a/runtime/Sources/Core/Bridge.cpp +++ b/runtime/Sources/Core/Bridge.cpp @@ -126,7 +126,7 @@ extern "C" gs->GetWindow()->GetSize(x, y); } - void mlx_clear_window(mlx_context mlx, mlx_window win, int color) + void mlx_clear_window(mlx_context mlx, mlx_window win, mlx_color color) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr gs = mlx->app->GetGraphicsSupport(win); @@ -201,7 +201,7 @@ extern "C" mlx->app->OnEvent(win, static_cast(event), funct_ptr, param); } - void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, int color) + void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, mlx_color color) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr gs = mlx->app->GetGraphicsSupport(win); @@ -248,16 +248,16 @@ extern "C" mlx->app->DestroyTexture(image); } - int mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y) + mlx_color mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr texture = mlx->app->GetTexture(image); if(!texture) - return 0; + return { .rgba = 0x00000000 }; return texture->GetPixel(x, y); } - void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, int color) + void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, mlx_color color) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr texture = mlx->app->GetTexture(image); @@ -278,7 +278,7 @@ extern "C" gs->TexturePut(texture, x, y, 1.0f, 1.0f, 0.0f); } - void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str) + void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, mlx_color color, char* str) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr gs = mlx->app->GetGraphicsSupport(win); @@ -381,7 +381,7 @@ extern "C" gs->GetWindow()->Restore(); } - void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, int* pixels, size_t pixels_size) + void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, mlx_color* pixels, size_t pixels_size) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr gs = mlx->app->GetGraphicsSupport(win); @@ -390,7 +390,7 @@ extern "C" gs->PixelPutArray(x, y, pixels, pixels_size); } - void mlx_pixel_put_region(mlx_context mlx, mlx_window win, int x, int y, int w, int h, int* pixels) + void mlx_pixel_put_region(mlx_context mlx, mlx_window win, int x, int y, int w, int h, mlx_color* pixels) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr gs = mlx->app->GetGraphicsSupport(win); @@ -399,7 +399,7 @@ extern "C" gs->PixelPutRegion(x, y, w, h, pixels); } - void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* dst) + void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, mlx_color* dst) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr texture = mlx->app->GetTexture(image); @@ -408,7 +408,7 @@ extern "C" texture->GetRegion(x, y, w, h, dst); } - void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* pixels) + void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, mlx_color* pixels) { MLX_CHECK_APPLICATION_POINTER(mlx); mlx::NonOwningPtr texture = mlx->app->GetTexture(image); diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index 5fa5ce6..08141de 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -2,21 +2,10 @@ #include #include #include +#include namespace mlx { - #if SDL_BYTEORDER == SDL_BIG_ENDIAN - constexpr const std::uint32_t rmask = 0xff000000; - constexpr const std::uint32_t gmask = 0x00ff0000; - constexpr const std::uint32_t bmask = 0x0000ff00; - constexpr const std::uint32_t amask = 0x000000ff; - #else - constexpr const std::uint32_t rmask = 0x000000ff; - constexpr const std::uint32_t gmask = 0x0000ff00; - constexpr const std::uint32_t bmask = 0x00ff0000; - constexpr const std::uint32_t amask = 0xff000000; - #endif - namespace Internal { struct WindowInfos @@ -65,7 +54,7 @@ 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(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask); + infos->icon = SDL_CreateRGBSurfaceFrom(static_cast(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, Rmask(), Gmask(), Bmask(), Amask()); SDL_SetWindowIcon(infos->window, infos->icon); m_windows_registry.insert(infos); diff --git a/runtime/Sources/Graphics/PutPixelManager.cpp b/runtime/Sources/Graphics/PutPixelManager.cpp index 79753ce..06cedf6 100644 --- a/runtime/Sources/Graphics/PutPixelManager.cpp +++ b/runtime/Sources/Graphics/PutPixelManager.cpp @@ -2,10 +2,11 @@ #include #include +#include namespace mlx { - NonOwningPtr PutPixelManager::DrawPixel(int x, int y, std::uint64_t draw_layer, int color) + NonOwningPtr PutPixelManager::DrawPixel(int x, int y, std::uint64_t draw_layer, mlx_color color) { MLX_PROFILE_FUNCTION(); bool is_newlayer; @@ -16,7 +17,7 @@ namespace mlx return (is_newlayer ? layer : nullptr); } - NonOwningPtr PutPixelManager::DrawPixelsArray(int x, int y, std::uint64_t draw_layer, int* pixels, std::size_t pixels_size) + NonOwningPtr PutPixelManager::DrawPixelsArray(int x, int y, std::uint64_t draw_layer, mlx_color* pixels, std::size_t pixels_size) { MLX_PROFILE_FUNCTION(); bool is_newlayer; @@ -27,7 +28,7 @@ namespace mlx return (is_newlayer ? layer : nullptr); } - NonOwningPtr PutPixelManager::DrawPixelsRegion(int x, int y, int w, int h, std::uint64_t draw_layer, int* pixels) + NonOwningPtr PutPixelManager::DrawPixelsRegion(int x, int y, int w, int h, std::uint64_t draw_layer, mlx_color* pixels) { MLX_PROFILE_FUNCTION(); bool is_newlayer; @@ -70,7 +71,7 @@ namespace mlx try { m_placements[draw_layer] = m_textures.at(m_current_texture_index).get(); - m_textures.at(m_current_texture_index)->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f }); + m_textures.at(m_current_texture_index)->Clear(VK_NULL_HANDLE, Vec4f{ 0.f }); NonOwningPtr texture = m_textures.at(m_current_texture_index).get(); m_current_texture_index++; return texture; diff --git a/runtime/Sources/Renderer/Image.cpp b/runtime/Sources/Renderer/Image.cpp index 00e773b..7c40b18 100644 --- a/runtime/Sources/Renderer/Image.cpp +++ b/runtime/Sources/Renderer/Image.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #define STB_IMAGE_IMPLEMENTATION @@ -200,20 +199,21 @@ namespace mlx Image::Destroy(); } - void Texture::SetPixel(int x, int y, int color) noexcept + void Texture::SetPixel(int x, int y, mlx_color color) noexcept { MLX_PROFILE_FUNCTION(); if(x < 0 || y < 0 || static_cast(x) > m_width || static_cast(y) > m_height) return; if(!m_staging_buffer.has_value()) OpenCPUBuffer(); - // Needs to reverse bytes order because why not - color = ByteSwap(color); - m_cpu_buffer[(y * m_width) + x] = color; + if constexpr(std::endian::native == std::endian::little) + m_cpu_buffer[(y * m_width) + x] = mlx_color{ .r = color.a, .g = color.b, .b = color.g, .a = color.r }; + else + m_cpu_buffer[(y * m_width) + x] = color; m_has_been_modified = true; } - void Texture::SetRegion(int x, int y, int w, int h, int* pixels) noexcept + void Texture::SetRegion(int x, int y, int w, int h, mlx_color* pixels) noexcept { MLX_PROFILE_FUNCTION(); if(x < 0 || y < 0 || static_cast(x) > m_width || static_cast(y) > m_height) @@ -231,40 +231,48 @@ namespace mlx break; moving_y++; } - // Needs to reverse bytes order because why not - int color = ByteSwap(pixels[i]); - m_cpu_buffer[(moving_y * m_width) + moving_x] = color; + if constexpr(std::endian::native == std::endian::little) + m_cpu_buffer[(moving_y * m_width) + moving_x] = mlx_color{ .r = pixels[i].a, .g = pixels[i].b, .b = pixels[i].g, .a = pixels[i].r }; + else + m_cpu_buffer[(moving_y * m_width) + moving_x] = pixels[i]; } m_has_been_modified = true; } - void Texture::SetLinearRegion(int x, int y, std::size_t len, int* pixels) noexcept + void Texture::SetLinearRegion(int x, int y, std::size_t len, mlx_color* pixels) noexcept { MLX_PROFILE_FUNCTION(); if(x < 0 || y < 0 || static_cast(x) > m_width || static_cast(y) > m_height) return; if(!m_staging_buffer.has_value()) OpenCPUBuffer(); - for(std::size_t i = 0; i < len; i++) + if constexpr(std::endian::native == std::endian::little) + for(std::size_t i = 0; i < len; i++) + m_cpu_buffer[(y * m_width) + x + i] = mlx_color{ .r = pixels[i].a, .g = pixels[i].b, .b = pixels[i].g, .a = pixels[i].r }; + else { - // Needs to reverse bytes order because why not - int color = ByteSwap(pixels[i]); - m_cpu_buffer[(y * m_width) + x + i] = color; + std::memcpy(&m_cpu_buffer[(y * m_width) + x], pixels, len); } m_has_been_modified = true; } - int Texture::GetPixel(int x, int y) noexcept + mlx_color Texture::GetPixel(int x, int y) noexcept { MLX_PROFILE_FUNCTION(); if(x < 0 || y < 0 || static_cast(x) > m_width || static_cast(y) > m_height) - return 0; + return { .rgba = 0x00000000 }; if(!m_staging_buffer.has_value()) OpenCPUBuffer(); - return m_cpu_buffer[(y * m_width) + x]; + if constexpr(std::endian::native == std::endian::little) + { + mlx_color color = m_cpu_buffer[(y * m_width) + x]; + return { .r = color.a, .g = color.b, .b = color.g, .a = color.r }; + } + else + return m_cpu_buffer[(y * m_width) + x]; } - void Texture::GetRegion(int x, int y, int w, int h, int* dst) noexcept + void Texture::GetRegion(int x, int y, int w, int h, mlx_color* dst) noexcept { MLX_PROFILE_FUNCTION(); if(x < 0 || y < 0 || static_cast(x) > m_width || static_cast(y) > m_height) @@ -280,8 +288,13 @@ namespace mlx break; moving_y++; } - // Needs to reverse bytes order because why not - dst[i] = ByteSwap(m_cpu_buffer[(moving_y * m_width) + moving_x]); + if constexpr(std::endian::native == std::endian::little) + { + mlx_color color = m_cpu_buffer[(moving_y * m_width) + moving_x]; + dst[i] = mlx_color{ .r = color.a, .g = color.b, .b = color.g, .a = color.r }; + } + else + dst[i] = m_cpu_buffer[(moving_y * m_width) + moving_x]; } } @@ -291,12 +304,13 @@ namespace mlx Image::Clear(cmd, std::move(color)); if(m_staging_buffer.has_value()) { - std::uint8_t color_bytes[4]; - color_bytes[0] = static_cast(color.r * 255.f); - color_bytes[1] = static_cast(color.g * 255.f); - color_bytes[2] = static_cast(color.b * 255.f); - color_bytes[3] = static_cast(color.a * 255.f); - std::fill(m_cpu_buffer.begin(), m_cpu_buffer.end(), *reinterpret_cast(color_bytes)); + mlx_color processed_color{ + .r = static_cast(color.r * 255.f), + .g = static_cast(color.g * 255.f), + .b = static_cast(color.b * 255.f), + .a = static_cast(color.a * 255.f) + }; + std::fill(m_cpu_buffer.begin(), m_cpu_buffer.end(), processed_color); } }