Skip to content

Commit

Permalink
working on texts and fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Oct 26, 2024
1 parent ef61b79 commit d4bd8b6
Show file tree
Hide file tree
Showing 31 changed files with 411 additions and 184 deletions.
8 changes: 7 additions & 1 deletion runtime/Includes/Core/Application.inl
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,14 @@ namespace mlx
font = std::make_shared<Font>("default", dogica_ttf, scale);
else
font = std::make_shared<Font>(filepath, scale);
if(!m_font_registry.IsFontKnown(font))
for(auto& gs : m_graphics)
{
if(gs)
gs->GetScene().BindFont(font);
}
if(m_font_registry.IsFontKnown(font))
return;
font->BuildFont();
m_font_registry.RegisterFont(font);
}

Expand Down
1 change: 1 addition & 0 deletions runtime/Includes/Core/Graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace mlx

[[nodiscard]] MLX_FORCEINLINE bool HasWindow() const noexcept { return m_has_window; }
[[nodiscard]] MLX_FORCEINLINE Renderer& GetRenderer() { return m_renderer; }
[[nodiscard]] MLX_FORCEINLINE Scene& GetScene() { return *p_scene; }

~GraphicsSupport();

Expand Down
32 changes: 26 additions & 6 deletions runtime/Includes/Core/Graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace mlx
void GraphicsSupport::ResetRenderData() noexcept
{
MLX_PROFILE_FUNCTION();
p_scene->ResetSprites();
p_scene->ResetScene();
m_put_pixel_manager.ResetRenderData();
m_draw_layer = 0;
m_pixelput_called = false;
Expand All @@ -27,10 +27,30 @@ namespace mlx
void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str)
{
MLX_PROFILE_FUNCTION();
(void)x;
(void)y;
(void)color;
(void)str;
Vec4f vec_color = {
static_cast<float>((color & 0x000000FF)) / 255.f,
static_cast<float>((color & 0x0000FF00) >> 8) / 255.f,
static_cast<float>((color & 0x00FF0000) >> 16) / 255.f,
static_cast<float>((color & 0xFF000000) >> 24) / 255.f
};

NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color);
if(!text)
{
Text& new_text = p_scene->CreateText(str);
new_text.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
new_text.SetColor(std::move(vec_color));
if(m_pixelput_called)
{
m_draw_layer++;
m_pixelput_called = false;
}
}
else if(!p_scene->IsTextAtGivenDrawLayer(str, m_draw_layer))
{
p_scene->BringToFront(text.Get());
m_draw_layer++;
}
}

void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y)
Expand All @@ -50,7 +70,7 @@ namespace mlx
}
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
{
p_scene->BringToFront(std::move(sprite));
p_scene->BringToFront(sprite.Get());
m_draw_layer++;
}
}
Expand Down
47 changes: 47 additions & 0 deletions runtime/Includes/Graphics/Drawable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef __MLX_DRAWABLE__
#define __MLX_DRAWABLE__

#include <Graphics/Enums.h>

namespace mlx
{
class Drawable
{
friend class Render2DPass;

public:
inline Drawable(DrawableType type) : m_type(type) {}

inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec2f position) noexcept { m_position = position; }

inline virtual void Update([[maybe_unused]] VkCommandBuffer cmd) {}

[[nodiscard]] MLX_FORCEINLINE const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] MLX_FORCEINLINE const Vec2f& GetPosition() const noexcept { return m_position; }
[[nodiscard]] MLX_FORCEINLINE std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
[[nodiscard]] MLX_FORCEINLINE DrawableType GetType() const noexcept { return m_type; }

inline virtual ~Drawable() { if(p_set) p_set->ReturnDescriptorSetToPool(); }

protected:
[[nodiscard]] inline bool IsSetInit() const noexcept { return p_set && p_set->IsInit(); }
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return p_set ? p_set->GetSet(frame_index) : VK_NULL_HANDLE; }

inline void UpdateDescriptorSet(std::shared_ptr<DescriptorSet> set)
{
p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType());
}

virtual void Bind(std::size_t frame_index, VkCommandBuffer cmd) = 0;

protected:
std::shared_ptr<DescriptorSet> p_set;
std::shared_ptr<Mesh> p_mesh;
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec2f m_position = Vec2f{ 0.0f, 0.0f };
DrawableType m_type;
};
}

#endif
13 changes: 13 additions & 0 deletions runtime/Includes/Graphics/Enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __MLX_GRAPHICS_ENUMS__
#define __MLX_GRAPHICS_ENUMS__

namespace mlx
{
enum class DrawableType
{
Sprite,
Text
};
}

#endif
5 changes: 4 additions & 1 deletion runtime/Includes/Graphics/Font.inl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace mlx

bool FontRegistry::IsFontKnown(std::shared_ptr<Font> font)
{
return m_fonts_registry.find(font) != m_fonts_registry.end();
return std::find_if(m_fonts_registry.begin(), m_fonts_registry.end(), [&font](std::shared_ptr<Font> rhs)
{
return font->GetName() == rhs->GetName() && font->GetScale() == rhs->GetScale();
}) != m_fonts_registry.end();
}
}
19 changes: 15 additions & 4 deletions runtime/Includes/Graphics/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#define __MLX_SCENE__

#include <Renderer/Renderer.h>
#include <Graphics/Text.h>
#include <Graphics/Font.h>
#include <Graphics/Sprite.h>
#include <Graphics/Drawable.h>
#include <Renderer/ViewerData.h>

namespace mlx
Expand All @@ -14,20 +17,28 @@ namespace mlx

Sprite& CreateSprite(NonOwningPtr<class Texture> texture) noexcept;
NonOwningPtr<Sprite> GetSpriteFromTextureAndPosition(NonOwningPtr<Texture> texture, const Vec2f& position) const;
void BringToFront(NonOwningPtr<Sprite> sprite);
void TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture);
bool IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const;

inline void ResetSprites() { m_sprites.clear(); }
Text& CreateText(const std::string& text) noexcept;
NonOwningPtr<Text> GetTextFromPositionAndColor(const std::string& text, const Vec2f& position, const Vec4f& color) const;
bool IsTextAtGivenDrawLayer(const std::string& text, std::uint64_t draw_layer) const;

[[nodiscard]] MLX_FORCEINLINE const std::vector<std::shared_ptr<Sprite>>& GetSprites() const noexcept { return m_sprites; }
inline void BindFont(std::shared_ptr<Font> font) { Verify((bool)font, "invalid fond pointer"); p_bound_font = font; }

void BringToFront(NonOwningPtr<Drawable> drawable);

inline void ResetScene() { m_drawables.clear(); }

[[nodiscard]] MLX_FORCEINLINE const std::vector<std::shared_ptr<Drawable>>& GetDrawables() const noexcept { return m_drawables; }
[[nodiscard]] MLX_FORCEINLINE ViewerData& GetViewerData() noexcept { return m_viewer_data; }

~Scene() = default;

private:
std::vector<std::shared_ptr<Sprite>> m_sprites;
std::vector<std::shared_ptr<Drawable>> m_drawables;
ViewerData m_viewer_data;
std::shared_ptr<Font> p_bound_font;
};
}

Expand Down
29 changes: 9 additions & 20 deletions runtime/Includes/Graphics/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,30 @@
#include <Graphics/Mesh.h>
#include <Renderer/Descriptor.h>
#include <Renderer/Image.h>
#include <Graphics/Drawable.h>

namespace mlx
{
class Sprite
class Sprite : public Drawable
{
friend class Render2DPass;

public:
Sprite(NonOwningPtr<Texture> texture);
Sprite(std::shared_ptr<Mesh> mesh, NonOwningPtr<Texture> texture);

inline void SetColor(Vec4f color) noexcept { m_color = color; }
inline void SetPosition(Vec2f position) noexcept { m_position = position; }
MLX_FORCEINLINE void Update(VkCommandBuffer cmd) override
{
Verify((bool)p_texture, "a sprite has no texture attached (internal mlx issue, please report to the devs)");
p_texture->Update(cmd);
}

[[nodiscard]] MLX_FORCEINLINE const Vec4f& GetColor() const noexcept { return m_color; }
[[nodiscard]] MLX_FORCEINLINE const Vec2f& GetPosition() const noexcept { return m_position; }
[[nodiscard]] MLX_FORCEINLINE std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
[[nodiscard]] MLX_FORCEINLINE NonOwningPtr<Texture> GetTexture() const { return p_texture; }

inline ~Sprite() { if(p_set) p_set->ReturnDescriptorSetToPool(); }
inline ~Sprite() = default;

private:
[[nodiscard]] inline bool IsSetInit() const noexcept { return p_set && p_set->IsInit(); }
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return p_set ? p_set->GetSet(frame_index) : VK_NULL_HANDLE; }

inline void UpdateDescriptorSet(std::shared_ptr<DescriptorSet> set)
{
p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType());
}

inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd) override
{
if(!p_set)
return;
Expand All @@ -45,11 +38,7 @@ namespace mlx
}

private:
std::shared_ptr<DescriptorSet> p_set;
NonOwningPtr<Texture> p_texture;
std::shared_ptr<Mesh> p_mesh;
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
Vec2f m_position = Vec2f{ 0.0f, 0.0f };
};
}

Expand Down
22 changes: 18 additions & 4 deletions runtime/Includes/Graphics/Text.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@
#define __MLX_TEXT__

#include <Graphics/Font.h>
#include <Graphics/Mesh.h>
#include <Graphics/Drawable.h>

namespace mlx
{
class Text
class Text : public Drawable
{
friend class Render2DPass;

public:
Text(const std::string& text, std::shared_ptr<Font> font);
inline Text(const std::string& text, std::shared_ptr<Font> font, std::shared_ptr<Mesh> mesh) : Drawable(DrawableType::Text) { Init(text, font, mesh); }

[[nodiscard]] inline const std::string& GetText() const { return m_text; }
[[nodiscard]] inline std::shared_ptr<Font> GetFont() const { return p_font; }
[[nodiscard]] MLX_FORCEINLINE std::uint32_t GetColor() const noexcept { return m_color; }

~Text();
virtual ~Text() = default;

private:
void Init(const std::string& text, std::shared_ptr<Font> font, std::shared_ptr<Mesh> mesh);

inline void Bind(std::size_t frame_index, VkCommandBuffer cmd) override
{
if(!p_set)
return;
p_set->SetImage(frame_index, 0, const_cast<Texture&>(p_font->GetTexture()));
p_set->Update(frame_index, cmd);
}

private:
std::shared_ptr<Font> p_font;
std::string m_text;
std::uint32_t m_color;
};
}

Expand Down
2 changes: 2 additions & 0 deletions runtime/Includes/PreCompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
#include <Utils/NonOwningPtr.h>
#include <Utils/NonCopyable.h>

constexpr const int RANGE = 1024;

using Handle = void*;

#endif
2 changes: 1 addition & 1 deletion runtime/Includes/Renderer/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace mlx
bool m_is_multisampled = false;
};

class Texture : public Image
class Texture: public Image
{
public:
Texture() = default;
Expand Down
1 change: 1 addition & 0 deletions runtime/Sources/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace mlx

m_fps.Init();
p_render_core = std::make_unique<RenderCore>();
LoadFont("default", 6.0f);
}

void Application::Run() noexcept
Expand Down
Loading

0 comments on commit d4bd8b6

Please sign in to comment.