Skip to content

Commit

Permalink
Merge pull request #3 from 420verfl0w/indev
Browse files Browse the repository at this point in the history
Merge from dev to master
  • Loading branch information
Kbz-8 authored Dec 8, 2023
2 parents ecd43de + d5fa70d commit 197c710
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions includes/mlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions src/core/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* memory.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */
/* Updated: 2023/12/08 12:56:14 by kbz_8 ### ########.fr */
/* */
/* ************************************************************************** */

#include <core/memory.h>
#include <core/errors.h>
#include <algorithm>
#include <stdlib.h>

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);
});
}
}
38 changes: 38 additions & 0 deletions src/core/memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* memory.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <utils/singleton.h>
#include <list>

namespace mlx
{
class MemManager : public Singleton<MemManager>
{
friend class Singleton<MemManager>;

public:
void* alloc(std::size_t size);
void free(void* ptr);

private:
MemManager() = default;
~MemManager();

private:
std::list<void*> _blocks;
};
}

#endif
19 changes: 18 additions & 1 deletion src/platform/inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/platform/inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -68,7 +68,7 @@ namespace mlx
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::array<Hook, 5>> _events_hooks;
std::unordered_map<uint32_t, std::array<Hook, 6>> _events_hooks;
SDL_Event _event;
std::array<uint8_t, 8> _mouse;

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/descriptors/vk_descriptor_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/descriptors/vk_descriptor_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/images/texture_atlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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()); }

Expand Down
22 changes: 13 additions & 9 deletions src/renderer/text_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -20,7 +20,11 @@
#define STB_RECT_PACK_IMPLEMENTATION
#include <stb_rect_pack.h>

#include <core/memory.h>

#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 <stb_truetype.h>

constexpr const int RANGE = 1024;
Expand Down Expand Up @@ -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<uint8_t> tmp_bitmap(RANGE * RANGE);
std::vector<uint8_t> 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)
Expand All @@ -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<uint8_t> tmp_bitmap(RANGE * RANGE);
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);

std::ifstream file(filepath, std::ios::binary);
if(!file.is_open())
Expand All @@ -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)
Expand All @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down

0 comments on commit 197c710

Please sign in to comment.