adding imputs system

This commit is contained in:
2022-10-05 17:08:36 +02:00
commit 3ee3a5899b
15 changed files with 825 additions and 0 deletions

24
src/core/application.cpp git.filemode.normal_file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* application.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:58:11 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include "application.h"
namespace mlx::core
{
void Application::run() noexcept
{
while(_in.is_running())
{
_in.update();
}
}
}

46
src/core/application.h git.filemode.normal_file
View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* application.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:58:25 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_APPLICATION__
#define __MLX_APPLICATION__
#include <vector>
#include <memory>
#include <utility>
#include <platform/inputs.h>
#include <platform/window.h>
namespace mlx::core
{
class Application
{
public:
Application() = default;
inline void new_window(std::size_t w, std::size_t h, std::string title)
{
_wins.emplace_back(std::make_unique<Window>(w, h, std::move(title)));
}
void run() noexcept;
~Application() = default;
private:
Input _in;
std::vector<std::unique_ptr<Window>> _wins;
bool _is_loop_running = false;
};
}
#endif // __MLX_APPLICATION__

42
src/core/bridge.cpp git.filemode.normal_file
View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bridge.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2022/10/05 14:02:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <SDL2/SDL.h>
#include "errors.h"
#include "application.h"
extern "C"
{
static std::unique_ptr<mlx::core::Application> __main_app = nullptr;
void mlx_init()
{
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
mlx::core::error::report(e_kind::fatal_error, "Unable to init the SDL2");
__main_app.reset(new mlx::core::Application());
}
void mlx_new_window(int w, int h, const char* title)
{
if(__main_app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can create new windows");
__main_app->new_window(w, h, title);
}
int mlx_loop()
{
if(__main_app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "You must initialize the mlx before you can run the main loop");
__main_app->run();
}
}

33
src/core/errors.cpp git.filemode.normal_file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:48:06 by maldavid #+# #+# */
/* Updated: 2022/10/04 21:18:35 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <cstdlib>
#include "errors.h"
namespace mlx::core::error
{
void report(e_kind kind, std::string msg)
{
switch(kind)
{
case e_kind::message: std::cout << "[MicroLibX] Message : " << msg << std::endl; break;
case e_kind::warning: std::cout << "[MicroLibX] Warning : " << msg << std::endl; break;
case e_kind::error: std::cerr << "[MicroLibX] Error : " << msg << std::endl; break;
case e_kind::fatal_error:
std::cerr << "[MicroLibX] Fatal Error : " << msg << std::endl;
std::exit(EXIT_SUCCESS);
break;
}
}
}

31
src/core/errors.h git.filemode.normal_file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:42:32 by maldavid #+# #+# */
/* Updated: 2022/10/04 21:51:42 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_ERRORS__
#define __MLX_ERRORS__
#include <string>
enum class e_kind
{
message,
warning,
error,
fatal_error
};
namespace mlx::core::error
{
void report(e_kind kind, std::string msg);
}
#endif // __MLX_ERRORS__

54
src/platform/inputs.cpp git.filemode.normal_file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inputs.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:39:42 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include "inputs.h"
#include <cstring>
namespace mlx
{
Input::Input()
{
std::memset(_keys.data(), 0, SDL_NUM_SCANCODES);
std::memset(_mouse.data(), 0, 8);
}
void Input::update()
{
_xRel = 0;
_yRel = 0;
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); break;
case SDL_KEYUP: _keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::up); break;
case SDL_MOUSEBUTTONDOWN: _mouse[_event.button.button] = static_cast<uint8_t>(action::down); break;
case SDL_MOUSEBUTTONUP: _mouse[_event.button.button] = static_cast<uint8_t>(action::up); break;
default: break;
}
if(_event.type == SDL_MOUSEMOTION)
{
_x = _event.motion.x;
_y = _event.motion.y;
_xRel = _event.motion.xrel;
_yRel = _event.motion.yrel;
}
}
}
}

56
src/platform/inputs.h git.filemode.normal_file
View File

@@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inputs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2022/10/05 16:45:55 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <SDL2/SDL.h>
#include <cstdint>
#include <array>
namespace mlx
{
enum class action : uint8_t { up = (1 << 1), down = (1 << 2) };
class Input
{
public:
Input();
void update();
inline bool getInKey(const SDL_Scancode key, action type = action::down) const noexcept { return _keys[key] & static_cast<uint8_t>(type); }
inline bool getInMouse(const uint8_t button, action type = action::down) const noexcept { return _mouse[button] & static_cast<uint8_t>(type); }
inline bool isMouseMoving() const noexcept { return _xRel || _yRel; }
inline int getX() const noexcept { return _x; }
inline int getY() const noexcept { return _y; }
inline int getXRel() const noexcept { return _xRel; }
inline int getYRel() const noexcept { return _yRel; }
inline bool is_running() const noexcept { return !_end; }
inline void finish() noexcept { _end = true; }
~Input() = default;
private:
std::array<uint8_t, SDL_NUM_SCANCODES> _keys;
std::array<uint8_t, 8> _mouse;
SDL_Event _event;
int _x = 0;
int _y = 0;
int _xRel = 0;
int _yRel = 0;
bool _end = false;
};
}

30
src/platform/window.cpp git.filemode.normal_file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* window.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */
/* Updated: 2022/10/04 22:09:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include "window.h"
#include <core/errors.h>
namespace mlx
{
Window::Window(std::size_t w, std::size_t h, std::string title)
{
_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());
}
Window::~Window()
{
if(_win)
SDL_DestroyWindow(_win);
}
}

33
src/platform/window.h git.filemode.normal_file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* window.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */
/* Updated: 2022/10/04 21:59:54 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_WINDOW__
#define __MLX_WINDOW__
#include <SDL2/SDL.h>
#include <string>
namespace mlx
{
class Window
{
public:
Window(std::size_t w, std::size_t h, std::string title);
~Window();
private:
SDL_Window* _win = nullptr;
};
}
#endif