reworking application and bridge

This commit is contained in:
2024-12-15 03:35:17 +01:00
parent e365c8a48c
commit 5a36b90a72
22 changed files with 464 additions and 505 deletions

View File

@@ -11,35 +11,25 @@
namespace mlx
{
// TODO : FIX THIS DAMN GOD CLASS !!!!!!!!!!!!!!!!
class Application
{
public:
Application();
inline void GetMousePos(int* x, int* y) noexcept;
inline void MouseMove(Handle win, int x, int y) noexcept;
inline void OnEvent(Handle win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline void GetScreenSize(Handle win, int* w, int* h) noexcept;
inline void GetScreenSize(mlx_window win, int* w, int* h) noexcept;
inline void SetFPSCap(std::uint32_t fps) noexcept;
inline Handle NewGraphicsSuport(std::size_t w, std::size_t h, const char* title, bool is_resizable);
inline void ClearGraphicsSupport(Handle win, int color);
inline void DestroyGraphicsSupport(Handle win);
inline void SetGraphicsSupportPosition(Handle win, int x, int y);
inline void OnEvent(mlx_window win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline void PixelPut(Handle win, int x, int y, std::uint32_t color) const noexcept;
inline void StringPut(Handle win, int x, int y, std::uint32_t color, char* str);
inline mlx_window NewGraphicsSuport(const mlx_window_create_info* info);
inline NonOwningPtr<GraphicsSupport> GetGraphicsSupport(mlx_window win);
inline void DestroyGraphicsSupport(mlx_window win);
Handle NewTexture(int w, int h);
Handle NewStbTexture(char* file, int* w, int* h); // stb textures are image files (png, jpg, bpm, ...)
inline void TexturePut(Handle win, Handle img, int x, int y, float scale, float angle);
inline int GetTexturePixel(Handle img, int x, int y);
inline void SetTexturePixel(Handle img, int x, int y, std::uint32_t color);
void DestroyTexture(Handle ptr);
mlx_image NewTexture(int w, int h);
mlx_image NewStbTexture(char* file, int* w, int* h); // stb textures are image files (png, jpg, bpm, ...)
inline NonOwningPtr<Texture> GetTexture(mlx_image image);
void DestroyTexture(mlx_image img);
inline void LoopHook(int (*f)(void*), void* param);
inline void LoopEnd() noexcept;

View File

@@ -1,33 +1,34 @@
#pragma once
#include <Core/Application.h>
#include <Core/Handles.h>
#include <Embedded/DogicaTTF.h>
#ifndef DISABLE_ALL_SAFETIES
#define CHECK_WINDOW_PTR(win) \
#define CHECK_WINDOW_PTR(win, retval) \
if(win == nullptr) \
{ \
Error("invalid window ptr (NULL)"); \
return; \
Error("invalid window handle (NULL)"); \
return retval; \
} \
else if(std::find_if(m_graphics.begin(), m_graphics.end(), [win](const std::unique_ptr<GraphicsSupport>& gs){ return gs && *static_cast<int*>(win) == gs->GetID(); }) == m_graphics.end()) \
else if(std::find_if(m_graphics.begin(), m_graphics.end(), [win](const std::unique_ptr<GraphicsSupport>& gs){ return gs && win->id == gs->GetID(); }) == m_graphics.end()) \
{ \
Error("invalid window ptr"); \
return; \
Error("invalid window handle"); \
return retval; \
} else {}
#define CHECK_IMAGE_PTR(img, retval) \
if(img == nullptr) \
{ \
Error("invalid image ptr (NULL)"); \
retval; \
Error("invalid image handle (NULL)"); \
return retval; \
} \
else if(!m_image_registry.IsTextureKnown(static_cast<Texture*>(img))) \
else if(!m_image_registry.IsTextureKnown(image->texture)) \
{ \
Error("invalid image ptr"); \
retval; \
Error("invalid image handle"); \
return retval; \
} else {}
#else
#define CHECK_WINDOW_PTR(win)
#define CHECK_WINDOW_PTR(win, retval)
#define CHECK_IMAGE_PTR(img, retval)
#endif
@@ -39,32 +40,12 @@ namespace mlx
*y = m_in.GetY();
}
void Application::MouseMove(Handle win, int x, int y) noexcept
void Application::OnEvent(mlx_window win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
{
CHECK_WINDOW_PTR(win);
if(!m_graphics[*static_cast<int*>(win)]->HasWindow())
{
Warning("trying to move the mouse relative to a window that is targeting an image and not a real window, this is not allowed (move ignored)");
CHECK_WINDOW_PTR(win, );
if(!m_graphics[win->id]->HasWindow())
return;
}
m_graphics[*static_cast<int*>(win)]->GetWindow()->MoveMouse(x, y);
}
void Application::OnEvent(Handle win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
{
CHECK_WINDOW_PTR(win);
if(!m_graphics[*static_cast<int*>(win)]->HasWindow())
{
Warning("trying to add event hook for a window that is targeting an image and not a real window, this is not allowed (hook ignored)");
return;
}
m_in.OnEvent(m_graphics[*static_cast<int*>(win)]->GetWindow()->GetID(), event, funct_ptr, param);
}
void Application::GetScreenSize(Handle win, int* w, int* h) noexcept
{
CHECK_WINDOW_PTR(win);
m_graphics[*static_cast<int*>(win)]->GetWindow()->GetScreenSizeWindowIsOn(w, h);
m_in.OnEvent(m_graphics[win->id]->GetWindow()->GetID(), event, funct_ptr, param);
}
void Application::SetFPSCap(std::uint32_t fps) noexcept
@@ -72,81 +53,38 @@ namespace mlx
m_fps.SetMaxFPS(fps);
}
void* Application::NewGraphicsSuport(std::size_t w, std::size_t h, const char* title, bool is_resizable)
mlx_window Application::NewGraphicsSuport(const mlx_window_create_info* info)
{
MLX_PROFILE_FUNCTION();
if(m_image_registry.IsTextureKnown(reinterpret_cast<Texture*>(const_cast<char*>(title))))
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, reinterpret_cast<Texture*>(const_cast<char*>(title)), m_graphics.size()));
else
if(!info)
{
if(title == NULL)
{
FatalError("invalid window title (NULL)");
return nullptr;
}
if(static_cast<void*>(const_cast<char*>(title)) == static_cast<void*>(this))
{
for(std::size_t i = 0; i < 8; i++)
{
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(std::rand() % 1920, std::rand() % 1080, "让我们在月光下做爱吧", m_graphics.size(), is_resizable));
m_graphics.back()->GetWindow()->SetPosition(std::rand() % 1920, std::rand() % 1080);
}
}
else
{
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, title, m_graphics.size(), is_resizable));
m_in.RegisterWindow(m_graphics.back()->GetWindow());
}
Error("invalid window create info (NULL)");
return nullptr;
}
mlx_window window;
try { window = new mlx_window_handler; }
catch(...) { return nullptr; }
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(info, m_graphics.size()));
m_in.RegisterWindow(m_graphics.back()->GetWindow());
m_graphics.back()->GetScene().BindFont(p_last_font_bound);
return static_cast<void*>(&m_graphics.back()->GetID());
window->id = m_graphics.back()->GetID();
return window;
}
void Application::ClearGraphicsSupport(Handle win, int color)
NonOwningPtr<GraphicsSupport> Application::GetGraphicsSupport(mlx_window win)
{
CHECK_WINDOW_PTR(win, nullptr);
return m_graphics[win->id].get();
}
void Application::DestroyGraphicsSupport(mlx_window win)
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
m_graphics[*static_cast<int*>(win)]->ResetRenderData(color);
}
void Application::DestroyGraphicsSupport(Handle win)
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
m_graphics[*static_cast<int*>(win)].reset();
}
void Application::SetGraphicsSupportPosition(Handle win, int x, int y)
{
CHECK_WINDOW_PTR(win);
if(!m_graphics[*static_cast<int*>(win)]->HasWindow())
Warning("trying to move a window that is targeting an image and not a real window, this is not allowed");
else
m_graphics[*static_cast<int*>(win)]->GetWindow()->SetPosition(x, y);
}
void Application::PixelPut(Handle win, int x, int y, std::uint32_t color) const noexcept
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
m_graphics[*static_cast<int*>(win)]->PixelPut(x, y, color);
}
void Application::StringPut(Handle win, int x, int y, std::uint32_t color, char* str)
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
if(str == nullptr)
{
Error("invalid text (NULL)");
return;
}
if(std::strlen(str) == 0)
{
Warning("trying to put an empty text");
return;
}
m_graphics[*static_cast<int*>(win)]->StringPut(x, y, color, str);
CHECK_WINDOW_PTR(win, );
m_graphics[win->id].reset();
delete win;
}
void Application::LoadFont(const std::filesystem::path& filepath, float scale)
@@ -172,40 +110,16 @@ namespace mlx
}
}
void Application::TexturePut(Handle win, Handle img, int x, int y, float scale, float angle)
NonOwningPtr<Texture> Application::GetTexture(mlx_image image)
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
CHECK_IMAGE_PTR(img, return);
NonOwningPtr<Texture> texture = static_cast<Texture*>(img);
if(!texture->IsInit())
Error("trying to put a texture that has been destroyed");
else
m_graphics[*static_cast<int*>(win)]->TexturePut(texture, x, y, scale, angle);
}
int Application::GetTexturePixel(Handle img, int x, int y)
{
MLX_PROFILE_FUNCTION();
CHECK_IMAGE_PTR(img, return 0);
NonOwningPtr<Texture> texture = static_cast<Texture*>(img);
CHECK_IMAGE_PTR(image, nullptr);
NonOwningPtr<Texture> texture = image->texture;
if(!texture->IsInit())
{
Error("trying to get a pixel from texture that has been destroyed");
return 0;
Error("trying to use a texture that has been destroyed");
return nullptr;
}
return texture->GetPixel(x, y);
}
void Application::SetTexturePixel(Handle img, int x, int y, std::uint32_t color)
{
MLX_PROFILE_FUNCTION();
CHECK_IMAGE_PTR(img, return);
NonOwningPtr<Texture> texture = static_cast<Texture*>(img);
if(!texture->IsInit())
Error("trying to set a pixel on texture that has been destroyed");
else
texture->SetPixel(x, y, color);
return texture;
}
void Application::LoopHook(int (*f)(void*), void* param)

View File

@@ -1,6 +1,7 @@
#ifndef __MLX_GRAPHICS__
#define __MLX_GRAPHICS__
#include <mlx.h>
#include <Platform/Window.h>
#include <Renderer/Renderer.h>
#include <Graphics/Scene.h>
@@ -14,8 +15,7 @@ namespace mlx
class GraphicsSupport : public NonCopyable
{
public:
GraphicsSupport(std::size_t w, std::size_t h, NonOwningPtr<Texture> render_target, int id);
GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id, bool is_resizable);
GraphicsSupport(const mlx_window_create_info* info, int id);
[[nodiscard]] MLX_FORCEINLINE int& GetID() noexcept { return m_id; }
[[nodiscard]] inline std::shared_ptr<Window> GetWindow() { return p_window; }
@@ -24,8 +24,8 @@ namespace mlx
inline void ResetRenderData(int color) noexcept;
inline void PixelPut(int x, int y, std::uint32_t color) noexcept;
inline void StringPut(int x, int y, std::uint32_t color, std::string str);
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 TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept;

View File

@@ -7,10 +7,10 @@ namespace mlx
{
MLX_PROFILE_FUNCTION();
Vec4f vec_color = {
static_cast<float>((color & 0x000000FF)) / 255.0f,
static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0xFF000000) >> 24) / 255.0f,
static_cast<float>((color & 0x00FF0000) >> 16) / 255.0f,
static_cast<float>((color & 0xFF000000) >> 24) / 255.0f
static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0x000000FF)) / 255.0f,
};
p_scene->ResetScene(std::move(vec_color));
m_put_pixel_manager.ResetRenderData();
@@ -18,7 +18,7 @@ namespace mlx
m_pixelput_called = false;
}
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept
void GraphicsSupport::PixelPut(int x, int y, int color) noexcept
{
MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
@@ -30,17 +30,17 @@ namespace mlx
}
}
void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str)
void GraphicsSupport::StringPut(int x, int y, int color, std::string str)
{
MLX_PROFILE_FUNCTION();
if(str.empty())
return;
Vec4f vec_color = {
static_cast<float>((color & 0x000000FF)) / 255.0f,
static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0xFF000000) >> 24) / 255.0f,
static_cast<float>((color & 0x00FF0000) >> 16) / 255.0f,
static_cast<float>((color & 0xFF000000) >> 24) / 255.0f
static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0x000000FF)) / 255.0f,
};
NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color);

25
runtime/Includes/Core/Handles.h git.filemode.normal_file
View File

@@ -0,0 +1,25 @@
#ifndef __MLX_HANDLES__
#define __MLX_HANDLES__
#include <Core/Application.h>
#include <Renderer/Image.h>
extern "C"
{
struct mlx_context_handler
{
mlx::NonOwningPtr<mlx::Application> app;
};
struct mlx_window_handler
{
int id;
};
struct mlx_image_handler
{
mlx::NonOwningPtr<mlx::Texture> texture;
};
}
#endif

View File

@@ -1,6 +1,7 @@
#ifndef __MLX_SDL_MANAGER__
#define __MLX_SDL_MANAGER__
#include <mlx.h>
#include <Maths/Vec2.h>
namespace mlx
@@ -10,7 +11,7 @@ namespace mlx
public:
SDLManager();
Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden, std::int32_t& id, bool is_resizable);
Handle CreateWindow(const mlx_window_create_info* info, std::int32_t& id, bool hidden);
void DestroyWindow(Handle window) noexcept;
void InputsFetcher(func::function<void(mlx_event_type, int, int)> functor);

View File

@@ -11,7 +11,7 @@ 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, std::uint64_t draw_layer, std::uint32_t color);
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t draw_layer, int color);
void ResetRenderData();
~PutPixelManager();

View File

@@ -1,6 +1,7 @@
#ifndef __MLX_WINDOW__
#define __MLX_WINDOW__
#include <mlx.h>
#include <Maths/Vec2.h>
#include <Core/SDLManager.h>
@@ -9,7 +10,7 @@ namespace mlx
class Window
{
public:
Window(std::size_t w, std::size_t h, const std::string& title, bool is_resizable, bool hidden = false);
Window(const mlx_window_create_info* info, bool hidden = false);
inline Handle GetWindowHandle() const noexcept { return p_window; }
inline int GetWidth() const noexcept { return m_width; }

View File

@@ -44,6 +44,7 @@
#include <iterator>
#include <stb_truetype.h>
#include <variant>
#include <bit>
#include <type_traits>
#include <string_view>
#include <sstream>

View File

@@ -83,7 +83,7 @@ namespace mlx
void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name);
void Destroy() noexcept override;
void SetPixel(int x, int y, std::uint32_t color) noexcept;
void SetPixel(int x, int y, int color) noexcept;
int GetPixel(int x, int y) noexcept;
void Update(VkCommandBuffer cmd);
@@ -94,7 +94,7 @@ namespace mlx
void OpenCPUBuffer();
private:
std::vector<std::uint32_t> m_cpu_buffer;
std::vector<int> m_cpu_buffer;
std::optional<GPUBuffer> m_staging_buffer;
bool m_has_been_modified = false;
};