finxing memory usages

This commit is contained in:
2024-12-16 01:24:25 +01:00
parent 5b726fe74a
commit feb3fcbd1f
24 changed files with 315 additions and 79 deletions

View File

@@ -5,7 +5,7 @@ if [ -e a.out ]; then
fi
if [ $(uname -s) = 'Darwin' ]; then
clang main.c ../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -g;
clang main.c ../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -lm -g;
else
clang main.c ../libmlx.so -lSDL2 -g -Wall -Wextra -Werror;
clang main.c ../libmlx.so -lSDL2 -lm -g -Wall -Wextra -Werror;
fi

View File

@@ -1,5 +1,7 @@
#include <stdio.h>
#include <math.h>
#include "../includes/mlx.h"
#include "../includes/mlx_extended.h"
typedef struct
{
@@ -16,8 +18,11 @@ int update(void* param)
static int i = 0;
mlx_t* mlx = (mlx_t*)param;
if(i == 200)
if(i > 200)
{
mlx_clear_window(mlx->mlx, mlx->win, 0x334D4DFF);
mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40, 0.5f, 0.5f, i);
}
if(i >= 250)
mlx_set_font_scale(mlx->mlx, "default", 16.f);
@@ -27,7 +32,6 @@ int update(void* param)
mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFF2066FF, "this text should be hidden");
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_png, 100, 100);
//mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40, 0.5f, 75.0f);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60);
mlx_set_font(mlx->mlx, "default");
@@ -40,7 +44,10 @@ int update(void* param)
color += (color < 255);
}
//mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, 2.0f, 0.0f);
if(i < 200)
mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, 0.5f, 2.0f, 0.0f);
else
mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, fabs(sin(i / 100.0f)), fabs(cos(i / 100.0f) * 2.0f), 0.0f);
mlx_set_font_scale(mlx->mlx, "default", 8.f);
mlx_string_put(mlx->mlx, mlx->win, 210, 175, 0xFFAF2BFF, "hidden");

View File

@@ -6,7 +6,7 @@
/* By: maldavid <contact@kbz8.me> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2024/12/15 01:58:12 by maldavid ### ########.fr */
/* Updated: 2024/12/15 13:59:00 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -78,7 +78,6 @@ MLX_API void mlx_destroy_context(mlx_context mlx);
*/
typedef struct mlx_window_create_info
{
void* mlx_extension;
const char* title;
int width;
int height;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <contact@kbz8.me> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/14 16:17:10 by maldavid #+# #+# */
/* Updated: 2024/12/14 17:42:06 by maldavid ### ########.fr */
/* Updated: 2024/12/15 13:58:58 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,16 +26,6 @@ extern "C" {
/* Window related functions */
typedef struct mlx_window_create_info_extension
{
int position_x;
int position_y;
int max_width;
int max_height;
int min_width;
int min_height;
} mlx_window_create_info_extension;
/**
* @brief Sets maximum window size
*

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -109,12 +109,7 @@ namespace mlx
Application::~Application()
{
for(auto& window : m_graphics)
{
if(window && window->GetWindow()->GetName() == "让我们在月光下做爱吧")
window.reset();
}
m_mesh_registry.Reset();
m_font_registry.Reset();
p_render_core.reset();
p_sdl_manager.reset();

View File

@@ -1,3 +1,4 @@
#include "mlx_extended.h"
#include <PreCompiled.h>
#include <Core/Application.h>
@@ -274,7 +275,7 @@ extern "C"
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
gs->TexturePut(texture, x, y, 1.0f, 0.0f);
gs->TexturePut(texture, x, y, 1.0f, 1.0f, 0.0f);
}
void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str)
@@ -332,4 +333,87 @@ extern "C"
}
mlx->app->LoadFont(file, scale);
}
// Extended
void mlx_set_window_max_size(mlx_context mlx, mlx_window win, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->SetMaxSize(x, y);
}
void mlx_set_window_min_size(mlx_context mlx, mlx_window win, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->SetMinSize(x, y);
}
void mlx_maximise_window(mlx_context mlx, mlx_window win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->Maximize();
}
void mlx_minimize_window(mlx_context mlx, mlx_window win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->Minimize();
}
void mlx_restore_window(mlx_context mlx, mlx_window win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->GetWindow()->Restore();
}
void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, int* pixels)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
}
void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* dst)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
}
void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* pixels)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
}
void mlx_put_transformed_image_to_window(mlx_context mlx, mlx_window win, mlx_image image, int x, int y, float scale_x, float scale_y, float angle)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
gs->TexturePut(texture, x, y, scale_x, scale_y, angle);
}
}

View File

@@ -68,15 +68,16 @@ namespace mlx
#ifdef MLX_COMPILER_MSVC
void* ptr2 = _aligned_realloc(ptr, alignment, size);
if(it != s_blocks.end())
s_blocks.erase(it);
#else
void* ptr2 = AlignedMalloc(alignment, size);
if(it != s_blocks.end())
{
std::memcpy(ptr2, ptr, it->size);
Free(ptr);
}
#endif
if(it != s_blocks.end())
s_blocks.erase(it);
if(ptr2 != nullptr)
s_blocks.emplace_back(ptr, size, true);
return ptr2;

View File

@@ -59,6 +59,8 @@ namespace mlx
flags |= SDL_WINDOW_SHOWN;
if(info->is_resizable)
flags |= SDL_WINDOW_RESIZABLE;
if(info->is_fullscreen)
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
infos->window = SDL_CreateWindow(info->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, info->width, info->height, flags);
if(!infos->window)
@@ -148,6 +150,31 @@ namespace mlx
SDL_SetWindowFullscreen(static_cast<Internal::WindowInfos*>(window)->window, (enable ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0));
}
void SDLManager::SetWindowMaxSize(Handle window, int x, int y) const noexcept
{
SDL_SetWindowMaximumSize(static_cast<Internal::WindowInfos*>(window)->window, x, y);
}
void SDLManager::SetWindowMinSize(Handle window, int x, int y) const noexcept
{
SDL_SetWindowMinimumSize(static_cast<Internal::WindowInfos*>(window)->window, x, y);
}
void SDLManager::MaximizeWindow(Handle window) const noexcept
{
SDL_MaximizeWindow(static_cast<Internal::WindowInfos*>(window)->window);
}
void SDLManager::MinimizeWindow(Handle window) const noexcept
{
SDL_MinimizeWindow(static_cast<Internal::WindowInfos*>(window)->window);
}
void SDLManager::RestoreWindow(Handle window) const noexcept
{
SDL_RestoreWindow(static_cast<Internal::WindowInfos*>(window)->window);
}
void SDLManager::GetWindowPosition(Handle window, int* x, int* y) const noexcept
{
SDL_GetWindowPosition(static_cast<Internal::WindowInfos*>(window)->window, x, y);

View File

@@ -17,24 +17,43 @@ namespace mlx
else
FatalError("a renderer was created without window nor render target attached (wtf)");
auto it = m_placements.find(draw_layer);
if(it != m_placements.end())
{
it->second->SetPixel(x, y, color);
return nullptr;
}
bool adjusment = false;
if(m_current_texture_index >= m_textures.size())
{
#ifdef DEBUG
auto res = m_textures.try_emplace(draw_layer, CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(draw_layer));
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_put_pixel_layer_" + std::to_string(draw_layer)));
#else
auto res = m_textures.try_emplace(draw_layer, CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{});
m_textures.push_back(std::make_unique<Texture>(CPUBuffer{}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, std::string_view{}));
#endif
if(res.second)
res.first->second.Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
res.first->second.SetPixel(x, y, color);
return (res.second ? &res.first->second : nullptr);
m_current_texture_index++;
adjusment = true;
}
try
{
m_placements[draw_layer] = m_textures.at(m_current_texture_index - adjusment).get();
m_textures.at(m_current_texture_index - adjusment)->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
m_textures.at(m_current_texture_index - adjusment)->SetPixel(x, y, color);
return m_textures.at(m_current_texture_index - adjusment).get();
}
catch(...)
{
Error("PutPixelManager: invalid texture index; % is not in range of 0-% (internal mlx issue, please report to devs)", m_current_texture_index - 1, m_textures.size());
return nullptr;
}
}
void PutPixelManager::ResetRenderData()
{
m_textures.clear();
}
PutPixelManager::~PutPixelManager()
{
ResetRenderData();
m_placements.clear();
for(auto& texture : m_textures)
texture->Clear(VK_NULL_HANDLE, Vec4f{ 0.0f });
m_current_texture_index = 0;
}
}

View File

@@ -27,16 +27,16 @@ namespace mlx
return *sprite;
}
NonOwningPtr<Sprite> Scene::GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale, float rotation) const
NonOwningPtr<Sprite> Scene::GetSpriteFromTexturePositionScaleRotation(NonOwningPtr<Texture> texture, const Vec2f& position, float scale_x, float scale_y, float rotation) const
{
MLX_PROFILE_FUNCTION();
auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture, &position, scale, rotation](std::shared_ptr<Drawable> drawable)
auto it = std::find_if(m_drawables.begin(), m_drawables.end(), [&texture, &position, scale_x, scale_y, rotation](std::shared_ptr<Drawable> drawable)
{
if(!drawable || drawable->GetType() != DrawableType::Sprite)
return false;
return static_cast<Sprite*>(drawable.get())->GetTexture() == texture &&
drawable->GetPosition() == position &&
drawable->GetScale() == Vec2f{ scale, scale } &&
drawable->GetScale() == Vec2f{ scale_x, scale_y } &&
drawable->GetRotation().ToEulerAngles() == EulerAnglesf{ 0.0f, 0.0f, rotation };
});
return static_cast<Sprite*>(it != m_drawables.end() ? it->get() : nullptr);

View File

@@ -32,7 +32,10 @@ namespace mlx
0,
};
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
std::shared_ptr<Mesh> mesh = MeshRegistry::Get().FindMesh({ Mesh::SubMesh{ data, indices, Mesh::SubMesh::NoBuild{} } });
if(mesh)
return mesh;
mesh = std::make_shared<Mesh>();
mesh->AddSubMesh({ std::move(data), std::move(indices) });
return mesh;
}
@@ -42,6 +45,7 @@ namespace mlx
MLX_PROFILE_FUNCTION();
Verify((bool)texture, "Sprite: invalid texture (internal mlx issue, please report to devs)");
p_mesh = CreateQuad(0, 0, texture->GetWidth(), texture->GetHeight());
MeshRegistry::Get().RegisterMesh(p_mesh);
p_texture = texture;
}

View File

@@ -40,8 +40,13 @@ namespace mlx
index_data.emplace_back(index + 0);
}
std::shared_ptr<Mesh> mesh = std::make_shared<Mesh>();
std::shared_ptr<Mesh> mesh = MeshRegistry::Get().FindMesh({ Mesh::SubMesh{ vertex_data, index_data, Mesh::SubMesh::NoBuild{} } });
if(!mesh)
{
mesh = std::make_shared<Mesh>();
mesh->AddSubMesh({ std::move(vertex_data), std::move(index_data) });
MeshRegistry::Get().RegisterMesh(mesh);
}
Init(text, font, mesh);
}

View File

@@ -79,7 +79,7 @@ namespace mlx
m_surface = p_window->CreateVulkanSurface(RenderCore::Get().GetInstance());
DebugLog("Vulkan: surface created");
m_swapchain = kvfCreateSwapchainKHR(RenderCore::Get().GetDevice(), RenderCore::Get().GetPhysicalDevice(), m_surface, extent, VK_NULL_HANDLE, false);
m_swapchain = kvfCreateSwapchainKHR(RenderCore::Get().GetDevice(), RenderCore::Get().GetPhysicalDevice(), m_surface, extent, m_swapchain, false);
m_images_count = kvfGetSwapchainImagesCount(m_swapchain);
m_min_images_count = kvfGetSwapchainMinImagesCount(m_swapchain);