Skip to content

Commit

Permalink
adding SDL initialisation and quit, improving inputs code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Aug 28, 2023
1 parent 3b0442d commit a7ac7b2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 89 deletions.
14 changes: 13 additions & 1 deletion src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

#include "application.h"
#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())
Expand Down Expand Up @@ -52,4 +59,9 @@ namespace mlx::core
Texture* texture = static_cast<Texture*>(ptr);
texture->destroy();
}

Application::~Application()
{
SDL_Quit();
}
}
6 changes: 3 additions & 3 deletions src/core/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace mlx::core

void run() noexcept;

~Application() = default;
~Application();

private:
std::list<Texture> _textures;
Expand Down
53 changes: 28 additions & 25 deletions src/platform/inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
Expand Down Expand Up @@ -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;
}

Expand Down
59 changes: 0 additions & 59 deletions test/.gdb_history

This file was deleted.

2 changes: 1 addition & 1 deletion test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down

0 comments on commit a7ac7b2

Please sign in to comment.