svensson94
svensson94

Reputation: 21

How to make a simple menu with C++ and Raylib?

Im a novice programmer and im trying to use the Raylib library in C++.

But i cant get a simple Startmenu to work. Ive been trying to call void functions, used a switch and a simple if statement... How do i make a simple menu in raylib using switch or if statements without closing and open a new window of the program? Somehere in the While-loop i guess?



#include "raylib.h"




int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");

    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {

       BeginDrawing();
        ClearBackground(RAYWHITE);
        DrawText("Congrats! You created your firstwindow!", 190, 200, 50, LIGHTGRAY);
        EndDrawing();
        
        if(IsKeyPressed(KEY_Q)) DrawText("New thing here", 200, 210, 60, GREEN);
        if(IsKeyPressed(KEY_W)) DrawText("New thing here number two", 200, 210, 60, BLACK);

        

           
            
        }
        //----------------------------------------------------------------------------------
   
    CloseWindow();   
    return 0;


   }

Ive been trying with a Break and Pause and Goto things, how do i end the While loop without closing the window, do i need change the statements for the while loop?

Upvotes: 2

Views: 4723

Answers (2)

arellak
arellak

Reputation: 112

I'm not sure if I understand your question right...you want to go out of the main game loop without closing the window? That's not really possible because you render all of your window stuff in there. For me it seems like you lack a bit of fundamental understanding of the language itself. For example your if won't work the way you want to because it will only flash the text for a second instead of showing it the whole time.

bool showText = false;

while(...) {
    if(IsKeyPressed(KEY_Q)) showText = !showText; // if you press Q again the text will 
    disappear

    if(showText) DrawText(...);
}

Probably not the cleanest way but it works. I hope my answers helped you. If you're not sure about something you could take a look at Raylib Examples

Upvotes: 1

Tecelli Akıntuğ
Tecelli Akıntuğ

Reputation: 101

If I understood the problem correctly, you want a menu before the game launches. You can achieve this in the same window instead of trying to launch a new one.

Now the exact solution will change depending on what kind of game you're making but the simplest solution I could think of is to have an if statement for checking if we're in the menu in the while loop. That could look something like this.

    bool isInMenu = true
    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        // Update here
        if(isInMenu)
        {
            if(IsKeyPressed(KEY_Q)) isInMenu = false;
        }
        else
        {
            if(IsKeyPressed(KEY_W)) isInMenu = true;
        }

        // Draw here
        BeginDrawing();
        if(isInMenu)
        {
            ClearBackground(RAYWHITE);
            DrawText("This is the menu", 190, 200, 50, LIGHTGRAY);
        }
        else
        {
            ClearBackground(RAYWHITE);
            DrawText("Congrats! You created your firstwindow!", 190, 200, 50, LIGHTGRAY);
        }
        
        EndDrawing();

    }
    //------------------------------------------------------------------------------
   

Upvotes: 0

Related Questions