Skip to content

Commit

Permalink
working on fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Oct 21, 2024
1 parent 05d07e5 commit e7cb785
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 46 deletions.
2 changes: 2 additions & 0 deletions runtime/Includes/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Core/SDLManager.h>
#include <Core/Memory.h>
#include <Core/Fps.h>
#include <Graphics/Font.h>

namespace mlx
{
Expand Down Expand Up @@ -54,6 +55,7 @@ namespace mlx
std::unique_ptr<SDLManager> p_sdl_manager;
FpsManager m_fps;
Inputs m_in;
FontRegistry m_font_registry;
ImageRegistry m_image_registry;
std::vector<std::unique_ptr<GraphicsSupport>> m_graphics;
std::function<int(Handle)> f_loop_hook;
Expand Down
26 changes: 14 additions & 12 deletions runtime/Includes/Embedded/2DFragment.spv.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
3,2,35,7,0,0,1,0,39,0,0,0,48,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
3,2,35,7,0,0,1,0,39,0,0,0,51,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
3,0,0,0,0,0,1,0,0,0,15,0,8,0,4,0,0,0,28,0,0,0,109,97,105,110,0,0,0,0,
10,0,0,0,16,0,0,0,22,0,0,0,16,0,3,0,28,0,0,0,7,0,0,0,3,0,3,0,0,0,
0,0,100,0,0,0,5,0,4,0,19,0,0,0,86,101,114,116,79,117,116,0,6,0,5,0,19,0,0,0,
Expand Down Expand Up @@ -29,14 +29,16 @@
7,0,0,0,248,0,2,0,29,0,0,0,59,0,4,0,24,0,0,0,30,0,0,0,7,0,0,0,59,0,
4,0,20,0,0,0,31,0,0,0,7,0,0,0,65,0,5,0,13,0,0,0,32,0,0,0,31,0,0,0,
12,0,0,0,63,0,3,0,32,0,0,0,10,0,0,0,65,0,5,0,18,0,0,0,33,0,0,0,31,0,
0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,61,0,4,0,3,0,0,0,34,0,0,0,
5,0,0,0,65,0,5,0,18,0,0,0,35,0,0,0,31,0,0,0,17,0,0,0,61,0,4,0,14,0,
0,0,36,0,0,0,35,0,0,0,87,0,5,0,8,0,0,0,37,0,0,0,34,0,0,0,36,0,0,0,
65,0,5,0,13,0,0,0,38,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,38,0,0,0,37,0,
0,0,65,0,5,0,13,0,0,0,42,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,
43,0,0,0,42,0,0,0,81,0,5,0,1,0,0,0,44,0,0,0,43,0,0,0,3,0,0,0,180,0,
5,0,27,0,0,0,45,0,0,0,44,0,0,0,26,0,0,0,247,0,3,0,39,0,0,0,0,0,0,0,
250,0,4,0,45,0,0,0,40,0,0,0,41,0,0,0,248,0,2,0,40,0,0,0,252,0,1,0,248,0,
2,0,41,0,0,0,249,0,2,0,39,0,0,0,248,0,2,0,39,0,0,0,61,0,4,0,23,0,0,0,
46,0,0,0,30,0,0,0,81,0,5,0,8,0,0,0,47,0,0,0,46,0,0,0,0,0,0,0,62,0,
3,0,22,0,0,0,47,0,0,0,253,0,1,0,56,0,1,0
0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,65,0,5,0,13,0,0,0,34,0,0,0,
31,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,35,0,0,0,34,0,0,0,61,0,4,0,3,0,
0,0,36,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,37,0,0,0,31,0,0,0,17,0,0,0,
61,0,4,0,14,0,0,0,38,0,0,0,37,0,0,0,87,0,5,0,8,0,0,0,39,0,0,0,36,0,
0,0,38,0,0,0,133,0,5,0,8,0,0,0,40,0,0,0,35,0,0,0,39,0,0,0,65,0,5,0,
13,0,0,0,41,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,41,0,0,0,40,0,0,0,65,0,
5,0,13,0,0,0,45,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,46,0,0,0,
45,0,0,0,81,0,5,0,1,0,0,0,47,0,0,0,46,0,0,0,3,0,0,0,180,0,5,0,27,0,
0,0,48,0,0,0,47,0,0,0,26,0,0,0,247,0,3,0,42,0,0,0,0,0,0,0,250,0,4,0,
48,0,0,0,43,0,0,0,44,0,0,0,248,0,2,0,43,0,0,0,252,0,1,0,248,0,2,0,44,0,
0,0,249,0,2,0,42,0,0,0,248,0,2,0,42,0,0,0,61,0,4,0,23,0,0,0,49,0,0,0,
30,0,0,0,81,0,5,0,8,0,0,0,50,0,0,0,49,0,0,0,0,0,0,0,62,0,3,0,22,0,
0,0,50,0,0,0,253,0,1,0,56,0,1,0
74 changes: 43 additions & 31 deletions runtime/Includes/Embedded/ScreenFragment.spv.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
3,2,35,7,0,0,1,0,39,0,0,0,34,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
3,0,0,0,0,0,1,0,0,0,15,0,7,0,4,0,0,0,21,0,0,0,109,97,105,110,0,0,0,0,
10,0,0,0,18,0,0,0,16,0,3,0,21,0,0,0,7,0,0,0,3,0,3,0,0,0,0,0,100,0,
0,0,5,0,4,0,14,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,14,0,0,0,0,0,0,0,
117,118,0,0,5,0,4,0,19,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,19,0,0,0,0,0,
0,0,99,111,108,111,114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0,
5,0,3,0,10,0,0,0,117,118,0,0,5,0,4,0,18,0,0,0,99,111,108,111,114,0,0,0,5,0,
4,0,21,0,0,0,109,97,105,110,0,0,0,0,71,0,4,0,5,0,0,0,33,0,0,0,0,0,0,0,
71,0,4,0,5,0,0,0,34,0,0,0,0,0,0,0,71,0,4,0,10,0,0,0,30,0,0,0,0,0,
0,0,71,0,4,0,18,0,0,0,30,0,0,0,0,0,0,0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0,28,0,0,0,27,0,0,0,87,0,5,0,16,0,0,0,29,0,
0,0,26,0,0,0,28,0,0,0,65,0,5,0,31,0,0,0,30,0,0,0,23,0,0,0,12,0,0,0,
62,0,3,0,30,0,0,0,29,0,0,0,61,0,4,0,19,0,0,0,32,0,0,0,23,0,0,0,81,0,
5,0,16,0,0,0,33,0,0,0,32,0,0,0,0,0,0,0,62,0,3,0,18,0,0,0,33,0,0,0,
253,0,1,0,56,0,1,0
3,2,35,7,0,0,1,0,39,0,0,0,52,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,11,0,
6,0,29,0,0,0,71,76,83,76,46,115,116,100,46,52,53,48,0,0,0,0,14,0,3,0,0,0,0,0,
1,0,0,0,15,0,7,0,4,0,0,0,31,0,0,0,109,97,105,110,0,0,0,0,15,0,0,0,23,0,
0,0,16,0,3,0,31,0,0,0,7,0,0,0,3,0,3,0,0,0,0,0,100,0,0,0,5,0,4,0,
19,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,19,0,0,0,0,0,0,0,117,118,0,0,5,0,
4,0,24,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,24,0,0,0,0,0,0,0,99,111,108,111,
114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0,5,0,3,0,15,0,
0,0,117,118,0,0,5,0,4,0,23,0,0,0,99,111,108,111,114,0,0,0,5,0,6,0,30,0,0,0,
76,105,110,101,97,114,84,111,115,82,71,66,0,0,0,0,5,0,4,0,31,0,0,0,109,97,105,110,0,0,
0,0,71,0,4,0,5,0,0,0,33,0,0,0,0,0,0,0,71,0,4,0,5,0,0,0,34,0,0,0,
0,0,0,0,71,0,4,0,15,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,23,0,0,0,30,0,
0,0,0,0,0,0,72,0,5,0,19,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0,
24,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,22,0,3,0,1,0,0,0,32,0,0,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,0,32,0,0,0,12,0,7,0,
6,0,0,0,35,0,0,0,29,0,0,0,26,0,0,0,34,0,0,0,10,0,0,0,254,0,2,0,35,0,
0,0,56,0,1,0,54,0,5,0,11,0,0,0,31,0,0,0,0,0,0,0,12,0,0,0,248,0,2,0,
36,0,0,0,59,0,4,0,25,0,0,0,37,0,0,0,7,0,0,0,59,0,4,0,7,0,0,0,38,0,
0,0,7,0,0,0,59,0,4,0,20,0,0,0,39,0,0,0,7,0,0,0,65,0,5,0,18,0,0,0,
40,0,0,0,39,0,0,0,17,0,0,0,63,0,3,0,40,0,0,0,15,0,0,0,61,0,4,0,3,0,
0,0,41,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,42,0,0,0,39,0,0,0,17,0,0,0,
61,0,4,0,13,0,0,0,43,0,0,0,42,0,0,0,87,0,5,0,21,0,0,0,44,0,0,0,41,0,
0,0,43,0,0,0,79,0,8,0,6,0,0,0,45,0,0,0,44,0,0,0,44,0,0,0,0,0,0,0,
1,0,0,0,2,0,0,0,62,0,3,0,38,0,0,0,45,0,0,0,57,0,5,0,6,0,0,0,46,0,
0,0,30,0,0,0,38,0,0,0,80,0,5,0,21,0,0,0,47,0,0,0,46,0,0,0,28,0,0,0,
65,0,5,0,49,0,0,0,48,0,0,0,37,0,0,0,17,0,0,0,62,0,3,0,48,0,0,0,47,0,
0,0,61,0,4,0,24,0,0,0,50,0,0,0,37,0,0,0,81,0,5,0,21,0,0,0,51,0,0,0,
50,0,0,0,0,0,0,0,62,0,3,0,23,0,0,0,51,0,0,0,253,0,1,0,56,0,1,0
21 changes: 19 additions & 2 deletions runtime/Includes/Graphics/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace mlx
inline const std::string& GetName() const { return m_name; }
inline float GetScale() const noexcept { return m_scale; }
inline const std::array<stbtt_packedchar, 96>& GetCharData() const { return m_cdata; }
inline const Texture& GetTexture() const noexcept { return m_atlas; }
inline const Font& GetFont() const noexcept { return m_atlas; }
inline bool operator==(const Font& rhs) const { return rhs.m_name == m_name && rhs.m_scale == m_scale; }
inline bool operator!=(const Font& rhs) const { return rhs.m_name != m_name || rhs.m_scale != m_scale; }

Expand All @@ -27,11 +27,28 @@ namespace mlx

private:
std::array<stbtt_packedchar, 96> m_cdata;
Texture m_atlas;
Font m_atlas;
std::variant<std::filesystem::path, std::vector<std::uint8_t>> m_build_data;
std::string m_name;
float m_scale;
};

class FontRegistry
{
public:
FontRegistry() = default;

inline void RegisterFont(std::shared_ptr<Font> font);
inline void UnregisterFont(std::shared_ptr<Font> font);
inline bool IsFontKnown(std::shared_ptr<Font> font);

~FontRegistry() = default;

private:
std::unordered_set<std::shared_ptr<Font>> m_fonts_registry;
};
}

#include <Graphics/Font.inl>

#endif
20 changes: 20 additions & 0 deletions runtime/Includes/Graphics/Font.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <Graphics/Font.h>

namespace mlx
{
void FontRegistry::RegisterFont(std::shared_ptr<Font> font)
{
m_fonts_registry.insert(font);
}

void FontRegistry::UnregisterFont(std::shared_ptr<Font> font)
{
m_fonts_registry.erase(font);
}

bool FontRegistry::IsFontKnown(std::shared_ptr<Font> font)
{
return m_fonts_registry.find(font) != m_fonts_registry.end();
}
}
1 change: 0 additions & 1 deletion runtime/Sources/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace mlx
if(f_loop_hook)
f_loop_hook(p_param);

#pragma omp parallel for
for(auto& gs : m_graphics)
{
if(gs)
Expand Down
52 changes: 52 additions & 0 deletions runtime/Sources/Graphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,56 @@

namespace mlx
{
constexpr const int RANGE = 1024;

void Font::BuildFont()
{
MLX_PROFILE_FUNCTION();
std::vector<std::uint8_t> file_bytes;
if(std::holds_alternative<std::filesystem::path>(m_build_data))
{
std::ifstream file(std::get<std::filesystem::path>(m_build_data), std::ios::binary);
if(!file.is_open())
{
Error("Font : cannot open font file, %", m_name);
return;
}
std::ifstream::pos_type file_size = std::filesystem::file_size(std::get<std::filesystem::path>(m_build_data));
file.seekg(0, std::ios::beg);
file_bytes.resize(file_size);
file.read(reinterpret_cast<char*>(file_bytes.data()), file_size);
file.close();
}

CPUBuffer tmp_bitmap(RANGE * RANGE);

stbtt_pack_context pc;
stbtt_PackBegin(&pc, tmp_bitmap.GetData(), RANGE, RANGE, RANGE, 1, nullptr);
if(std::holds_alternative<std::filesystem::path>(m_build_data))
stbtt_PackFontRange(&pc, file_bytes.data(), 0, m_scale, 32, 96, m_cdata.data());
else
stbtt_PackFontRange(&pc, std::get<std::vector<std::uint8_t>>(m_build_data).data(), 0, m_scale, 32, 96, m_cdata.data());
stbtt_PackEnd(&pc);

CPUBuffer vulkan_bitmap(RANGE * RANGE * 4);

for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
{
vulkan_bitmap.GetData()[j + 0] = tmp_bitmap.GetData()[i];
vulkan_bitmap.GetData()[j + 1] = tmp_bitmap.GetData()[i];
vulkan_bitmap.GetData()[j + 2] = tmp_bitmap.GetData()[i];
vulkan_bitmap.GetData()[j + 3] = tmp_bitmap.GetData()[i];
}

#ifdef DEBUG
m_atlas.Init(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_SRGB, false, m_name + "_font_altas");
#else
m_atlas.Init(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_SRGB, false, {});
#endif
}

void Font::Destroy()
{
m_atlas.Destroy();
}
}

0 comments on commit e7cb785

Please sign in to comment.