Skip to content

Commit

Permalink
C++: FreeType demo
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Nov 28, 2022
1 parent 42b1bc6 commit 8a48fa6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
34 changes: 34 additions & 0 deletions doc/cpp-demo/MyGame/Button1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Godot;
using ImGuiGodot;
using ImGuiNET;
using System;

public partial class Button1 : Button
{
private IntPtr _freeTypeBuilder = IntPtr.Zero;

public override void _Ready()
{
Pressed += OnPressed;
}

private void OnPressed()
{
ImGuiGD.ResetFonts();
ImGuiGD.AddFont(GD.Load<FontFile>("res://Hack-Regular.ttf"), 18);
ImGuiGD.RebuildFontAtlas();

// toggle builder with each click
unsafe
{
ImFontAtlas* atlas = ImGui.GetIO().NativePtr->Fonts;
if (_freeTypeBuilder == IntPtr.Zero)
_freeTypeBuilder = (IntPtr)atlas->FontBuilderIO;

if (atlas->FontBuilderIO == null)
atlas->FontBuilderIO = (IntPtr*)_freeTypeBuilder;
else
atlas->FontBuilderIO = null;
}
}
}
11 changes: 10 additions & 1 deletion doc/cpp-demo/MyGame/main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[gd_scene format=3 uid="uid://d02ytucir7qh2"]
[gd_scene load_steps=2 format=3 uid="uid://d02ytucir7qh2"]

[ext_resource type="Script" path="res://Button1.cs" id="1_sb8ai"]

[node name="Control" type="Control"]
layout_mode = 3
Expand All @@ -9,3 +11,10 @@ grow_horizontal = 2
grow_vertical = 2

[node name="MyCppNode" type="MyCppNode" parent="."]

[node name="Button1" type="Button" parent="."]
layout_mode = 0
offset_right = 132.0
offset_bottom = 59.0
text = "reload font"
script = ExtResource("1_sb8ai")
5 changes: 5 additions & 0 deletions doc/cpp-demo/src/MyCppNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <godot_cpp/classes/node.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include <imgui.h>
#include <imgui_freetype.h>
#include <implot.h>
using namespace godot;

Expand Down Expand Up @@ -62,6 +63,10 @@ void MyCppNode::_ready()

ImGui::SetCurrentContext((ImGuiContext*)ctx);
ImGui::SetAllocatorFunctions((ImGuiMemAllocFunc)mem_alloc, (ImGuiMemFreeFunc)mem_free);

// will be used in the next RebuildFontAtlas
ImGui::GetIO().Fonts->FontBuilderIO = ImGuiFreeType::GetBuilderForFreeType();

ImPlot::CreateContext();
set_process(true);
}
Expand Down

0 comments on commit 8a48fa6

Please sign in to comment.