This commit is contained in:
2024-10-21 19:45:33 +02:00
parent 0304834008
commit 7a3e5f37fa
22 changed files with 121 additions and 68 deletions

View File

@@ -49,8 +49,6 @@ namespace mlx
int m_id;
bool m_has_window;
bool m_insert_new_pixel_put_texture = false;
};
}

View File

@@ -8,21 +8,18 @@ namespace mlx
MLX_PROFILE_FUNCTION();
p_scene->ResetSprites();
m_put_pixel_manager.ResetRenderData();
m_insert_new_pixel_put_texture = true;
m_draw_layer = 0;
}
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_insert_new_pixel_put_texture, color);
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
if(texture)
{
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ 0.0f, 0.0f });
m_draw_layer++;
}
m_insert_new_pixel_put_texture = false;
}
void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str)
@@ -42,14 +39,10 @@ namespace mlx
{
Sprite& new_sprite = p_scene->CreateSprite(texture);
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
m_insert_new_pixel_put_texture = true;
m_draw_layer++;
}
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
{
p_scene->BringToFront(std::move(sprite));
m_insert_new_pixel_put_texture = true;
}
m_draw_layer++;
}
void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale)

View File

@@ -20,7 +20,7 @@ namespace mlx
private:
static MemManager* s_instance;
inline static std::list<void*> s_blocks;
inline static std::vector<void*> s_blocks;
};
}

37
runtime/Includes/Graphics/Font.h git.filemode.normal_file
View File

@@ -0,0 +1,37 @@
#ifndef __MLX_FONT__
#define __MLX_FONT__
#include <Renderer/Image.h>
namespace mlx
{
class Font
{
public:
Font(const std::filesystem::path& path, float scale) : m_build_data(path), m_name(path.string()), m_scale(scale) {}
Font(const std::string& name, const std::vector<std::uint8_t>& ttf_data, float scale) : m_build_data(ttf_data), m_name(name), m_scale(scale) {}
void Destroy();
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 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; }
inline ~Font() { Destroy(); }
private:
void BuildFont();
private:
std::array<stbtt_packedchar, 96> m_cdata;
Texture m_atlas;
std::variant<std::filesystem::path, std::vector<std::uint8_t>> m_build_data;
std::string m_name;
float m_scale;
};
}
#endif

View File

@@ -16,20 +16,7 @@ namespace mlx
IndexBuffer ibo;
std::size_t triangle_count = 0;
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
{
CPUBuffer vb(vertices.size() * sizeof(Vertex));
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
vbo.Init(vb.GetSize(), 0, "mlx_mesh");
vbo.SetData(std::move(vb));
CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
ibo.Init(ib.GetSize(), 0, "mlx_mesh");
ibo.SetData(std::move(ib));
triangle_count = vertices.size() / 3;
}
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices);
};
public:
@@ -48,21 +35,8 @@ namespace mlx
private:
std::vector<SubMesh> m_sub_meshes;
};
class MeshRegistry
{
public:
MeshRegistry() = default;
inline void RegisterMesh(std::shared_ptr<Mesh> mesh);
inline void UnregisterMesh(std::shared_ptr<Mesh> mesh);
inline bool IsMeshKnown(std::shared_ptr<Mesh> mesh);
~MeshRegistry() = default;
private:
std::unordered_set<std::shared_ptr<Mesh>> m_mesh_registry;
};
}
#include <Graphics/Mesh.inl>
#endif

20
runtime/Includes/Graphics/Mesh.inl git.filemode.normal_file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <Graphics/Mesh.h>
namespace mlx
{
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
{
CPUBuffer vb(vertices.size() * sizeof(Vertex));
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
vbo.Init(vb.GetSize(), 0, "mlx_mesh");
vbo.SetData(std::move(vb));
CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
ibo.Init(ib.GetSize(), 0, "mlx_mesh");
ibo.SetData(std::move(ib));
triangle_count = vertices.size() / 3;
}
}

View File

@@ -11,13 +11,13 @@ namespace mlx
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}
// Return a valid pointer when a new texture has been created
NonOwningPtr<Texture> DrawPixel(int x, int y, bool insert_new_texture, std::uint32_t color);
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t draw_layer, std::uint32_t color);
void ResetRenderData();
~PutPixelManager();
private:
std::list<Texture> m_textures;
std::unordered_map<std::uint64_t, Texture> m_textures;
NonOwningPtr<class Renderer> p_renderer;
};
}

View File

@@ -15,6 +15,7 @@ namespace mlx
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; }

View File

@@ -54,7 +54,7 @@
#include <dlfcn.h>
#endif
#if defined(MLX_PLAT_LINUX)
#ifdef MLX_PLAT_LINUX
#include <math.h> // sincos
#endif

View File

@@ -48,6 +48,10 @@ namespace mlx
[[nodiscard]] MLX_FORCEINLINE bool IsInit() const noexcept { return m_image != VK_NULL_HANDLE; }
[[nodiscard]] MLX_FORCEINLINE ImageType GetType() const noexcept { return m_type; }
#ifdef DEBUG
[[nodiscard]] MLX_FORCEINLINE const std::string& GetDebugName() const { return m_debug_name; }
#endif
virtual ~Image() = default;
protected:

View File

@@ -11,7 +11,7 @@ namespace mlx
{
public:
Render2DPass() = default;
void Init(class Renderer& renderer);
void Init();
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
void Destroy();
~Render2DPass() = default;

View File

@@ -11,7 +11,7 @@ namespace mlx
{
public:
FinalPass() = default;
void Init(class Renderer& renderer);
void Init();
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
void Destroy();
~FinalPass() = default;

View File

@@ -11,9 +11,11 @@ namespace mlx
{
public:
RenderPasses() = default;
void Init(class Renderer& renderer);
void Pass(class Scene& scene, class Renderer& renderer);
void Destroy();
~RenderPasses() = default;
private: