adding SDL initialisation and quit, improving inputs code readability

This commit is contained in:
Kbz-8
2023-08-28 10:53:22 +02:00
parent 3b0442df7e
commit a7ac7b2318
5 changed files with 45 additions and 89 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2023/04/25 15:12:57 by maldavid ### ########.fr */
/* Updated: 2023/08/28 10:19:53 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,10 +14,17 @@
#include <renderer/images/texture.h>
#include <renderer/core/render_core.h>
#include <array>
#include <core/errors.h>
#include <utils/endian.h>
namespace mlx::core
{
Application::Application() : _in(std::make_unique<Input>())
{
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
error::report(e_kind::fatal_error, "SDL error : unable to init all subsystems : %s", SDL_GetError());
}
void Application::run() noexcept
{
while(_in->is_running())
@@ -52,4 +59,9 @@ namespace mlx::core
Texture* texture = static_cast<Texture*>(ptr);
texture->destroy();
}
Application::~Application()
{
SDL_Quit();
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/04/25 15:23:31 by maldavid ### ########.fr */
/* Updated: 2023/08/28 10:19:35 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -30,7 +30,7 @@ namespace mlx::core
class Application
{
public:
Application() : _in(std::make_unique<Input>()) {}
Application();
inline void getMousePos(int* x, int* y) noexcept;
inline void mouseMove(void* win, int x, int y) noexcept;
@@ -58,7 +58,7 @@ namespace mlx::core
void run() noexcept;
~Application() = default;
~Application();
private:
std::list<Texture> _textures;

View File

@@ -6,11 +6,12 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2023/04/19 12:14:38 by maldavid ### ########.fr */
/* Updated: 2023/08/28 10:49:03 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include "inputs.h"
#include <mlx.h>
#include <cstring>
namespace mlx
@@ -40,91 +41,93 @@ namespace mlx
uint32_t id = _event.window.windowID;
if(!_events_hooks.count(id))
continue;
auto& hooks = _events_hooks[id];
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);
if(hooks[MLX_KEYDOWN].hook)
hooks[MLX_KEYDOWN].hook(_event.key.keysym.scancode, hooks[MLX_KEYDOWN].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);
if(hooks[MLX_KEYUP].hook)
hooks[MLX_KEYUP].hook(_event.key.keysym.scancode, hooks[MLX_KEYUP].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);
if(hooks[MLX_MOUSEDOWN].hook)
hooks[MLX_MOUSEDOWN].hook(_event.button.button, hooks[MLX_MOUSEDOWN].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);
if(hooks[MLX_MOUSEUP].hook)
hooks[MLX_MOUSEUP].hook(_event.button.button, hooks[MLX_MOUSEUP].param);
break;
}
case SDL_WINDOWEVENT:
{
auto& win_hook = hooks[MLX_WINDOW_EVENT];
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);
if(win_hook.hook)
win_hook.hook(0, win_hook.param);
break;
}
case SDL_WINDOWEVENT_MOVED:
{
if(_events_hooks[id][4].hook)
_events_hooks[id][4].hook(1, _events_hooks[id][4].param);
if(win_hook.hook)
win_hook.hook(1, win_hook.param);
break;
}
case SDL_WINDOWEVENT_MINIMIZED:
{
if(_events_hooks[id][4].hook)
_events_hooks[id][4].hook(2, _events_hooks[id][4].param);
if(win_hook.hook)
win_hook.hook(2, win_hook.param);
break;
}
case SDL_WINDOWEVENT_MAXIMIZED:
{
if(_events_hooks[id][4].hook)
_events_hooks[id][4].hook(3, _events_hooks[id][4].param);
if(win_hook.hook)
win_hook.hook(3, win_hook.param);
break;
}
case SDL_WINDOWEVENT_ENTER:
{
if(_events_hooks[id][4].hook)
_events_hooks[id][4].hook(4, _events_hooks[id][4].param);
if(win_hook.hook)
win_hook.hook(4, win_hook.param);
break;
}
case SDL_WINDOWEVENT_FOCUS_GAINED:
{
if(_events_hooks[id][4].hook)
_events_hooks[id][4].hook(4, _events_hooks[id][4].param);
if(win_hook.hook)
win_hook.hook(4, win_hook.param);
break;
}
case SDL_WINDOWEVENT_LEAVE:
{
if(_events_hooks[id][4].hook)
_events_hooks[id][4].hook(5, _events_hooks[id][4].param);
if(win_hook.hook)
win_hook.hook(5, win_hook.param);
break;
}
case SDL_WINDOWEVENT_FOCUS_LOST:
{
if(_events_hooks[id][4].hook)
_events_hooks[id][4].hook(4, _events_hooks[id][4].param);
if(win_hook.hook)
win_hook.hook(4, win_hook.param);
break;
}

View File

@@ -1,59 +0,0 @@
run
a
q
run
bt
q
run
a
q
run
bt
a
y
q
run
p (i / 400) * 100
q
run
bt
q
run
bt
q
b create_image
run
tui n
n
n
n
n
n
n
n
n
s
n
n
s
s
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
s
s
q

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2023/08/02 12:36:11 by maldavid ### ########.fr */
/* Updated: 2023/08/28 10:52:33 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */