Skip to content

Commit

Permalink
remove context structure. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
wutipong authored Jan 26, 2024
1 parent 3fac6c6 commit 7e33611
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 101 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(font-render-tester)

add_executable(font-render-tester
"src/colors.hpp"
"src/context.hpp"
"src/debug_settings.hpp"
"src/draw_glyph.cpp"
"src/draw_glyph.hpp"
"src/font.cpp"
Expand All @@ -20,6 +20,7 @@ add_executable(font-render-tester
"src/texture.cpp"
"src/texture.hpp"
)

target_include_directories(font-render-tester PRIVATE
"ext/imgui-filebrowser"
"ext/IconFontCppHeaders"
Expand Down
16 changes: 0 additions & 16 deletions src/context.hpp

This file was deleted.

12 changes: 12 additions & 0 deletions src/debug_settings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef DEBUG_SETTINGS_HPP
#define DEBUG_SETTINGS_HPP

struct DebugSettings {
bool enabled{false};
bool debugGlyphBound{true};
bool debugBaseline{true};
bool debugCaret{true};
bool debugAscend{true};
bool debugDescend{true};
};
#endif
10 changes: 5 additions & 5 deletions src/draw_glyph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* direction is to match the modern rendering apis such as OpenGL or DirectX.
*/

void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
void DrawGlyph(SDL_Renderer *renderer, DebugSettings &debug, const Font &font,
const Glyph &g, const SDL_Color &color, const int &x,
const int &y) {

Expand Down Expand Up @@ -40,15 +40,15 @@ void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,

SDL_RenderCopyF(renderer, g.texture, nullptr, &rect);

if (ctx.debug && ctx.debugGlyphBound) {
if (debug.enabled && debug.debugGlyphBound) {
SDL_SetRenderDrawColor(renderer, debugGlyphBoundColor.r,
debugGlyphBoundColor.g, debugGlyphBoundColor.b,
debugGlyphBoundColor.a);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_RenderDrawRectF(renderer, &rect);
}

if (ctx.debug && ctx.debugCaret) {
if (debug.enabled && debug.debugCaret) {
SDL_FRect rect{
static_cast<float>(x),
static_cast<float>(y),
Expand All @@ -66,12 +66,12 @@ void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
}
}

void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
void DrawGlyph(SDL_Renderer *renderer, DebugSettings &debug, const Font &font,
const Glyph &g, const SDL_Color &color, const int &x,
const int &y, const hb_glyph_position_t &hb_glyph_pos) {

auto xPos = x + HBPosToFloat(hb_glyph_pos.x_offset);
auto yPos = y + HBPosToFloat(hb_glyph_pos.y_offset);

DrawGlyph(renderer, ctx, font, g, color, xPos, yPos);
DrawGlyph(renderer, debug, font, g, color, xPos, yPos);
}
4 changes: 2 additions & 2 deletions src/draw_glyph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#include "font.hpp"

void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
void DrawGlyph(SDL_Renderer *renderer, DebugSettings &debug, const Font &font,
const Glyph &g, const SDL_Color &color, const int &x,
const int &y);

void DrawGlyph(SDL_Renderer *renderer, Context &ctx, const Font &font,
void DrawGlyph(SDL_Renderer *renderer, DebugSettings &debug, const Font &font,
const Glyph &g, const SDL_Color &color, const int &x,
const int &y, const hb_glyph_position_t &hb_glyph_pos);

Expand Down
2 changes: 1 addition & 1 deletion src/font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <ft2build.h>
#include FT_FREETYPE_H

#include "context.hpp"
#include "debug_settings.hpp"
#include <functional>
#include <hb-ot.h>
#include <iterator>
Expand Down
14 changes: 4 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "context.hpp"
#include "io_util.hpp"
#include "main_scene.hpp"
#include <IconsForkAwesome.h>
Expand All @@ -10,7 +9,6 @@
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/spdlog.h>


static constexpr char imguiIni[] = "imgui.ini";
static constexpr char logfile[] = "log.txt";

Expand All @@ -29,8 +27,6 @@ int main(int argc, char **argv) {
logger->flush_on(spdlog::level::info);
spdlog::set_default_logger(logger);

Context ctx{};

SDL_Init(SDL_INIT_VIDEO);

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
Expand Down Expand Up @@ -78,7 +74,7 @@ int main(int argc, char **argv) {
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer2_Init(renderer);

if (!SceneInit(ctx)) {
if (!SceneInit()) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error",
"Unable to initialize the new scene", window);

Expand All @@ -92,27 +88,25 @@ int main(int argc, char **argv) {
if (event.type == SDL_QUIT)
break;
}
ctx.windowBound = {0};
SDL_GetWindowSize(window, &ctx.windowBound.w, &ctx.windowBound.h);

ImGui_ImplSDL2_NewFrame(window);
ImGui_ImplSDLRenderer2_NewFrame();

ImGui::NewFrame();

SceneDoUI(ctx);
SceneDoUI();

ImGui::EndFrame();
ImGui::Render();

SceneTick(renderer, ctx);
SceneTick(renderer);

ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer);
SDL_Delay(1);
}

SceneCleanUp(ctx);
SceneCleanUp();

ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
Expand Down
56 changes: 31 additions & 25 deletions src/main_scene.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "main_scene.hpp"

#include "colors.hpp"
#include "debug_settings.hpp"
#include "font.hpp"
#include "io_util.hpp"
#include "settings.hpp"
Expand Down Expand Up @@ -109,6 +110,9 @@ const std::vector<LanguagePair> languages{
#endif
};

DebugSettings debug{};

bool isShowingTextEditor = true;
int selectedScript = 0;
int selectedLanguage = 0;

Expand Down Expand Up @@ -144,7 +148,7 @@ ListFontFiles(const std::filesystem::path &path) {
return output;
}

void OnDirectorySelected(Context &ctx, const std::filesystem::path &path) {
void OnDirectorySelected(const std::filesystem::path &path) {
std::filesystem::path newPath = path;

if (!std::filesystem::exists(newPath)) {
Expand All @@ -155,7 +159,7 @@ void OnDirectorySelected(Context &ctx, const std::filesystem::path &path) {
}

void RenderText(SDL_Renderer *renderer, bool isShaping, const char *language,
hb_script_t script, TextDirection direction, Context &ctx) {
hb_script_t script, TextDirection direction, DebugSettings &debug) {
if (!font.IsValid())
return;

Expand All @@ -164,46 +168,48 @@ void RenderText(SDL_Renderer *renderer, bool isShaping, const char *language,
SDL_Color sdlColor = foregroundColor;

if (!isShaping) {
TextRenderNoShape(renderer, ctx, font, str, sdlColor);
TextRenderNoShape(renderer, debug, font, str, sdlColor);
return;
}

switch (direction) {
case TextDirection::LeftToRight:
TextRenderLeftToRight(renderer, ctx, font, str, sdlColor, language, script);
TextRenderLeftToRight(renderer, debug, font, str, sdlColor, language, script);
return;

case TextDirection::TopToBottom:
TextRenderTopToBottom(renderer, ctx, font, str, sdlColor, language, script);
TextRenderTopToBottom(renderer, debug, font, str, sdlColor, language, script);
return;

#ifdef ENABLE_RTL
case TextDirection::RightToLeft:
TextRenderRightToLeft(renderer, ctx, font, str, sdlColor, language, script);
TextRenderRightToLeft(renderer, debug, font, str, sdlColor, language, script);
return;
#endif
}
};
} // namespace

bool SceneInit(Context &context) {
bool SceneInit() {
if (!Font::Init())
return false;

auto [fontPath] = LoadSettings();
fontDirPath = fontPath.string();

dirChooser.SetTitle("Browse for font directory");
OnDirectorySelected(context, fontDirPath);
OnDirectorySelected(fontDirPath);
dirChooser.SetPwd(fontDirPath);

std::copy(std::cbegin(exampleText), std::cend(exampleText), buffer.begin());

return true;
}

void SceneTick(SDL_Renderer *renderer, Context &ctx) {
SDL_Rect viewport = ctx.windowBound;
void SceneTick(SDL_Renderer *renderer) {
auto window = SDL_RenderGetWindow(renderer);
SDL_Rect viewport{0};
SDL_GetWindowSize(window, &viewport.w, &viewport.h);

viewport.x += padding;
viewport.y += padding;
Expand All @@ -222,17 +228,17 @@ void SceneTick(SDL_Renderer *renderer, Context &ctx) {
auto script = scripts[selectedScript].script;

RenderText(renderer, isShaping, language.data(), script, selectedDirection,
ctx);
debug);

SDL_RenderGetViewport(renderer, nullptr);
}

void SceneCleanUp(Context &context) {
void SceneCleanUp() {
Font::CleanUp();
SaveSettings({.fontPath = fontDirPath});
}

void SceneDoUI(Context &context) {
void SceneDoUI() {
int newSelected = selectedFontIndex;
bool showAbout = false;
if (ImGui::BeginMainMenuBar()) {
Expand Down Expand Up @@ -262,8 +268,8 @@ void SceneDoUI(Context &context) {
}

if (ImGui::BeginMenu("View##menu")) {
ImGui::MenuItem("Text editor##view-menu", "", &context.showTextEditor);
ImGui::MenuItem("Debug##view-menu", "", &context.debug);
ImGui::MenuItem("Text editor##view-menu", "", &isShowingTextEditor);
ImGui::MenuItem("Debug##view-menu", "", &debug.enabled);

ImGui::EndMenu();
}
Expand Down Expand Up @@ -433,45 +439,45 @@ void SceneDoUI(Context &context) {
}
ImGui::End();

if (context.showTextEditor) {
if (ImGui::Begin("Input text", &context.showTextEditor)) {
if (isShowingTextEditor) {
if (ImGui::Begin("Input text", &isShowingTextEditor)) {
ImGui::InputTextMultiline("##InputText", buffer.data(), buffer.size());
}
ImGui::End();
}

if (context.debug) {
if (ImGui::Begin("Debug", &context.debug)) {
if (debug.enabled) {
if (ImGui::Begin("Debug", &debug.enabled)) {
if (ImGui::CollapsingHeader("Features", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::Checkbox("Baseline", &context.debugBaseline);
ImGui::Checkbox("Baseline", &debug.debugBaseline);
ImGui::SameLine();
ImGui::ColorButton(
"Baseline", SDLColorToImVec4(debugBaselineColor),
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel);

ImGui::Checkbox("Caret Positions", &context.debugCaret);
ImGui::Checkbox("Caret Positions", &debug.debugCaret);
ImGui::SameLine();
ImGui::ColorButton(
"Caret Positions", SDLColorToImVec4(debugCaretColor),
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel);

ImGui::Checkbox("Glyph Bound", &context.debugGlyphBound);
ImGui::Checkbox("Glyph Bound", &debug.debugGlyphBound);
ImGui::SameLine();
ImGui::ColorButton(
"Glyph Bound", SDLColorToImVec4(debugGlyphBoundColor),
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel);

ImGui::Checkbox("Ascend", &context.debugAscend);
ImGui::Checkbox("Ascend", &debug.debugAscend);
ImGui::SameLine();
ImGui::ColorButton(
"Ascend", SDLColorToImVec4(debugAscendColor),
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel);

ImGui::Checkbox("Descend", &context.debugDescend);
ImGui::Checkbox("Descend", &debug.debugDescend);
ImGui::SameLine();
ImGui::ColorButton(
"Descend", SDLColorToImVec4(debugDescendColor),
Expand All @@ -484,7 +490,7 @@ void SceneDoUI(Context &context) {

dirChooser.Display();
if (dirChooser.HasSelected()) {
OnDirectorySelected(context, dirChooser.GetSelected());
OnDirectorySelected(dirChooser.GetSelected());
dirChooser.ClearSelected();
newSelected = -1;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main_scene.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef MAIN_SCENE_HPP
#define MAIN_SCENE_HPP

#include "context.hpp"
#include "debug_settings.hpp"
#include <SDL2/SDL.h>

bool SceneInit(Context &context);
void SceneTick(SDL_Renderer *renderer, Context &ctx);
void SceneCleanUp(Context &context);
void SceneDoUI(Context &context);
bool SceneInit();
void SceneTick(SDL_Renderer *renderer);
void SceneCleanUp();
void SceneDoUI();

#endif
Loading

0 comments on commit 7e33611

Please sign in to comment.