working on texts and fonts

This commit is contained in:
2024-10-27 01:14:07 +02:00
parent 3aaa5df929
commit 028cb57ff4
31 changed files with 414 additions and 187 deletions

View File

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

View File

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

View File

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

47
runtime/Includes/Graphics/Drawable.h git.filemode.normal_file
View File

@@ -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
runtime/Includes/Graphics/Enums.h git.filemode.normal_file
View File

@@ -0,0 +1,13 @@
#ifndef __MLX_GRAPHICS_ENUMS__
#define __MLX_GRAPHICS_ENUMS__
namespace mlx
{
enum class DrawableType
{
Sprite,
Text
};
}
#endif

View File

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

View File

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

View File

@@ -6,10 +6,11 @@
#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;
@@ -17,26 +18,18 @@ namespace mlx
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; }
[[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(); }
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)
MLX_FORCEINLINE void Update(VkCommandBuffer cmd) override
{
p_set = RenderCore::Get().GetDescriptorPoolManager().GetAvailablePool().RequestDescriptorSet(set->GetShaderLayout(), set->GetShaderType());
Verify((bool)p_texture, "a sprite has no texture attached (internal mlx issue, please report to the devs)");
p_texture->Update(cmd);
}
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
[[nodiscard]] MLX_FORCEINLINE NonOwningPtr<Texture> GetTexture() const { return p_texture; }
inline ~Sprite() = default;
private:
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd) override
{
if(!p_set)
return;
@@ -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 };
};
}

View File

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

View File

@@ -96,6 +96,8 @@
#include <Utils/NonOwningPtr.h>
#include <Utils/NonCopyable.h>
constexpr const int RANGE = 1024;
using Handle = void*;
#endif

View File

@@ -71,7 +71,7 @@ namespace mlx
bool m_is_multisampled = false;
};
class Texture : public Image
class Texture: public Image
{
public:
Texture() = default;