mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 14:43:34 +00:00
finxing memory usages
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <Core/Memory.h>
|
||||
#include <Core/Fps.h>
|
||||
#include <Graphics/Font.h>
|
||||
#include <Graphics/Mesh.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
@@ -47,6 +48,7 @@ namespace mlx
|
||||
Inputs m_in;
|
||||
FontRegistry m_font_registry;
|
||||
ImageRegistry m_image_registry;
|
||||
MeshRegistry m_mesh_registry;
|
||||
std::vector<std::unique_ptr<GraphicsSupport>> m_graphics;
|
||||
std::shared_ptr<Font> p_last_font_bound;
|
||||
std::function<int(Handle)> f_loop_hook;
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace mlx
|
||||
|
||||
inline void PixelPut(int x, int y, int color) noexcept;
|
||||
inline void StringPut(int x, int y, int, std::string str);
|
||||
inline void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale, float angle);
|
||||
inline void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale_x, float scale_y, float angle);
|
||||
|
||||
inline void TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept;
|
||||
|
||||
|
||||
@@ -59,10 +59,10 @@ namespace mlx
|
||||
p_scene->BringToDrawLayer(text.Get(), m_draw_layer);
|
||||
}
|
||||
|
||||
void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y, float scale, float angle)
|
||||
void GraphicsSupport::TexturePut(NonOwningPtr<Texture> texture, int x, int y, float scale_x, float scale_y, float angle)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale, angle);
|
||||
NonOwningPtr<Sprite> sprite = p_scene->GetSpriteFromTexturePositionScaleRotation(texture, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, scale_x, scale_y, angle);
|
||||
if(!sprite)
|
||||
{
|
||||
if(m_pixelput_called)
|
||||
@@ -73,7 +73,7 @@ namespace mlx
|
||||
Sprite& new_sprite = p_scene->CreateSprite(texture);
|
||||
new_sprite.SetCenter(Vec2f{ texture->GetWidth() / 2.0f, texture->GetHeight() / 2.0f });
|
||||
new_sprite.SetPosition(Vec2f{ static_cast<float>(x), static_cast<float>(y) });
|
||||
new_sprite.SetScale(Vec2f{ scale, scale });
|
||||
new_sprite.SetScale(Vec2f{ scale_x, scale_y });
|
||||
new_sprite.SetRotation(angle);
|
||||
}
|
||||
else if(!p_scene->IsTextureAtGivenDrawLayer(texture, m_draw_layer))
|
||||
|
||||
@@ -25,6 +25,12 @@ namespace mlx
|
||||
void SetWindowSize(Handle window, int x, int y) const noexcept;
|
||||
void SetWindowTitle(Handle window, std::string_view title) const noexcept;
|
||||
void SetWindowFullscreen(Handle window, bool enable) const noexcept;
|
||||
void SetWindowMaxSize(Handle window, int x, int y) const noexcept;
|
||||
void SetWindowMinSize(Handle window, int x, int y) const noexcept;
|
||||
void MaximizeWindow(Handle window) const noexcept;
|
||||
void MinimizeWindow(Handle window) const noexcept;
|
||||
void RestoreWindow(Handle window) const noexcept;
|
||||
|
||||
void GetWindowPosition(Handle window, int* x, int* y) const noexcept;
|
||||
void GetWindowSize(Handle window, int* x, int* y) const noexcept;
|
||||
|
||||
|
||||
@@ -12,11 +12,16 @@ namespace mlx
|
||||
public:
|
||||
struct SubMesh
|
||||
{
|
||||
struct NoBuild {};
|
||||
|
||||
VertexBuffer vbo;
|
||||
IndexBuffer ibo;
|
||||
std::vector<Vertex> vertex_data;
|
||||
std::vector<std::uint32_t> index_data;
|
||||
std::size_t triangle_count = 0;
|
||||
|
||||
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices);
|
||||
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices, NoBuild);
|
||||
};
|
||||
|
||||
public:
|
||||
@@ -35,6 +40,27 @@ namespace mlx
|
||||
private:
|
||||
std::vector<SubMesh> m_sub_meshes;
|
||||
};
|
||||
|
||||
// A registry just to avoid destroying meshes when clearing a window
|
||||
class MeshRegistry
|
||||
{
|
||||
public:
|
||||
inline MeshRegistry();
|
||||
|
||||
inline void RegisterMesh(std::shared_ptr<Mesh> mesh);
|
||||
inline std::shared_ptr<Mesh> FindMesh(const std::vector<Mesh::SubMesh>& sub_meshes);
|
||||
inline void UnregisterMesh(std::shared_ptr<Mesh> mesh);
|
||||
inline void Reset();
|
||||
|
||||
inline static bool IsInit() noexcept { return s_instance != nullptr; }
|
||||
inline static MeshRegistry& Get() noexcept { return *s_instance; }
|
||||
|
||||
inline ~MeshRegistry();
|
||||
|
||||
private:
|
||||
inline static MeshRegistry* s_instance = nullptr;
|
||||
std::unordered_set<std::shared_ptr<Mesh>> m_meshes_registry;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Graphics/Mesh.inl>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
|
||||
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices) : vertex_data(vertices), index_data(indices)
|
||||
{
|
||||
CPUBuffer vb(vertices.size() * sizeof(Vertex));
|
||||
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
|
||||
@@ -17,4 +17,61 @@ namespace mlx
|
||||
|
||||
triangle_count = vertices.size() / 3;
|
||||
}
|
||||
|
||||
Mesh::SubMesh::SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices, NoBuild) : vertex_data(vertices), index_data(indices) {}
|
||||
|
||||
MeshRegistry::MeshRegistry()
|
||||
{
|
||||
s_instance = this;
|
||||
}
|
||||
|
||||
void MeshRegistry::RegisterMesh(std::shared_ptr<Mesh> mesh)
|
||||
{
|
||||
m_meshes_registry.insert(mesh);
|
||||
}
|
||||
|
||||
std::shared_ptr<Mesh> MeshRegistry::FindMesh(const std::vector<Mesh::SubMesh>& sub_meshes)
|
||||
{
|
||||
for(const std::shared_ptr<Mesh>& mesh : m_meshes_registry)
|
||||
{
|
||||
if(mesh->GetSubMeshCount() != sub_meshes.size()) // If the number of submeshes is different than the one we want to find no need to test
|
||||
continue;
|
||||
bool found = true;
|
||||
for(std::size_t i = 0; i < sub_meshes.size(); i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
const Mesh::SubMesh& registered_sub_mesh = mesh->GetSubMesh(i);
|
||||
if(registered_sub_mesh.vertex_data != sub_meshes[i].vertex_data || registered_sub_mesh.index_data != sub_meshes[i].index_data)
|
||||
{
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(found)
|
||||
return mesh;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void MeshRegistry::UnregisterMesh(std::shared_ptr<Mesh> mesh)
|
||||
{
|
||||
m_meshes_registry.erase(mesh);
|
||||
}
|
||||
|
||||
void MeshRegistry::Reset()
|
||||
{
|
||||
m_meshes_registry.clear();
|
||||
}
|
||||
|
||||
MeshRegistry::~MeshRegistry()
|
||||
{
|
||||
s_instance = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,15 +10,17 @@ namespace mlx
|
||||
public:
|
||||
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}
|
||||
|
||||
// Return a valid pointer when a new texture has been created
|
||||
// Returns a valid pointer when a new texture has been created
|
||||
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t draw_layer, int color);
|
||||
void ResetRenderData();
|
||||
|
||||
~PutPixelManager();
|
||||
~PutPixelManager() = default;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::uint64_t, Texture> m_textures;
|
||||
std::unordered_map<std::uint64_t, NonOwningPtr<Texture>> m_placements;
|
||||
std::vector<std::unique_ptr<Texture>> m_textures;
|
||||
NonOwningPtr<class Renderer> p_renderer;
|
||||
std::size_t m_current_texture_index = 0;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace mlx
|
||||
Scene() = default;
|
||||
|
||||
Sprite& CreateSprite(NonOwningPtr<class Texture> texture) noexcept;
|
||||
NonOwningPtr<Sprite> GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale, float rotation) const;
|
||||
NonOwningPtr<Sprite> GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale_x, float scale_y, float rotation) const;
|
||||
void TryEraseSpriteFromTexture(NonOwningPtr<Texture> texture);
|
||||
bool IsTextureAtGivenDrawLayer(NonOwningPtr<Texture> texture, std::uint64_t draw_layer) const;
|
||||
|
||||
|
||||
@@ -12,26 +12,31 @@ namespace mlx
|
||||
public:
|
||||
Window(const mlx_window_create_info* info, bool hidden = false);
|
||||
|
||||
inline Handle GetWindowHandle() const noexcept { return p_window; }
|
||||
inline int GetWidth() noexcept { SDLManager::Get().GetWindowSize(p_window, &m_width, &m_height); return m_width; }
|
||||
inline int GetHeight() noexcept { SDLManager::Get().GetWindowSize(p_window, &m_width, &m_height); return m_height; }
|
||||
inline std::uint32_t GetID() const noexcept { return m_id; }
|
||||
inline const std::string& GetName() const { return m_name; }
|
||||
MLX_FORCEINLINE Handle GetWindowHandle() const noexcept { return p_window; }
|
||||
MLX_FORCEINLINE int GetWidth() noexcept { SDLManager::Get().GetWindowSize(p_window, &m_width, &m_height); return m_width; }
|
||||
MLX_FORCEINLINE int GetHeight() noexcept { SDLManager::Get().GetWindowSize(p_window, &m_width, &m_height); return m_height; }
|
||||
MLX_FORCEINLINE std::uint32_t GetID() const noexcept { return m_id; }
|
||||
MLX_FORCEINLINE const std::string& GetName() const { return m_name; }
|
||||
|
||||
inline void MoveMouse(int x, int y) { SDLManager::Get().MoveMouseOnWindow(p_window, x, y); }
|
||||
inline void GetScreenSizeWindowIsOn(int* x, int* y) { SDLManager::Get().GetScreenSizeWindowIsOn(p_window, x, y); }
|
||||
MLX_FORCEINLINE void MoveMouse(int x, int y) { SDLManager::Get().MoveMouseOnWindow(p_window, x, y); }
|
||||
MLX_FORCEINLINE void GetScreenSizeWindowIsOn(int* x, int* y) { SDLManager::Get().GetScreenSizeWindowIsOn(p_window, x, y); }
|
||||
|
||||
inline void SetPosition(int x, int y) { SDLManager::Get().SetWindowPosition(p_window, x, y); }
|
||||
inline void SetSize(int x, int y) { SDLManager::Get().SetWindowSize(p_window, x, y); m_width = x; m_height = y; }
|
||||
inline void SetTitle(std::string title) { SDLManager::Get().SetWindowTitle(p_window, title); m_name = std::move(title); }
|
||||
inline void SetFullscreen(bool enable) { SDLManager::Get().SetWindowFullscreen(p_window, enable); }
|
||||
MLX_FORCEINLINE void SetPosition(int x, int y) { SDLManager::Get().SetWindowPosition(p_window, x, y); }
|
||||
MLX_FORCEINLINE void SetSize(int x, int y) { SDLManager::Get().SetWindowSize(p_window, x, y); m_width = x; m_height = y; }
|
||||
MLX_FORCEINLINE void SetTitle(std::string title) { SDLManager::Get().SetWindowTitle(p_window, title); m_name = std::move(title); }
|
||||
MLX_FORCEINLINE void SetFullscreen(bool enable) { SDLManager::Get().SetWindowFullscreen(p_window, enable); }
|
||||
MLX_FORCEINLINE void SetMaxSize(int x, int y) { SDLManager::Get().SetWindowMaxSize(p_window, x, y); }
|
||||
MLX_FORCEINLINE void SetMinSize(int x, int y) { SDLManager::Get().SetWindowMinSize(p_window, x, y); }
|
||||
MLX_FORCEINLINE void Maximize() { SDLManager::Get().MaximizeWindow(p_window); }
|
||||
MLX_FORCEINLINE void Minimize() { SDLManager::Get().MinimizeWindow(p_window); }
|
||||
MLX_FORCEINLINE void Restore() { SDLManager::Get().RestoreWindow(p_window); }
|
||||
|
||||
inline void GetPosition(int* x, int* y) { SDLManager::Get().GetWindowPosition(p_window, x, y); }
|
||||
inline void GetSize(int* x, int* y) { *x = GetWidth(); *y = GetHeight(); }
|
||||
MLX_FORCEINLINE void GetPosition(int* x, int* y) { SDLManager::Get().GetWindowPosition(p_window, x, y); }
|
||||
MLX_FORCEINLINE void GetSize(int* x, int* y) { *x = GetWidth(); *y = GetHeight(); }
|
||||
|
||||
inline VkSurfaceKHR CreateVulkanSurface(VkInstance instance) const noexcept { return SDLManager::Get().CreateVulkanSurface(p_window, instance); }
|
||||
inline std::vector<const char*> GetRequiredVulkanInstanceExtentions() const noexcept { return SDLManager::Get().GetRequiredVulkanInstanceExtentions(p_window); }
|
||||
inline Vec2ui GetVulkanDrawableSize() const noexcept { return SDLManager::Get().GetVulkanDrawableSize(p_window); }
|
||||
MLX_FORCEINLINE VkSurfaceKHR CreateVulkanSurface(VkInstance instance) const noexcept { return SDLManager::Get().CreateVulkanSurface(p_window, instance); }
|
||||
MLX_FORCEINLINE std::vector<const char*> GetRequiredVulkanInstanceExtentions() const noexcept { return SDLManager::Get().GetRequiredVulkanInstanceExtentions(p_window); }
|
||||
MLX_FORCEINLINE Vec2ui GetVulkanDrawableSize() const noexcept { return SDLManager::Get().GetVulkanDrawableSize(p_window); }
|
||||
|
||||
void Destroy() noexcept;
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ namespace mlx
|
||||
alignas(16) Vec2f uv = Vec2f{ 0.0f, 0.0f };
|
||||
|
||||
Vertex() = default;
|
||||
Vertex(Vec4f p, Vec2f u) : position(std::move(p)), uv(std::move(u)) {}
|
||||
inline Vertex(Vec4f p, Vec2f u) : position(std::move(p)), uv(std::move(u)) {}
|
||||
|
||||
[[nodiscard]] inline bool operator==(const Vertex& rhs) const noexcept;
|
||||
|
||||
[[nodiscard]] inline static VkVertexInputBindingDescription GetBindingDescription();
|
||||
[[nodiscard]] inline static std::array<VkVertexInputAttributeDescription, 2> GetAttributeDescriptions();
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
bool Vertex::operator==(const Vertex& rhs) const noexcept
|
||||
{
|
||||
return position == rhs.position && uv == rhs.uv;
|
||||
}
|
||||
|
||||
VkVertexInputBindingDescription Vertex::GetBindingDescription()
|
||||
{
|
||||
VkVertexInputBindingDescription binding_description{};
|
||||
|
||||
Reference in New Issue
Block a user