raysan5 / raygui

A simple and easy-to-use immediate-mode gui library
zlib License
3.61k stars 303 forks source link

Raygui objects not deleting using control variables / Unable to have proper screen management #425

Closed BlizzardBunny closed 2 months ago

BlizzardBunny commented 2 months ago

Hello! I'm trying to make the screen change when I press a button. I was able to do this using raylib before. However, I recently converted my primitive buttons to raygui buttons. And now, when I press the button, the raygui objects do not disappear when the screen is changed. Currently, I have an enum that determines when the buttons are drawn. But for some reason, they are drawn anyways. I would greatly appreciate any help on this! Thank you!

Here is my code:

#include <iostream>
#include <raylib.h>
#define RAYGUI_IMPLEMENTATION
#include <raygui.h>
#include <style_dark.h>

using namespace std;

typedef enum Screen { TITLE = 0, HWC, VLOG, EXIT } Screen;
typedef enum HWCState {PreUpload, PostUpload} HWCState;

Screen currScreen = TITLE;

const int screenWidth = 800;
const int screenHeight = 600;

HWCState hwcState = PreUpload;

void DrawTitleScreen()
{
    int titleFont = 64;
    int textLen = MeasureText("LUMBERJACK", titleFont);
    GuiSetStyle(DEFAULT, TEXT_SIZE, titleFont * 1.5f);
    GuiLabel(Rectangle{ ((screenWidth / 2) - (textLen / 2)) * 1.0f, (screenHeight / 4) * 1.0f , textLen * 1.0f, titleFont * 1.0f }, "LUMBERJACK");
    GuiSetStyle(DEFAULT, TEXT_SIZE, titleFont/2);
    GuiLabel(Rectangle{ ((screenWidth / 2) - (textLen / 2)) * 1.0f, ((screenHeight / 4) + titleFont) * 1.0f , textLen * 1.0f, titleFont * 1.0f }, "Log Analyzer");

    int Yoffset = 10;
    if (GuiButton(Rectangle{ ((screenWidth / 2) - (textLen / 2))*1.0f, ((screenHeight / 3) + titleFont + Yoffset)*1.0f, textLen*1.0f, titleFont*1.0f }, "HWC Log")) currScreen = HWC;
    if (GuiButton(Rectangle{ ((screenWidth / 2) - (textLen / 2)) * 1.0f, ((screenHeight / 3) + (2 * (titleFont + Yoffset))) * 1.0f, textLen * 1.0f, titleFont * 1.0f }, "VLog")) currScreen = VLOG;
    if (GuiButton(Rectangle{ ((screenWidth / 2) - (textLen / 2)) * 1.0f, ((screenHeight / 3) + (3 * (titleFont + Yoffset))) * 1.0f, textLen * 1.0f, titleFont * 1.0f }, "EXIT")) currScreen = EXIT;
}

void DrawHWCLogScreen()
{
    int titleFont = 64;
    int textLen = MeasureText("HWC Log", titleFont);
    DrawText("HWC Log", screenWidth / 80, screenHeight / 80, titleFont, RED);

    switch (hwcState)
    {
        case PreUpload:
        {

        }break;
    }
}

int main() 
{

    InitWindow(screenWidth, screenHeight, "LUMBERJACK : Log Analyzer");
    SetTargetFPS(60);

    while (WindowShouldClose() == false) 
    {
        BeginDrawing();
        GuiLoadStyleDark();

        switch(currScreen)
        {
            case TITLE:
            {
                DrawTitleScreen();
            }break;
            case HWC:
            {
                DrawHWCLogScreen();
            }break;
            case VLOG:
            {
                int titleFont = 64;
                int textLen = MeasureText("VLOG", titleFont);
                DrawText("VLOG", (screenWidth / 2) - (textLen / 2), screenHeight / 4, titleFont, RED);
            }break;
            case EXIT:
            {
                EndDrawing();
                CloseWindow();
                return 0;
            }
        }

        EndDrawing();
    }

    CloseWindow();
    return 0;
}
Vonixxx commented 2 months ago

I have an issue similar to this, unless I misunderstood OP's problem.

I use Raylib with Odinlang:

main :: proc() {
    ray.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE)
    defer ray.CloseWindow()

    ray.ClearBackground(Color.background)

    for !ray.WindowShouldClose() {
        ray.BeginDrawing()
        defer ray.EndDrawing()

        if !ray.IsMusicStreamPlaying(Music.song_loaded) {
            ray.GuiLabel(
                {
                    Pos.label_no_song_playing.x,
                    Pos.label_no_song_playing.y,
                    Size.label_no_song_playing.w,
                    Size.label_no_song_playing.h,
                },
                Gui.label_no_song_playing,
            )
        }
    }
}

I have posted only the relevant code snippets.

The label shows upon launching my program, which is expected, but when I play a song and the condition is no longer fulfilled, the label persists and is never cleared.

If I reverse the condition, only showing the label when the song plays, the label does not show upon launching my program, and shows when I play a song, but upon pausing the song it also is not cleared.

What do I have to do in order to clear the label?

BlizzardBunny commented 2 months ago

Hello.

Why is this closed as completed? Has there been a solution to this issue?

Thanks!

raysan5 commented 2 months ago

@BlizzardBunny This is closed because it is not a raygui issue but a user code issue.

BlizzardBunny commented 2 months ago

@raysan5 what is the issue with the code?

raysan5 commented 2 months ago

@BlizzardBunny GuiLoadStyle() loaded 60 times per second, EndDrawing() called twice, CloseWindow() called twice...

BlizzardBunny commented 2 months ago

EndDrawing() and CloseWindow() are not called twice. There are two calls in the code because it is called in case EXIT and then main() returns. But they are never called twice in the same case.

I will see if moving the GuiLoadStyle() fixes the issue. However, Vonixxx experienced the same issue without calling GuiLoadStyle() at all. Is there also a user code issue in their code block?

raysan5 commented 2 months ago

@BlizzardBunny I'm afraid that code is not in C, so the language could influence the result.

Could you provide a simple example illustrating the issue?

Also, did you try debugging the code?

BlizzardBunny commented 2 months ago

Hello. When making a simple test code, the issue does not show up. So it is a user code issue.

Perhaps it has something to do with the switch case or with function calls. Either way, I will continue to debug. Thank you for your help.