raylib-extras / rlImGui

A Raylib integration with DearImGui
zlib License
296 stars 66 forks source link

Platform DRM #65

Closed colesnicov closed 5 months ago

colesnicov commented 5 months ago

Hello. I encountered a nasty problem. When I run on a DRM platform, the console is flooded with the message:

WARNING: SetMouseCursor() not implemented on target platform
WARNING: SetMouseCursor() not implemented on target platform
WARNING: SetMouseCursor() not implemented on target platform
WARNING: SetMouseCursor() not implemented on target platform
WARNING: SetMouseCursor() not implemented on target platform
WARNING: SetMouseCursor() not implemented on target platform
WARNING: SetMouseCursor() not implemented on target platform

It happens because of this line https://github.com/raylib-extras/rlImGui/blob/bd69c9bec0448f48902e8aedae36879730ee1030/rlImGui.cpp#L104 where the screen ID is asked for every new frame.

I solved it for myself like this:

I removed the line https://github.com/raylib-extras/rlImGui/blob/bd69c9bec0448f48902e8aedae36879730ee1030/rlImGui.cpp#L104

Next, I declared a variable at the beginning of the file

static int monitor = 0;

and finally I added this line to the function https://github.com/raylib-extras/rlImGui/blob/bd69c9bec0448f48902e8aedae36879730ee1030/rlImGui.cpp#L270

monitor = GetCurrentMonitor();

After this modification, the notification is printed only when the program is started. I don't see how it will behave in the case of multiple monitors. I only have one monitor so I can't test it and that's why there is no RP either.

JeffM2501 commented 5 months ago

I'm not sure this is the right fix. if the DRM platform doesn't support mouse cursors, then rlImGui needs to set the correct backend flags on that platform and not try to set cursors or get the monitor. commit b89413d61b140a64e24f0a782fd6b26c2766c3e4 should handle this.

colesnicov commented 5 months ago

No, it is wan't work. The compilation parameter must still be passed -DPLATFORM_DRM.. and does fail..

Invoking: GCC C++ Compiler
g++ -std=c++17 -DPLATFORM_DRM=1 -I"/home/denis/Odey/OdeySDK/include" -I"/home/denis/Odey/OdeySDK/3rd/imgui" -I"/home/denis/Odey/OdeySDK/3rd/fs/include" -I"/home/denis/Odey/OdeySDK/3rd/concurrentqueue" -I"/home/denis/Odey/OdeySDK/3rd/jsonxx" -I"/home/denis/Odey/OdeySDK/3rd/minilog" -I"/home/denis/Odey/OdeySDK/3rd/imgui-notify/example/src" -I"/home/denis/Odey/OdeySDK/3rd/timercpp" -I/usr/include/freetype2 -I"/home/denis/Odey/OdeySDK/3rd/cpp-httplib/include" -I"/home/denis/Odey/OdeySDK/3rd/rlImGui" -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"3rd/jsonxx/jsonxx.d" -MT"3rd/jsonxx/jsonxx.o" -o "3rd/jsonxx/jsonxx.o" "../3rd/jsonxx/jsonxx.cc"
../3rd/rlImGui/rlImGui.cpp: In function ‘void ImGuiNewFrame(float)’:
../3rd/rlImGui/rlImGui.cpp:123:9: error: ‘resolutionScale’ was not declared in this scope
  123 |         resolutionScale = Vector2{ 1,1 };
      |         ^~~~~~~~~~~~~~~
../3rd/rlImGui/rlImGui.cpp:126:41: error: ‘resolutionScale’ was not declared in this scope
  126 |     io.DisplayFramebufferScale = ImVec2(resolutionScale.x, resolutionScale.y);
      |                                         ^~~~~~~~~~~~~~~
../3rd/rlImGui/rlImGui.cpp: In function ‘void rlImGuiShutdown()’:
../3rd/rlImGui/rlImGui.cpp:484:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
  484 |     if (GlobalContext != nullptr)
      |     ^~
../3rd/rlImGui/rlImGui.cpp:487:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
  487 |         ImGui::SetCurrentContext(GlobalContext);
      |         ^~~~~

Also there are a lot of warning messages about parentheses:

../3rd/rlImGui/rlImGui.cpp: In function ‘void rlImGuiShutdown()’:
../3rd/rlImGui/rlImGui.cpp:484:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
  484 |     if (GlobalContext != nullptr)
      |     ^~
../3rd/rlImGui/rlImGui.cpp:487:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
  487 |         ImGui::SetCurrentContext(GlobalContext);
JeffM2501 commented 5 months ago

Ok, I'll take another pass at it. I don't have a RPI, so it's hard to test for me.

JeffM2501 commented 5 months ago

Ok, I've made some changes. PLATFORM_DRM will be required, I have no other way (that I can see) to tell if rlImGui can even call multi monitor stuff or set cursors, so sadly we'll need that flag. raylib itself does not tell me what platform it was built for, and what that platform supports

I fixed the error, and cleaned up the mixed tab/spaces and that should hopefully fix the warnings.

Please let me know how that works out.

colesnicov commented 5 months ago

I don't have a RPI, so it's hard to test for me.

That's what I thought.


That's why I've prepared an extract from the `ImGuiNewFrame' function for you here after processing by the preprocessor:

Snippets from function ImGuiNewFrame after ran

 g++ -E ./rlImGui.cpp -I../imgui

here and got this:

static void ImGuiNewFrame(float deltaTime)
{
    ImGuiIO& io = ImGui::GetIO();

    if (IsWindowFullscreen())
    {
        int monitor = GetCurrentMonitor();
        io.DisplaySize.x = float(GetMonitorWidth(monitor));
        io.DisplaySize.y = float(GetMonitorHeight(monitor));
    }
    else
    {
        io.DisplaySize.x = float(GetScreenWidth());
        io.DisplaySize.y = float(GetScreenHeight());
    }

    Vector2 resolutionScale = GetWindowScaleDPI();

    if (!IsWindowState(FLAG_WINDOW_HIGHDPI))
        resolutionScale = Vector2{ 1,1 };

    io.DisplayFramebufferScale = ImVec2(resolutionScale.x, resolutionScale.y);

    io.DeltaTime = deltaTime;

    if (io.WantSetMousePos)
    {
        SetMousePosition((int)io.MousePos.x, (int)io.MousePos.y);
    }
    else
    {
        io.AddMousePosEvent((float)GetMouseX(), (float)GetMouseY());
    }
   ...

And with -DPLATFORM_DRM after ran

 g++ -E ./rlImGui.cpp -I../imgui -DPLATFORM_DRM

here and got this snippets (I made a comment in the code, what is missing):

static void ImGuiNewFrame(float deltaTime)
{
    ImGuiIO& io = ImGui::GetIO();
    io.DisplaySize.x = float(GetScreenWidth());
    io.DisplaySize.y = float(GetScreenHeight());

    /**
   * This missing
   * Vector2 resolutionScale = GetWindowScaleDPI();
   */

    if (!IsWindowState(FLAG_WINDOW_HIGHDPI))
        resolutionScale = Vector2{ 1,1 };

    io.DisplayFramebufferScale = ImVec2(resolutionScale.x, resolutionScale.y);

    io.DeltaTime = deltaTime;

    if (io.WantSetMousePos)
    {
        SetMousePosition((int)io.MousePos.x, (int)io.MousePos.y);
    }
    else
    {
        io.AddMousePosEvent((float)GetMouseX(), (float)GetMouseY());
    }
 ...

You can see that the line

if (!IsWindowState(FLAG_WINDOW_HIGHDPI))
        resolutionScale = Vector2{ 1,1 };

is being assigned but not declared anywhere.

Because this line is https://github.com/raylib-extras/rlImGui/blob/51b29d6c56523bb4ffed047e111656c5c58f3045/rlImGui.cpp#L115

And the error occurs here https://github.com/raylib-extras/rlImGui/blob/51b29d6c56523bb4ffed047e111656c5c58f3045/rlImGui.cpp#L123

colesnicov commented 5 months ago

Everything is alright.

JeffM2501 commented 5 months ago

Thanks, that helps. I've pushed a new change.

colesnicov commented 5 months ago

Tests passed on RPI Raspbian and on x86 Ubuntu.

JeffM2501 commented 5 months ago

image