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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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
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;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -96,6 +96,8 @@
|
||||
#include <Utils/NonOwningPtr.h>
|
||||
#include <Utils/NonCopyable.h>
|
||||
|
||||
constexpr const int RANGE = 1024;
|
||||
|
||||
using Handle = void*;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace mlx
|
||||
|
||||
m_fps.Init();
|
||||
p_render_core = std::make_unique<RenderCore>();
|
||||
LoadFont("default", 6.0f);
|
||||
}
|
||||
|
||||
void Application::Run() noexcept
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
constexpr const int RANGE = 1024;
|
||||
|
||||
void Font::BuildFont()
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
@@ -56,6 +54,8 @@ namespace mlx
|
||||
#else
|
||||
m_atlas.Init(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_SRGB, false, {});
|
||||
#endif
|
||||
|
||||
DebugLog("Font: loaded %", m_name);
|
||||
}
|
||||
|
||||
void Font::Destroy()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "Graphics/Enums.h"
|
||||
#include <PreCompiled.h>
|
||||
#include <Graphics/Scene.h>
|
||||
#include <Renderer/Renderer.h>
|
||||
@@ -10,64 +11,117 @@ namespace mlx
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Verify((bool)texture, "Scene: invalid texture (internal mlx issue, please report to devs)");
|
||||
|
||||
#pragma omp parallel for
|
||||
for(auto& sprite : m_sprites)
|
||||
for(auto& drawable : m_drawables)
|
||||
{
|
||||
if(texture->GetWidth() == sprite->GetTexture()->GetWidth() && texture->GetHeight() == sprite->GetTexture()->GetHeight())
|
||||
if(!drawable || drawable->GetType() != DrawableType::Sprite)
|
||||
continue;
|
||||
if(texture->GetWidth() == static_cast<Sprite*>(drawable.get())->GetTexture()->GetWidth() && texture->GetHeight() == static_cast<Sprite*>(drawable.get())->GetTexture()->GetHeight())
|
||||
{
|
||||
std::shared_ptr<Sprite> new_sprite = std::make_shared<Sprite>(sprite->GetMesh(), texture);
|
||||
m_sprites.push_back(new_sprite);
|
||||
std::shared_ptr<Sprite> new_sprite = std::make_shared<Sprite>(drawable->GetMesh(), texture);
|
||||
m_drawables.push_back(new_sprite);
|
||||
return *new_sprite;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Sprite> sprite = std::make_shared<Sprite>(texture);
|
||||
m_sprites.push_back(sprite);
|
||||
m_drawables.push_back(sprite);
|
||||
return *sprite;
|
||||
}
|
||||
|
||||
NonOwningPtr<Sprite> Scene::GetSpriteFromTextureAndPosition(NonOwningPtr<Texture> texture, const Vec2f& position) const
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [&texture, &position](std::shared_ptr<Sprite> sprite)
|
||||
auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture, &position](std::shared_ptr<Drawable> drawable)
|
||||
{
|
||||
return sprite->GetTexture() == texture && sprite->GetPosition().x == position.x && sprite->GetPosition().y == position.y;
|
||||
if(!drawable || drawable->GetType() != DrawableType::Sprite)
|
||||
return false;
|
||||
return static_cast<Sprite*>(drawable.get())->GetTexture() == texture && drawable->GetPosition() == position;
|
||||
});
|
||||
return (it != m_sprites.end() ? it->get() : nullptr);
|
||||
}
|
||||
|
||||
void Scene::BringToFront(NonOwningPtr<Sprite> sprite)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto it = std::find_if(m_sprites.begin(), m_sprites.end(), [&sprite](std::shared_ptr<Sprite> sprite_ptr)
|
||||
{
|
||||
return sprite_ptr.get() == sprite.Get();
|
||||
});
|
||||
if(it == m_sprites.end())
|
||||
return;
|
||||
std::rotate(it, it + 1, m_sprites.end());
|
||||
return static_cast<Sprite*>(it != m_drawables.end() ? it->get() : nullptr);
|
||||
}
|
||||
|
||||
void Scene::TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto it = m_sprites.begin();
|
||||
auto it = m_drawables.begin();
|
||||
do
|
||||
{
|
||||
it = std::find_if(m_sprites.begin(), m_sprites.end(), [&texture](std::shared_ptr<Sprite> sprite)
|
||||
it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture](std::shared_ptr<Drawable> drawable)
|
||||
{
|
||||
return sprite->GetTexture() == texture;
|
||||
if(!drawable || drawable->GetType() != DrawableType::Sprite)
|
||||
return false;
|
||||
return static_cast<Sprite*>(drawable.get())->GetTexture() == texture;
|
||||
});
|
||||
if(it != m_sprites.end())
|
||||
m_sprites.erase(it);
|
||||
} while(it != m_sprites.end());
|
||||
if(it != m_drawables.end())
|
||||
m_drawables.erase(it);
|
||||
} while(it != m_drawables.end());
|
||||
}
|
||||
|
||||
bool Scene::IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(draw_layer >= m_sprites.size())
|
||||
if(draw_layer >= m_drawables.size())
|
||||
return false;
|
||||
return m_sprites[draw_layer]->GetTexture() == texture;
|
||||
if(!m_drawables[draw_layer] || m_drawables[draw_layer]->GetType() != DrawableType::Sprite)
|
||||
return false;
|
||||
return static_cast<Sprite*>(m_drawables[draw_layer].get())->GetTexture() == texture;
|
||||
}
|
||||
|
||||
Text& Scene::CreateText(const std::string& text) noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
|
||||
Assert((bool)p_bound_font, "no font bound");
|
||||
|
||||
for(auto& drawable : m_drawables)
|
||||
{
|
||||
if(!drawable || drawable->GetType() != DrawableType::Text)
|
||||
continue;
|
||||
if(text == static_cast<Text*>(drawable.get())->GetText() && p_bound_font == static_cast<Text*>(drawable.get())->GetFont())
|
||||
{
|
||||
std::shared_ptr<Text> new_text = std::make_shared<Text>(text, p_bound_font, drawable->GetMesh());
|
||||
m_drawables.push_back(new_text);
|
||||
return *new_text;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Text> new_text = std::make_shared<Text>(text, p_bound_font);
|
||||
m_drawables.push_back(new_text);
|
||||
return *new_text;
|
||||
}
|
||||
|
||||
NonOwningPtr<Text> Scene::GetTextFromPositionAndColor(const std::string& text, const Vec2f& position, const Vec4f& color) const
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&text, &position, &color](std::shared_ptr<Drawable> drawable)
|
||||
{
|
||||
if(!drawable || drawable->GetType() != DrawableType::Text)
|
||||
return false;
|
||||
return static_cast<Text*>(drawable.get())->GetText() == text && drawable->GetPosition() == position && drawable->GetColor() == color;
|
||||
});
|
||||
return static_cast<Text*>(it != m_drawables.end() ? it->get() : nullptr);
|
||||
}
|
||||
|
||||
bool Scene::IsTextAtGivenDrawLayer(const std::string& text, std::uint64_t draw_layer) const
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(draw_layer >= m_drawables.size())
|
||||
return false;
|
||||
if(!m_drawables[draw_layer] || m_drawables[draw_layer]->GetType() != DrawableType::Text)
|
||||
return false;
|
||||
Text* ptr = static_cast<Text*>(m_drawables[draw_layer].get());
|
||||
return ptr->GetText() == text && ptr->GetFont() == p_bound_font;
|
||||
}
|
||||
|
||||
void Scene::BringToFront(NonOwningPtr<Drawable> drawable)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&drawable](std::shared_ptr<Drawable> drawable_ptr)
|
||||
{
|
||||
return drawable_ptr.get() == drawable.Get();
|
||||
});
|
||||
if(it == m_drawables.end())
|
||||
return;
|
||||
std::rotate(it, it + 1, m_drawables.end());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace mlx
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Sprite::Sprite(NonOwningPtr<Texture> texture)
|
||||
Sprite::Sprite(NonOwningPtr<Texture> texture) : Drawable(DrawableType::Sprite)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Verify((bool)texture, "Sprite: invalid texture (internal mlx issue, please report to devs)");
|
||||
@@ -45,7 +45,7 @@ namespace mlx
|
||||
p_texture = texture;
|
||||
}
|
||||
|
||||
Sprite::Sprite(std::shared_ptr<Mesh> mesh, NonOwningPtr<Texture> texture)
|
||||
Sprite::Sprite(std::shared_ptr<Mesh> mesh, NonOwningPtr<Texture> texture) : Drawable(DrawableType::Sprite)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Verify((bool)texture, "Sprite: invalid texture (internal mlx issue, please report to devs)");
|
||||
|
||||
61
runtime/Sources/Graphics/Text.cpp
git.filemode.normal_file
61
runtime/Sources/Graphics/Text.cpp
git.filemode.normal_file
@@ -0,0 +1,61 @@
|
||||
#include <PreCompiled.h>
|
||||
|
||||
#include <Graphics/Text.h>
|
||||
|
||||
#define STB_RECT_PACK_IMPLEMENTATION
|
||||
#include <stb_rect_pack.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
Text::Text(const std::string& text, std::shared_ptr<Font> font) : Drawable(DrawableType::Text)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
|
||||
Assert(font != nullptr, "invalid font");
|
||||
|
||||
std::vector<Vertex> vertex_data;
|
||||
std::vector<std::uint32_t> index_data;
|
||||
|
||||
float stb_x = 0.0f;
|
||||
float stb_y = 0.0f;
|
||||
|
||||
for(char c : text)
|
||||
{
|
||||
if(c < 32)
|
||||
continue;
|
||||
|
||||
stbtt_aligned_quad q;
|
||||
stbtt_GetPackedQuad(font->GetCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
|
||||
|
||||
std::size_t index = vertex_data.size();
|
||||
|
||||
vertex_data.emplace_back(Vec4f{ q.x0, q.y0, 0.0f, 0.0f }, Vec2f{ q.s0, q.t0 });
|
||||
vertex_data.emplace_back(Vec4f{ q.x1, q.y0, 0.0f, 0.0f }, Vec2f{ q.s1, q.t0 });
|
||||
vertex_data.emplace_back(Vec4f{ q.x1, q.y1, 0.0f, 0.0f }, Vec2f{ q.s1, q.t1 });
|
||||
vertex_data.emplace_back(Vec4f{ q.x0, q.y1, 0.0f, 0.0f }, Vec2f{ q.s0, q.t1 });
|
||||
|
||||
index_data.emplace_back(index + 0);
|
||||
index_data.emplace_back(index + 1);
|
||||
index_data.emplace_back(index + 2);
|
||||
index_data.emplace_back(index + 2);
|
||||
index_data.emplace_back(index + 3);
|
||||
index_data.emplace_back(index + 0);
|
||||
}
|
||||
|
||||
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
|
||||
mesh->AddSubMesh({ std::move(vertex_data), std::move(index_data) });
|
||||
Init(text, font, mesh);
|
||||
}
|
||||
|
||||
void Text::Init(const std::string& text, std::shared_ptr<Font> font, std::shared_ptr<Mesh> mesh)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
|
||||
Assert(font != nullptr, "invalid font");
|
||||
Assert(mesh != nullptr, "invalid mesh");
|
||||
|
||||
p_mesh = mesh;
|
||||
p_font = font;
|
||||
m_text = text;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,12 @@
|
||||
#include <stb_image.h>
|
||||
#endif
|
||||
|
||||
#ifdef IMAGE_OPTIMIZED
|
||||
#define TILING VK_IMAGE_TILING_OPTIMAL
|
||||
#else
|
||||
#define TILING VK_IMAGE_TILING_LINEAR
|
||||
#endif
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled, [[maybe_unused]] std::string_view debug_name)
|
||||
@@ -153,7 +159,7 @@ namespace mlx
|
||||
void Texture::Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Image::Init(ImageType::Color, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, is_multisampled, std::move(debug_name));
|
||||
Image::Init(ImageType::Color, width, height, format, TILING, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, is_multisampled, std::move(debug_name));
|
||||
Image::CreateImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
Image::CreateSampler();
|
||||
if(pixels)
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace mlx
|
||||
allocator_create_info.pVulkanFunctions = &vma_vulkan_func;
|
||||
|
||||
kvfCheckVk(vmaCreateAllocator(&allocator_create_info, &m_allocator));
|
||||
DebugLog("Graphics allocator : created new allocator");
|
||||
DebugLog("Graphics Allocator: created new allocator");
|
||||
}
|
||||
|
||||
VmaAllocation GPUAllocator::CreateBuffer(const VkBufferCreateInfo* binfo, const VmaAllocationCreateInfo* vinfo, VkBuffer& buffer, const char* name) noexcept
|
||||
@@ -145,7 +145,7 @@ namespace mlx
|
||||
std::ofstream file(name);
|
||||
if(!file.is_open())
|
||||
{
|
||||
Error("Graphics allocator : unable to dump memory to a json file");
|
||||
Error("Graphics Allocator: unable to dump memory to a json file");
|
||||
return;
|
||||
}
|
||||
char* str = nullptr;
|
||||
@@ -166,11 +166,11 @@ namespace mlx
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(m_active_images_allocations != 0)
|
||||
Error("Graphics allocator : some user-dependant allocations were not freed before destroying the display (% active allocations). You may have not destroyed all the MLX resources you've created", m_active_images_allocations);
|
||||
Error("Graphics Allocator: some user-dependant allocations were not freed before destroying the display (% active allocations). You may have not destroyed all the MLX resources you've created", m_active_images_allocations);
|
||||
else if(m_active_buffers_allocations != 0)
|
||||
Error("Graphics allocator : some MLX-dependant allocations were not freed before destroying the display (% active allocations). This is an error in the MLX, please report this should not happen", m_active_buffers_allocations);
|
||||
Error("Graphics Allocator: some MLX-dependant allocations were not freed before destroying the display (% active allocations). This is an error in the MLX, please report this should not happen", m_active_buffers_allocations);
|
||||
if(m_active_images_allocations < 0 || m_active_buffers_allocations < 0)
|
||||
Warning("Graphics allocator : the impossible happened, the MLX has freed more allocations than it has made (wtf)");
|
||||
Warning("Graphics Allocator: the impossible happened, the MLX has freed more allocations than it has made (wtf)");
|
||||
vmaDestroyAllocator(m_allocator);
|
||||
m_active_buffers_allocations = 0;
|
||||
m_active_images_allocations = 0;
|
||||
|
||||
@@ -92,32 +92,31 @@ namespace mlx
|
||||
|
||||
VkCommandBuffer cmd = renderer.GetActiveCommandBuffer();
|
||||
|
||||
const auto& sprites = scene.GetSprites();
|
||||
const auto& drawables = scene.GetDrawables();
|
||||
|
||||
for(auto sprite : sprites)
|
||||
for(auto drawable : drawables)
|
||||
{
|
||||
// Check every textures and update modified ones to GPU before starting the render pass
|
||||
if(!sprite->IsSetInit())
|
||||
sprite->UpdateDescriptorSet(p_texture_set);
|
||||
Verify((bool)sprite->GetTexture(), "a sprite has no texture attached (internal mlx issue, please report to the devs)");
|
||||
sprite->GetTexture()->Update(cmd);
|
||||
if(!drawable->IsSetInit())
|
||||
drawable->UpdateDescriptorSet(p_texture_set);
|
||||
drawable->Update(cmd);
|
||||
}
|
||||
|
||||
m_pipeline.BindPipeline(cmd, 0, {});
|
||||
for(auto sprite : sprites)
|
||||
for(auto drawable : drawables)
|
||||
{
|
||||
SpriteData sprite_data;
|
||||
sprite_data.position = Vec4f{ sprite->GetPosition(), 0.0f, 1.0f };
|
||||
sprite_data.color = sprite->GetColor();
|
||||
SpriteData drawable_data;
|
||||
drawable_data.position = Vec4f{ drawable->GetPosition(), 0.0f, 1.0f };
|
||||
drawable_data.color = drawable->GetColor();
|
||||
|
||||
sprite->Bind(frame_index, cmd);
|
||||
drawable->Bind(frame_index, cmd);
|
||||
|
||||
std::array<VkDescriptorSet, 2> sets = { p_viewer_data_set->GetSet(frame_index), sprite->GetSet(frame_index) };
|
||||
std::array<VkDescriptorSet, 2> sets = { p_viewer_data_set->GetSet(frame_index), drawable->GetSet(frame_index) };
|
||||
|
||||
RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &sprite_data);
|
||||
RenderCore::Get().vkCmdPushConstants(cmd, m_pipeline.GetPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(SpriteData), &drawable_data);
|
||||
RenderCore::Get().vkCmdBindDescriptorSets(cmd, m_pipeline.GetPipelineBindPoint(), m_pipeline.GetPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
|
||||
|
||||
sprite->GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef());
|
||||
drawable->GetMesh()->Draw(cmd, renderer.GetDrawCallsCounterRef(), renderer.GetPolygonDrawnCounterRef());
|
||||
}
|
||||
m_pipeline.EndPipeline(cmd);
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ namespace mlx
|
||||
{
|
||||
PFN_vkVoidFunction function = RenderCore::Get().vkGetInstanceProcAddr(static_cast<VkInstance>(context), name);
|
||||
if(!function)
|
||||
FatalError("Vulkan loader : could not load '%'", name);
|
||||
//DebugLog("Vulkan loader : loaded %", name);
|
||||
FatalError("Vulkan Loader: could not load '%'", name);
|
||||
//DebugLog("Vulkan Loader: loaded %", name);
|
||||
return function;
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@ namespace mlx
|
||||
{
|
||||
PFN_vkVoidFunction function = RenderCore::Get().vkGetDeviceProcAddr(static_cast<VkDevice>(context), name);
|
||||
if(!function)
|
||||
FatalError("Vulkan loader : could not load '%'", name);
|
||||
//DebugLog("Vulkan loader : loaded %", name);
|
||||
FatalError("Vulkan Loader: could not load '%'", name);
|
||||
//DebugLog("Vulkan Loader: loaded %", name);
|
||||
return function;
|
||||
}
|
||||
|
||||
@@ -95,13 +95,13 @@ namespace mlx
|
||||
RESTORE_GCC_PEDANTIC_WARNINGS
|
||||
if(RenderCore::Get().vkGetInstanceProcAddr)
|
||||
{
|
||||
DebugLog("Vulkan loader : libvulkan loaded using '%'", libname);
|
||||
DebugLog("Vulkan Loader: libvulkan loaded using '%'", libname);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!p_module || !RenderCore::Get().vkGetInstanceProcAddr)
|
||||
FatalError("Vulkan loader : failed to load libvulkan");
|
||||
FatalError("Vulkan Loader: failed to load libvulkan");
|
||||
LoadGlobalFunctions(nullptr, Internal::vkGetInstanceProcAddrStub);
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ namespace mlx
|
||||
#define MLX_VULKAN_GLOBAL_FUNCTION(fn) RenderCore::Get().fn = reinterpret_cast<PFN_##fn>(load(context, #fn));
|
||||
#include <Renderer/Vulkan/VulkanDefs.h>
|
||||
#undef MLX_VULKAN_GLOBAL_FUNCTION
|
||||
DebugLog("Vulkan loader : global functions loaded");
|
||||
DebugLog("Vulkan Loader: global functions loaded");
|
||||
}
|
||||
|
||||
void VulkanLoader::LoadInstanceFunctions(void* context, PFN_vkVoidFunction (*load)(void*, const char*)) noexcept
|
||||
@@ -128,7 +128,7 @@ namespace mlx
|
||||
#define MLX_VULKAN_INSTANCE_FUNCTION(fn) RenderCore::Get().fn = reinterpret_cast<PFN_##fn>(load(context, #fn));
|
||||
#include <Renderer/Vulkan/VulkanDefs.h>
|
||||
#undef MLX_VULKAN_INSTANCE_FUNCTION
|
||||
DebugLog("Vulkan loader : instance functions loaded");
|
||||
DebugLog("Vulkan Loader: instance functions loaded");
|
||||
}
|
||||
|
||||
void VulkanLoader::LoadDeviceFunctions(void* context, PFN_vkVoidFunction (*load)(void*, const char*)) noexcept
|
||||
@@ -136,7 +136,7 @@ namespace mlx
|
||||
#define MLX_VULKAN_DEVICE_FUNCTION(fn) RenderCore::Get().fn = reinterpret_cast<PFN_##fn>(load(context, #fn));
|
||||
#include <Renderer/Vulkan/VulkanDefs.h>
|
||||
#undef MLX_VULKAN_DEVICE_FUNCTION
|
||||
DebugLog("Vulkan loader : device functions loaded");
|
||||
DebugLog("Vulkan Loader: device functions loaded");
|
||||
}
|
||||
|
||||
VulkanLoader::~VulkanLoader()
|
||||
@@ -147,6 +147,6 @@ namespace mlx
|
||||
dlclose(p_module);
|
||||
#endif
|
||||
p_module = nullptr;
|
||||
DebugLog("Vulkan loader : libvulkan unloaded");
|
||||
DebugLog("Vulkan Loader: libvulkan unloaded");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user