Skip to content

Commit 38768c8

Browse files
committed
improving font management
1 parent c7433ec commit 38768c8

File tree

7 files changed

+665
-22
lines changed

7 files changed

+665
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
*.so
66
*.out
77
.cache/
8+
test/.gdb_history

Makefile

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
9-
# Updated: 2023/11/13 06:08:16 by maldavid ### ########.fr #
9+
# Updated: 2023/11/24 10:03:17 by maldavid ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

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

20-
OBJS = $(SRCS:.cpp=.o)
20+
OBJ_DIR = objs
21+
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
2122

2223
OS = $(shell uname -s)
2324
DEBUG ?= false
@@ -26,7 +27,9 @@ IMAGES_OPTIMIZED ?= true
2627
FORCE_INTEGRATED_GPU ?= false
2728
GRAPHICS_MEMORY_DUMP ?= false
2829

29-
CXX = clang++
30+
MODE = "release"
31+
32+
CXX = clang++
3033

3134
ifeq ($(TOOLCHAIN), gcc)
3235
CXX = g++
@@ -43,6 +46,7 @@ endif
4346

4447
ifeq ($(DEBUG), true)
4548
CXXFLAGS += -g -D DEBUG
49+
MODE = "debug"
4650
endif
4751

4852
ifeq ($(FORCE_INTEGRATED_GPU), true)
@@ -57,24 +61,30 @@ ifeq ($(GRAPHICS_MEMORY_DUMP), true)
5761
CXXFLAGS += -D GRAPHICS_MEMORY_DUMP
5862
endif
5963

60-
RM = rm -f
64+
RM = rm -rf
6165

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

6670
all: $(NAME)
6771

68-
$(NAME): $(OBJS)
69-
@echo "\e[1;32m[linking ...]\e[1;00m "$@
72+
$(NAME): $(OBJ_DIR) $(OBJS)
73+
@printf "\033[1;32m[linking ... "$(MODE)"]\033[1;00m "$@"\n"
7074
@$(CXX) -shared -o $(NAME) $(OBJS) $(LDLIBS)
71-
@echo "\e[1;32m[build finished]\e[1;00m"
75+
@printf "\033[1;32m[build finished]\033[1;00m\n"
76+
77+
$(OBJ_DIR):
78+
@mkdir -p $(sort $(addprefix $(OBJ_DIR)/, $(dir $(SRCS))))
79+
@printf "\033[1;32m[created objs directory]\033[1;00m\n"
7280

7381
clean:
74-
@$(RM) $(OBJS)
82+
@$(RM) $(OBJ_DIR)
83+
@printf "\033[1;32m[object files removed]\033[1;00m\n"
7584

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

7989
re: fclean all
8090

src/core/graphics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
9-
/* Updated: 2023/11/20 07:18:35 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/24 19:19:25 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -74,8 +74,8 @@ namespace mlx
7474
GraphicsSupport::~GraphicsSupport()
7575
{
7676
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
77-
_pixel_put_pipeline.destroy();
7877
_text_put_pipeline->destroy();
78+
_pixel_put_pipeline.destroy();
7979
_renderer->destroy();
8080
}
8181
}

src/renderer/text_pipeline.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
9-
/* Updated: 2023/11/23 14:26:48 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/24 19:03:50 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -17,6 +17,9 @@
1717
#include <iostream>
1818
#include <cstdio>
1919

20+
#define STB_RECT_PACK_IMPLEMENTATION
21+
#include <stb_rect_pack.h>
22+
2023
#define STB_TRUETYPE_IMPLEMENTATION
2124
#include <stb_truetype.h>
2225

@@ -29,7 +32,7 @@ namespace mlx
2932
text(std::move(_text))
3033
{}
3134

32-
void TextDrawData::init(TextLibrary& library, std::array<stbtt_bakedchar, 96>& cdata) noexcept
35+
void TextDrawData::init(TextLibrary& library, std::array<stbtt_packedchar, 96>& cdata) noexcept
3336
{
3437
std::vector<Vertex> vertexData;
3538
std::vector<uint16_t> indexData;
@@ -43,7 +46,7 @@ namespace mlx
4346
continue;
4447

4548
stbtt_aligned_quad q;
46-
stbtt_GetBakedQuad(cdata.data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
49+
stbtt_GetPackedQuad(cdata.data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
4750

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

@@ -73,7 +76,10 @@ namespace mlx
7376
_renderer = renderer;
7477
uint8_t tmp_bitmap[RANGE * RANGE];
7578
uint8_t vulkan_bitmap[RANGE * RANGE * 4];
76-
stbtt_BakeFontBitmap(dogica_ttf, 0, 6.0f, tmp_bitmap, RANGE, RANGE, 32, 96, _cdata.data());
79+
stbtt_pack_context pc;
80+
stbtt_PackBegin(&pc, tmp_bitmap, RANGE, RANGE, RANGE, 1, nullptr);
81+
stbtt_PackFontRange(&pc, dogica_ttf, 0, 6.0, 32, 96, _cdata.data());
82+
stbtt_PackEnd(&pc);
7783
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
7884
{
7985
vulkan_bitmap[j + 0] = tmp_bitmap[i];
@@ -102,7 +108,10 @@ namespace mlx
102108
file.read(reinterpret_cast<char*>(bytes.data()), fileSize);
103109
file.close();
104110

105-
stbtt_BakeFontBitmap(bytes.data(), 0, scale, tmp_bitmap, RANGE, RANGE, 32, 96, _cdata.data());
111+
stbtt_pack_context pc;
112+
stbtt_PackBegin(&pc, tmp_bitmap, RANGE, RANGE, RANGE, 1, nullptr);
113+
stbtt_PackFontRange(&pc, bytes.data(), 0, scale, 32, 96, _cdata.data());
114+
stbtt_PackEnd(&pc);
106115
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
107116
{
108117
vulkan_bitmap[j + 0] = tmp_bitmap[i];

src/renderer/text_pipeline.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
9-
/* Updated: 2023/11/23 14:26:34 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/24 19:08:04 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -32,7 +32,7 @@ namespace mlx
3232
std::string text;
3333

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

6767
~TextPutPipeline() = default;
6868

6969
private:
70-
std::array<stbtt_bakedchar, 96> _cdata;
70+
std::array<stbtt_packedchar, 96> _cdata;
7171
TextureAtlas _atlas;
7272
TextLibrary _library;
7373
std::unordered_set<TextDrawData> _drawlist;

test/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
9-
/* Updated: 2023/11/23 14:32:15 by maldavid ### ########.fr */
9+
/* Updated: 2023/11/24 19:08:57 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

0 commit comments

Comments
 (0)