From c485f039fb9a215e46ef4802906ff3007b279f61 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Wed, 10 Jan 2024 20:22:53 +0100 Subject: [PATCH] fixing color management issue with texts --- example/main.c | 5 +++-- src/core/application.h | 4 ++-- src/core/application.inl | 2 +- src/core/bridge.cpp | 6 +++--- src/core/graphics.h | 4 ++-- src/core/graphics.inl | 2 +- src/renderer/text_pipeline.cpp | 21 ++++++++++++++------- src/renderer/text_pipeline.h | 8 ++++---- 8 files changed, 30 insertions(+), 22 deletions(-) diff --git a/example/main.c b/example/main.c index 4d67f8a..d2cb659 100644 --- a/example/main.c +++ b/example/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2024/01/09 00:25:08 by maldavid ### ########.fr */ +/* Updated: 2024/01/10 20:21:45 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ int update(void *param) mlx = (t_mlx *)param; mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo, 100, 100); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60); + mlx_string_put(mlx->mlx, mlx->win, 90, 120, 0xFFFF2066, "this text should be hidden"); if (i == 0) mlx_set_font_scale(mlx->mlx, mlx->win, "font.ttf", 16.f); mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); @@ -110,7 +111,7 @@ int main(int argc, char **argv) mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 10, 190); mlx.img = create_image(&mlx); - mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFFFF2000, \ + mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFF0020FF, \ "that text will disappear"); mlx_loop_hook(mlx.mlx, update, &mlx); mlx_loop(mlx.mlx); diff --git a/src/core/application.h b/src/core/application.h index cb3aa07..6b4ee97 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ -/* Updated: 2024/01/10 14:17:24 by maldavid ### ########.fr */ +/* Updated: 2024/01/10 19:57:12 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ namespace mlx::core inline void destroyGraphicsSupport(void* win); inline void pixelPut(void* win, int x, int y, uint32_t color) const noexcept; - inline void stringPut(void* win, int x, int y, int color, char* str); + inline void stringPut(void* win, int x, int y, uint32_t color, char* str); void* newTexture(int w, int h); void* newStbTexture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...) diff --git a/src/core/application.inl b/src/core/application.inl index c1aa586..9249264 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -97,7 +97,7 @@ namespace mlx::core _graphics[*static_cast(win)]->pixelPut(x, y, color); } - void Application::stringPut(void* win, int x, int y, int color, char* str) + void Application::stringPut(void* win, int x, int y, uint32_t color, char* str) { MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index beedec8..f2d947b 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ -/* Updated: 2023/12/31 00:22:58 by maldavid ### ########.fr */ +/* Updated: 2024/01/10 19:54:51 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -193,8 +193,8 @@ extern "C" unsigned char color_bits[4]; color_bits[0] = (color & 0x00FF0000) >> 16; color_bits[1] = (color & 0x0000FF00) >> 8; - color_bits[2] = color & 0x000000FF; - color_bits[3] = 0xFF; + color_bits[2] = (color & 0x000000FF); + color_bits[3] = (color & 0xFF000000) >> 24; static_cast(mlx)->stringPut(win, x, y, *reinterpret_cast(color_bits), str); return 0; } diff --git a/src/core/graphics.h b/src/core/graphics.h index fc789eb..749db10 100644 --- a/src/core/graphics.h +++ b/src/core/graphics.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ -/* Updated: 2024/01/10 14:18:48 by maldavid ### ########.fr */ +/* Updated: 2024/01/10 19:59:58 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ namespace mlx inline void clearRenderData() noexcept; inline void pixelPut(int x, int y, uint32_t color) noexcept; - inline void stringPut(int x, int y, int color, std::string str); + inline void stringPut(int x, int y, uint32_t color, std::string str); inline void texturePut(Texture* texture, int x, int y); inline void loadFont(const std::filesystem::path& filepath, float scale); diff --git a/src/core/graphics.inl b/src/core/graphics.inl index d1718ad..e1d4073 100644 --- a/src/core/graphics.inl +++ b/src/core/graphics.inl @@ -32,7 +32,7 @@ namespace mlx _pixel_put_pipeline.setPixel(x, y, color); } - void GraphicsSupport::stringPut(int x, int y, int color, std::string str) + void GraphicsSupport::stringPut(int x, int y, uint32_t color, std::string str) { MLX_PROFILE_FUNCTION(); _text_put_pipeline->put(x, y, color, str); diff --git a/src/renderer/text_pipeline.cpp b/src/renderer/text_pipeline.cpp index c40162b..aecc25d 100644 --- a/src/renderer/text_pipeline.cpp +++ b/src/renderer/text_pipeline.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ -/* Updated: 2024/01/10 18:26:24 by maldavid ### ########.fr */ +/* Updated: 2024/01/10 20:20:30 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ constexpr const int RANGE = 1024; namespace mlx { - TextDrawData::TextDrawData(std::string _text, int _color, int _x, int _y) : + TextDrawData::TextDrawData(std::string _text, uint32_t _color, int _x, int _y) : x(_x), y(_y), color(_color), text(std::move(_text)) {} @@ -56,10 +56,17 @@ namespace mlx std::size_t index = vertexData.size(); - vertexData.emplace_back(glm::vec2{q.x0, q.y0}, glm::vec4{color & 0x00FF0000, color & 0x0000FF00, color & 0x000000FF, 0xFFFFFFFF}, glm::vec2{q.s0, q.t0}); - vertexData.emplace_back(glm::vec2{q.x1, q.y0}, glm::vec4{color & 0x00FF0000, color & 0x0000FF00, color & 0x000000FF, 0xFFFFFFFF}, glm::vec2{q.s1, q.t0}); - vertexData.emplace_back(glm::vec2{q.x1, q.y1}, glm::vec4{color & 0x00FF0000, color & 0x0000FF00, color & 0x000000FF, 0xFFFFFFFF}, glm::vec2{q.s1, q.t1}); - vertexData.emplace_back(glm::vec2{q.x0, q.y1}, glm::vec4{color & 0x00FF0000, color & 0x0000FF00, color & 0x000000FF, 0xFFFFFFFF}, glm::vec2{q.s0, q.t1}); + glm::vec4 vertex_color = { + static_cast((color & 0x000000FF)) / 255.f, + static_cast((color & 0x0000FF00) >> 8) / 255.f, + static_cast((color & 0x00FF0000) >> 16) / 255.f, + static_cast((color & 0xFF000000) >> 24) / 255.f + }; + + vertexData.emplace_back(glm::vec2{q.x0, q.y0}, vertex_color, glm::vec2{q.s0, q.t0}); + vertexData.emplace_back(glm::vec2{q.x1, q.y0}, vertex_color, glm::vec2{q.s1, q.t0}); + vertexData.emplace_back(glm::vec2{q.x1, q.y1}, vertex_color, glm::vec2{q.s1, q.t1}); + vertexData.emplace_back(glm::vec2{q.x0, q.y1}, vertex_color, glm::vec2{q.s0, q.t1}); indexData.emplace_back(index + 0); indexData.emplace_back(index + 1); @@ -93,7 +100,7 @@ namespace mlx _font_in_use = &const_cast(*_font_set.emplace(*_renderer, filepath, scale).first); } - void TextPutPipeline::put(int x, int y, int color, std::string str) + void TextPutPipeline::put(int x, int y, uint32_t color, std::string str) { MLX_PROFILE_FUNCTION(); auto res = _drawlist.emplace(std::move(str), color, x, y); diff --git a/src/renderer/text_pipeline.h b/src/renderer/text_pipeline.h index efb5058..caa6718 100644 --- a/src/renderer/text_pipeline.h +++ b/src/renderer/text_pipeline.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */ -/* Updated: 2023/12/14 17:39:51 by maldavid ### ########.fr */ +/* Updated: 2024/01/10 19:58:22 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,10 @@ namespace mlx TextID id; int x; int y; - int color; + uint32_t color; std::string text; - TextDrawData(std::string text, int _color, int _x, int _y); + TextDrawData(std::string text, uint32_t _color, int _x, int _y); void init(TextLibrary& library, Font* const font) noexcept; bool operator==(const TextDrawData& rhs) const { return text == rhs.text && x == rhs.x && y == rhs.y && color == rhs.color; } TextDrawData() = default; @@ -62,7 +62,7 @@ namespace mlx TextPutPipeline() = default; void init(Renderer* renderer) noexcept; - void put(int x, int y, int color, std::string str); + void put(int x, int y, uint32_t color, std::string str); inline void clear() { _drawlist.clear(); _library.clearLibrary(); } void loadFont(const std::filesystem::path& filepath, float scale); void render(std::array& sets);