Skip to content

Commit d08a97f

Browse files
committed
adding multiple font support
1 parent c013237 commit d08a97f

20 files changed

+131
-86
lines changed

includes/mlx.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
9-
/* Updated: 2023/12/11 20:35:41 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 16:27:44 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -282,7 +282,7 @@ MLX_API int mlx_string_put(void* mlx, void* win, int x, int y, int color, char*
282282
*
283283
* @param mlx Internal MLX application
284284
* @param win Internal window
285-
* @param filepath Filepath to the font
285+
* @param filepath Filepath to the font or "default" to reset to the embedded font
286286
*
287287
* @return (void)
288288
*/
@@ -293,7 +293,7 @@ MLX_API void mlx_set_font(void* mlx, void* win, char* filepath);
293293
*
294294
* @param mlx Internal MLX application
295295
* @param win Internal window
296-
* @param filepath Filepath to the font
296+
* @param filepath Filepath to the font or "default" to reset to the embedded font
297297
* @param scale Scale to apply to the font
298298
*
299299
* @return (void)

src/core/bridge.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
9-
/* Updated: 2023/12/11 15:56:18 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 17:47:17 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -178,7 +178,7 @@ extern "C"
178178
void mlx_set_font(void* mlx, void* win, char* filepath)
179179
{
180180
std::filesystem::path file(filepath);
181-
if(file.extension() != ".ttf" && file.extension() != ".tte")
181+
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
182182
{
183183
mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
184184
return;
@@ -189,7 +189,7 @@ extern "C"
189189
void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
190190
{
191191
std::filesystem::path file(filepath);
192-
if(file.extension() != ".ttf" && file.extension() != ".tte")
192+
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
193193
{
194194
mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
195195
return;

src/core/graphics.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
9-
/* Updated: 2023/12/10 22:20:38 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 17:14:30 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -51,11 +51,7 @@ namespace mlx
5151
sets[1] = _pixel_put_pipeline.getDescriptorSet();
5252
vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
5353
_pixel_put_pipeline.render(*_renderer);
54-
55-
sets[1] = _text_put_pipeline->getDescriptorSet();
56-
vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
57-
_text_put_pipeline->render();
58-
54+
_text_put_pipeline->render(sets);
5955
_renderer->endFrame();
6056

6157
for(auto& data : _textures_to_render)

src/core/graphics.inl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,7 @@ namespace mlx
4747
void GraphicsSupport::texturePut(Texture* texture, int x, int y)
4848
{
4949
_textures_to_render.emplace_back(texture, x, y);
50-
std::size_t hash = std::hash<TextureRenderData>{}(_textures_to_render.back());
51-
_textures_to_render.back().hash = hash;
52-
53-
auto it = std::find_if(_textures_to_render.begin(), _textures_to_render.end() - 1, [=](const TextureRenderData& rhs)
54-
{
55-
return rhs.hash == hash;
56-
});
57-
50+
auto it = std::find(_textures_to_render.begin(), _textures_to_render.end() - 1, _textures_to_render.back());
5851
if(it != _textures_to_render.end() - 1)
5952
_textures_to_render.erase(it);
6053
}

src/renderer/descriptors/vk_descriptor_set.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
9-
/* Updated: 2023/12/07 20:00:13 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 16:45:11 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -42,7 +42,7 @@ namespace mlx
4242
#endif
4343
}
4444

45-
void DescriptorSet::writeDescriptor(int binding, UBO* ubo) noexcept
45+
void DescriptorSet::writeDescriptor(int binding, UBO* ubo) const noexcept
4646
{
4747
auto device = Render_Core::get().getDevice().get();
4848

@@ -66,7 +66,7 @@ namespace mlx
6666
}
6767
}
6868

69-
void DescriptorSet::writeDescriptor(int binding, VkImageView view, VkSampler sampler) noexcept
69+
void DescriptorSet::writeDescriptor(int binding, VkImageView view, VkSampler sampler) const noexcept
7070
{
7171
auto device = Render_Core::get().getDevice().get();
7272

src/renderer/descriptors/vk_descriptor_set.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
9-
/* Updated: 2023/12/08 19:09:31 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 17:12:49 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -25,10 +25,10 @@ namespace mlx
2525
public:
2626
void init(class Renderer* renderer, class DescriptorPool* pool, class DescriptorSetLayout* layout);
2727

28-
void writeDescriptor(int binding, class UBO* ubo) noexcept;
29-
void writeDescriptor(int binding, VkImageView view, VkSampler sampler) noexcept;
28+
void writeDescriptor(int binding, class UBO* ubo) const noexcept;
29+
void writeDescriptor(int binding, VkImageView view, VkSampler sampler) const noexcept;
3030

31-
inline bool isInit() noexcept { return _pool != nullptr && _renderer != nullptr; }
31+
inline bool isInit() const noexcept { return _pool != nullptr && _renderer != nullptr; }
3232

3333
DescriptorSet duplicate();
3434

src/renderer/font.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/12/11 22:06:09 by kbz_8 #+# #+# */
9-
/* Updated: 2023/12/12 23:57:53 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 19:11:41 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -18,7 +18,7 @@ constexpr const int RANGE = 1024;
1818

1919
namespace mlx
2020
{
21-
Font::Font(Renderer& renderer, const std::filesystem::path& path, float scale) : non_copyable(), _name(path.string())
21+
Font::Font(Renderer& renderer, const std::filesystem::path& path, float scale) : non_copyable(), _name(path.string()), _scale(scale)
2222
{
2323
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE);
2424
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);
@@ -47,20 +47,20 @@ namespace mlx
4747
vulkan_bitmap[j + 3] = tmp_bitmap[i];
4848
}
4949
#ifdef DEBUG
50-
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, std::string(name + "_font_altas").c_str(), true);
50+
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, std::string(_name + "_font_altas").c_str(), true);
5151
#else
5252
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, nullptr, true);
5353
#endif
5454
_atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate());
5555
}
5656

57-
Font::Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale)
57+
Font::Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale) : non_copyable(), _name(name), _scale(scale)
5858
{
5959
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE);
6060
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);
6161
stbtt_pack_context pc;
6262
stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr);
63-
stbtt_PackFontRange(&pc, ttf_data.data(), 0, 6.0, 32, 96, _cdata.data());
63+
stbtt_PackFontRange(&pc, ttf_data.data(), 0, scale, 32, 96, _cdata.data());
6464
stbtt_PackEnd(&pc);
6565
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
6666
{
@@ -70,7 +70,7 @@ namespace mlx
7070
vulkan_bitmap[j + 3] = tmp_bitmap[i];
7171
}
7272
#ifdef DEBUG
73-
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, std::string(name + "_font_altas").c_str(), true);
73+
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, std::string(_name + "_font_altas").c_str(), true);
7474
#else
7575
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, nullptr, true);
7676
#endif

src/renderer/font.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/12/11 21:17:04 by kbz_8 #+# #+# */
9-
/* Updated: 2023/12/13 00:25:30 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 17:51:40 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -17,24 +17,29 @@
1717
#include <stb_truetype.h>
1818
#include <utils/non_copyable.h>
1919
#include <renderer/images/texture_atlas.h>
20+
#include <utils/combine_hash.h>
2021

2122
namespace mlx
2223
{
2324
class Font : public non_copyable
2425
{
2526
public:
27+
Font() = delete;
2628
Font(class Renderer& renderer, const std::filesystem::path& path, float scale);
2729
Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale);
2830
inline const std::string& getName() const { return _name; }
31+
inline float getScale() const noexcept { return _scale; }
2932
inline const std::array<stbtt_packedchar, 96>& getCharData() const { return _cdata; }
30-
inline VkDescriptorSet getDescriptorSet() noexcept { return _atlas.getSet(); }
31-
inline bool operator==(const Font& rhs) { return rhs._name == _name; }
33+
inline const TextureAtlas& getAtlas() const noexcept { return _atlas; }
34+
inline bool operator==(const Font& rhs) const { return rhs._name == _name; }
35+
inline bool operator!=(const Font& rhs) const { return rhs._name != _name; }
3236
~Font();
3337

3438
private:
3539
std::array<stbtt_packedchar, 96> _cdata;
3640
TextureAtlas _atlas;
3741
std::string _name;
42+
float _scale = 0;
3843
};
3944
}
4045

@@ -45,7 +50,9 @@ namespace std
4550
{
4651
std::size_t operator()(const mlx::Font& f) const noexcept
4752
{
48-
return std::hash<std::string>()(f.getName());
53+
std::size_t hash = 0;
54+
mlx::hashCombine(hash, f.getName(), f.getScale());
55+
return hash;
4956
}
5057
};
5158
}

src/renderer/images/texture.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
9-
/* Updated: 2023/12/08 19:10:09 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 16:28:07 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -76,16 +76,4 @@ namespace mlx
7676
};
7777
}
7878

79-
namespace std
80-
{
81-
template <>
82-
struct hash<mlx::TextureRenderData>
83-
{
84-
size_t operator()(const mlx::TextureRenderData& td) const noexcept
85-
{
86-
return std::hash<mlx::Texture*>()(td.texture) + std::hash<int>()(td.x) + std::hash<int>()(td.y);
87-
}
88-
};
89-
}
90-
9179
#endif

src/renderer/images/texture_atlas.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */
9-
/* Updated: 2023/11/14 05:36:46 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/14 16:39:54 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -36,7 +36,7 @@ namespace mlx
3636
}
3737
}
3838

39-
void TextureAtlas::render(Renderer& renderer, int x, int y, uint32_t ibo_size)
39+
void TextureAtlas::render(Renderer& renderer, int x, int y, uint32_t ibo_size) const
4040
{
4141
auto cmd = renderer.getActiveCmdBuffer().get();
4242

src/renderer/images/texture_atlas.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
9-
/* Updated: 2023/12/08 19:10:22 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 17:12:54 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -26,12 +26,12 @@ namespace mlx
2626
TextureAtlas() = default;
2727

2828
void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, const char* name, bool dedicated_memory = false);
29-
void render(class Renderer& renderer, int x, int y, uint32_t ibo_size);
29+
void render(class Renderer& renderer, int x, int y, uint32_t ibo_size) const;
3030
void destroy() noexcept override;
3131

3232
inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; }
3333
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
34-
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
34+
inline void updateSet(int binding) const noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
3535

3636
~TextureAtlas() = default;
3737

src/renderer/images/vk_image.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
9-
/* Updated: 2023/12/08 19:10:38 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 16:44:40 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -50,10 +50,10 @@ namespace mlx
5050

5151
inline VkImage get() noexcept { return _image; }
5252
inline VkImage operator()() noexcept { return _image; }
53-
inline VkImageView getImageView() noexcept { return _image_view; }
54-
inline VkFormat getFormat() noexcept { return _format; }
55-
inline VkImageTiling getTiling() noexcept { return _tiling; }
56-
inline VkSampler getSampler() noexcept { return _sampler; }
53+
inline VkImageView getImageView() const noexcept { return _image_view; }
54+
inline VkFormat getFormat() const noexcept { return _format; }
55+
inline VkImageTiling getTiling() const noexcept { return _tiling; }
56+
inline VkSampler getSampler() const noexcept { return _sampler; }
5757
inline uint32_t getWidth() const noexcept { return _width; }
5858
inline uint32_t getHeight() const noexcept { return _height; }
5959

src/renderer/pixel_put.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */
9-
/* Updated: 2023/12/10 22:33:59 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 18:26:03 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -22,14 +22,14 @@ namespace mlx
2222

2323
_buffer.create(Buffer::kind::dynamic, sizeof(uint32_t) * (width * height), VK_BUFFER_USAGE_TRANSFER_SRC_BIT, "__mlx_pixel_put_pipeline_texture");
2424
_buffer.mapMem(&_buffer_map);
25-
_cpu_map = std::vector<uint32_t>(height * width, 0);
25+
_cpu_map = std::vector<uint32_t>(height * width + 1, 0);
2626
_width = width;
2727
_height = height;
2828
}
2929

30-
void PixelPutPipeline::setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept
30+
void PixelPutPipeline::setPixel(int x, int y, uint32_t color) noexcept
3131
{
32-
if(x > _width || y > _height)
32+
if(x < 0 || y < 0 || x > static_cast<int>(_width) || y > static_cast<int>(_height))
3333
return;
3434
_cpu_map[(y * _width) + x] = color;
3535
_has_been_modified = true;

src/renderer/pixel_put.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */
9-
/* Updated: 2023/12/08 19:11:43 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/14 18:24:58 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -26,7 +26,7 @@ namespace mlx
2626

2727
void init(uint32_t width, uint32_t height, class Renderer& renderer) noexcept;
2828

29-
void setPixel(uint32_t x, uint32_t y, uint32_t color) noexcept;
29+
void setPixel(int x, int y, uint32_t color) noexcept;
3030
void present() noexcept;
3131
void render(class Renderer& renderer) noexcept;
3232
inline VkDescriptorSet getDescriptorSet() noexcept { return _texture.getSet(); }

0 commit comments

Comments
 (0)