-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding SDL initialisation and quit, improving inputs code readability
- Loading branch information
Showing
5 changed files
with
45 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
|
@@ -52,4 +59,9 @@ namespace mlx::core | |
Texture* texture = static_cast<Texture*>(ptr); | ||
texture->destroy(); | ||
} | ||
|
||
Application::~Application() | ||
{ | ||
SDL_Quit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|