diff --git a/Makefile b/Makefile index 295a146..f5c7986 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maldavid +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# # -# Updated: 2023/11/24 10:03:17 by maldavid ### ########.fr # +# Updated: 2023/12/07 15:25:52 by kbz_8 ### ########.fr # # # # **************************************************************************** # @@ -17,7 +17,7 @@ SRCS += $(wildcard $(addsuffix /*.cpp, ./src/platform)) SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer)) SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer/**)) -OBJ_DIR = objs +OBJ_DIR = objs/makefile OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o)) OS = $(shell uname -s) diff --git a/includes/mlx.h b/includes/mlx.h index 7b6aa0d..7c10776 100644 --- a/includes/mlx.h +++ b/includes/mlx.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ -/* Updated: 2023/11/23 14:32:06 by maldavid ### ########.fr */ +/* Updated: 2023/12/08 12:14:31 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,8 @@ typedef enum MLX_KEYUP = 1, MLX_MOUSEDOWN = 2, MLX_MOUSEUP = 3, - MLX_WINDOW_EVENT = 4 + MLX_MOUSEWHEEL = 4, + MLX_WINDOW_EVENT = 5 } mlx_event_type; /** diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 9b1fb36..9240ddc 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ -/* Updated: 2023/11/25 10:12:36 by maldavid ### ########.fr */ +/* Updated: 2023/12/07 23:05:05 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,8 @@ extern "C" mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times"); return NULL; } - mlx::Render_Core::get().init(); mlx::core::Application* app = new mlx::core::Application; + mlx::Render_Core::get().init(); if(app == nullptr) mlx::core::error::report(e_kind::fatal_error, "Tout a pété"); init = true; diff --git a/src/core/memory.cpp b/src/core/memory.cpp new file mode 100644 index 0000000..3fd1c5b --- /dev/null +++ b/src/core/memory.cpp @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* memory.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */ +/* Updated: 2023/12/08 12:56:14 by kbz_8 ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +namespace mlx +{ + void* MemManager::alloc(std::size_t size) + { + void* ptr = std::malloc(size); + if(ptr != nullptr) + _blocks.push_back(ptr); + return ptr; + } + + void MemManager::free(void* ptr) + { + auto it = std::find(_blocks.begin(), _blocks.end(), ptr); + if(it == _blocks.end()) + { + core::error::report(e_kind::error, "Memory Manager : trying to free a pointer not allocated by the memory manager"); + return; + } + std::free(*it); + _blocks.erase(it); + } + + MemManager::~MemManager() + { + std::for_each(_blocks.begin(), _blocks.end(), [](void* ptr) + { + std::free(ptr); + }); + } +} diff --git a/src/core/memory.h b/src/core/memory.h new file mode 100644 index 0000000..5e8c3eb --- /dev/null +++ b/src/core/memory.h @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* memory.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/07 16:31:51 by kbz_8 #+# #+# */ +/* Updated: 2023/12/08 12:56:21 by kbz_8 ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_MEMORY__ +#define __MLX_MEMORY__ + +#include +#include + +namespace mlx +{ + class MemManager : public Singleton + { + friend class Singleton; + + public: + void* alloc(std::size_t size); + void free(void* ptr); + + private: + MemManager() = default; + ~MemManager(); + + private: + std::list _blocks; + }; +} + +#endif diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index 02b5f26..7dce064 100644 --- a/src/platform/inputs.cpp +++ b/src/platform/inputs.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ -/* Updated: 2023/08/28 10:49:03 by maldavid ### ########.fr */ +/* Updated: 2023/12/08 12:17:40 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,6 +77,23 @@ namespace mlx 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]; diff --git a/src/platform/inputs.h b/src/platform/inputs.h index 60c98d1..030171c 100644 --- a/src/platform/inputs.h +++ b/src/platform/inputs.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ -/* Updated: 2023/04/19 12:14:43 by maldavid ### ########.fr */ +/* Updated: 2023/12/08 12:14:39 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,7 +68,7 @@ namespace mlx private: std::array _keys; std::unordered_map> _windows; - std::unordered_map> _events_hooks; + std::unordered_map> _events_hooks; SDL_Event _event; std::array _mouse; diff --git a/src/renderer/descriptors/vk_descriptor_set.cpp b/src/renderer/descriptors/vk_descriptor_set.cpp index bde9e16..28bc816 100644 --- a/src/renderer/descriptors/vk_descriptor_set.cpp +++ b/src/renderer/descriptors/vk_descriptor_set.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */ -/* Updated: 2023/11/12 01:14:52 by maldavid ### ########.fr */ +/* Updated: 2023/12/07 20:00:13 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/descriptors/vk_descriptor_set.h b/src/renderer/descriptors/vk_descriptor_set.h index 2a10be0..0d39d72 100644 --- a/src/renderer/descriptors/vk_descriptor_set.h +++ b/src/renderer/descriptors/vk_descriptor_set.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */ -/* Updated: 2023/03/31 17:28:36 by maldavid ### ########.fr */ +/* Updated: 2023/12/07 19:47:07 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/renderer/images/texture_atlas.h b/src/renderer/images/texture_atlas.h index db142c2..74c7c90 100644 --- a/src/renderer/images/texture_atlas.h +++ b/src/renderer/images/texture_atlas.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */ -/* Updated: 2023/11/14 05:36:30 by maldavid ### ########.fr */ +/* Updated: 2023/12/07 18:50:53 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ namespace mlx void render(class Renderer& renderer, int x, int y, uint32_t ibo_size); void destroy() noexcept override; - inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); } + inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; } inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; } inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); } diff --git a/src/renderer/text_pipeline.cpp b/src/renderer/text_pipeline.cpp index 7f6866b..48b8f08 100644 --- a/src/renderer/text_pipeline.cpp +++ b/src/renderer/text_pipeline.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ -/* Updated: 2023/11/25 10:40:39 by maldavid ### ########.fr */ +/* Updated: 2023/12/07 22:29:45 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,11 @@ #define STB_RECT_PACK_IMPLEMENTATION #include +#include + #define STB_TRUETYPE_IMPLEMENTATION +#define STB_malloc(x, u) ((void)(u), MemManager::get().alloc(x)) +#define STB_free(x, u) ((void)(u), MemManager::get().free(x)) #include constexpr const int RANGE = 1024; @@ -74,10 +78,10 @@ namespace mlx void TextPutPipeline::init(Renderer* renderer) noexcept { _renderer = renderer; - uint8_t tmp_bitmap[RANGE * RANGE]; - uint8_t vulkan_bitmap[RANGE * RANGE * 4]; + std::vector tmp_bitmap(RANGE * RANGE); + std::vector vulkan_bitmap(RANGE * RANGE * 4); stbtt_pack_context pc; - stbtt_PackBegin(&pc, tmp_bitmap, RANGE, RANGE, RANGE, 1, nullptr); + stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr); stbtt_PackFontRange(&pc, dogica_ttf, 0, 6.0, 32, 96, _cdata.data()); stbtt_PackEnd(&pc); for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4) @@ -87,14 +91,14 @@ namespace mlx vulkan_bitmap[j + 2] = tmp_bitmap[i]; vulkan_bitmap[j + 3] = tmp_bitmap[i]; } - _atlas.create(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); + _atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); _atlas.setDescriptor(renderer->getFragDescriptorSet().duplicate()); } void TextPutPipeline::loadFont(const std::filesystem::path& filepath, float scale) { - uint8_t tmp_bitmap[RANGE * RANGE]; - uint8_t vulkan_bitmap[RANGE * RANGE * 4]; + std::vector tmp_bitmap(RANGE * RANGE); + std::vector vulkan_bitmap(RANGE * RANGE * 4); std::ifstream file(filepath, std::ios::binary); if(!file.is_open()) @@ -109,7 +113,7 @@ namespace mlx file.close(); stbtt_pack_context pc; - stbtt_PackBegin(&pc, tmp_bitmap, RANGE, RANGE, RANGE, 1, nullptr); + stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr); stbtt_PackFontRange(&pc, bytes.data(), 0, scale, 32, 96, _cdata.data()); stbtt_PackEnd(&pc); for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4) @@ -120,7 +124,7 @@ namespace mlx vulkan_bitmap[j + 3] = tmp_bitmap[i]; } destroy(); - _atlas.create(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); + _atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); _atlas.setDescriptor(_renderer->getFragDescriptorSet().duplicate()); } diff --git a/test/main.c b/test/main.c index 4326b30..e1e107b 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/11/25 11:57:57 by maldavid ### ########.fr */ +/* Updated: 2023/12/08 12:23:07 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */