From f52c4e51c94c19f1452611e3941dd0a8203f71ea Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Wed, 5 Oct 2022 20:37:04 +0200 Subject: [PATCH] working on window manager --- Makefile | 2 +- includes/mlx.h | 7 ++++++- src/core/application.h | 22 +++++++++++++++++----- src/core/bridge.cpp | 32 +++++++++++++++++++++++++++++--- src/platform/inputs.cpp | 4 ++-- src/platform/inputs.h | 17 +++++++++++------ src/platform/window.cpp | 4 ++-- src/platform/window.h | 7 +++++-- test/main.c | 11 +++++++++-- test/run.sh | 1 + 10 files changed, 83 insertions(+), 24 deletions(-) create mode 100755 test/run.sh diff --git a/Makefile b/Makefile index b73705b..c80d352 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maldavid +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# # -# Updated: 2022/10/04 22:08:52 by maldavid ### ########.fr # +# Updated: 2022/10/05 19:25:51 by maldavid ### ########.fr # # # # **************************************************************************** # diff --git a/includes/mlx.h b/includes/mlx.h index ef90f0a..00d5267 100644 --- a/includes/mlx.h +++ b/includes/mlx.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ -/* Updated: 2022/10/04 22:27:45 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 18:42:22 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,11 @@ void mlx_init(); void mlx_new_window(int w, int h, const char* title); int mlx_loop(); +int mlx_mouse_show(); +int mlx_mouse_hide(); +int mlx_mouse_move(void* win_ptr, int x, int y); +int mlx_mouse_get_pos(void* win_ptr, int* x, int* y); + #ifdef __cplusplus } #endif diff --git a/src/core/application.h b/src/core/application.h index 1061035..93f7676 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ -/* Updated: 2022/10/05 16:58:25 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 19:23:15 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ #include #include +#include "errors.h" + #include #include @@ -25,11 +27,21 @@ namespace mlx::core class Application { public: - Application() = default; + Application() : _in(_wins) {} - inline void new_window(std::size_t w, std::size_t h, std::string title) + inline void* new_window(std::size_t w, std::size_t h, std::string title) { - _wins.emplace_back(std::make_unique(w, h, std::move(title))); + _wins.emplace_back(std::make_shared(w, h, std::move(title), _wins.size())); + return reinterpret_cast(&_wins.back()->get_id()); + } + + inline int get_mouse_pos(void* win_ptr, int* x, int* y) noexcept + { + if(*reinterpret_cast(win_ptr) > _wins.size()) + { + error::report(e_kind::error, "Invalid window pointer"); + return -1; + } } void run() noexcept; @@ -38,7 +50,7 @@ namespace mlx::core private: Input _in; - std::vector> _wins; + std::vector> _wins; bool _is_loop_running = false; }; } diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 9b447be..15abcee 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ -/* Updated: 2022/10/05 14:02:27 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 18:45:52 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,11 +26,11 @@ extern "C" __main_app.reset(new mlx::core::Application()); } - void mlx_new_window(int w, int h, const char* title) + void* mlx_new_window(int w, int h, const char* title) { if(__main_app == nullptr) mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can create new windows"); - __main_app->new_window(w, h, title); + return __main_app->new_window(w, h, title); } int mlx_loop() @@ -39,4 +39,30 @@ extern "C" mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can run the main loop"); __main_app->run(); } + + int mlx_mouse_show() + { + if(__main_app == nullptr) + mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can modify the cursor"); + return SDL_ShowCursor(SDL_ENABLE); + } + + int mlx_mouse_hide() + { + if(__main_app == nullptr) + mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can modify the cursor"); + return SDL_ShowCursor(SDL_DISABLE); + } + + int mlx_mouse_move(void* win_ptr, int x, int y) + { + if(__main_app == nullptr) + mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can modify the mouse"); + } + + int mlx_mouse_get_pos(void* win_ptr, int* x, int* y) + { + if(__main_app == nullptr) + mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can access to the mouse"); + } } diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index ba5f823..3bd8b53 100644 --- a/src/platform/inputs.cpp +++ b/src/platform/inputs.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ -/* Updated: 2022/10/05 16:39:42 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 19:24:30 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ namespace mlx { - Input::Input() + Input::Input(const std::vector>& wins) : _wins(wins) { std::memset(_keys.data(), 0, SDL_NUM_SCANCODES); std::memset(_mouse.data(), 0, 8); diff --git a/src/platform/inputs.h b/src/platform/inputs.h index d58deca..d6abbda 100644 --- a/src/platform/inputs.h +++ b/src/platform/inputs.h @@ -6,13 +6,17 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ -/* Updated: 2022/10/05 16:45:55 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 19:53:18 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include #include +#include +#include +#include +#include + +#include "window.h" namespace mlx { @@ -21,7 +25,7 @@ namespace mlx class Input { public: - Input(); + Input(const std::vector>& wins); void update(); @@ -37,14 +41,15 @@ namespace mlx inline int getYRel() const noexcept { return _yRel; } inline bool is_running() const noexcept { return !_end; } - inline void finish() noexcept { _end = true; } + inline constexpr void finish() noexcept { _end = true; } ~Input() = default; private: + SDL_Event _event; std::array _keys; std::array _mouse; - SDL_Event _event; + std::vector> _wins; int _x = 0; int _y = 0; diff --git a/src/platform/window.cpp b/src/platform/window.cpp index e7bed3e..1faea2c 100644 --- a/src/platform/window.cpp +++ b/src/platform/window.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */ -/* Updated: 2022/10/04 22:09:27 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 18:39:11 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ namespace mlx { - Window::Window(std::size_t w, std::size_t h, std::string title) + Window::Window(std::size_t w, std::size_t h, std::string title, int id) : _id(id) { _win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, /*SDL_WINDOW_VULKAN |*/ SDL_WINDOW_SHOWN); if(!_win) diff --git a/src/platform/window.h b/src/platform/window.h index 2eb1ce3..90e4038 100644 --- a/src/platform/window.h +++ b/src/platform/window.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */ -/* Updated: 2022/10/04 21:59:54 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 19:10:35 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,12 +21,15 @@ namespace mlx class Window { public: - Window(std::size_t w, std::size_t h, std::string title); + Window(std::size_t w, std::size_t h, std::string title, int id); + + inline int& get_id() noexcept { return _id; } ~Window(); private: SDL_Window* _win = nullptr; + int _id; }; } diff --git a/test/main.c b/test/main.c index d9fe52a..d2a5aae 100644 --- a/test/main.c +++ b/test/main.c @@ -6,16 +6,23 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2022/10/05 16:58:46 by maldavid ### ########.fr */ +/* Updated: 2022/10/05 19:25:46 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include "../includes/mlx.h" int main(void) { + void *win_ptr; + int x; + int y; + mlx_init(); - mlx_new_window(400, 400, "My window"); + win_ptr = mlx_new_window(400, 400, "My window"); + mlx_mouse_get_pos(win_ptr, &x, &y); + printf("%d, %d\n", x, y); mlx_loop(); return (0); } diff --git a/test/run.sh b/test/run.sh new file mode 100755 index 0000000..e60dbaa --- /dev/null +++ b/test/run.sh @@ -0,0 +1 @@ +clear && gcc main.c ../libMicroX.so `sdl2-config --cflags --libs` -lSDL2 && ./a.out