new feature, font loading

This commit is contained in:
2023-11-23 14:37:42 +01:00
parent 9f869d93e2
commit d5a8b96be8
12 changed files with 127 additions and 15 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
*

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();

View File

@@ -67,6 +67,11 @@ namespace mlx::core
_graphics[*static_cast<int*>(win)]->stringPut(x, y, color, str);
}
void Application::loadFont(void* win, const std::filesystem::path& filepath, float scale)
{
_graphics[*static_cast<int*>(win)]->loadFont(filepath, scale);
}
void Application::texturePut(void* win, void* img, int x, int y)
{
Texture* texture = static_cast<Texture*>(img);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();

View File

@@ -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);
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <fstream>
#include <utils/dogica_ttf.h>
#include <iostream>
#include <cstdio>
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
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<TextData> text_data = std::make_shared<TextData>();
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<uint8_t> bytes(fileSize);
file.read(reinterpret_cast<char*>(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();
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

8
test/.gdb_history git.filemode.normal_file
View File

@@ -0,0 +1,8 @@
run
q
run
bt
q
run
bt
q

BIN
test/font.ttf git.filemode.normal_file

Binary file not shown.

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}