diff --git a/includes/mlx.h b/includes/mlx.h index 3494443..7b6aa0d 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/25 15:09:04 by maldavid ### ########.fr */ +/* Updated: 2023/11/23 14:32:06 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -274,6 +274,30 @@ 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); +/** + * @brief Loads a font to be used by `mlx_string_put` + * + * @param mlx Internal MLX application + * @param win Internal window + * @param filepath Filepath to the font + * + * @return (void) + */ +void mlx_set_font(void* mlx, void* win, char* filepath); + +/** + * @brief Loads a font to be used by `mlx_string_put` and scales it + * + * @param mlx Internal MLX application + * @param win Internal window + * @param filepath Filepath to the font + * @param scale Scale to apply to the font + * + * @return (void) + */ +void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale); + + /** * @brief Clears the given window (resets all rendered data) * diff --git a/src/core/application.h b/src/core/application.h index 96c3373..007e525 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/11/17 11:57:42 by maldavid ### ########.fr */ +/* Updated: 2023/11/23 14:25:43 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,6 +56,8 @@ namespace mlx::core inline void loopHook(int (*f)(void*), void* param); inline void loopEnd() noexcept; + inline void loadFont(void* win, const std::filesystem::path& filepath, float scale); + void run() noexcept; ~Application(); diff --git a/src/core/application.inl b/src/core/application.inl index bcdf48c..85fbd8f 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -67,6 +67,11 @@ namespace mlx::core _graphics[*static_cast(win)]->stringPut(x, y, color, str); } + void Application::loadFont(void* win, const std::filesystem::path& filepath, float scale) + { + _graphics[*static_cast(win)]->loadFont(filepath, scale); + } + void Application::texturePut(void* win, void* img, int x, int y) { Texture* texture = static_cast(img); diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 7b424fa..37a67ba 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/11/14 11:43:30 by maldavid ### ########.fr */ +/* Updated: 2023/11/23 14:32:41 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,8 +27,11 @@ extern "C" return NULL; } mlx::Render_Core::get().init(); + mlx::core::Application* app = new mlx::core::Application; + if(app == nullptr) + mlx::core::error::report(e_kind::fatal_error, "Tout a pété"); init = true; - return new mlx::core::Application(); + return app; } void* mlx_new_window(mlx::core::Application* mlx, int w, int h, const char* title) @@ -169,6 +172,28 @@ extern "C" return 0; } + void mlx_set_font(mlx::core::Application* mlx, void* win, char* filepath) + { + std::filesystem::path file(filepath); + if(file.extension() != ".ttf" && file.extension() != ".tte") + { + mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath); + return; + } + mlx->loadFont(win, file, 16.f); + } + + void mlx_set_font_scale(mlx::core::Application* mlx, void* win, char* filepath, float scale) + { + std::filesystem::path file(filepath); + if(file.extension() != ".ttf" && file.extension() != ".tte") + { + mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath); + return; + } + mlx->loadFont(win, file, scale); + } + int mlx_clear_window(mlx::core::Application* mlx, void* win) { mlx->clearGraphicsSupport(win); diff --git a/src/core/graphics.h b/src/core/graphics.h index 2e43bd6..0f2860f 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/11/14 11:39:55 by maldavid ### ########.fr */ +/* Updated: 2023/11/23 14:26:06 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,6 +44,7 @@ namespace mlx 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 texturePut(Texture* texture, int x, int y); + inline void loadFont(const std::filesystem::path& filepath, float scale); ~GraphicsSupport(); diff --git a/src/core/graphics.inl b/src/core/graphics.inl index d7bc2aa..c205af9 100644 --- a/src/core/graphics.inl +++ b/src/core/graphics.inl @@ -46,4 +46,9 @@ namespace mlx { _textures_to_render.emplace(texture, x, y); } + + void GraphicsSupport::loadFont(const std::filesystem::path& filepath, float scale) + { + _text_put_pipeline->loadFont(filepath, scale); + } } diff --git a/src/renderer/core/render_core.cpp b/src/renderer/core/render_core.cpp index 4d0593e..13e46cc 100644 --- a/src/renderer/core/render_core.cpp +++ b/src/renderer/core/render_core.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/17 23:33:34 by maldavid #+# #+# */ -/* Updated: 2023/11/16 14:06:46 by maldavid ### ########.fr */ +/* Updated: 2023/11/20 12:04:51 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/text_pipeline.cpp b/src/renderer/text_pipeline.cpp index e91ae8e..1d6497a 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: 2023/11/14 05:36:09 by maldavid ### ########.fr */ +/* Updated: 2023/11/23 14:26:48 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,14 @@ #include #include +#include #include #define STB_TRUETYPE_IMPLEMENTATION #include +constexpr const int RANGE = 1024; + namespace mlx { TextDrawData::TextDrawData(std::string _text, int _color, int _x, int _y) : @@ -40,7 +43,7 @@ namespace mlx continue; stbtt_aligned_quad q; - stbtt_GetBakedQuad(cdata.data(), 512, 512, c - 32, &stb_x, &stb_y, &q, 1); + stbtt_GetBakedQuad(cdata.data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1); std::size_t index = vertexData.size(); @@ -59,25 +62,59 @@ namespace mlx std::shared_ptr text_data = std::make_shared(); text_data->init(text, std::move(vertexData), std::move(indexData)); id = library.addTextToLibrary(text_data); + + #ifdef DEBUG + core::error::report(e_kind::message, "Text put : registered new text to render"); + #endif } void TextPutPipeline::init(Renderer* renderer) noexcept { _renderer = renderer; - uint8_t tmp_bitmap[512 * 512]; - uint8_t vulkan_bitmap[(512 * 512) * 4]; - stbtt_BakeFontBitmap(dogica_ttf, 0, 6.0f, tmp_bitmap, 512, 512, 32, 96, _cdata.data()); - for(int i = 0, j = 0; i < 512 * 512; i++, j += 4) + uint8_t tmp_bitmap[RANGE * RANGE]; + uint8_t vulkan_bitmap[RANGE * RANGE * 4]; + stbtt_BakeFontBitmap(dogica_ttf, 0, 6.0f, tmp_bitmap, RANGE, RANGE, 32, 96, _cdata.data()); + for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4) { vulkan_bitmap[j + 0] = tmp_bitmap[i]; vulkan_bitmap[j + 1] = tmp_bitmap[i]; vulkan_bitmap[j + 2] = tmp_bitmap[i]; vulkan_bitmap[j + 3] = tmp_bitmap[i]; } - _atlas.create(vulkan_bitmap, 512, 512, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); + _atlas.create(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); _atlas.setDescriptor(renderer->getFragDescriptorSet().duplicate()); } + void TextPutPipeline::loadFont(const std::filesystem::path& filepath, float scale) + { + uint8_t tmp_bitmap[RANGE * RANGE]; + uint8_t vulkan_bitmap[RANGE * RANGE * 4]; + + std::ifstream file(filepath, std::ios::binary); + if(!file.is_open()) + { + core::error::report(e_kind::error, "Font load : cannot open font file, %s", filepath.string().c_str()); + return; + } + std::ifstream::pos_type fileSize = std::filesystem::file_size(filepath); + file.seekg(0, std::ios::beg); + std::vector bytes(fileSize); + file.read(reinterpret_cast(bytes.data()), fileSize); + file.close(); + + stbtt_BakeFontBitmap(bytes.data(), 0, scale, tmp_bitmap, RANGE, RANGE, 32, 96, _cdata.data()); + for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4) + { + vulkan_bitmap[j + 0] = tmp_bitmap[i]; + vulkan_bitmap[j + 1] = tmp_bitmap[i]; + vulkan_bitmap[j + 2] = tmp_bitmap[i]; + vulkan_bitmap[j + 3] = tmp_bitmap[i]; + } + destroy(); + _atlas.create(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); + _atlas.setDescriptor(_renderer->getFragDescriptorSet().duplicate()); + } + void TextPutPipeline::put(int x, int y, int color, std::string str) { auto res = _drawlist.emplace(std::move(str), color, x, y); @@ -99,6 +136,7 @@ namespace mlx void TextPutPipeline::destroy() noexcept { _library.clearLibrary(); + _drawlist.clear(); _atlas.destroy(); } } diff --git a/src/renderer/text_pipeline.h b/src/renderer/text_pipeline.h index e3e3052..2e68a9a 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/11/14 12:43:45 by maldavid ### ########.fr */ +/* Updated: 2023/11/23 14:26:34 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,6 +60,7 @@ namespace mlx void put(int x, int y, int color, std::string str); inline VkDescriptorSet getDescriptorSet() noexcept { return _atlas.getSet(); } inline void clear() { _drawlist.clear(); _library.clearLibrary(); } + void loadFont(const std::filesystem::path& filepath, float scale); void render(); void destroy() noexcept; diff --git a/test/.gdb_history b/test/.gdb_history new file mode 100644 index 0000000..1febdf8 --- /dev/null +++ b/test/.gdb_history @@ -0,0 +1,8 @@ +run +q +run +bt +q +run +bt +q diff --git a/test/font.ttf b/test/font.ttf new file mode 100644 index 0000000..85c1472 Binary files /dev/null and b/test/font.ttf differ diff --git a/test/main.c b/test/main.c index 562e762..81a3577 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/11/17 09:08:51 by maldavid ### ########.fr */ +/* Updated: 2023/11/23 14:32:15 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,10 @@ int update(t_mlx *mlx) } i++; if (i == 5000) + { mlx_clear_window(mlx->mlx, mlx->win); + mlx_set_font_scale(mlx->mlx, mlx->win, "font.ttf", 16.f); + } return (0); }