working input API

This commit is contained in:
2023-04-12 21:53:14 +02:00
parent fe45d0953d
commit 4fa9873f44
11 changed files with 182 additions and 75 deletions

35
4 git.filemode.normal_file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* window.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */
/* Updated: 2023/04/12 18:35:51 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <platform/window.h>
#include <core/errors.h>
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();
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 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_move(void* mlx, void* win, int x, int y);
int mlx_mouse_get_pos(void* mlx, 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_autorepeaton(void* mlx);
int mlx_do_key_autorepeatoff(void* mlx); int mlx_do_key_autorepeatoff(void* mlx);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 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 getMousePos(int* x, int* y) noexcept;
inline void mouseMove(void* win, 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 enableAutoRepeat() noexcept;
inline constexpr void disableAutoRepeat() noexcept; inline constexpr void disableAutoRepeat() noexcept;

View File

@@ -27,9 +27,9 @@ namespace mlx::core
SDL_FlushEvent(SDL_MOUSEMOTION); 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<int*>(win)]->getWindow()->getID(), event, funct_ptr, param);
} }
constexpr void Application::enableAutoRepeat() noexcept 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) void* Application::newGraphicsSuport(std::size_t w, std::size_t h, std::string title)
{ {
_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, std::move(title), _graphics.size())); _graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, std::move(title), _graphics.size()));
_in->addWindow(_graphics.back()->getWindow());
return static_cast<void*>(&_graphics.back()->getID()); return static_cast<void*>(&_graphics.back()->getID());
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 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; 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::core::Application*>(mlx)->onEvent(event, funct_ptr, param); static_cast<mlx::core::Application*>(mlx)->onEvent(win, event, funct_ptr, param);
return 0; return 0;
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 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)) 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<uint8_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(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) if(_event.type == SDL_MOUSEMOTION)
{ {
_x = _event.motion.x; _x = _event.motion.x;
@@ -90,6 +39,105 @@ namespace mlx
_xRel = _event.motion.xrel; _xRel = _event.motion.xrel;
_yRel = _event.motion.yrel; _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<uint8_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(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;
}
} }
} }
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 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 <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <unordered_map>
#include "window.h" #include "window.h"
@@ -53,21 +54,26 @@ namespace mlx
inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; } inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; }
inline constexpr void disableAutoRepeat() noexcept { _auto_repeat = false; } 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<MLX_Window> window)
{ {
_events_hooks[event].hook = funct_ptr; _windows[window->getID()] = window;
_events_hooks[event].param = param; _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; ~Input() = default;
private: private:
std::array<uint8_t, SDL_NUM_SCANCODES> _keys; std::array<uint8_t, SDL_NUM_SCANCODES> _keys;
std::unordered_map<uint32_t, std::shared_ptr<MLX_Window>> _windows;
std::unordered_map<uint32_t, std::array<Hook, 5>> _events_hooks;
SDL_Event _event; SDL_Event _event;
std::array<uint8_t, 8> _mouse; std::array<uint8_t, 8> _mouse;
std::vector<class* MLX_Window> _window;
std::array<Hook, 5> _events_hooks;
int _x = 0; int _x = 0;
int _y = 0; int _y = 0;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:36:44 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); _win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN);
if(!_win) if(!_win)
core::error::report(e_kind::fatal_error, std::string("unable to open a new window, ") + SDL_GetError()); 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) if(_win)
SDL_DestroyWindow(_win); SDL_DestroyWindow(_win);
} }
MLX_Window::~MLX_Window()
{
destroy();
}
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:53:12 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 SDL_Window* getNativeWindow() const noexcept { return _win; }
inline int getWidth() const noexcept { return _width; } inline int getWidth() const noexcept { return _width; }
inline int getHeight() const noexcept { return _height; } inline int getHeight() const noexcept { return _height; }
inline uint32_t getID() const noexcept { return _id; }
void destroy() noexcept;
~MLX_Window(); ~MLX_Window();
@@ -33,6 +36,7 @@ namespace mlx
SDL_Window* _win = nullptr; SDL_Window* _win = nullptr;
int _width = 0; int _width = 0;
int _height = 0; int _height = 0;
uint32_t _id = -1;
}; };
} }

6
test/.gdb_history git.filemode.normal_file
View File

@@ -0,0 +1,6 @@
run
bt
q
run*
bt
q

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 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) int window_hook(int event, t_mlx *param)
{ {
(void)param; if (event == 0)
printf("%d\n", event); mlx_loop_end(param->mlx);
return (0); return (0);
} }
@@ -87,7 +87,8 @@ int main(void)
mlx.mlx = mlx_init(); mlx.mlx = mlx_init();
mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window"); 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.logo = mlx_png_file_to_image(mlx.mlx, "42_logo.png", &w, &h);
mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF); mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF);
mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200);