diff --git a/Makefile b/Makefile index 27f0102..890e9a4 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ IMAGES_OPTIMIZED ?= true FORCE_INTEGRATED_GPU ?= false GRAPHICS_MEMORY_DUMP ?= false PROFILER ?= false +FORCE_WAYLAND ?= false +DISABLE_ALL_SAFETIES ?= false _ENABLEDFLAGS = SRCS = $(wildcard $(addsuffix /*.cpp, runtime/Sources/Core)) @@ -57,6 +59,14 @@ ifeq ($(PROFILER), true) _ENABLEDFLAGS += PROFILER endif +ifeq ($(FORCE_WAYLAND), true) + _ENABLEDFLAGS += FORCE_WAYLAND +endif + +ifeq ($(DISABLE_ALL_SAFETIES), true) + _ENABLEDFLAGS += DISABLE_ALL_SAFETIES +endif + CXXFLAGS += $(addprefix -D, $(_ENABLEDFLAGS)) RM = rm -rf diff --git a/runtime/Includes/Core/Application.inl b/runtime/Includes/Core/Application.inl index 7e6cfd1..c76adfb 100644 --- a/runtime/Includes/Core/Application.inl +++ b/runtime/Includes/Core/Application.inl @@ -1,29 +1,34 @@ #pragma once #include -#define CHECK_WINDOW_PTR(win) \ - if(win == nullptr) \ - { \ - Error("invalid window ptr (NULL)"); \ - return; \ - } \ - else if(*static_cast(win) < 0 || *static_cast(win) > static_cast(m_graphics.size()))\ - { \ - Error("invalid window ptr"); \ - return; \ - } else {} +#ifndef DISABLE_ALL_SAFETIES + #define CHECK_WINDOW_PTR(win) \ + if(win == nullptr) \ + { \ + Error("invalid window ptr (NULL)"); \ + return; \ + } \ + else if(std::find_if(m_graphics.begin(), m_graphics.end(), [win](const std::unique_ptr& gs){ return *static_cast(win) == gs->GetID(); } != m_graphics.end())) \ + { \ + Error("invalid window ptr"); \ + return; \ + } else {} -#define CHECK_IMAGE_PTR(img, retval) \ - if(img == nullptr) \ - { \ - Error("invalid image ptr (NULL)"); \ - retval; \ - } \ - else if(!m_image_registry.IsTextureKnown(static_cast(img))) \ - { \ - Error("invalid image ptr"); \ - retval; \ - } else {} + #define CHECK_IMAGE_PTR(img, retval) \ + if(img == nullptr) \ + { \ + Error("invalid image ptr (NULL)"); \ + retval; \ + } \ + else if(!m_image_registry.IsTextureKnown(static_cast(img))) \ + { \ + Error("invalid image ptr"); \ + retval; \ + } else {} +#else + #define CHECK_WINDOW_PTR(win) + #define CHECK_IMAGE_PTR(img, retval) +#endif namespace mlx { @@ -41,6 +46,7 @@ namespace mlx 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)"); return; } + m_graphics[*static_cast(win)]->GetWindow()->MoveMouse(x, y); } void Application::OnEvent(Handle win, int event, int (*funct_ptr)(int, void*), void* param) noexcept @@ -57,8 +63,7 @@ namespace mlx void Application::GetScreenSize(Handle win, int* w, int* h) noexcept { CHECK_WINDOW_PTR(win); - *w = 0; - *h = 0; + m_graphics[*static_cast(win)]->GetWindow()->GetScreenSizeWindowIsOn(x, y); } void Application::SetFPSCap(std::uint32_t fps) noexcept @@ -96,6 +101,7 @@ namespace mlx MLX_PROFILE_FUNCTION(); CHECK_WINDOW_PTR(win); m_graphics[*static_cast(win)].reset(); + m_graphics.erase(m_graphics.begin() + *static_cast(win)); } void Application::PixelPut(Handle win, int x, int y, std::uint32_t color) const noexcept diff --git a/runtime/Includes/Core/Format.h b/runtime/Includes/Core/Format.h index e948709..3fdc6bc 100644 --- a/runtime/Includes/Core/Format.h +++ b/runtime/Includes/Core/Format.h @@ -1,9 +1,6 @@ #ifndef __MLX_FORMAT__ #define __MLX_FORMAT__ -#include -#include - namespace mlx { template diff --git a/runtime/Includes/Core/Format.inl b/runtime/Includes/Core/Format.inl index 3bc490a..0b5b3d8 100644 --- a/runtime/Includes/Core/Format.inl +++ b/runtime/Includes/Core/Format.inl @@ -1,7 +1,5 @@ #pragma once #include -#include -#include namespace mlx { diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h index 28c4653..5c8de28 100644 --- a/runtime/Includes/Core/Graphics.h +++ b/runtime/Includes/Core/Graphics.h @@ -46,9 +46,6 @@ namespace mlx std::uint64_t m_current_depth = 0; - std::size_t m_width = 0; - std::size_t m_height = 0; - int m_id; bool m_has_window; diff --git a/runtime/Includes/Core/Graphics.inl b/runtime/Includes/Core/Graphics.inl index 84a1e96..c0f4c02 100644 --- a/runtime/Includes/Core/Graphics.inl +++ b/runtime/Includes/Core/Graphics.inl @@ -25,6 +25,10 @@ namespace mlx void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str) { MLX_PROFILE_FUNCTION(); + (void)x; + (void)y; + (void)color; + (void)str; } void GraphicsSupport::TexturePut(NonOwningPtr texture, int x, int y) @@ -44,6 +48,8 @@ namespace mlx void GraphicsSupport::LoadFont(const std::filesystem::path& filepath, float scale) { MLX_PROFILE_FUNCTION(); + (void)filepath; + (void)scale; } void GraphicsSupport::TryEraseSpritesInScene(NonOwningPtr texture) noexcept diff --git a/runtime/Includes/Core/Logs.h b/runtime/Includes/Core/Logs.h index 42bfe7c..4b3746b 100644 --- a/runtime/Includes/Core/Logs.h +++ b/runtime/Includes/Core/Logs.h @@ -39,7 +39,7 @@ namespace mlx void Assert(bool cond, unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args); #else template - void Assert(bool cond, unsigned int line, std::string_view file, std::string_view function, std::string message, const Args&... args) {} + void Assert([[maybe_unused]] bool cond, [[maybe_unused]] unsigned int line, [[maybe_unused]] std::string_view file, [[maybe_unused]] std::string_view function, [[maybe_unused]] std::string message, [[maybe_unused]] const Args&... args) {} #endif } diff --git a/runtime/Includes/Core/SDLManager.h b/runtime/Includes/Core/SDLManager.h index cdc514c..df49d13 100644 --- a/runtime/Includes/Core/SDLManager.h +++ b/runtime/Includes/Core/SDLManager.h @@ -19,6 +19,8 @@ namespace mlx VkSurfaceKHR CreateVulkanSurface(Handle window, VkInstance instance) const noexcept; std::vector GetRequiredVulkanInstanceExtentions(Handle window) const noexcept; Vec2ui GetVulkanDrawableSize(Handle window) const noexcept; + void MoveMouseOnWindow(Handle window, int x, int y) const noexcept; + void GetScreenSizeWindowIsOn(Handle window, int* x, int* y) const noexcept; inline void SetEventCallback(func::function functor, void* userdata) { f_callback = std::move(functor); p_callback_data = userdata; } diff --git a/runtime/Includes/Platform/Window.h b/runtime/Includes/Platform/Window.h index 24adacf..5895a70 100644 --- a/runtime/Includes/Platform/Window.h +++ b/runtime/Includes/Platform/Window.h @@ -16,6 +16,9 @@ namespace mlx inline int GetHeight() const noexcept { return m_height; } inline std::uint32_t GetID() const noexcept { return m_id; } + 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); } + inline VkSurfaceKHR CreateVulkanSurface(VkInstance instance) const noexcept { return SDLManager::Get().CreateVulkanSurface(p_window, instance); } inline std::vector GetRequiredVulkanInstanceExtentions() const noexcept { return SDLManager::Get().GetRequiredVulkanInstanceExtentions(p_window); } inline Vec2ui GetVulkanDrawableSize() const noexcept { return SDLManager::Get().GetVulkanDrawableSize(p_window); } diff --git a/runtime/Includes/PreCompiled.h b/runtime/Includes/PreCompiled.h index 59cf651..acaac11 100644 --- a/runtime/Includes/PreCompiled.h +++ b/runtime/Includes/PreCompiled.h @@ -42,6 +42,10 @@ #include #include #include +#include +#include +#include +#include #ifndef MLX_PLAT_WINDOWS #include diff --git a/runtime/Sources/Core/Bridge.cpp b/runtime/Sources/Core/Bridge.cpp index 89d741d..deff1a2 100644 --- a/runtime/Sources/Core/Bridge.cpp +++ b/runtime/Sources/Core/Bridge.cpp @@ -7,10 +7,14 @@ static void* __mlx_ptr = nullptr; -#define MLX_CHECK_APPLICATION_POINTER(ptr) \ - if(ptr != __mlx_ptr || ptr == NULL) \ - mlx::FatalError("invalid mlx pointer passed to '%'", MLX_FUNC_SIG); \ - else {} // just to avoid issues with possible if-else statements outside this macro +#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); \ + else {} // just to avoid issues with possible if-else statements outside this macro +#else + #define MLX_CHECK_APPLICATION_POINTER(ptr) +#endif extern "C" { diff --git a/runtime/Sources/Core/Graphics.cpp b/runtime/Sources/Core/Graphics.cpp index 52f6137..a2dbd33 100644 --- a/runtime/Sources/Core/Graphics.cpp +++ b/runtime/Sources/Core/Graphics.cpp @@ -6,8 +6,6 @@ namespace mlx GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, NonOwningPtr render_target, int id) : m_put_pixel_manager(&m_renderer), p_window(nullptr), - m_width(w), - m_height(h), m_id(id), m_has_window(false) { @@ -24,8 +22,6 @@ namespace mlx GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) : m_put_pixel_manager(&m_renderer), p_window(std::make_shared(w, h, title)), - m_width(w), - m_height(h), m_id(id), m_has_window(true) { diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp index 44d9e44..8619340 100644 --- a/runtime/Sources/Core/SDLManager.cpp +++ b/runtime/Sources/Core/SDLManager.cpp @@ -159,6 +159,20 @@ namespace mlx return Vec2ui{ extent }; } + void SDLManager::MoveMouseOnWindow(Handle window, int x, int y) const noexcept + { + SDL_WarpMouseInWindow(static_cast(window), x, y); + SDL_PumpEvents(); + } + + void SDLManager::GetScreenSizeWindowIsOn(Handle window, int* x, int* y) const noexcept + { + SDL_DisplayMode DM; + SDL_GetDesktopDisplayMode(SDL_GetWindowDisplayIndex(static_cast(window)), &DM); + *x = DM.w; + *y = DM.h; + } + std::int32_t SDLManager::GetX() const noexcept { int dummy; diff --git a/xmake.lua b/xmake.lua index fb8e175..857433a 100644 --- a/xmake.lua +++ b/xmake.lua @@ -37,6 +37,11 @@ option("force_wayland") add_defines("FORCE_WAYLAND") option_end() +option("disable_all_safeties") + set_default(false) + add_defines("DISABLE_ALL_SAFETIES") +option_end() + -- Targets target("mlx") @@ -48,6 +53,8 @@ target("mlx") add_options("graphics_memory_dump") add_options("profiler") add_options("force_wayland") + add_options("disable_all_safeties") + add_includedirs("runtime/Includes", "runtime/Sources", "includes", "third_party") set_pcxxheader("runtime/Includes/PreCompiled.h")