mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 22:53:34 +00:00
working on code refactor
This commit is contained in:
@@ -6,103 +6,110 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
|
||||
/* Updated: 2024/04/02 17:06:34 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/04/23 15:06:26 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <PreCompiled.h>
|
||||
|
||||
#include <Core/Application.h>
|
||||
#include <renderer/texts/text_library.h>
|
||||
#include <renderer/texts/font_library.h>
|
||||
#include <renderer/images/texture.h>
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <Core/memory.h>
|
||||
#include <Renderer/Texts/TextLibrary.h>
|
||||
#include <Renderer/Texts/FontLibrary.h>
|
||||
#include <Renderer/Images/Texture.h>
|
||||
#include <Renderer/Core/RenderCore.h>
|
||||
#include <Core/Memory.h>
|
||||
#include <Core/EventBus.h>
|
||||
|
||||
namespace mlx::core
|
||||
namespace mlx
|
||||
{
|
||||
Application::Application() : _fps(), _in(std::make_unique<Input>())
|
||||
Application::Application() : m_fps(), m_in()
|
||||
{
|
||||
EventBus::RegisterListener({[](const EventBase& event)
|
||||
{
|
||||
|
||||
}, "__internal_application" });
|
||||
|
||||
_fps.init();
|
||||
m_fps.init();
|
||||
}
|
||||
|
||||
void Application::run() noexcept
|
||||
void Application::Run() noexcept
|
||||
{
|
||||
while(_in->isRunning())
|
||||
m_in.Run();
|
||||
|
||||
while(m_in.IsRunning())
|
||||
{
|
||||
if(!_fps.update())
|
||||
if(!m_fps.Update())
|
||||
continue;
|
||||
_in->update();
|
||||
m_in.Update();
|
||||
|
||||
if(_loop_hook)
|
||||
_loop_hook(_param);
|
||||
if(f_loop_hook)
|
||||
f_loop_hook(p_param);
|
||||
|
||||
for(auto& gs : _graphics)
|
||||
gs->render();
|
||||
for(auto& gs : m_graphics)
|
||||
{
|
||||
if(gs)
|
||||
gs->Render();
|
||||
}
|
||||
}
|
||||
|
||||
Render_Core::get().getSingleTimeCmdManager().updateSingleTimesCmdBuffersSubmitState();
|
||||
RenderCore::Get().GetSingleTimeCmdManager().UpdateSingleTimesCmdBuffersSubmitState();
|
||||
|
||||
for(auto& gs : _graphics)
|
||||
for(auto& gs : m_graphics)
|
||||
{
|
||||
if(!gs)
|
||||
continue;
|
||||
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
|
||||
gs->getRenderer().getCmdBuffer(i).waitForExecution();
|
||||
gs->GetRenderer().GetCmdBuffer(i).WaitForExecution();
|
||||
}
|
||||
}
|
||||
|
||||
void* Application::newTexture(int w, int h)
|
||||
void* Application::NewTexture(int w, int h)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
Texture* texture = new Texture;
|
||||
#ifdef DEBUG
|
||||
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_unamed_user_texture");
|
||||
texture->Create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_unamed_user_texture");
|
||||
#else
|
||||
_textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, nullptr);
|
||||
texture->Create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, nullptr);
|
||||
#endif
|
||||
return &_textures.front();
|
||||
m_image_registry.RegisterTexture(texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
void* Application::newStbTexture(char* file, int* w, int* h)
|
||||
void* Application::NewStbTexture(char* file, int* w, int* h)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_textures.emplace_front(stbTextureLoad(file, w, h));
|
||||
return &_textures.front();
|
||||
Texture* texture = StbTextureLoad(file, w, h);
|
||||
m_image_registry.RegisterTexture(texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
void Application::destroyTexture(void* ptr)
|
||||
void Application::DestroyTexture(void* ptr)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
vkDeviceWaitIdle(Render_Core::get().getDevice().get()); // TODO : synchronize with another method than waiting for GPU to be idle
|
||||
if(ptr == nullptr)
|
||||
vkDeviceWaitIdle(RenderCore::Get().GetDevice().Get()); // TODO : synchronize with another method than waiting for GPU to be idle
|
||||
if(!m_image_registry.Find(ptr))
|
||||
{
|
||||
core::error::report(e_kind::error, "invalid image ptr (NULL)");
|
||||
Error("invalid image ptr");
|
||||
return;
|
||||
}
|
||||
|
||||
auto it = std::find_if(_textures.begin(), _textures.end(), [=](const Texture& texture) { return &texture == ptr; });
|
||||
if(it == _textures.end())
|
||||
{
|
||||
core::error::report(e_kind::error, "invalid image ptr");
|
||||
return;
|
||||
}
|
||||
Texture* texture = static_cast<Texture*>(ptr);
|
||||
if(!texture->isInit())
|
||||
core::error::report(e_kind::error, "trying to destroy a texture that has already been destroyed");
|
||||
if(!texture->IsInit())
|
||||
Error("trying to destroy a texture that has already been destroyed");
|
||||
else
|
||||
texture->destroy();
|
||||
texture->Destroy();
|
||||
for(auto& gs : _graphics)
|
||||
gs->tryEraseTextureFromManager(texture);
|
||||
_textures.erase(it);
|
||||
{
|
||||
if(gs)
|
||||
gs->TryEraseTextureFromManager(texture);
|
||||
}
|
||||
m_image_registry.UnregisterTexture(texture);
|
||||
delete texture;
|
||||
}
|
||||
|
||||
Application::~Application()
|
||||
{
|
||||
TextLibrary::get().clearLibrary();
|
||||
FontLibrary::get().clearLibrary();
|
||||
TextLibrary::Get().ClearLibrary();
|
||||
FontLibrary::Get().ClearLibrary();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* bridge.cpp :+: :+: :+: */
|
||||
/* Bridge.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
|
||||
/* Updated: 2024/03/25 23:05:46 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/04/23 14:44:27 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pre_compiled.h>
|
||||
#include <PreCompiled.h>
|
||||
|
||||
#include <pre_compiled.h>
|
||||
#include "errors.h"
|
||||
#include "application.h"
|
||||
#include <renderer/core/render_core.h>
|
||||
#include <Core/Application.h>
|
||||
#include <Renderer/Core/RenderCore.h>
|
||||
#include <mlx.h>
|
||||
#include <core/memory.h>
|
||||
#include <Core/Memory.h>
|
||||
|
||||
static void* __mlx_ptr = nullptr;
|
||||
|
||||
#define MLX_CHECK_APPLICATION_POINTER(ptr) \
|
||||
if(ptr != __mlx_ptr || ptr == NULL) \
|
||||
mlx::core::error::report(e_kind::fatal_error, "invalid mlx pointer passed to '%s'", MLX_FUNC_SIG); \
|
||||
mlx::FatalError("invalid mlx pointer passed to '%'", MLX_FUNC_SIG); \
|
||||
else {} // just to avoid issues with possible if-else statements outside this macro
|
||||
|
||||
extern "C"
|
||||
@@ -32,14 +30,14 @@ extern "C"
|
||||
{
|
||||
if(__mlx_ptr != nullptr)
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times");
|
||||
return NULL; // not nullptr for the C compatibility
|
||||
Error("MLX cannot be initialized multiple times");
|
||||
return nullptr;
|
||||
}
|
||||
mlx::MemManager::get(); // just to initialize the C garbage collector
|
||||
mlx::core::Application* app = new mlx::core::Application;
|
||||
mlx::Render_Core::get().init();
|
||||
mlx::MemManager::Get(); // just to initialize the C garbage collector
|
||||
mlx::Application* app = new mlx::Application;
|
||||
if(app == nullptr)
|
||||
mlx::core::error::report(e_kind::fatal_error, "Tout a pété");
|
||||
mlx::FatalError("Tout a pété");
|
||||
mlx::RenderCore::Get().Init();
|
||||
__mlx_ptr = static_cast<void*>(app);
|
||||
return __mlx_ptr;
|
||||
}
|
||||
@@ -49,30 +47,30 @@ extern "C"
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if(w <= 0 || h <= 0)
|
||||
{
|
||||
mlx::core::error::report(e_kind::fatal_error, "invalid window size (%d x %d)", w, h);
|
||||
mlx::FatalError("invalid window size (%d x %d)", w, h);
|
||||
return NULL; // not nullptr for the C compatibility
|
||||
}
|
||||
return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
|
||||
return static_cast<mlx::Application*>(mlx)->NewGraphicsSuport(w, h, title);
|
||||
}
|
||||
|
||||
int mlx_loop_hook(void* mlx, int (*f)(void*), void* param)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->loopHook(f, param);
|
||||
static_cast<mlx::Application*>(mlx)->LoopHook(f, param);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_loop(void* mlx)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->run();
|
||||
static_cast<mlx::Application*>(mlx)->Run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_loop_end(void* mlx)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->loopEnd();
|
||||
static_cast<mlx::Application*>(mlx)->LoopEnd();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -89,21 +87,21 @@ extern "C"
|
||||
int mlx_mouse_move(void* mlx, void* win, int x, int y)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->mouseMove(win, x, y);
|
||||
static_cast<mlx::Application*>(mlx)->MouseMove(win, x, y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_mouse_get_pos(void* mlx, int* x, int* y)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->getMousePos(x, y);
|
||||
static_cast<mlx::Application*>(mlx)->GetMousePos(x, y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->onEvent(win, static_cast<int>(event), funct_ptr, param);
|
||||
static_cast<mlx::Application*>(mlx)->OnEvent(win, static_cast<int>(event), funct_ptr, param);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -111,14 +109,17 @@ extern "C"
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if (width <= 0 || height <= 0)
|
||||
mlx::core::error::report(e_kind::fatal_error, "invalid image size (%d x %d)", width, height);
|
||||
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
|
||||
{
|
||||
mlx::Error("invalid image size (% x %)", width, height);
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<mlx::Application*>(mlx)->NewTexture(width, height);
|
||||
}
|
||||
|
||||
int mlx_get_image_pixel(void* mlx, void* img, int x, int y)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
int color = static_cast<mlx::core::Application*>(mlx)->getTexturePixel(img, x, y);
|
||||
int color = static_cast<mlx::Application*>(mlx)->GetTexturePixel(img, x, y);
|
||||
unsigned char color_bits[4];
|
||||
color_bits[0] = (color & 0x000000FF);
|
||||
color_bits[1] = (color & 0x0000FF00) >> 8;
|
||||
@@ -135,20 +136,20 @@ extern "C"
|
||||
color_bits[1] = (color & 0x0000FF00) >> 8;
|
||||
color_bits[2] = (color & 0x000000FF);
|
||||
color_bits[3] = (color & 0xFF000000) >> 24;
|
||||
static_cast<mlx::core::Application*>(mlx)->setTexturePixel(img, x, y, *reinterpret_cast<unsigned int*>(color_bits));
|
||||
static_cast<mlx::Application*>(mlx)->SetTexturePixel(img, x, y, *reinterpret_cast<unsigned int*>(color_bits));
|
||||
}
|
||||
|
||||
int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->texturePut(win, img, x, y);
|
||||
static_cast<mlx::Application*>(mlx)->TexturePut(win, img, x, y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_destroy_image(void* mlx, void* img)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->destroyTexture(img);
|
||||
static_cast<mlx::Application*>(mlx)->DestroyTexture(img);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -156,42 +157,51 @@ extern "C"
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if (filename == nullptr)
|
||||
mlx::core::error::report(e_kind::fatal_error, "PNG loader : filename is NULL");
|
||||
{
|
||||
mlx::Error("PNG loader : filename is NULL");
|
||||
return nullptr;
|
||||
}
|
||||
std::filesystem::path file(filename);
|
||||
if(file.extension() != ".png")
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "PNG loader : not a png file '%s'", filename);
|
||||
mlx::Error("PNG loader : not a png file '%'", filename);
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
|
||||
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
|
||||
}
|
||||
|
||||
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if (filename == nullptr)
|
||||
mlx::core::error::report(e_kind::fatal_error, "JPG loader : filename is NULL");
|
||||
{
|
||||
mlx::Error("JPG loader : filename is NULL");
|
||||
return nullptr;
|
||||
}
|
||||
std::filesystem::path file(filename);
|
||||
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "JPG loader : not a jpg file '%s'", filename);
|
||||
mlx::Error("JPG loader : not a jpg file '%'", filename);
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
|
||||
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
|
||||
}
|
||||
|
||||
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if (filename == nullptr)
|
||||
mlx::core::error::report(e_kind::fatal_error, "BMP loader : filename is NULL");
|
||||
{
|
||||
mlx::Error("BMP loader : filename is NULL");
|
||||
return nullptr;
|
||||
}
|
||||
std::filesystem::path file(filename);
|
||||
if(file.extension() != ".bmp" && file.extension() != ".dib")
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "BMP loader : not a bmp file '%s'", filename);
|
||||
mlx::Error("BMP loader : not a bmp file '%'", filename);
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
|
||||
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
|
||||
}
|
||||
|
||||
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
|
||||
@@ -202,7 +212,7 @@ extern "C"
|
||||
color_bits[1] = (color & 0x0000FF00) >> 8;
|
||||
color_bits[2] = (color & 0x000000FF);
|
||||
color_bits[3] = (color & 0xFF000000) >> 24;
|
||||
static_cast<mlx::core::Application*>(mlx)->pixelPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits));
|
||||
static_cast<mlx::Application*>(mlx)->PixelPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -214,7 +224,7 @@ extern "C"
|
||||
color_bits[1] = (color & 0x0000FF00) >> 8;
|
||||
color_bits[2] = (color & 0x000000FF);
|
||||
color_bits[3] = (color & 0xFF000000) >> 24;
|
||||
static_cast<mlx::core::Application*>(mlx)->stringPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits), str);
|
||||
static_cast<mlx::Application*>(mlx)->StringPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits), str);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -223,19 +233,19 @@ extern "C"
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if (filepath == nullptr)
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "Font loader : filepath is NULL");
|
||||
mlx::Error("Font loader : filepath is NULL");
|
||||
return;
|
||||
}
|
||||
std::filesystem::path file(filepath);
|
||||
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
|
||||
mlx::Error("TTF loader : not a truetype font file '%'", filepath);
|
||||
return;
|
||||
}
|
||||
if(std::strcmp(filepath, "default") == 0)
|
||||
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, 6.f);
|
||||
static_cast<mlx::Application*>(mlx)->LoadFont(win, file, 6.f);
|
||||
else
|
||||
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, 16.f);
|
||||
static_cast<mlx::Application*>(mlx)->LoadFont(win, file, 16.f);
|
||||
}
|
||||
|
||||
void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
|
||||
@@ -243,37 +253,37 @@ extern "C"
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if (filepath == nullptr)
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "Font loader : filepath is NULL");
|
||||
mlx::Error("Font loader : filepath is NULL");
|
||||
return;
|
||||
}
|
||||
std::filesystem::path file(filepath);
|
||||
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "TTF loader : not a truetype font file '%s'", filepath);
|
||||
mlx::Error("TTF loader : not a truetype font file '%'", filepath);
|
||||
return;
|
||||
}
|
||||
static_cast<mlx::core::Application*>(mlx)->loadFont(win, file, scale);
|
||||
static_cast<mlx::Application*>(mlx)->LoadFont(win, file, scale);
|
||||
}
|
||||
|
||||
int mlx_clear_window(void* mlx, void* win)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->clearGraphicsSupport(win);
|
||||
static_cast<mlx::Application*>(mlx)->ClearGraphicsSupport(win);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_destroy_window(void* mlx, void* win)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win);
|
||||
static_cast<mlx::Application*>(mlx)->DestroyGraphicsSupport(win);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mlx_destroy_display(void* mlx)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
delete static_cast<mlx::core::Application*>(mlx);
|
||||
mlx::Render_Core::get().destroy();
|
||||
delete static_cast<mlx::Application*>(mlx);
|
||||
mlx::RenderCore::Get().Destroy();
|
||||
__mlx_ptr = nullptr;
|
||||
return 0;
|
||||
}
|
||||
@@ -281,7 +291,7 @@ extern "C"
|
||||
int mlx_get_screens_size(void* mlx, void* win, int* w, int* h)
|
||||
{
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
static_cast<mlx::core::Application*>(mlx)->getScreenSize(win, w, h);
|
||||
static_cast<mlx::Application*>(mlx)->GetScreenSize(win, w, h);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -290,15 +300,15 @@ extern "C"
|
||||
MLX_CHECK_APPLICATION_POINTER(mlx);
|
||||
if(fps < 0)
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "You cannot set a negative FPS cap (nice try)");
|
||||
fps = -fps;
|
||||
mlx::Error("You cannot set a negative FPS cap (nice try)");
|
||||
return 0;
|
||||
}
|
||||
if(fps == 0)
|
||||
{
|
||||
mlx::core::error::report(e_kind::error, "You cannot set a FPS cap to 0 (nice try)");
|
||||
mlx::Error("You cannot set a FPS cap to 0 (nice try)");
|
||||
return 0;
|
||||
}
|
||||
static_cast<mlx::core::Application*>(mlx)->setFPSCap(static_cast<std::uint32_t>(fps));
|
||||
static_cast<mlx::Application*>(mlx)->SetFPSCap(static_cast<std::uint32_t>(fps));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,73 +1,68 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* graphics.cpp :+: :+: :+: */
|
||||
/* Graphics.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
|
||||
/* Updated: 2024/03/27 00:32:34 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/04/23 14:03:51 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pre_compiled.h>
|
||||
#include <PreCompiled.h>
|
||||
|
||||
#include <core/graphics.h>
|
||||
#include <Core/Graphics.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, Texture* render_target, int id) :
|
||||
_window(nullptr),
|
||||
_renderer(std::make_unique<Renderer>()),
|
||||
_width(w),
|
||||
_height(h),
|
||||
_id(id),
|
||||
_has_window(false)
|
||||
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, NonOwningPtr<Texture> render_target, int id) :
|
||||
p_window(nullptr),
|
||||
m_width(w),
|
||||
m_height(h),
|
||||
m_id(id),
|
||||
m_has_window(false)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_renderer->setWindow(nullptr);
|
||||
_renderer->init(render_target);
|
||||
_pixel_put_pipeline.init(w, h, *_renderer);
|
||||
_text_manager.init(*_renderer);
|
||||
m_renderer.SetWindow(nullptr);
|
||||
m_renderer.Init(render_target);
|
||||
m_pixel_put_pipeline.Init(w, h, m_renderer);
|
||||
m_text_manager.Init(m_renderer);
|
||||
}
|
||||
|
||||
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) :
|
||||
_window(std::make_shared<Window>(w, h, title)),
|
||||
_renderer(std::make_unique<Renderer>()),
|
||||
_width(w),
|
||||
_height(h),
|
||||
_id(id),
|
||||
_has_window(true)
|
||||
p_window(std::make_shared<Window>(w, h, title)),
|
||||
m_width(w),
|
||||
m_height(h),
|
||||
m_id(id),
|
||||
m_has_window(true)
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
_renderer->setWindow(_window.get());
|
||||
_renderer->init(nullptr);
|
||||
_pixel_put_pipeline.init(w, h, *_renderer);
|
||||
_text_manager.init(*_renderer);
|
||||
m_renderer.SetWindow(p_window.get());
|
||||
m_renderer.Init(nullptr);
|
||||
m_pixel_put_pipeline.Init(w, h, m_renderer);
|
||||
m_text_manager.Init(m_renderer);
|
||||
}
|
||||
|
||||
void GraphicsSupport::render() noexcept
|
||||
void GraphicsSupport::Render() noexcept
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
if(!_renderer->beginFrame())
|
||||
if(!m_renderer.BeginFrame())
|
||||
return;
|
||||
_proj = glm::ortho<float>(0, _width, 0, _height);
|
||||
_renderer->getUniformBuffer()->setData(sizeof(_proj), &_proj);
|
||||
m_proj = glm::ortho<float>(0, m_width, 0, m_height);
|
||||
m_renderer.GetUniformBuffer()->SetData(sizeof(m_proj), &m_proj);
|
||||
|
||||
static std::array<VkDescriptorSet, 2> sets = {
|
||||
_renderer->getVertDescriptorSet().get(),
|
||||
VK_NULL_HANDLE
|
||||
};
|
||||
m_renderer.getVertDescriptorSet().Bind();
|
||||
|
||||
for(auto& data : m_drawlist)
|
||||
data->Render(m_renderer);
|
||||
|
||||
m_pixel_put_pipeline.Render(m_renderer);
|
||||
|
||||
m_renderer.EndFrame();
|
||||
|
||||
for(auto& data : _drawlist)
|
||||
data->render(sets, *_renderer);
|
||||
|
||||
_pixel_put_pipeline.render(sets, *_renderer);
|
||||
|
||||
_renderer->endFrame();
|
||||
|
||||
for(auto& data : _drawlist)
|
||||
data->resetUpdate();
|
||||
data->ResetUpdate();
|
||||
|
||||
#ifdef GRAPHICS_MEMORY_DUMP
|
||||
// dump memory to file every two seconds
|
||||
@@ -75,7 +70,7 @@ namespace mlx
|
||||
static std::int64_t timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
|
||||
if(std::chrono::duration<std::uint64_t>{static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) - timer} >= 1s)
|
||||
{
|
||||
Render_Core::get().getAllocator().dumpMemoryToJson();
|
||||
RenderCore::Get().GetAllocator().DumpMemoryToJson();
|
||||
timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
|
||||
}
|
||||
#endif
|
||||
@@ -84,11 +79,11 @@ namespace mlx
|
||||
GraphicsSupport::~GraphicsSupport()
|
||||
{
|
||||
MLX_PROFILE_FUNCTION();
|
||||
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
|
||||
_text_manager.destroy();
|
||||
_pixel_put_pipeline.destroy();
|
||||
_renderer->destroy();
|
||||
if(_window)
|
||||
_window->destroy();
|
||||
vkDeviceWaitIdle(RenderCore::Get().GetDevice().Get());
|
||||
m_text_manager.Destroy();
|
||||
m_pixel_put_pipeline.Destroy();
|
||||
m_renderer->Destroy();
|
||||
if(p_window)
|
||||
p_window->Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +1,63 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* memory.cpp :+: :+: :+: */
|
||||
/* Memory.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */
|
||||
/* Updated: 2024/03/25 19:01:02 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/04/23 14:05:52 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pre_compiled.h>
|
||||
#include <PreCompiled.h>
|
||||
|
||||
#include <core/memory.h>
|
||||
#include <core/errors.h>
|
||||
#include <Core/Memory.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void* MemManager::malloc(std::size_t size)
|
||||
void* MemManager::Malloc(std::size_t size)
|
||||
{
|
||||
void* ptr = std::malloc(size);
|
||||
if(ptr != nullptr)
|
||||
_blocks.push_back(ptr);
|
||||
s_blocks.push_back(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* MemManager::calloc(std::size_t n, std::size_t size)
|
||||
void* MemManager::Calloc(std::size_t n, std::size_t size)
|
||||
{
|
||||
void* ptr = std::calloc(n, size);
|
||||
if(ptr != nullptr)
|
||||
_blocks.push_back(ptr);
|
||||
s_blocks.push_back(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void* MemManager::realloc(void* ptr, std::size_t size)
|
||||
void* MemManager::Realloc(void* ptr, std::size_t size)
|
||||
{
|
||||
void* ptr2 = std::realloc(ptr, size);
|
||||
if(ptr2 != nullptr)
|
||||
_blocks.push_back(ptr2);
|
||||
auto it = std::find(_blocks.begin(), _blocks.end(), ptr);
|
||||
if(it != _blocks.end())
|
||||
_blocks.erase(it);
|
||||
s_blocks.push_back(ptr2);
|
||||
auto it = std::find(s_blocks.begin(), s_blocks.end(), ptr);
|
||||
if(it != s_blocks.end())
|
||||
s_blocks.erase(it);
|
||||
return ptr2;
|
||||
}
|
||||
|
||||
void MemManager::free(void* ptr)
|
||||
void MemManager::Free(void* ptr)
|
||||
{
|
||||
auto it = std::find(_blocks.begin(), _blocks.end(), ptr);
|
||||
if(it == _blocks.end())
|
||||
auto it = std::find(s_blocks.begin(), s_blocks.end(), ptr);
|
||||
if(it == s_blocks.end())
|
||||
{
|
||||
core::error::report(e_kind::error, "Memory Manager : trying to free a pointer not allocated by the memory manager");
|
||||
Error("Memory Manager : trying to free a pointer not allocated by the memory manager");
|
||||
return;
|
||||
}
|
||||
std::free(*it);
|
||||
_blocks.erase(it);
|
||||
s_blocks.erase(it);
|
||||
}
|
||||
|
||||
MemManager::~MemManager()
|
||||
{
|
||||
std::for_each(_blocks.begin(), _blocks.end(), [](void* ptr)
|
||||
std::for_each(s_blocks.begin(), s_blocks.end(), [](void* ptr)
|
||||
{
|
||||
std::free(ptr);
|
||||
});
|
||||
|
||||
@@ -1,52 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* profiler.cpp :+: :+: :+: */
|
||||
/* Profiler.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/10 13:56:21 by maldavid #+# #+# */
|
||||
/* Updated: 2024/03/25 19:01:06 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/04/23 14:08:51 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <pre_compiled.h>
|
||||
#include <PreCompiled.h>
|
||||
|
||||
#include <core/profiler.h>
|
||||
#include <core/errors.h>
|
||||
#include <iostream>
|
||||
#include <Core/Profiler.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void Profiler::beginRuntimeSession()
|
||||
void Profiler::BeginRuntimeSession()
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
if(_runtime_session_began)
|
||||
std::lock_guard lock(m_mutex);
|
||||
if(m_runtime_session_began)
|
||||
return;
|
||||
_output_stream.open("./runtime_profile.mlx.json", std::ofstream::out | std::ofstream::trunc);
|
||||
m_output_stream.open("./runtime_profile.mlx.json", std::ofstream::out | std::ofstream::trunc);
|
||||
|
||||
if(_output_stream.is_open())
|
||||
writeHeader();
|
||||
if(m_output_stream.is_open())
|
||||
WriteHeader();
|
||||
else
|
||||
core::error::report(e_kind::error, "Profiler : cannot open runtime profile file");
|
||||
_runtime_session_began = true;
|
||||
Error("Profiler : cannot open runtime profile file");
|
||||
m_runtime_session_began = true;
|
||||
}
|
||||
|
||||
void Profiler::appendProfileData(ProfileResult&& result)
|
||||
void Profiler::AppendProfileData(ProfileResult&& result)
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
auto it = _profile_data.find(result.name);
|
||||
if(it != _profile_data.end())
|
||||
auto it = m_profile_data.find(result.name);
|
||||
if(it != m_profile_data.end())
|
||||
{
|
||||
result.elapsed_time = (result.elapsed_time + it->second.second.elapsed_time) / it->second.first;
|
||||
_profile_data[result.name].first++;
|
||||
_profile_data[result.name].second = result;
|
||||
m_profile_data[result.name].first++;
|
||||
m_profile_data[result.name].second = result;
|
||||
}
|
||||
else
|
||||
_profile_data[result.name] = std::make_pair(1, result);
|
||||
m_profile_data[result.name] = std::make_pair(1, result);
|
||||
}
|
||||
|
||||
void Profiler::writeProfile(const ProfileResult& result)
|
||||
void Profiler::WriteProfile(const ProfileResult& result)
|
||||
{
|
||||
std::stringstream json;
|
||||
json << std::setprecision(9) << std::fixed;
|
||||
@@ -56,26 +54,26 @@ namespace mlx
|
||||
json << "\t\"thread id\" : " << result.thread_id << "," << '\n';
|
||||
json << "\t\"average duration\" : \"" << result.elapsed_time.count() << "ms\"\n";
|
||||
json << "}";
|
||||
_output_stream << json.str();
|
||||
m_output_stream << json.str();
|
||||
}
|
||||
|
||||
void Profiler::endRuntimeSession()
|
||||
void Profiler::EndRuntimeSession()
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
if(!_runtime_session_began)
|
||||
std::lock_guard lock(m_mutex);
|
||||
if(!m_runtime_session_began)
|
||||
return;
|
||||
for(auto& [_, pair] : _profile_data)
|
||||
writeProfile(pair.second);
|
||||
writeFooter();
|
||||
_output_stream.close();
|
||||
_profile_data.clear();
|
||||
_runtime_session_began = false;
|
||||
for(auto& [_, pair] : m_profile_data)
|
||||
WriteProfile(pair.second);
|
||||
WriteFooter();
|
||||
m_output_stream.close();
|
||||
m_profile_data.clear();
|
||||
m_runtime_session_began = false;
|
||||
}
|
||||
|
||||
Profiler::~Profiler()
|
||||
{
|
||||
if(!_runtime_session_began)
|
||||
if(!m_runtime_session_began)
|
||||
return;
|
||||
endRuntimeSession();
|
||||
EndRuntimeSession();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/06 11:26:37 by maldavid #+# #+# */
|
||||
/* Updated: 2024/03/25 19:00:36 by maldavid ### ########.fr */
|
||||
/* Updated: 2024/04/23 14:09:35 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -20,6 +20,6 @@ namespace mlx
|
||||
static std::mt19937_64 engine(random_device());
|
||||
static std::uniform_int_distribution<std::uint64_t> uniform_distribution;
|
||||
|
||||
UUID::UUID() : _uuid(uniform_distribution(engine)) {}
|
||||
UUID::UUID(std::uint64_t uuid) : _uuid(uuid) {}
|
||||
UUID::UUID() : m_uuid(uniform_distribution(engine)) {}
|
||||
UUID::UUID(std::uint64_t uuid) : m_uuid(uuid) {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user