git rm --cached
This commit is contained in:
150
MacroLibX/src/platform/inputs.cpp
Normal file
150
MacroLibX/src/platform/inputs.cpp
Normal file
@ -0,0 +1,150 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* inputs.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/11 19:01:14 by kbz_8 ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "inputs.h"
|
||||
#include <mlx.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
void Input::update()
|
||||
{
|
||||
_xRel = 0;
|
||||
_yRel = 0;
|
||||
|
||||
while(SDL_PollEvent(&_event))
|
||||
{
|
||||
if(_event.type == SDL_MOUSEMOTION)
|
||||
{
|
||||
_x = _event.motion.x;
|
||||
_y = _event.motion.y;
|
||||
|
||||
_xRel = _event.motion.xrel;
|
||||
_yRel = _event.motion.yrel;
|
||||
}
|
||||
|
||||
uint32_t id = _event.window.windowID;
|
||||
if(!_events_hooks.count(id))
|
||||
continue;
|
||||
auto& hooks = _events_hooks[id];
|
||||
|
||||
switch(_event.type)
|
||||
{
|
||||
case SDL_KEYDOWN:
|
||||
{
|
||||
if(hooks[MLX_KEYDOWN].hook)
|
||||
hooks[MLX_KEYDOWN].hook(_event.key.keysym.scancode, hooks[MLX_KEYDOWN].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_KEYUP:
|
||||
{
|
||||
if(hooks[MLX_KEYUP].hook)
|
||||
hooks[MLX_KEYUP].hook(_event.key.keysym.scancode, hooks[MLX_KEYUP].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
{
|
||||
if(hooks[MLX_MOUSEDOWN].hook)
|
||||
hooks[MLX_MOUSEDOWN].hook(_event.button.button, hooks[MLX_MOUSEDOWN].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
{
|
||||
if(hooks[MLX_MOUSEUP].hook)
|
||||
hooks[MLX_MOUSEUP].hook(_event.button.button, hooks[MLX_MOUSEUP].param);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEWHEEL:
|
||||
{
|
||||
if(hooks[MLX_MOUSEWHEEL].hook)
|
||||
{
|
||||
if(_event.wheel.y > 0) // scroll up
|
||||
hooks[MLX_MOUSEWHEEL].hook(1, hooks[MLX_MOUSEWHEEL].param);
|
||||
else if(_event.wheel.y < 0) // scroll down
|
||||
hooks[MLX_MOUSEWHEEL].hook(2, hooks[MLX_MOUSEWHEEL].param);
|
||||
|
||||
if(_event.wheel.x > 0) // scroll right
|
||||
hooks[MLX_MOUSEWHEEL].hook(3, hooks[MLX_MOUSEWHEEL].param);
|
||||
else if(_event.wheel.x < 0) // scroll left
|
||||
hooks[MLX_MOUSEWHEEL].hook(4, hooks[MLX_MOUSEWHEEL].param);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
{
|
||||
auto& win_hook = hooks[MLX_WINDOW_EVENT];
|
||||
switch(_event.window.event)
|
||||
{
|
||||
case SDL_WINDOWEVENT_CLOSE:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(0, win_hook.param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_MOVED:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(1, win_hook.param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_MINIMIZED:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(2, win_hook.param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_MAXIMIZED:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(3, win_hook.param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_ENTER:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(4, win_hook.param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(4, win_hook.param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_LEAVE:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(5, win_hook.param);
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||
{
|
||||
if(win_hook.hook)
|
||||
win_hook.hook(4, win_hook.param);
|
||||
break;
|
||||
}
|
||||
|
||||
default : break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
77
MacroLibX/src/platform/inputs.h
Normal file
77
MacroLibX/src/platform/inputs.h
Normal file
@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* inputs.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/11 19:47:20 by kbz_8 ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <mlx_profile.h>
|
||||
|
||||
#include "window.h"
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
struct Hook
|
||||
{
|
||||
std::function<int(int, void*)> hook;
|
||||
void* param = nullptr;
|
||||
};
|
||||
|
||||
class Input
|
||||
{
|
||||
public:
|
||||
Input() = default;
|
||||
|
||||
void update();
|
||||
|
||||
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 constexpr void finish() noexcept { _end = true; }
|
||||
|
||||
inline void addWindow(std::shared_ptr<MLX_Window> window)
|
||||
{
|
||||
_windows[window->getID()] = window;
|
||||
_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;
|
||||
|
||||
private:
|
||||
std::unordered_map<uint32_t, std::shared_ptr<MLX_Window>> _windows;
|
||||
std::unordered_map<uint32_t, std::array<Hook, 6>> _events_hooks;
|
||||
SDL_Event _event;
|
||||
|
||||
int _x = 0;
|
||||
int _y = 0;
|
||||
int _xRel = 0;
|
||||
int _yRel = 0;
|
||||
|
||||
bool _end = false;
|
||||
};
|
||||
}
|
57
MacroLibX/src/platform/window.cpp
Normal file
57
MacroLibX/src/platform/window.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* window.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/27 16:57:28 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <platform/window.h>
|
||||
#include <core/errors.h>
|
||||
#include <utils/icon_mlx.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
||||
constexpr const uint32_t rmask = 0xff000000;
|
||||
constexpr const uint32_t gmask = 0x00ff0000;
|
||||
constexpr const uint32_t bmask = 0x0000ff00;
|
||||
constexpr const uint32_t amask = 0x000000ff;
|
||||
#else
|
||||
constexpr const uint32_t rmask = 0x000000ff;
|
||||
constexpr const uint32_t gmask = 0x0000ff00;
|
||||
constexpr const uint32_t bmask = 0x00ff0000;
|
||||
constexpr const uint32_t amask = 0xff000000;
|
||||
#endif
|
||||
|
||||
MLX_Window::MLX_Window(std::size_t w, std::size_t h, const std::string& title) : _width(w), _height(h)
|
||||
{
|
||||
if(title.find("vvaas") != std::string::npos)
|
||||
core::error::report(e_kind::message, "vvaas est mauvais");
|
||||
_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());
|
||||
_id = SDL_GetWindowID(_win);
|
||||
_icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask);
|
||||
SDL_SetWindowIcon(_win, _icon);
|
||||
}
|
||||
|
||||
void MLX_Window::destroy() noexcept
|
||||
{
|
||||
if(_win != nullptr)
|
||||
{
|
||||
SDL_DestroyWindow(_win);
|
||||
_win = nullptr;
|
||||
}
|
||||
if(_icon != nullptr)
|
||||
{
|
||||
SDL_FreeSurface(_icon);
|
||||
_icon = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
45
MacroLibX/src/platform/window.h
Normal file
45
MacroLibX/src/platform/window.h
Normal file
@ -0,0 +1,45 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* window.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */
|
||||
/* Updated: 2023/12/21 00:24:26 by maldavid ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef __MLX_WINDOW__
|
||||
#define __MLX_WINDOW__
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <string>
|
||||
#include <mlx_profile.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
class MLX_Window
|
||||
{
|
||||
public:
|
||||
MLX_Window(std::size_t w, std::size_t h, const std::string& title);
|
||||
|
||||
inline SDL_Window* getNativeWindow() const noexcept { return _win; }
|
||||
inline int getWidth() const noexcept { return _width; }
|
||||
inline int getHeight() const noexcept { return _height; }
|
||||
inline uint32_t getID() const noexcept { return _id; }
|
||||
|
||||
void destroy() noexcept;
|
||||
|
||||
~MLX_Window() = default;
|
||||
|
||||
private:
|
||||
SDL_Surface* _icon = nullptr;
|
||||
SDL_Window* _win = nullptr;
|
||||
int _width = 0;
|
||||
int _height = 0;
|
||||
uint32_t _id = -1;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user