haskell-game / dear-imgui.hs

Haskell bindings to Dear ImGui, an immediate mode GUI toolkit
BSD 3-Clause "New" or "Revised" License
143 stars 31 forks source link

On using withCString #47

Closed dpwiz closed 2 years ago

dpwiz commented 3 years ago

I was looking through C++ code and found that it assumes its text data is in UTF-8.

This may not be necessarily the case, since Foreign.withCString is locale-dependent and some users may have ASCII/C or some other national locales. This regularly trips up Windows users with symptoms like "invalid argument (invalid character)".

Besides, using withCString will waste CPU on the same [Char] -> CString conversion for every call on every frame. While the raw bindings are exempt from this churn, the library can provide smart constructors that will take in something like Text, encode to UTF8 ByteString once and then use its pointer for raw calls. Anyway, even with Text-to-BS encoding happening every frame at least it will be correct for any environment.

dpwiz commented 3 years ago

GHC.Foreign is encoding-aware :thinking:

https://hackage.haskell.org/package/base-4.15.0.0/docs/GHC-Foreign.html#v:withCString

dpwiz commented 3 years ago

Due to how text labels are used in the dearimgui code perhaps would be better to use a special Label data type.

There are 3 cases:

  1. Label, no id
  2. Label and id
  3. No label, only id

IDK if a completely empty label is allowed.

They can be set up at runtime, or compile time.

I think current String arguments should use such a Label instead, with IsString (IsLabel for type-3 labels?) instance and TH/QQ wrappers.