ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.85k stars 300 forks source link

Incorrect fontGlyph struct in imgui.net #301

Open pixtur opened 2 years ago

pixtur commented 2 years ago

I posted the following issue on the dear imgui repo: https://github.com/ocornut/imgui/issues/5020

As ocornut points out...

The structure looks correct in cimgui.h: https://github.com/cimgui/cimgui/blob/master/cimgui.h#L1117

struct ImFontGlyph
{
    unsigned int Colored : 1;
    unsigned int Visible : 1;
    unsigned int Codepoint : 30;
    float AdvanceX;
    float X0, Y0, X1, Y1;
    float U0, V0, U1, V1;
};

But is wrong in imgui.net: https://github.com/mellinoe/ImGui.NET/blob/master/src/ImGui.NET/Generated/ImFontGlyph.gen.cs

        public uint Colored;
        public uint Visible;
        public uint Codepoint;

This is a problem with however imgui.net is generated.

Need work on imgui.net side: https://stackoverflow.com/questions/14464/bit-fields-in-c-sharp

I'll let you move this to imgui.net or push a PR/fix for it.

I'm not really sure to fix this in imgui.net so I'm not sure how to create a pull request.

exitearthinc commented 2 years ago

Hi ... I just ran into this issue as well. To properly fix this issue we'll have to modify the code generator. But right now I don't have time to look into that so here's a very quick hack.

Just replace ImFontGlyph.gen.cs with this... it replaces the 3 packed variables with a single uint masked & shifted.


using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;

namespace ImGuiNET
{
    public unsafe partial struct ImFontGlyph
    {
        /* Generated C#
        public uint Colored;
        public uint Visible;
        public uint Codepoint;
        */

        /* C Structure
        unsigned int Colored : 1;
        unsigned int Visible : 1;
        unsigned int Codepoint : 30;
        */

        //Begin Fix
        public uint Packed;
        //End Fix

        public float AdvanceX;
        public float X0;
        public float Y0;
        public float X1;
        public float Y1;
        public float U0;
        public float V0;
        public float U1;
        public float V1;
    }
    public unsafe partial struct ImFontGlyphPtr
    {
        public ImFontGlyph* NativePtr { get; }
        public ImFontGlyphPtr(ImFontGlyph* nativePtr) => NativePtr = nativePtr;
        public ImFontGlyphPtr(IntPtr nativePtr) => NativePtr = (ImFontGlyph*)nativePtr;
        public static implicit operator ImFontGlyphPtr(ImFontGlyph* nativePtr) => new ImFontGlyphPtr(nativePtr);
        public static implicit operator ImFontGlyph* (ImFontGlyphPtr wrappedPtr) => wrappedPtr.NativePtr;
        public static implicit operator ImFontGlyphPtr(IntPtr nativePtr) => new ImFontGlyphPtr(nativePtr);

        /* Generated C#
        public ref uint Colored => ref Unsafe.AsRef<uint>(&NativePtr->Colored);
        public ref uint Visible => ref Unsafe.AsRef<uint>(&NativePtr->Visible);
        public ref uint Codepoint => ref Unsafe.AsRef<uint>(&NativePtr->Codepoint);
        */

        //Begin Fix
        public bool Colored => (NativePtr->Packed & 1) == 1;
        public bool Visible => ((NativePtr->Packed >> 1) & 1) == 1;
        public uint Codepoint => NativePtr->Packed >> 2;
        //End Fix

        public ref float AdvanceX => ref Unsafe.AsRef<float>(&NativePtr->AdvanceX);
        public ref float X0 => ref Unsafe.AsRef<float>(&NativePtr->X0);
        public ref float Y0 => ref Unsafe.AsRef<float>(&NativePtr->Y0);
        public ref float X1 => ref Unsafe.AsRef<float>(&NativePtr->X1);
        public ref float Y1 => ref Unsafe.AsRef<float>(&NativePtr->Y1);
        public ref float U0 => ref Unsafe.AsRef<float>(&NativePtr->U0);
        public ref float V0 => ref Unsafe.AsRef<float>(&NativePtr->V0);
        public ref float U1 => ref Unsafe.AsRef<float>(&NativePtr->U1);
        public ref float V1 => ref Unsafe.AsRef<float>(&NativePtr->V1);
    }
}
mellinoe commented 2 years ago

I believe it would be fixed by this: https://github.com/mellinoe/ImGui.NET/pull/245