From ea2014819d498ee7fd1a68837aef874d0ab74181 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Tue, 12 Dec 2023 13:08:00 +0100 Subject: [PATCH] fxing macos libraries location issue --- Makefile | 5 ++-- src/renderer/font.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++ src/renderer/font.h | 37 ++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 src/renderer/font.cpp create mode 100644 src/renderer/font.h diff --git a/Makefile b/Makefile index 7c51147..01200a6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: maldavid +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/10/04 16:43:41 by maldavid #+# #+# # -# Updated: 2023/12/10 23:15:03 by kbz_8 ### ########.fr # +# Updated: 2023/12/12 13:00:48 by kbz_8 ### ########.fr # # # # **************************************************************************** # @@ -44,7 +44,8 @@ else endif ifeq ($(OS), Darwin) - LDLIBS += -lSDL2 + LDLIBS += -L /opt/homebrew/lib -lSDL2 + CXXFLAGS += -I /opt/homebrew/include endif ifeq ($(DEBUG), true) diff --git a/src/renderer/font.cpp b/src/renderer/font.cpp new file mode 100644 index 0000000..4bab8c2 --- /dev/null +++ b/src/renderer/font.cpp @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* font.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 22:06:09 by kbz_8 #+# #+# */ +/* Updated: 2023/12/11 22:31:11 by kbz_8 ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +constexpr const int RANGE = 1024; + +namespace mlx +{ + Font::Font(Renderer& renderer, const std::filesystem::path& path, float scale) : non_copyable(), _name(path.string()) + { + std::vector tmp_bitmap(RANGE * RANGE); + std::vector vulkan_bitmap(RANGE * RANGE * 4); + + std::ifstream file(path, std::ios::binary); + if(!file.is_open()) + { + core::error::report(e_kind::error, "Font load : cannot open font file, %s", _name.c_str()); + return; + } + std::ifstream::pos_type fileSize = std::filesystem::file_size(path); + file.seekg(0, std::ios::beg); + std::vector bytes(fileSize); + file.read(reinterpret_cast(bytes.data()), fileSize); + file.close(); + + stbtt_pack_context pc; + 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) + { + vulkan_bitmap[j + 0] = tmp_bitmap[i]; + vulkan_bitmap[j + 1] = tmp_bitmap[i]; + vulkan_bitmap[j + 2] = tmp_bitmap[i]; + vulkan_bitmap[j + 3] = tmp_bitmap[i]; + } + _atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true); + _atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate()); + } + + Font::~Font() + { + + } +} diff --git a/src/renderer/font.h b/src/renderer/font.h new file mode 100644 index 0000000..92c3b84 --- /dev/null +++ b/src/renderer/font.h @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* font.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/11 21:17:04 by kbz_8 #+# #+# */ +/* Updated: 2023/12/11 22:31:22 by kbz_8 ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_FONT__ +#define __MLX_FONT__ + +#include +#include +#include +#include + +namespace mlx +{ + class Font : public non_copyable + { + public: + Font(class Renderer& renderer, const std::filesystem::path& path, float scale); + inline const std::string& getName() const { return _name; } + ~Font(); + + private: + std::array _cdata; + TextureAtlas _atlas; + std::string _name; + }; +} + +#endif