Skip to content

Commit

Permalink
improving font management
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Nov 24, 2023
1 parent c7433ec commit 38768c8
Show file tree
Hide file tree
Showing 7 changed files with 665 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.so
*.out
.cache/
test/.gdb_history
30 changes: 20 additions & 10 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/13 06:08:16 by maldavid ### ########.fr #
# Updated: 2023/11/24 10:03:17 by maldavid ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -17,7 +17,8 @@ SRCS += $(wildcard $(addsuffix /*.cpp, ./src/platform))
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer))
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer/**))

OBJS = $(SRCS:.cpp=.o)
OBJ_DIR = objs
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))

OS = $(shell uname -s)
DEBUG ?= false
Expand All @@ -26,7 +27,9 @@ IMAGES_OPTIMIZED ?= true
FORCE_INTEGRATED_GPU ?= false
GRAPHICS_MEMORY_DUMP ?= false

CXX = clang++
MODE = "release"

CXX = clang++

ifeq ($(TOOLCHAIN), gcc)
CXX = g++
Expand All @@ -43,6 +46,7 @@ endif

ifeq ($(DEBUG), true)
CXXFLAGS += -g -D DEBUG
MODE = "debug"
endif

ifeq ($(FORCE_INTEGRATED_GPU), true)
Expand All @@ -57,24 +61,30 @@ ifeq ($(GRAPHICS_MEMORY_DUMP), true)
CXXFLAGS += -D GRAPHICS_MEMORY_DUMP
endif

RM = rm -f
RM = rm -rf

%.o: %.cpp
@echo "\e[1;32m[compiling... "$(CXX)"]\e[1;00m "$<
$(OBJ_DIR)/%.o: %.cpp
@printf "\033[1;32m[compiling... "$(MODE)" "$(CXX)"]\033[1;00m "$<"\n"
@$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

all: $(NAME)

$(NAME): $(OBJS)
@echo "\e[1;32m[linking ...]\e[1;00m "$@
$(NAME): $(OBJ_DIR) $(OBJS)
@printf "\033[1;32m[linking ... "$(MODE)"]\033[1;00m "$@"\n"
@$(CXX) -shared -o $(NAME) $(OBJS) $(LDLIBS)
@echo "\e[1;32m[build finished]\e[1;00m"
@printf "\033[1;32m[build finished]\033[1;00m\n"

$(OBJ_DIR):
@mkdir -p $(sort $(addprefix $(OBJ_DIR)/, $(dir $(SRCS))))
@printf "\033[1;32m[created objs directory]\033[1;00m\n"

clean:
@$(RM) $(OBJS)
@$(RM) $(OBJ_DIR)
@printf "\033[1;32m[object files removed]\033[1;00m\n"

fclean: clean
@$(RM) $(NAME)
@printf "\033[1;32m["$(NAME)" removed]\033[1;00m\n"

re: fclean all

Expand Down
4 changes: 2 additions & 2 deletions src/core/graphics.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/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2023/11/20 07:18:35 by maldavid ### ########.fr */
/* Updated: 2023/11/24 19:19:25 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -74,8 +74,8 @@ namespace mlx
GraphicsSupport::~GraphicsSupport()
{
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
_pixel_put_pipeline.destroy();
_text_put_pipeline->destroy();
_pixel_put_pipeline.destroy();
_renderer->destroy();
}
}
19 changes: 14 additions & 5 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/23 14:26:48 by maldavid ### ########.fr */
/* Updated: 2023/11/24 19:03:50 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,6 +17,9 @@
#include <iostream>
#include <cstdio>

#define STB_RECT_PACK_IMPLEMENTATION
#include <stb_rect_pack.h>

#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>

Expand All @@ -29,7 +32,7 @@ namespace mlx
text(std::move(_text))
{}

void TextDrawData::init(TextLibrary& library, std::array<stbtt_bakedchar, 96>& cdata) noexcept
void TextDrawData::init(TextLibrary& library, std::array<stbtt_packedchar, 96>& cdata) noexcept
{
std::vector<Vertex> vertexData;
std::vector<uint16_t> indexData;
Expand All @@ -43,7 +46,7 @@ namespace mlx
continue;

stbtt_aligned_quad q;
stbtt_GetBakedQuad(cdata.data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
stbtt_GetPackedQuad(cdata.data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);

std::size_t index = vertexData.size();

Expand Down Expand Up @@ -73,7 +76,10 @@ namespace mlx
_renderer = renderer;
uint8_t tmp_bitmap[RANGE * RANGE];
uint8_t vulkan_bitmap[RANGE * RANGE * 4];
stbtt_BakeFontBitmap(dogica_ttf, 0, 6.0f, tmp_bitmap, RANGE, RANGE, 32, 96, _cdata.data());
stbtt_pack_context pc;
stbtt_PackBegin(&pc, tmp_bitmap, 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)
{
vulkan_bitmap[j + 0] = tmp_bitmap[i];
Expand Down Expand Up @@ -102,7 +108,10 @@ namespace mlx
file.read(reinterpret_cast<char*>(bytes.data()), fileSize);
file.close();

stbtt_BakeFontBitmap(bytes.data(), 0, scale, tmp_bitmap, RANGE, RANGE, 32, 96, _cdata.data());
stbtt_pack_context pc;
stbtt_PackBegin(&pc, tmp_bitmap, 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];
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/text_pipeline.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/06 16:24:11 by maldavid #+# #+# */
/* Updated: 2023/11/23 14:26:34 by maldavid ### ########.fr */
/* Updated: 2023/11/24 19:08:04 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,7 +32,7 @@ namespace mlx
std::string text;

TextDrawData(std::string text, int _color, int _x, int _y);
void init(TextLibrary& library, std::array<stbtt_bakedchar, 96>& cdata) noexcept;
void init(TextLibrary& library, std::array<stbtt_packedchar, 96>& cdata) noexcept;
bool operator==(const TextDrawData& rhs) const { return text == rhs.text && x == rhs.x && y == rhs.y && color == rhs.color; }
};
}
Expand All @@ -59,15 +59,15 @@ namespace mlx
void init(Renderer* renderer) noexcept;
void put(int x, int y, int color, std::string str);
inline VkDescriptorSet getDescriptorSet() noexcept { return _atlas.getSet(); }
inline void clear() { _drawlist.clear(); _library.clearLibrary(); }
inline void clear() { _drawlist.clear(); }
void loadFont(const std::filesystem::path& filepath, float scale);
void render();
void destroy() noexcept;

~TextPutPipeline() = default;

private:
std::array<stbtt_bakedchar, 96> _cdata;
std::array<stbtt_packedchar, 96> _cdata;
TextureAtlas _atlas;
TextLibrary _library;
std::unordered_set<TextDrawData> _drawlist;
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/23 14:32:15 by maldavid ### ########.fr */
/* Updated: 2023/11/24 19:08:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
Loading

0 comments on commit 38768c8

Please sign in to comment.