Merge pull request #7 from 420verfl0w/malloc-custom

improving custom malloc, removing unused code in input system
This commit is contained in:
kbz_8
2023-12-11 19:35:38 +01:00
committed by GitHub
7 changed files with 41 additions and 33 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2023/12/09 17:44:13 by kbz_8 ### ########.fr */ /* Updated: 2023/12/11 15:12:39 by kbz_8 ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,11 +16,13 @@
#include <array> #include <array>
#include <core/errors.h> #include <core/errors.h>
#include <core/profile.h> #include <core/profile.h>
#include <core/memory.h>
namespace mlx::core namespace mlx::core
{ {
Application::Application() : _in(std::make_unique<Input>()) Application::Application() : _in(std::make_unique<Input>())
{ {
SDL_SetMemoryFunctions(MemManager::malloc, MemManager::calloc, MemManager::realloc, MemManager::free);
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0) 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()); error::report(e_kind::fatal_error, "SDL error : unable to init all subsystems : %s", SDL_GetError());
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/12/07 23:05:05 by kbz_8 ### ########.fr */ /* Updated: 2023/12/11 15:56:18 by kbz_8 ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,6 +16,7 @@
#include <renderer/core/render_core.h> #include <renderer/core/render_core.h>
#include <filesystem> #include <filesystem>
#include <mlx.h> #include <mlx.h>
#include <core/memory.h>
extern "C" extern "C"
{ {
@@ -25,8 +26,9 @@ extern "C"
if(init) if(init)
{ {
mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times"); mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times");
return NULL; return NULL; // not nullptr for the C compatibility
} }
mlx::MemManager::get(); // just to initialize the C garbage collector
mlx::core::Application* app = new mlx::core::Application; mlx::core::Application* app = new mlx::core::Application;
mlx::Render_Core::get().init(); mlx::Render_Core::get().init();
if(app == nullptr) if(app == nullptr)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */ /* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */
/* Updated: 2023/12/08 12:56:14 by kbz_8 ### ########.fr */ /* Updated: 2023/12/11 15:25:02 by kbz_8 ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -17,7 +17,7 @@
namespace mlx namespace mlx
{ {
void* MemManager::alloc(std::size_t size) void* MemManager::malloc(std::size_t size)
{ {
void* ptr = std::malloc(size); void* ptr = std::malloc(size);
if(ptr != nullptr) if(ptr != nullptr)
@@ -25,6 +25,25 @@ namespace mlx
return ptr; return ptr;
} }
void* MemManager::calloc(std::size_t n, std::size_t size)
{
void* ptr = std::calloc(n, size);
if(ptr != nullptr)
_blocks.push_back(ptr);
return ptr;
}
void* MemManager::realloc(void* ptr, std::size_t size)
{
void* ptr2 = std::realloc(ptr, size);
if(ptr2 != nullptr)
_blocks.push_back(ptr2);
auto it = std::find(_blocks.begin(), _blocks.end(), ptr);
if(it != _blocks.end())
_blocks.erase(it);
return ptr2;
}
void MemManager::free(void* ptr) void MemManager::free(void* ptr)
{ {
auto it = std::find(_blocks.begin(), _blocks.end(), ptr); auto it = std::find(_blocks.begin(), _blocks.end(), ptr);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 16:31:51 by kbz_8 #+# #+# */ /* Created: 2023/12/07 16:31:51 by kbz_8 #+# #+# */
/* Updated: 2023/12/08 19:05:15 by kbz_8 ### ########.fr */ /* Updated: 2023/12/11 15:13:46 by kbz_8 ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -24,15 +24,17 @@ namespace mlx
friend class Singleton<MemManager>; friend class Singleton<MemManager>;
public: public:
void* alloc(std::size_t size); static void* malloc(std::size_t size);
void free(void* ptr); static void* calloc(std::size_t n, std::size_t size);
static void* realloc(void* ptr, std::size_t size);
static void free(void* ptr);
private: private:
MemManager() = default; MemManager() = default;
~MemManager(); ~MemManager();
private: private:
std::list<void*> _blocks; inline static std::list<void*> _blocks;
}; };
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2023/12/08 12:17:40 by kbz_8 ### ########.fr */ /* Updated: 2023/12/11 19:01:14 by kbz_8 ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,12 +16,6 @@
namespace mlx namespace mlx
{ {
Input::Input()
{
std::memset(_keys.data(), 0, SDL_NUM_SCANCODES);
std::memset(_mouse.data(), 0, 8);
}
void Input::update() void Input::update()
{ {
_xRel = 0; _xRel = 0;
@@ -47,7 +41,6 @@ namespace mlx
{ {
case SDL_KEYDOWN: case SDL_KEYDOWN:
{ {
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::down);
if(hooks[MLX_KEYDOWN].hook) if(hooks[MLX_KEYDOWN].hook)
hooks[MLX_KEYDOWN].hook(_event.key.keysym.scancode, hooks[MLX_KEYDOWN].param); hooks[MLX_KEYDOWN].hook(_event.key.keysym.scancode, hooks[MLX_KEYDOWN].param);
break; break;
@@ -55,7 +48,6 @@ namespace mlx
case SDL_KEYUP: case SDL_KEYUP:
{ {
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::up);
if(hooks[MLX_KEYUP].hook) if(hooks[MLX_KEYUP].hook)
hooks[MLX_KEYUP].hook(_event.key.keysym.scancode, hooks[MLX_KEYUP].param); hooks[MLX_KEYUP].hook(_event.key.keysym.scancode, hooks[MLX_KEYUP].param);
break; break;
@@ -63,7 +55,6 @@ namespace mlx
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
{ {
_mouse[_event.button.button] = static_cast<uint8_t>(action::down);
if(hooks[MLX_MOUSEDOWN].hook) if(hooks[MLX_MOUSEDOWN].hook)
hooks[MLX_MOUSEDOWN].hook(_event.button.button, hooks[MLX_MOUSEDOWN].param); hooks[MLX_MOUSEDOWN].hook(_event.button.button, hooks[MLX_MOUSEDOWN].param);
break; break;
@@ -71,7 +62,6 @@ namespace mlx
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
{ {
_mouse[_event.button.button] = static_cast<uint8_t>(action::up);
if(hooks[MLX_MOUSEUP].hook) if(hooks[MLX_MOUSEUP].hook)
hooks[MLX_MOUSEUP].hook(_event.button.button, hooks[MLX_MOUSEUP].param); hooks[MLX_MOUSEUP].hook(_event.button.button, hooks[MLX_MOUSEUP].param);
break; break;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2023/12/08 18:54:03 by kbz_8 ### ########.fr */ /* Updated: 2023/12/11 19:06:13 by kbz_8 ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -24,8 +24,6 @@
namespace mlx namespace mlx
{ {
enum class action : uint8_t { up = (1 << 1), down = (1 << 2) };
struct Hook struct Hook
{ {
std::function<int(int, void*)> hook; std::function<int(int, void*)> hook;
@@ -35,13 +33,10 @@ namespace mlx
class Input class Input
{ {
public: public:
Input(); Input() = default;
void update(); 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 bool isMouseMoving() const noexcept { return _xRel || _yRel; }
inline int getX() const noexcept { return _x; } inline int getX() const noexcept { return _x; }
@@ -68,11 +63,9 @@ namespace mlx
~Input() = default; ~Input() = default;
private: private:
std::array<uint8_t, SDL_NUM_SCANCODES> _keys;
std::unordered_map<uint32_t, std::shared_ptr<MLX_Window>> _windows; std::unordered_map<uint32_t, std::shared_ptr<MLX_Window>> _windows;
std::unordered_map<uint32_t, std::array<Hook, 6>> _events_hooks; std::unordered_map<uint32_t, std::array<Hook, 6>> _events_hooks;
SDL_Event _event; SDL_Event _event;
std::array<uint8_t, 8> _mouse;
int _x = 0; int _x = 0;
int _y = 0; int _y = 0;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ /* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2023/12/10 22:34:35 by kbz_8 ### ########.fr */ /* Updated: 2023/12/11 15:12:02 by kbz_8 ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -23,8 +23,8 @@
#include <core/memory.h> #include <core/memory.h>
#define STB_TRUETYPE_IMPLEMENTATION #define STB_TRUETYPE_IMPLEMENTATION
#define STB_malloc(x, u) ((void)(u), MemManager::get().alloc(x)) #define STB_malloc(x, u) ((void)(u), MemManager::malloc(x))
#define STB_free(x, u) ((void)(u), MemManager::get().free(x)) #define STB_free(x, u) ((void)(u), MemManager::free(x))
#include <stb_truetype.h> #include <stb_truetype.h>
constexpr const int RANGE = 1024; constexpr const int RANGE = 1024;