From bada255057f1cd138588caa479f651f7fff3e3da Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 6 Apr 2023 17:26:19 +0200 Subject: [PATCH] working on text put pipeline --- includes/mlx.h | 4 +- src/core/application.cpp | 7 +- src/core/application.h | 15 ++-- src/core/application.inl | 25 ++++--- src/core/bridge.cpp | 8 ++- src/core/graphics.cpp | 3 +- src/core/graphics.h | 6 +- src/core/graphics.inl | 5 ++ .../text_pipeline.cpp} | 26 +++---- src/renderer/text_pipeline.h | 38 ++++++++++ src/utils/endian.h | 27 ++++++++ src/utils/xpm_reader.cpp | 69 ------------------- 12 files changed, 125 insertions(+), 108 deletions(-) rename src/{utils/xpm_reader.h => renderer/text_pipeline.cpp} (61%) create mode 100644 src/renderer/text_pipeline.h create mode 100644 src/utils/endian.h delete mode 100644 src/utils/xpm_reader.cpp diff --git a/includes/mlx.h b/includes/mlx.h index 3d89c00..555911e 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: 2023/04/06 15:27:07 by maldavid ### ########.fr */ +/* Updated: 2023/04/06 16:07:38 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,8 @@ void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height); void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height); void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height); +int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str); + int mlx_clear_window(void* mlx, void* win); int mlx_destroy_window(void* mlx, void* win); diff --git a/src/core/application.cpp b/src/core/application.cpp index 192239b..c699100 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,16 +6,15 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2023/04/06 15:27:21 by maldavid ### ########.fr */ +/* Updated: 2023/04/06 15:35:31 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include "application.h" #include #include -#include // for LSBFirst #include -#include +#include namespace mlx::core { @@ -59,7 +58,7 @@ namespace mlx::core char* map = static_cast(texture->openCPUmap()); *bits_per_pixel = sizeof(uint32_t) * 8; *size_line = texture->getWidth(); - *endian = LSBFirst; + *endian = isSystemBigEndian(); return map; } diff --git a/src/core/application.h b/src/core/application.h index 6fcd529..3278079 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: 2023/04/06 15:27:32 by maldavid ### ########.fr */ +/* Updated: 2023/04/06 16:08:34 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ namespace mlx::core Application() : _in() {} inline void getMousePos(int* x, int* y) noexcept; - inline void mouseMove(void* win_ptr, int x, int y) noexcept; + inline void mouseMove(void* win, int x, int y) noexcept; inline constexpr void enableAutoRepeat() noexcept; inline constexpr void disableAutoRepeat() noexcept; @@ -43,15 +43,16 @@ namespace mlx::core inline void getScreenSize(int* w, int* h) noexcept; inline void* newGraphicsSuport(std::size_t w, std::size_t h, std::string title); - inline void clearGraphicsSupport(void* win_ptr); - inline void destroyGraphicsSupport(void* win_ptr); + inline void clearGraphicsSupport(void* win); + inline void destroyGraphicsSupport(void* win); - inline void pixelPut(void* win_ptr, int x, int y, int color) const noexcept; + inline void pixelPut(void* win, int x, int y, int color) const noexcept; + inline void stringPut(void* win, int x, int y, int 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, ...) - char* mapTexture(void* img_ptr, int* bits_per_pixel, int* size_line, int* endian); - inline void texturePut(void* win_ptr, void* img, int x, int y); + char* mapTexture(void* img, int* bits_per_pixel, int* size_line, int* endian); + inline void texturePut(void* win, void* img, int x, int y); void destroyTexture(void* ptr); inline void loopHook(int (*f)(void*), void* param); diff --git a/src/core/application.inl b/src/core/application.inl index b929f96..8f4c16b 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -20,9 +20,9 @@ namespace mlx::core *y = _in.getY(); } - void Application::mouseMove(void* win_ptr, int x, int y) noexcept + void Application::mouseMove(void* win, int x, int y) noexcept { - SDL_WarpMouseInWindow(_graphics[*static_cast(win_ptr)]->getWindow()->getNativeWindow(), x, y); + SDL_WarpMouseInWindow(_graphics[*static_cast(win)]->getWindow()->getNativeWindow(), x, y); SDL_PumpEvents(); SDL_FlushEvent(SDL_MOUSEMOTION); } @@ -51,26 +51,31 @@ namespace mlx::core return static_cast(&_graphics.back()->getID()); } - void Application::clearGraphicsSupport(void* win_ptr) + void Application::clearGraphicsSupport(void* win) { - _graphics[*static_cast(win_ptr)]->clearRenderData(); + _graphics[*static_cast(win)]->clearRenderData(); } - void Application::destroyGraphicsSupport(void* win_ptr) + void Application::destroyGraphicsSupport(void* win) { - _graphics[*static_cast(win_ptr)].reset(); + _graphics[*static_cast(win)].reset(); } - void Application::pixelPut(void* win_ptr, int x, int y, int color) const noexcept + void Application::pixelPut(void* win, int x, int y, int color) const noexcept { - _graphics[*static_cast(win_ptr)]->pixelPut(x, y, color); + _graphics[*static_cast(win)]->pixelPut(x, y, color); } - void Application::texturePut(void* win_ptr, void* img, int x, int y) + void Application::stringPut(void* win, int x, int y, int color, char* str) + { + + } + + void Application::texturePut(void* win, void* img, int x, int y) { TextureID id = *static_cast(img); std::shared_ptr texture = _texture_lib.getTexture(id); - _graphics[*static_cast(win_ptr)]->texturePut(texture, x, y); + _graphics[*static_cast(win)]->texturePut(texture, x, y); } void Application::loopHook(int (*f)(void*), void* param) diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 07d799d..eb99721 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/04/06 15:27:54 by maldavid ### ########.fr */ +/* Updated: 2023/04/06 16:09:47 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -142,6 +142,12 @@ extern "C" return 0; } + int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str) + { + static_cast(mlx)->stringPut(win, x, y, color, str); + return 0; + } + int mlx_clear_window(void* mlx, void* win) { static_cast(mlx)->clearGraphicsSupport(win); diff --git a/src/core/graphics.cpp b/src/core/graphics.cpp index c689afd..b9e046f 100644 --- a/src/core/graphics.cpp +++ b/src/core/graphics.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */ -/* Updated: 2023/04/02 17:33:47 by maldavid ### ########.fr */ +/* Updated: 2023/04/06 16:39:13 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ namespace mlx _renderer->setWindow(_window.get()); _renderer->init(); _pixel_put_pipeline.init(w, h, *_renderer); + _text_put_pipeline.init(*_renderer); } void GraphicsSupport::endRender() noexcept diff --git a/src/core/graphics.h b/src/core/graphics.h index f72b484..eed289c 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: 2023/04/03 14:23:57 by maldavid ### ########.fr */ +/* Updated: 2023/04/06 16:38:50 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ namespace mlx inline void clearRenderData() noexcept; inline void pixelPut(int x, int y, int color) noexcept; + inline void stringPut(int x, int y, int color, std::string str); inline void texturePut(std::shared_ptr texture, int x, int y); void endRender() noexcept; @@ -47,9 +48,10 @@ namespace mlx private: std::unordered_set _textures_to_render; + PixelPutPipeline _pixel_put_pipeline; + TextPutPipeline _text_put_pipeline; glm::mat4 _proj = glm::mat4(1.0); std::shared_ptr _window; - PixelPutPipeline _pixel_put_pipeline; std::unique_ptr _renderer; int _id; }; diff --git a/src/core/graphics.inl b/src/core/graphics.inl index 4cc0abd..95503bd 100644 --- a/src/core/graphics.inl +++ b/src/core/graphics.inl @@ -36,6 +36,11 @@ namespace mlx _pixel_put_pipeline.setPixel(x, y, color); } + void GraphicsSupport::stringPut(int x, int y, int color, std::string str) + { + _text_put_pipeline.put(x, y, color, str); + } + void GraphicsSupport::texturePut(std::shared_ptr texture, int x, int y) { _textures_to_render.emplace(texture, x, y); diff --git a/src/utils/xpm_reader.h b/src/renderer/text_pipeline.cpp similarity index 61% rename from src/utils/xpm_reader.h rename to src/renderer/text_pipeline.cpp index ec1e33c..1d72e7b 100644 --- a/src/utils/xpm_reader.h +++ b/src/renderer/text_pipeline.cpp @@ -1,26 +1,26 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* xpm_reader.h :+: :+: :+: */ +/* text_pipeline.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/04/05 13:25:55 by maldavid #+# #+# */ -/* Updated: 2023/04/05 13:35:48 by maldavid ### ########.fr */ +/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ +/* Updated: 2023/04/06 16:42:10 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef __MLX_XPM_READER__ -#define __MLX_XPM_READER__ - -#include -#include -#include +#include namespace mlx { - std::vector parseXpmData(char** data, int* w, int* h); - std::vector parseXpmFile(std::filesystem::path file, int* w, int* h); -} + void TextPutPipeline::init(Renderer& renderer) noexcept + { -#endif + } + + void TextPutPipeline::put(int x, int y, int color, std::string str) + { + + } +} diff --git a/src/renderer/text_pipeline.h b/src/renderer/text_pipeline.h new file mode 100644 index 0000000..6127d11 --- /dev/null +++ b/src/renderer/text_pipeline.h @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* text_pipeline.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */ +/* Updated: 2023/04/06 16:41:09 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_TEXT_PIPELINE__ +#define __MLX_TEXT_PIPELINE__ + +#include +#include +#include + +namespace mlx +{ + class TextPutPipeline + { + public: + TextPutPipeline() = default; + + void init(Renderer& renderer) noexcept; + + void put(int x, int y, int color, std::string str); + + ~TextPutPipeline() = default; + + private: + + }; +} + +#endif diff --git a/src/utils/endian.h b/src/utils/endian.h new file mode 100644 index 0000000..4ff8ede --- /dev/null +++ b/src/utils/endian.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* endian.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/06 15:33:15 by maldavid #+# #+# */ +/* Updated: 2023/04/06 15:35:25 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_ENDIAN__ +#define __MLX_ENDIAN__ + +namespace mlx +{ + inline bool isSystemBigEndian() + { + const int value = 0x01; + const void* address = static_cast(&value); + const unsigned char* least_significant_address = static_cast(address); + return (*least_significant_address != 0x01); + } +} + +#endif diff --git a/src/utils/xpm_reader.cpp b/src/utils/xpm_reader.cpp deleted file mode 100644 index 021c05b..0000000 --- a/src/utils/xpm_reader.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* xpm_reader.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/04/05 13:37:21 by maldavid #+# #+# */ -/* Updated: 2023/04/06 15:20:14 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -namespace mlx -{ - std::vector parseXpmFile(std::filesystem::path file, int* w, int* h) - { - return {}; - } - - std::vector parseXpmData(char** data, int* w, int* h) - { - if(data == nullptr) - { - core::error::report(e_kind::error, "Xpm reader : null data"); - return {}; - } - - int32_t width = -1; - int32_t height = -1; - int32_t ncolors = -1; - int32_t cpp = -1; // chars per pixels - // - std::stringstream stream; - - stream.str(data[0]); - stream >> width; - stream >> height; - stream >> ncolors; - stream >> cpp; - if(width == -1 || height == -1 || ncolors == -1 || cpp == -1 || !stream.eof()) - { - core::error::report(e_kind::error, "Xpm reader : invalid pixmap description"); - return {}; - } - - *w = width; - *h = height; - - stream.clear(); - std::vector colors; - colors.reserve(ncolors); - std::cout << ncolors << std::endl; - for(int32_t i = 1; i < ncolors; ++i) - { - std::cout << (bool)(i < ncolors) << std::endl; - stream.str(data[i]); - std::string c; - stream >> c; - - std::cout << ncolors << " " << i << " " << c << std::endl; - } - } -}