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

@@ -50,37 +50,49 @@ namespace mlx
RenderCore::Get().WaitDeviceIdle();
}
void* Application::NewTexture(int w, int h)
mlx_image Application::NewTexture(int w, int h)
{
MLX_PROFILE_FUNCTION();
mlx_image image;
try { image = new mlx_image_handler; }
catch(...) { return nullptr; }
Texture* texture;
try { texture = new Texture({}, w, h, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_user_image"); }
catch(...) { return nullptr; }
m_image_registry.RegisterTexture(texture);
return texture;
image->texture = texture;
return image;
}
void* Application::NewStbTexture(char* file, int* w, int* h)
mlx_image Application::NewStbTexture(char* file, int* w, int* h)
{
MLX_PROFILE_FUNCTION();
mlx_image image;
try { image = new mlx_image_handler; }
catch(...) { return nullptr; }
Texture* texture = StbTextureLoad(file, w, h);
if(texture == nullptr)
return nullptr;
m_image_registry.RegisterTexture(texture);
return texture;
image->texture = texture;
return image;
}
void Application::DestroyTexture(void* ptr)
void Application::DestroyTexture(mlx_image image)
{
MLX_PROFILE_FUNCTION();
RenderCore::Get().WaitDeviceIdle();
if(!m_image_registry.IsTextureKnown(static_cast<Texture*>(ptr)))
if(!m_image_registry.IsTextureKnown(image->texture))
{
Error("invalid image ptr");
Error("invalid image handle");
return;
}
Texture* texture = static_cast<Texture*>(ptr);
Texture* texture = image->texture.Get();
if(!texture->IsInit())
Error("trying to destroy a texture that has already been destroyed");
else
@@ -92,6 +104,7 @@ namespace mlx
gs->TryEraseSpritesInScene(texture);
}
delete texture;
delete image;
}
Application::~Application()

View File

@@ -5,13 +5,14 @@
#include <Renderer/RenderCore.h>
#include <mlx.h>
#include <Core/Memory.h>
#include <Core/Handles.h>
static void* __mlx_ptr = nullptr;
static mlx::Application* __internal_application_ptr = nullptr;
#ifndef DISABLE_ALL_SAFETIES
#define MLX_CHECK_APPLICATION_POINTER(ptr) \
if(ptr != __mlx_ptr || ptr == NULL) \
mlx::FatalError("invalid mlx pointer passed to '%'", MLX_FUNC_SIG); \
if(ptr == NULL || ptr->app.Get() != __internal_application_ptr) \
mlx::FatalError("invalid mlx handle passed to '%'", MLX_FUNC_SIG); \
else {} // just to avoid issues with possible if-else statements outside this macro
#else
#define MLX_CHECK_APPLICATION_POINTER(ptr)
@@ -19,148 +20,162 @@ static void* __mlx_ptr = nullptr;
extern "C"
{
void* mlx_init()
mlx_context mlx_init()
{
if(__mlx_ptr != nullptr)
if(__internal_application_ptr != nullptr)
{
mlx::Error("MLX cannot be initialized multiple times");
return nullptr;
}
mlx::MemManager::Get(); // just to initialize the C garbage collector
mlx::Application* app = new mlx::Application;
if(app == nullptr)
mlx::FatalError("Tout a pété");
__mlx_ptr = static_cast<void*>(app);
return __mlx_ptr;
try { __internal_application_ptr = new mlx::Application; }
catch(...) { mlx::FatalError("internal application memory allocation failed"); }
mlx_context_handler* context;
try { context = new mlx_context_handler; }
catch(...) { mlx::FatalError("mlx_context memory allocation failed"); }
context->app = __internal_application_ptr;
return context;
}
void* mlx_new_window(void* mlx, int w, int h, const char* title)
void mlx_set_fps_goal(mlx_context mlx, int fps)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(w <= 0 || h <= 0)
if(fps < 0)
mlx::Error("You cannot set a negative FPS cap (nice try)");
else if(fps == 0)
mlx::Error("You cannot set a FPS cap to 0 (nice try)");
else
mlx->app->SetFPSCap(static_cast<std::uint32_t>(fps));
}
void mlx_destroy_context(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
delete mlx;
delete __internal_application_ptr;
__internal_application_ptr = nullptr;
}
mlx_window mlx_new_window(mlx_context mlx, const mlx_window_create_info* info)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
return mlx->app->NewGraphicsSuport(info);
}
void mlx_destroy_window(mlx_context mlx, mlx_window win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx->app->DestroyGraphicsSupport(win);
}
void mlx_set_window_position(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 && !gs->HasWindow())
return;
gs->GetWindow()->SetPosition(x, y);
}
void mlx_clear_window(mlx_context mlx, mlx_window win, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->ResetRenderData(color);
}
void mlx_get_screen_size(mlx_context mlx, mlx_window win, int* w, int* h)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
if(!gs->HasWindow())
{
mlx::FatalError("invalid window size (%d x %d)", w, h);
return NULL; // not nullptr for the C compatibility
*w = 0;
*h = 0;
}
return static_cast<mlx::Application*>(mlx)->NewGraphicsSuport(w, h, title, false);
else
gs->GetWindow()->GetScreenSizeWindowIsOn(w, h);
}
void* mlx_new_resizable_window(void* mlx, int w, int h, const char* title)
void mlx_loop_hook(mlx_context mlx, int (*f)(void*), void* param)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(w <= 0 || h <= 0)
{
mlx::FatalError("invalid window size (%d x %d)", w, h);
return NULL; // not nullptr for the C compatibility
}
return static_cast<mlx::Application*>(mlx)->NewGraphicsSuport(w, h, title, true);
mlx->app->LoopHook(f, param);
}
void mlx_set_window_position(void *mlx, void *win, int x, int y)
void mlx_loop(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->SetGraphicsSupportPosition(win, x, y);
mlx->app->Run();
}
void mlx_loop_hook(void* mlx, int (*f)(void*), void* param)
void mlx_loop_end(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->LoopHook(f, param);
mlx->app->LoopEnd();
}
void mlx_loop(void* mlx)
void mlx_mouse_show(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->Run();
}
void mlx_loop_end(void* mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->LoopEnd();
}
void mlx_mouse_show()
{
mlx::SDLManager::ShowCursor();
}
void mlx_mouse_hide()
void mlx_mouse_hide(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::SDLManager::HideCursor();
}
void mlx_mouse_move(void* mlx, void* win, int x, int y)
void mlx_mouse_move(mlx_context mlx, mlx_window win, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->MouseMove(win, x, y);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs && !gs->HasWindow())
return;
gs->GetWindow()->MoveMouse(x, y);
}
void mlx_mouse_get_pos(void* mlx, int* x, int* y)
void mlx_mouse_get_pos(mlx_context mlx, int* x, int* y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->GetMousePos(x, y);
mlx->app->GetMousePos(x, y);
}
void mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
void mlx_on_event(mlx_context mlx, mlx_window win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->OnEvent(win, static_cast<int>(event), funct_ptr, param);
mlx->app->OnEvent(win, static_cast<int>(event), funct_ptr, param);
}
void* mlx_new_image(void* mlx, int width, int height)
void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (width <= 0 || height <= 0)
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->PixelPut(x, y, color);
}
mlx_image mlx_new_image(mlx_context mlx, int width, int height)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(width <= 0 || height <= 0)
{
mlx::Error("invalid image size (% x %)", width, height);
return nullptr;
}
return static_cast<mlx::Application*>(mlx)->NewTexture(width, height);
return mlx->app->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::Application*>(mlx)->GetTexturePixel(img, x, y);
unsigned char color_bits[4];
color_bits[0] = (color & 0x000000FF);
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x00FF0000) >> 16;
color_bits[3] = (color & 0xFF000000) >> 24;
return *reinterpret_cast<int*>(color_bits);
}
void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::Application*>(mlx)->SetTexturePixel(img, x, y, *reinterpret_cast<unsigned int*>(color_bits));
}
void mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->TexturePut(win, img, x, y, 1.0f, 0.0f);
}
void mlx_transform_put_image_to_window(void* mlx, void* win, void* img, int x, int y, float scale, float angle)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->TexturePut(win, img, x, y, scale, angle);
}
void mlx_destroy_image(void* mlx, void* img)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->DestroyTexture(img);
}
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height)
mlx_image mlx_new_image_from_file(mlx_context mlx, char* filename, int* width, int* height)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
@@ -169,71 +184,74 @@ extern "C"
return nullptr;
}
std::filesystem::path file(filename);
if(file.extension() != ".png")
if( file.extension() != ".png" &&
file.extension() != ".jpg" &&
file.extension() != ".jpeg" &&
file.extension() != ".bmp" &&
file.extension() != ".dib")
{
mlx::Error("PNG loader: not a png file '%'", filename);
mlx::Error("PNG loader: not a valid file format '%'", filename);
return nullptr;
}
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
return mlx->app->NewStbTexture(filename, width, height);
}
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
void mlx_destroy_image(mlx_context mlx, mlx_image image)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
{
mlx::Error("JPG loader: filename is NULL");
return nullptr;
}
std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
{
mlx::Error("JPG loader: not a jpg file '%'", filename);
return nullptr;
}
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
mlx->app->DestroyTexture(image);
}
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
int mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
{
mlx::Error("BMP loader: filename is NULL");
return nullptr;
}
std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib")
{
mlx::Error("BMP loader: not a bmp file '%'", filename);
return nullptr;
}
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return 0;
return texture->GetPixel(x, y);
}
void mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::Application*>(mlx)->PixelPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits));
mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
if(!texture)
return;
texture->SetPixel(x, y, color);
}
void mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str)
void mlx_put_image_to_window(mlx_context mlx, mlx_window win, mlx_image image, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::Application*>(mlx)->StringPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits), str);
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, 1.0f, 0.0f);
}
void mlx_set_font(void* mlx, char* filepath)
void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
if(str == nullptr)
{
mlx::Error("invalid text (NULL)");
return;
}
if(std::strlen(str) == 0)
{
mlx::Warning("trying to put an empty text");
return;
}
gs->StringPut(x, y, color, str);
}
void mlx_set_font(mlx_context mlx, char* filepath)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
@@ -248,12 +266,12 @@ extern "C"
return;
}
if(std::strcmp(filepath, "default") == 0)
static_cast<mlx::Application*>(mlx)->LoadFont(file, 6.f);
mlx->app->LoadFont(file, 6.f);
else
static_cast<mlx::Application*>(mlx)->LoadFont(file, 16.f);
mlx->app->LoadFont(file, 16.f);
}
void mlx_set_font_scale(void* mlx, char* filepath, float scale)
void mlx_set_font_scale(mlx_context mlx, char* filepath, float scale)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
@@ -267,47 +285,6 @@ extern "C"
mlx::Error("TTF loader: not a truetype font file '%'", filepath);
return;
}
static_cast<mlx::Application*>(mlx)->LoadFont(file, scale);
}
void mlx_clear_window(void* mlx, void* win, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::Application*>(mlx)->ClearGraphicsSupport(win, *reinterpret_cast<unsigned int*>(color_bits));
}
void mlx_destroy_window(void* mlx, void* win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->DestroyGraphicsSupport(win);
}
void mlx_destroy_display(void* mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
delete static_cast<mlx::Application*>(mlx);
__mlx_ptr = nullptr;
}
void mlx_get_screens_size(void* mlx, void* win, int* w, int* h)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->GetScreenSize(win, w, h);
}
void mlx_set_fps_goal(void* mlx, int fps)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(fps < 0)
mlx::Error("You cannot set a negative FPS cap (nice try)");
else if(fps == 0)
mlx::Error("You cannot set a FPS cap to 0 (nice try)");
else
static_cast<mlx::Application*>(mlx)->SetFPSCap(static_cast<std::uint32_t>(fps));
mlx->app->LoadFont(file, scale);
}
}

View File

@@ -3,25 +3,15 @@
namespace mlx
{
GraphicsSupport::GraphicsSupport([[maybe_unused]] std::size_t w, [[maybe_unused]] std::size_t h, NonOwningPtr<Texture> render_target, int id) :
GraphicsSupport::GraphicsSupport(const mlx_window_create_info* info, int id) :
m_put_pixel_manager(&m_renderer),
p_window(nullptr),
m_id(id),
m_has_window(false)
m_id(id)
{
MLX_PROFILE_FUNCTION();
m_renderer.Init(render_target);
m_scene_renderer.Init(render_target);
p_scene = std::make_unique<Scene>();
}
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id, bool is_resizable) :
m_put_pixel_manager(&m_renderer),
p_window(std::make_shared<Window>(w, h, title, is_resizable)),
m_id(id),
m_has_window(true)
{
MLX_PROFILE_FUNCTION();
p_window = std::make_shared<Window>(info);
m_has_window = true;
m_renderer.Init(p_window.get());
m_scene_renderer.Init(nullptr);
p_scene = std::make_unique<Scene>();

View File

@@ -47,7 +47,7 @@ namespace mlx
DebugLog("SDL Manager initialized");
}
Handle SDLManager::CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden, std::int32_t& id, bool is_resizable)
Handle SDLManager::CreateWindow(const mlx_window_create_info* info, std::int32_t& id, bool hidden)
{
Internal::WindowInfos* infos = new Internal::WindowInfos;
Verify(infos != nullptr, "SDL: window allocation failed");
@@ -57,10 +57,10 @@ namespace mlx
flags |= SDL_WINDOW_HIDDEN;
else
flags |= SDL_WINDOW_SHOWN;
if(is_resizable)
if(info->is_resizable)
flags |= SDL_WINDOW_RESIZABLE;
infos->window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
infos->window = SDL_CreateWindow(info->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, info->width, info->height, flags);
if(!infos->window)
FatalError("SDL: unable to open a new window; %", SDL_GetError());
infos->icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask);