diff --git a/4 b/4 new file mode 100644 index 0000000..82888f1 --- /dev/null +++ b/4 @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* window.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */ +/* Updated: 2023/04/12 18:35:51 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +namespace mlx +{ + MLX_Window::MLX_Window(std::size_t w, std::size_t h, std::string title) : _width(w), _height(h) + { + _win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN); + if(!_win) + core::error::report(e_kind::fatal_error, std::string("unable to open a new window, ") + SDL_GetError()); + } + + void MLX_Window::destroy() noexcept + { + if(_win) + SDL_DestroyWindow(_win); + } + + MLX_Window::~MLX_Window() + { + destroy(); + } +} diff --git a/includes/mlx.h b/includes/mlx.h index ec3d2f4..3fa5114 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: 2023/04/12 11:12:00 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 19:33:39 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ int mlx_mouse_hide(); int mlx_mouse_move(void* mlx, void* win, int x, int y); int mlx_mouse_get_pos(void* mlx, int* x, int* y); -int mlx_on_event(void* mlx, mlx_event_type event, int (*f)(), void* param); +int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*f)(), void* param); int mlx_do_key_autorepeaton(void* mlx); int mlx_do_key_autorepeatoff(void* mlx); diff --git a/src/core/application.h b/src/core/application.h index 5e0ea13..86d57a7 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: 2023/04/12 11:14:24 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 19:34:45 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ namespace mlx::core inline void getMousePos(int* x, int* y) noexcept; inline void mouseMove(void* win, int x, int y) noexcept; - inline void onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept; + inline void onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept; inline constexpr void enableAutoRepeat() noexcept; inline constexpr void disableAutoRepeat() noexcept; diff --git a/src/core/application.inl b/src/core/application.inl index 3b9eadb..5be920d 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -27,9 +27,9 @@ namespace mlx::core SDL_FlushEvent(SDL_MOUSEMOTION); } - void Application::onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept + void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept { - _in->onEvent(event, funct_ptr, param); + _in->onEvent(_graphics[*static_cast(win)]->getWindow()->getID(), event, funct_ptr, param); } constexpr void Application::enableAutoRepeat() noexcept @@ -53,6 +53,7 @@ namespace mlx::core void* Application::newGraphicsSuport(std::size_t w, std::size_t h, std::string title) { _graphics.emplace_back(std::make_unique(w, h, std::move(title), _graphics.size())); + _in->addWindow(_graphics.back()->getWindow()); return static_cast(&_graphics.back()->getID()); } diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index ab0c6af..b26e490 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: 2023/04/12 11:16:52 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 19:34:22 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,9 +69,9 @@ extern "C" return 0; } - int mlx_on_event(void* mlx, int event, int (*funct_ptr)(int, void*), void* param) + int mlx_on_event(void* mlx, void* win, int event, int (*funct_ptr)(int, void*), void* param) { - static_cast(mlx)->onEvent(event, funct_ptr, param); + static_cast(mlx)->onEvent(win, event, funct_ptr, param); return 0; } diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index f12dc18..bfb27f6 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: 2023/04/12 11:31:28 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 19:54:20 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,57 +31,6 @@ namespace mlx while(SDL_PollEvent(&_event)) { - if(_event.window.event == SDL_WINDOWEVENT_CLOSE) - _end = true; - - switch(_event.type) - { - case SDL_KEYDOWN: - { - _keys[_event.key.keysym.scancode] = static_cast(action::down); - if(_events_hooks[0].hook) - _events_hooks[0].hook(_event.key.keysym.scancode, _events_hooks[0].param); - break; - } - - case SDL_KEYUP: - { - _keys[_event.key.keysym.scancode] = static_cast(action::up); - if(_events_hooks[1].hook) - _events_hooks[1].hook(_event.key.keysym.scancode, _events_hooks[1].param); - break; - } - - case SDL_MOUSEBUTTONDOWN: - { - _mouse[_event.button.button] = static_cast(action::down); - if(_events_hooks[2].hook) - _events_hooks[2].hook(_event.button.button, _events_hooks[2].param); - break; - } - - case SDL_MOUSEBUTTONUP: - { - _mouse[_event.button.button] = static_cast(action::up); - if(_events_hooks[3].hook) - _events_hooks[3].hook(_event.button.button, _events_hooks[3].param); - break; - } - - case SDL_WINDOWEVENT: - { - switch(_event.window.event) - { - //case - - default : break; - } - break; - } - - default: break; - } - if(_event.type == SDL_MOUSEMOTION) { _x = _event.motion.x; @@ -90,6 +39,105 @@ namespace mlx _xRel = _event.motion.xrel; _yRel = _event.motion.yrel; } + + uint32_t id = _event.window.windowID; + if(!_events_hooks.count(id)) + continue; + + switch(_event.type) + { + case SDL_KEYDOWN: + { + _keys[_event.key.keysym.scancode] = static_cast(action::down); + if(_events_hooks[id][0].hook) + _events_hooks[id][0].hook(_event.key.keysym.scancode, _events_hooks[id][0].param); + break; + } + + case SDL_KEYUP: + { + _keys[_event.key.keysym.scancode] = static_cast(action::up); + if(_events_hooks[id][1].hook) + _events_hooks[id][1].hook(_event.key.keysym.scancode, _events_hooks[id][1].param); + break; + } + + case SDL_MOUSEBUTTONDOWN: + { + _mouse[_event.button.button] = static_cast(action::down); + if(_events_hooks[id][2].hook) + _events_hooks[id][2].hook(_event.button.button, _events_hooks[id][2].param); + break; + } + + case SDL_MOUSEBUTTONUP: + { + _mouse[_event.button.button] = static_cast(action::up); + if(_events_hooks[id][3].hook) + _events_hooks[id][3].hook(_event.button.button, _events_hooks[id][3].param); + break; + } + + case SDL_WINDOWEVENT: + { + switch(_event.window.event) + { + case SDL_WINDOWEVENT_CLOSE: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(0, _events_hooks[id][4].param); + break; + } + case SDL_WINDOWEVENT_MOVED: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(1, _events_hooks[id][4].param); + break; + } + case SDL_WINDOWEVENT_MINIMIZED: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(2, _events_hooks[id][4].param); + break; + } + case SDL_WINDOWEVENT_MAXIMIZED: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(3, _events_hooks[id][4].param); + break; + } + case SDL_WINDOWEVENT_ENTER: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(4, _events_hooks[id][4].param); + break; + } + case SDL_WINDOWEVENT_FOCUS_GAINED: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(4, _events_hooks[id][4].param); + break; + } + case SDL_WINDOWEVENT_LEAVE: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(5, _events_hooks[id][4].param); + break; + } + case SDL_WINDOWEVENT_FOCUS_LOST: + { + if(_events_hooks[id][4].hook) + _events_hooks[id][4].hook(4, _events_hooks[id][4].param); + break; + } + + default : break; + } + break; + } + + default: break; + } } } } diff --git a/src/platform/inputs.h b/src/platform/inputs.h index ba0bbac..147bd4e 100644 --- a/src/platform/inputs.h +++ b/src/platform/inputs.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ -/* Updated: 2023/04/12 13:47:02 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 20:02:44 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include #include #include +#include #include "window.h" @@ -53,21 +54,26 @@ namespace mlx inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; } inline constexpr void disableAutoRepeat() noexcept { _auto_repeat = false; } - inline void onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept + inline void addWindow(std::shared_ptr window) { - _events_hooks[event].hook = funct_ptr; - _events_hooks[event].param = param; + _windows[window->getID()] = window; + _events_hooks[window->getID()] = {}; + } + + inline void onEvent(uint32_t id, int event, int (*funct_ptr)(int, void*), void* param) noexcept + { + _events_hooks[id][event].hook = funct_ptr; + _events_hooks[id][event].param = param; } ~Input() = default; private: std::array _keys; + std::unordered_map> _windows; + std::unordered_map> _events_hooks; SDL_Event _event; std::array _mouse; - std::vector _window; - - std::array _events_hooks; int _x = 0; int _y = 0; diff --git a/src/platform/window.cpp b/src/platform/window.cpp index 34adf03..760ea2d 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: 2023/04/02 15:39:59 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 18:45:05 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,11 +20,17 @@ namespace mlx _win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN); if(!_win) core::error::report(e_kind::fatal_error, std::string("unable to open a new window, ") + SDL_GetError()); + _id = SDL_GetWindowID(_win); } - MLX_Window::~MLX_Window() + void MLX_Window::destroy() noexcept { if(_win) SDL_DestroyWindow(_win); } + + MLX_Window::~MLX_Window() + { + destroy(); + } } diff --git a/src/platform/window.h b/src/platform/window.h index 07355cf..55c6dd6 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: 2023/04/02 15:39:53 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 19:06:24 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,9 @@ namespace mlx inline SDL_Window* getNativeWindow() const noexcept { return _win; } inline int getWidth() const noexcept { return _width; } inline int getHeight() const noexcept { return _height; } + inline uint32_t getID() const noexcept { return _id; } + + void destroy() noexcept; ~MLX_Window(); @@ -33,6 +36,7 @@ namespace mlx SDL_Window* _win = nullptr; int _width = 0; int _height = 0; + uint32_t _id = -1; }; } diff --git a/test/.gdb_history b/test/.gdb_history new file mode 100644 index 0000000..93747ee --- /dev/null +++ b/test/.gdb_history @@ -0,0 +1,6 @@ +run +bt +q +run* +bt +q diff --git a/test/main.c b/test/main.c index e3463bd..5770334 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/04/12 13:44:38 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 19:42:33 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,8 +73,8 @@ int key_hook(int key, t_mlx *param) int window_hook(int event, t_mlx *param) { - (void)param; - printf("%d\n", event); + if (event == 0) + mlx_loop_end(param->mlx); return (0); } @@ -87,7 +87,8 @@ int main(void) mlx.mlx = mlx_init(); mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window"); - mlx_on_event(mlx.mlx, MLX_KEYDOWN, key_hook, &mlx); + mlx_on_event(mlx.mlx, mlx.win, MLX_KEYDOWN, key_hook, &mlx); + mlx_on_event(mlx.mlx, mlx.win, MLX_WINDOW_EVENT, window_hook, &mlx); mlx.logo = mlx_png_file_to_image(mlx.mlx, "42_logo.png", &w, &h); mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200);