diff --git a/Makefile b/Makefile index e488de3..e93dcfa 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maldavid +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# # -# Updated: 2024/03/27 21:30:44 by maldavid ### ########.fr # +# Updated: 2024/05/25 16:08:57 by maldavid ### ########.fr # # # # **************************************************************************** # @@ -16,7 +16,6 @@ SRCS = $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Core)) SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Platform)) SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer)) SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer/**)) -SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Drivers/**)) OBJ_DIR = objs/makefile OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o)) @@ -31,7 +30,7 @@ IMAGES_OPTIMIZED ?= true FORCE_INTEGRATED_GPU ?= false GRAPHICS_MEMORY_DUMP ?= false PROFILER ?= false -LEGACY ?= false +FORCE_WAYLAND ?= false MODE = "release" @@ -73,8 +72,8 @@ ifeq ($(PROFILER), true) CXXFLAGS += -D PROFILER endif -ifeq ($(LEGACY), true) - CXXFLAGS += -D LEGACY +ifeq ($(FORCE_WAYLAND), true) + CXXFLAGS += -D FORCE_WAYLAND endif RM = rm -rf diff --git a/runtime/Includes/Core/Application.h b/runtime/Includes/Core/Application.h index 316dc68..27de075 100644 --- a/runtime/Includes/Core/Application.h +++ b/runtime/Includes/Core/Application.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ -/* Updated: 2024/04/21 20:39:33 by maldavid ### ########.fr */ +/* Updated: 2024/05/25 15:26:36 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,6 @@ #include #include -#include #include #include @@ -61,7 +60,6 @@ namespace mlx private: FpsManager m_fps; Input m_in; - DriverLoader m_driver_loader; ImageRegistry m_image_registry; std::vector> m_graphics; std::function f_loop_hook; @@ -69,6 +67,6 @@ namespace mlx }; } -#include +#include #endif // __MLX_APPLICATION__ diff --git a/runtime/Includes/Core/DriverLoader.inl b/runtime/Includes/Core/DriverLoader.inl deleted file mode 100644 index 6104d76..0000000 --- a/runtime/Includes/Core/DriverLoader.inl +++ /dev/null @@ -1,30 +0,0 @@ -/* **************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* DriverLoader.inl :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/03 14:55:01 by maldavid #+# #+# */ -/* Updated: 2024/04/03 14:55:01 by maldavid ### ########.fr */ -/* */ -/* **************************************************************************** */ - -#pragma once -#include - -namespace mlx -{ - template - bool DriverLoader::LoadDriver() - { - m_instances.emplace_back(new T)->InitDriver(); - } - - void DriverLoader::ShutdownAllDrivers() - { - for(auto& driver : m_instances) - driver->ShutdownDriver(); - m_instances.clear(); - } -} diff --git a/runtime/Includes/Core/Graphics.h b/runtime/Includes/Core/Graphics.h index e29ce56..448e518 100644 --- a/runtime/Includes/Core/Graphics.h +++ b/runtime/Includes/Core/Graphics.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ -/* Updated: 2024/04/23 14:02:48 by maldavid ### ########.fr */ +/* Updated: 2024/05/25 01:00:10 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,6 +70,6 @@ namespace mlx }; } -#include +#include #endif diff --git a/runtime/Includes/Core/DriverLoader.h b/runtime/Includes/Core/SDLManager.h similarity index 52% rename from runtime/Includes/Core/DriverLoader.h rename to runtime/Includes/Core/SDLManager.h index 18e5695..b32bf6f 100644 --- a/runtime/Includes/Core/DriverLoader.h +++ b/runtime/Includes/Core/SDLManager.h @@ -1,39 +1,41 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* DriverLoader.h :+: :+: :+: */ +/* SDLManager.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/02 16:56:10 by maldavid #+# #+# */ -/* Updated: 2024/04/03 15:02:44 by maldavid ### ########.fr */ +/* Created: 2024/05/25 15:28:59 by maldavid #+# #+# */ +/* Updated: 2024/05/25 16:11:50 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef __MLX_CORE_DRIVER_LOADER__ -#define __MLX_CORE_DRIVER_LOADER__ +#ifndef __MLX_SDL_MANAGER__ +#define __MLX_SDL_MANAGER__ -#include +#include namespace mlx { - class DriverLoader + class SDLManager : public Singleton { + friend class Singleton; + public: - DriverLoader() = default; + void Init() noexcept; + void Shutdown() noexcept; - template - inline bool LoadDriver(); - - inline void ShutdownAllDrivers(); - - ~DriverLoader() = default; + void* CreateWindow(const std::string& title, std::size_t w, std::size_t h); + void DestroyWindow(void* window) noexcept; private: - std::vector > m_instances; + SDLManager() = default; + ~SDLManager() = default; + + private: + std::unordered_set m_windows_registry; + bool m_drop_sdl_responsability = false; }; } -#include - #endif diff --git a/runtime/Includes/Drivers/DriverInstance.h b/runtime/Includes/Drivers/DriverInstance.h deleted file mode 100644 index c72cf97..0000000 --- a/runtime/Includes/Drivers/DriverInstance.h +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* DriverInstance.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/02 16:57:20 by maldavid #+# #+# */ -/* Updated: 2024/04/02 17:01:03 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_DRIVER_INSTANCE__ -#define __MLX_DRIVER_INSTANCE__ - -namespace mlx -{ - class DriverInstance - { - public: - DriverInstance() = default; - - virtual bool InitDriver() { m_is_up = true; return true; } - virtual void ShutdownDriver() { m_is_up = false; } - - inline bool IsRunning() const noexcept { return m_is_up; } - - virtual ~DriverInstance() = default; - - private: - bool m_is_up = false; - }; -} - -#endif diff --git a/runtime/Includes/Drivers/GLFW/GLFWDriverInstance.h b/runtime/Includes/Drivers/GLFW/GLFWDriverInstance.h deleted file mode 100644 index fe1d124..0000000 --- a/runtime/Includes/Drivers/GLFW/GLFWDriverInstance.h +++ /dev/null @@ -1,34 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* GLFWDriverInstance.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/02 17:01:51 by maldavid #+# #+# */ -/* Updated: 2024/04/02 17:04:12 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_GLFW_DRIVER_INSTANCE__ -#define __MLX_GLFW_DRIVER_INSTANCE__ - -#include - -namespace mlx -{ - class GLFWDriverInstance : public DriverInstance - { - public: - GLFWDriverInstance() = default; - - inline bool InitDriver() override; - inline void ShutdownDriver() override; - - ~GLFWDriverInstance() override = default; - }; -} - -#include - -#endif diff --git a/runtime/Includes/Drivers/GLFW/GLFWDriverInstance.inl b/runtime/Includes/Drivers/GLFW/GLFWDriverInstance.inl deleted file mode 100644 index b96b66d..0000000 --- a/runtime/Includes/Drivers/GLFW/GLFWDriverInstance.inl +++ /dev/null @@ -1,32 +0,0 @@ -/* **************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* GLFWDriverInstance.inl :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/04/02 17:04:23 by maldavid #+# #+# */ -/* Updated: 2024/04/02 17:04:23 by maldavid ### ########.fr */ -/* */ -/* **************************************************************************** */ - -#include - -namespace mlx -{ - bool GLFWDriverInstance::InitDriver() - { - glfwSetErrorCallback([]([[maybe_unused]] int code, const char* desc) - { - FatalError("GLFW Driver Error : %", desc); - }); - glfwInit(); - DebugLog("GLFW Driver loaded"); - } - - void GLFWDriverInstance::ShutdownDriver() - { - glfwTerminate(); - DebugLog("GLFW Driver shutted down"); - } -} diff --git a/runtime/Includes/Drivers/GLFW/GLFWInputs.h b/runtime/Includes/Drivers/GLFW/GLFWInputs.h deleted file mode 100644 index 0901ec9..0000000 --- a/runtime/Includes/Drivers/GLFW/GLFWInputs.h +++ /dev/null @@ -1,44 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* GLFWInputs.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/03/27 18:32:29 by maldavid #+# #+# */ -/* Updated: 2024/03/27 18:37:58 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef __MLX_GLFW_INPUTS__ -#define __MLX_GLFW_INPUTS__ - -#include - -namespace mlx -{ - class GLFWInputs : public Inputs - { - public: - GLFWInputs() = default; - - void Update() override noexcept; - - void RegisterWindow(std::shared_ptr window) override; - - inline std::int32_t GetX() override const noexcept { return m_x; } - inline std::int32_t GetY() override const noexcept { return m_y; } - inline std::int32_t GetXRel() override const noexcept { return m_x_rel; } - inline std::int32_t GetYRel() override const noexcept { return m_y_rel; } - - ~GLFWInputs() override = default; - - private: - std::int32_t m_x = 0; - std::int32_t m_y = 0; - std::int32_t m_x_rel = 0; - std::int32_t m_y_rel = 0; - }; -} - -#endif diff --git a/runtime/Includes/Platform/Window.h b/runtime/Includes/Platform/Window.h index d9e15a1..30c3945 100644 --- a/runtime/Includes/Platform/Window.h +++ b/runtime/Includes/Platform/Window.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */ -/* Updated: 2024/03/27 21:58:18 by maldavid ### ########.fr */ +/* Updated: 2024/05/25 16:11:00 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,18 +15,12 @@ namespace mlx { - #ifdef LEGACY - using WindowHandle = SDL_Window; - #else - using WindowHandle = GLFWwindow; - #endif - class Window { public: Window(std::size_t w, std::size_t h, const std::string& title); - inline NonOwningPtr GetWindowHandle() const noexcept = 0; + inline void* GetWindowHandle() const noexcept { return p_window; } inline int GetWidth() const noexcept { return m_width; } inline int GetHeight() const noexcept { return m_height; } inline std::uint32_t GetID() const noexcept { return m_id; } @@ -36,6 +30,7 @@ namespace mlx ~Window() = default; private: + void* p_window = nullptr; std::uint32_t m_id = -1; int m_width = 0; int m_height = 0; diff --git a/runtime/Includes/PreCompiled.h b/runtime/Includes/PreCompiled.h index 07647ff..d5f3ace 100644 --- a/runtime/Includes/PreCompiled.h +++ b/runtime/Includes/PreCompiled.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/25 17:37:23 by maldavid #+# #+# */ -/* Updated: 2024/04/23 13:49:52 by maldavid ### ########.fr */ +/* Updated: 2024/05/25 15:46:37 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,12 +23,8 @@ #include #include -#ifdef MLX_LEGACY - #include - #include -#else - #include -#endif +#include +#include #include #include diff --git a/runtime/Includes/Utils/NonOwningPtr.inl b/runtime/Includes/Utils/NonOwningPtr.inl index 0247092..b36a337 100644 --- a/runtime/Includes/Utils/NonOwningPtr.inl +++ b/runtime/Includes/Utils/NonOwningPtr.inl @@ -16,7 +16,7 @@ namespace mlx { template NonOwningPtr::NonOwningPtr(T* ptr) : p_ptr(ptr) {} - + template NonOwningPtr::NonOwningPtr(NonOwningPtr&& ptr) noexcept : p_ptr(ptr.p_ptr) { @@ -28,7 +28,7 @@ namespace mlx { p_ptr = ptr; } - + template NonOwningPtr& NonOwningPtr::operator=(NonOwningPtr&& ptr) noexcept { @@ -47,13 +47,13 @@ namespace mlx { return p_ptr; } - + template T* NonOwningPtr::operator->() const noexcept { return p_ptr; } - + template T& NonOwningPtr::operator*() const noexcept { diff --git a/runtime/Sources/Core/Application.cpp b/runtime/Sources/Core/Application.cpp index d0c754b..a097b9c 100644 --- a/runtime/Sources/Core/Application.cpp +++ b/runtime/Sources/Core/Application.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2024/04/23 15:06:26 by maldavid ### ########.fr */ +/* Updated: 2024/05/25 16:06:57 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ #include #include #include +#include namespace mlx { @@ -29,6 +30,7 @@ namespace mlx }, "__internal_application" }); m_fps.init(); + SDLManager::Get().Init(); } void Application::Run() noexcept @@ -111,5 +113,6 @@ namespace mlx { TextLibrary::Get().ClearLibrary(); FontLibrary::Get().ClearLibrary(); + SDLManager::Get().Shutdown(); } } diff --git a/runtime/Sources/Core/SDLManager.cpp b/runtime/Sources/Core/SDLManager.cpp new file mode 100644 index 0000000..c0f27b9 --- /dev/null +++ b/runtime/Sources/Core/SDLManager.cpp @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* SDLManager.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/25 15:44:03 by maldavid #+# #+# */ +/* Updated: 2024/05/25 16:46:48 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +namespace mlx +{ + #if SDL_BYTEORDER == SDL_BIG_ENDIAN + constexpr const std::uint32_t rmask = 0xff000000; + constexpr const std::uint32_t gmask = 0x00ff0000; + constexpr const std::uint32_t bmask = 0x0000ff00; + constexpr const std::uint32_t amask = 0x000000ff; + #else + constexpr const std::uint32_t rmask = 0x000000ff; + constexpr const std::uint32_t gmask = 0x0000ff00; + constexpr const std::uint32_t bmask = 0x00ff0000; + constexpr const std::uint32_t amask = 0xff000000; + #endif + + namespace details + { + struct WindowInfos + { + SDL_Window* window; + SDL_Surface* icon; + }; + } + + void SDLManager::Init() noexcept + { + MLX_PROFILE_FUNCTION(); + m_drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO); + if(m_drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init + return; + SDL_SetMemoryFunctions(MemManager::malloc, MemManager::calloc, MemManager::realloc, MemManager::free); + + #ifdef FORCE_WAYLAND + SDL_SetHint(SDL_HINT_VIDEODRIVER, "wayland,x11"); + #endif + + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0) + FatalError("SDL : unable to init all subsystems; %", SDL_GetError()); + } + + void* SDLManager::CreateWindow(const std::string& title, std::size_t w, std::size_t h) + { + details::WindowInfos* infos = new details::WindowInfos; + Verify(infos != nullptr, "SDL : window allocation failed"); + + infos->window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN); + if(!infos->window) + FatalError("SDL : unable to open a new window; %", SDL_GetError()); + infos->icon = SDL_CreateRGBSurfaceFrom(static_cast(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask); + SDL_SetWindowIcon(infos->window, infos->icon); + + m_windows_registry.insert(infos); + + return infos; + } + + void SDLManager::DestroyWindow(void* window) noexcept + { + Verify(m_windows_registry.find(window) != m_windows_registry.end(), "SDL : cannot destroy window; unknown window pointer"); + + details::WindowInfos* infos = static_cast(window); + if(infos->window != nullptr) + SDL_DestroyWindow(infos->window); + if(infos->icon != nullptr) + SDL_FreeSurface(infos->icon); + + m_windows_registry.erase(infos); + delete infos; + } + + void SDLManager::Shutdown() noexcept + { + if(m_drop_sdl_responsability) + return; + SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS); + SDL_Quit(); + } +} diff --git a/runtime/Sources/Drivers/GLFW/GLFWInputs.cpp b/runtime/Sources/Drivers/GLFW/GLFWInputs.cpp deleted file mode 100644 index 9379cf1..0000000 --- a/runtime/Sources/Drivers/GLFW/GLFWInputs.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* GLFWInputs.cpp :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: maldavid +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/03/27 18:39:32 by maldavid #+# #+# */ -/* Updated: 2024/03/27 18:42:18 by maldavid ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -namespace mlx -{ - void GLFWInputs::Update() noexcept - { - glfwPollEvents(); - } -} diff --git a/runtime/Sources/Platform/Window.cpp b/runtime/Sources/Platform/Window.cpp index 182f18f..1b48a77 100644 --- a/runtime/Sources/Platform/Window.cpp +++ b/runtime/Sources/Platform/Window.cpp @@ -1,40 +1,38 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* window.cpp :+: :+: :+: */ +/* Window.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */ -/* Updated: 2024/03/26 23:03:59 by maldavid ### ########.fr */ +/* Updated: 2024/05/25 16:13:31 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#include +#include -#include -#include -#include +#include +#include namespace mlx { - Window::Window(std::size_t w, std::size_t h, const std::string& title) : _width(w), _height(h) + Window::Window(std::size_t w, std::size_t h, const std::string& title) : m_width(w), m_height(h) { static std::uint64_t ids = 0; if(title.find("vvaas") != std::string::npos) - core::error::report(e_kind::message, "vvaas est mauvais"); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - _win = glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL);; - _id = ids++; + Message("vvaas est mauvais"); + p_window = SDLManager::Get().CreateWindow(title, w, h); + m_id = ids++; } - void Window::destroy() noexcept + void Window::Destroy() noexcept { - if(_win != nullptr) + if(p_window != nullptr) { - glfwDestroyWindow(_win); - _win = nullptr; + SDLManager::Get().DestroyWindow(p_window); + p_window = nullptr; } } } diff --git a/xmake.lua b/xmake.lua index 449cb19..d9f1b9b 100644 --- a/xmake.lua +++ b/xmake.lua @@ -44,6 +44,11 @@ option("profiler") add_defines("PROFILER") option_end() +option("force_wayland") + set_default(false) + add_defines("FORCE_WAYLAND") +option_end() + -- Targets target("mlx")