Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect fontGlyph struct in imgui.net #301

Open
pixtur opened this issue Feb 15, 2022 · 2 comments
Open

Incorrect fontGlyph struct in imgui.net #301

pixtur opened this issue Feb 15, 2022 · 2 comments

Comments

@pixtur
Copy link

pixtur commented Feb 15, 2022

I posted the following issue on the dear imgui repo: ocornut/imgui#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
Copy link

exitearthinc commented Mar 3, 2022

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
Copy link
Collaborator

I believe it would be fixed by this: #245

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants