ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.87k stars 305 forks source link

Unable to change the font for rendering text. #454

Closed MetalDeveloper closed 8 months ago

MetalDeveloper commented 9 months ago

Hello,

Recently while making something I came across something were I wanted to change the font, in the process I have came up with an idea of how to do it but for some reason it will not change the font and stays the default font. I don't know if this is an issue on my end or the program itself. I am trying to render text onto the screen. Here is my code to do so.

void DrawOverlay() {

 var font = ImGui.GetIO().Fonts.AddFontFromFileTTF(@"C:\Windows\Fonts\Roboto-Regular.ttf", 999);
 ImGui.PushFont(font);
 ImGui.PopFont();

 Vector2 screenSize = new Vector2(ImGui.GetIO().DisplaySize.X, ImGui.GetIO().DisplaySize.Y);

 if (drawWatermark)

{ ImGui.SetNextWindowSize(screenSize); ImGui.SetNextWindowPos(new Vector2(0, 0)); ImGui.Begin("Overlay", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoBringToFrontOnFocus | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse );

     ImDrawListPtr drawList = ImGui.GetWindowDrawList();

     drawList.AddRectFilled(new Vector2(2, 5), new Vector2(120, 30), ImGui.ColorConvertFloat4ToU32(new Vector4(0.08f, 0.08f, 0.085f, 1f)), 10f);
     drawList.AddText(font, 23f, watermarkPos, ImGui.ColorConvertFloat4ToU32(new Vector4(1f, 1f, 1f, 1f)), "ExampleText");

 }

}

Please help me fix this if I am doing something wrong on my end. Also yes, the custom ttf file is in the correct directory.

zaafar commented 9 months ago

Font changing isn’t easy in this lib, sometime code is better than 1000 words so use this as reference: https://github.com/zaafar/ClickableTransparentOverlay/blob/master/Examples/SingleThreadedOverlayWithCoroutines/SampleOverlay.cs#L116

MetalDeveloper commented 9 months ago

I tried that but I get this error when I run it. image

Here is my code (My goal is to make a watermark that is displayed on the screen):

if (drawWatermark) { ImGui.SetNextWindowSize(screenSize); ImGui.SetNextWindowPos(new Vector2(0, 0)); ImGui.Begin("Overlay", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoBackground | ImGuiWindowFlags.NoBringToFrontOnFocus | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse );

   ImDrawListPtr drawList = ImGui.GetWindowDrawList();

   if ((DateTime.Now - _lastTime).TotalSeconds >= 2.25)
   {
       // one second has elapsed 

       _fps = _framesRendered;
       _framesRendered = 0;
       _lastTime = DateTime.Now;
   }

   this.ReplaceFont(@"C:\Windows\Fonts\msyh.ttc", 23, FontGlyphRangeType.English);
   font2 = null;

   //drawList.AddRectFilled(new Vector2(2, 5), new Vector2(120, 45), ImGui.ColorConvertFloat4ToU32(new Vector4(0.08f, 0.08f, 0.085f, 1f)), 6f);
   drawList.AddText(watermarkPos, ImGui.ColorConvertFloat4ToU32(new Vector4(1f, 1f, 1f, 1f)), "MetalWare");
   drawList.AddText(fpsPos, ImGui.ColorConvertFloat4ToU32(new Vector4(1f, 1f, 1f, 1f)), "FPS: " + _fps);

   if (font2 != null)
   {
       ImGui.PushFont(font2.Value);
   }
zaafar commented 9 months ago

Make sure you are using the latest version of imgui.net. We fixed this bug few months ago.

MetalDeveloper commented 9 months ago

I was using an outdated version, im testing it rn.

MetalDeveloper commented 9 months ago

image i get this error when i updated to the latest release (im using .net 6.0)

MetalDeveloper commented 9 months ago

Only image works for me without crashing

zaafar commented 9 months ago

You don’t need vsync feature of that library, just remove that if condition.

MetalDeveloper commented 9 months ago

image I removed that line and i get that (thats not my code thats the actual im gui .net code)

MetalDeveloper commented 9 months ago

image i also get this with it

MetalDeveloper commented 8 months ago

Do you have a discord? Can I add you on their and you can help me easily?

zaafar commented 8 months ago

How can I reproduce this issue on my side? For this purpose, I am wondering if u can give me the changes required to reproduce the issue in the sample projects (e.g. https://github.com/ImGuiNET/ImGui.NET/tree/master/src/ImGui.NET.SampleProgram OR https://github.com/zaafar/ClickableTransparentOverlay/tree/master/Examples)

MetalDeveloper commented 8 months ago

Can I just send you a simple project that does the same thing?

MetalDeveloper commented 8 months ago

Can I add you on discord so you can help me easier?

MetalDeveloper commented 8 months ago

my discord is: metal_developer

MetalDeveloper commented 8 months ago

💀 you helped me then just said good luck when it created a problem

MetalDeveloper commented 8 months ago

i fixed it

zaafar commented 8 months ago

I knew you would be able to fix it yourself, good stuff! Closing the issue.

hoootis commented 8 months ago

how did you fix it? i cannot for the life of me get fonts working