Use different fonts

This commit is contained in:
blankie 2023-01-06 23:27:39 +07:00
parent 48ac4a560d
commit 680a1c93af
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
6 changed files with 47 additions and 22 deletions

3
.gitignore vendored
View File

@ -1,4 +1,7 @@
*.o
*.obj
fonts.cpp
logmeow
logmeow.exe
binary_to_compressed_c
binary_to_compressed_c.exe

View File

@ -16,7 +16,7 @@ CXX = clang++
EXE = logmeow
IMGUI_DIR = imgui
SOURCES = main.cpp log.cpp config.cpp event_loop.cpp
SOURCES = main.cpp fonts.cpp log.cpp config.cpp event_loop.cpp
SOURCES += $(IMGUI_DIR)/imgui.cpp $(IMGUI_DIR)/imgui_demo.cpp $(IMGUI_DIR)/imgui_draw.cpp $(IMGUI_DIR)/imgui_tables.cpp $(IMGUI_DIR)/imgui_widgets.cpp
SOURCES += $(IMGUI_DIR)/misc/cpp/imgui_stdlib.cpp $(IMGUI_DIR)/backends/imgui_impl_sdl.cpp $(IMGUI_DIR)/backends/imgui_impl_opengl3.cpp
OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
@ -44,7 +44,6 @@ LIBS =
##---------------------------------------------------------------------
ifeq ($(UNAME_S), Linux) #LINUX
ECHO_MESSAGE = "Linux"
LIBS += $(LINUX_GL_LIBS) -ldl `sdl2-config --libs`
CXXFLAGS += `sdl2-config --cflags`
@ -52,7 +51,6 @@ ifeq ($(UNAME_S), Linux) #LINUX
endif
ifeq ($(UNAME_S), Darwin) #APPLE
ECHO_MESSAGE = "Mac OS X"
LIBS += -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo `sdl2-config --libs`
LIBS += -L/usr/local/lib -L/opt/local/lib
@ -62,7 +60,6 @@ ifeq ($(UNAME_S), Darwin) #APPLE
endif
ifeq ($(OS), Windows_NT)
ECHO_MESSAGE = "MinGW"
LIBS += -lgdi32 -lopengl32 -limm32 `pkg-config --static --libs sdl2`
CXXFLAGS += `pkg-config --cflags sdl2`
@ -73,20 +70,34 @@ endif
## BUILD RULES
##---------------------------------------------------------------------
%.o:%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
%.o:$(IMGUI_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
%.o:$(IMGUI_DIR)/misc/cpp/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
%.o:$(IMGUI_DIR)/backends/%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
$(EXE): $(OBJS)
$(CXX) -o $@ $^ $(CXXFLAGS) -fuse-ld=lld $(LIBS)
%.o:%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $<
fonts.cpp: binary_to_compressed_c fonts.h imgui/misc/fonts/DroidSans.ttf imgui/misc/fonts/Cousine-Regular.ttf
echo -e '#include "fonts.h"\n\n' > fonts.cpp
./binary_to_compressed_c -base85 imgui/misc/fonts/DroidSans.ttf droid_sans >> fonts.cpp
./binary_to_compressed_c -base85 imgui/misc/fonts/Cousine-Regular.ttf cousine >> fonts.cpp
echo -e 'const char* get_droid_sans_compressed_data_base85() { return droid_sans_compressed_data_base85; }\n' >> fonts.cpp
echo -e 'const char* get_cousine_compressed_data_base85() { return cousine_compressed_data_base85; }\n' >> fonts.cpp
fonts.o: fonts.cpp
$(CXX) $(CXXFLAGS) -Wno-overlength-strings -Wno-unused-const-variable -c -o $@ $<
# dear imgui has some fucky wucky with -Wconversion, hence -Wno-conversion
binary_to_compressed_c: imgui/misc/fonts/binary_to_compressed_c.cpp
$(CXX) $(CXXFLAGS) -Wno-conversion -o $@ $<
%.o:$(IMGUI_DIR)/%.cpp
$(CXX) $(CXXFLAGS) -Wno-conversion -c -o $@ $<
%.o:$(IMGUI_DIR)/misc/cpp/%.cpp
$(CXX) $(CXXFLAGS) -Wno-conversion -c -o $@ $<
%.o:$(IMGUI_DIR)/backends/%.cpp
$(CXX) $(CXXFLAGS) -Wno-conversion -c -o $@ $<
clean:
rm -f $(EXE) $(OBJS)
rm -f $(EXE) $(OBJS) fonts.cpp binary_to_compressed_c

View File

@ -27,7 +27,7 @@ static inline void settings_window(Config& config, float* config_write_timer, bo
ImGui::End();
}
static inline void logs_window(bool* autoscrolling, bool* show_logs_window) {
static inline void logs_window(ImFont* monospace_font, bool* autoscrolling, bool* show_logs_window) {
if (!ImGui::Begin("LogMeow Logs", show_logs_window)) {
ImGui::End();
return;
@ -44,7 +44,9 @@ static inline void logs_window(bool* autoscrolling, bool* show_logs_window) {
ImGui::Separator();
// copied from imgui/imgui_demo.cpp: [SECTION] Example App: Debug Console / ShowExampleAppConsole()
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) {
ImGui::PushFont(monospace_font);
ImGui::TextUnformatted(log_entries.data(), &log_entries[log_entries.size()]);
ImGui::PopFont();
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
*autoscrolling = true;
ImGui::SetScrollHereY(1.0f);
@ -119,7 +121,7 @@ static inline void debug_window() {
ImGui::End();
}
void event_loop(Config& config, float* config_write_timer, bool* run_event_loop) {
void event_loop(ImFont* monospace_font, Config& config, float* config_write_timer, bool* run_event_loop) {
static bool show_settings_window = false;
static bool show_logs_window = false;
static size_t log_entries_read = 0;
@ -138,7 +140,7 @@ void event_loop(Config& config, float* config_write_timer, bool* run_event_loop)
if (show_logs_window) {
bool autoscrolling = false;
logs_window(&autoscrolling, &show_logs_window);
logs_window(monospace_font, &autoscrolling, &show_logs_window);
if (autoscrolling) {
log_entries_read = log_entries.size();
}

View File

@ -1,5 +1,7 @@
#pragma once
#include <imgui.h>
#include "config.h"
void event_loop(Config& config, float* config_write_timer, bool* run_event_loop);
void event_loop(ImFont* monospace_font, Config& config, float* config_write_timer, bool* run_event_loop);

4
fonts.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
const char* get_droid_sans_compressed_data_base85();
const char* get_cousine_compressed_data_base85();

View File

@ -12,6 +12,7 @@
#include <SDL_opengl.h>
#endif
#include "fonts.h"
#include "config.h"
#include "event_loop.h"
@ -109,6 +110,8 @@ int main(int, char**) {
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);
io.Fonts->AddFontFromMemoryCompressedBase85TTF(get_droid_sans_compressed_data_base85(), 16.0f);
ImFont* monospace_font = io.Fonts->AddFontFromMemoryCompressedBase85TTF(get_cousine_compressed_data_base85(), 15.0f);
// Main loop
bool run_event_loop = true;
@ -135,7 +138,7 @@ int main(int, char**) {
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
event_loop(config, &config_write_timer, &run_event_loop);
event_loop(monospace_font, config, &config_write_timer, &run_event_loop);
// Rendering
ImGui::Render();