mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 22:53:34 +00:00
working on texts and fonts
This commit is contained in:
47
runtime/Includes/Graphics/Drawable.h
git.filemode.normal_file
47
runtime/Includes/Graphics/Drawable.h
git.filemode.normal_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
13
runtime/Includes/Graphics/Enums.h
git.filemode.normal_file
@@ -0,0 +1,13 @@
|
||||
#ifndef __MLX_GRAPHICS_ENUMS__
|
||||
#define __MLX_GRAPHICS_ENUMS__
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
enum class DrawableType
|
||||
{
|
||||
Sprite,
|
||||
Text
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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 };
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user