Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· OpenAIAi2 WildChat-4.8M (AI2)

hot to add raygui in raylib project in C language?

  1. User

    hot to add raygui in raylib project in C language?

  2. Assistant

    To add Raygui to a Raylib project in C language, follow these steps:

    1. Download the latest version of Raylib and Raygui from their GitHub repositories:
    1. Add the necessary files to your project:
    • Copy the raylib.h header file from the Raylib repository into your project directory.
    • Copy the raygui.h header file and the raygui.c source file from the Raygui repository into your project directory.
    1. Modify your project's CMakeLists.txt file to include the Raygui source file:
    cmake_minimum_required(VERSION 3.1)
    project(MyRaylibProject)
    
    set(SOURCES
        main.c
        raygui.c
    )
    
    add_executable(MyRaylibProject ${SOURCES})
    
    target_link_libraries(MyRaylibProject raylib)
    
    1. Include the Raygui header files in your C code:
    #include "raylib.h"
    #include "raygui.h"
    
    1. Initialize Raylib and start using Raygui functions in your code:
    int main() {
        InitWindow(800, 600, "My Raylib Project");
    
        while (!WindowShouldClose()) {
            BeginDrawing();
            ClearBackground(RAYWHITE);
    
            // Use Raygui functions here
    
            EndDrawing();
        }
    
        CloseWindow();
    
        return 0;
    }
    
    1. Compile and run your project to see Raygui in action!

    That's how you can add Raygui to a Raylib project in C language. Remember to check for any licensing requirements for using Raygui in your project.

  3. User

    ok. Now how to add panel with button and text field to the source?

  4. Assistant

    To add a panel with a button and a text field to your Raylib project using Raygui, you can follow these steps:

    1. Inside your main function, create a panel using the GuiGroupBox function:
    GuiGroupBox(panelRec, "My Panel");
    
    1. Add a button to the panel using the GuiButton function:
    if (GuiButton(buttonRec, "Click Me")) {
        // Button action code here
    }
    
    1. Add a text field to the panel using the GuiTextBox function:
    GuiTextBox(textBoxRec, myTextField, MAX_CHARS, false);
    

    Here is a complete example of how to add a panel with a button and a text field to your Raylib project:

    #include "raylib.h"
    #include "raygui.h"
    
    #define MAX_CHARS 20
    
    int main()
    {
        const int screenWidth = 800;
        const int screenHeight = 600;
    
        InitWindow(screenWidth, screenHeight, "Raylib + Raygui Example");
    
        Rectangle panelRec = { 100, 100, 200, 150 };
        Rectangle buttonRec = { 120, 130, 160, 40 };
        Rectangle textBoxRec = { 120, 180, 160, 40 };
    
        char myTextField[MAX_CHARS] = { 0 };
    
        while (!WindowShouldClose())
        {
            BeginDrawing();
            ClearBackground(RAYWHITE);
    
            // Draw panel
            GuiGroupBox(panelRec, "My Panel");
    
            // Draw button
            if (GuiButton(buttonRec, "Click Me"))
            {
                // Button action code here
                printf("Button clicked!\n");
            }
    
            // Draw text field
            GuiTextBox(textBoxRec, myTextField, MAX_CHARS, false);
    
            EndDrawing();
        }
    
        CloseWindow();
    
        return 0;
    }
    

    Compile and run the code to see the panel with a button and a text field in action. Feel free to customize the dimensions, positions, and functionalities of the panel elements according to your requirements.

  5. User

    when building compiler tells: "C:/DEV/SDK/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\me\AppData\Local\Temp\cc0nj7BF.o:main.c:(.text+0x681): undefined reference to `GuiGroupBox'"

  6. Assistant

    The error message you are receiving indicates that the linker is unable to find a reference to the GuiGroupBox function in your code. This typically occurs when the necessary Raygui library files are not included in the build process.

    To resolve this issue, follow these steps:

    1. Ensure that the raygui.c file is included in your project and added to the list of source files in your CMakeLists.txt file.

    2. Update your CMakeLists.txt file to include the necessary Raygui source file:

    cmake_minimum_required(VERSION 3.1)
    project(MyRaylibProject)
    
    set(SOURCES
        main.c
        raygui.c
    )
    
    add_executable(MyRaylibProject ${SOURCES})
    
    target_link_libraries(MyRaylibProject raylib)
    
    1. Verify that the Raygui functions are being included correctly in your code by checking the raygui.h header file:
    #include "raylib.h"
    #include "raygui.h"
    
    1. Rebuild your project to ensure that the changes are applied correctly. If you are using an IDE, clean and rebuild the project to avoid any cached build issues.

    2. Once the project is rebuilt successfully, try running it again to see if the linker error has been resolved.

    By following these steps and ensuring that the Raygui source file is properly included in your project, you should be able to resolve the "undefined reference" error related to the GuiGroupBox function.

  7. User

    the last raygui 4.0 has no raylib.c file in src dir, but only raygui.h

  8. Assistant

    In Raygui version 4.0, the structure of the library has been changed, and the raygui.c file has been removed. Instead, the library is meant to be used as a single-header library, meaning you only need to include the raygui.h header file in your project.

    To use Raygui version 4.0 with Raylib, you can follow these updated steps:

    1. Download the latest version of Raylib and Raygui 4.0 from their GitHub repositories:
    1. Copy the raygui.h header file from the Raygui repository into your project directory.

    2. Update your CMakeLists.txt file to include the Raygui header file and the Raylib library:

    cmake_minimum_required(VERSION 3.1)
    project(MyRaylibProject)
    
    set(SOURCES
        main.c
    )
    
    add_executable(MyRaylibProject ${SOURCES})
    
    target_link_libraries(MyRaylibProject raylib)
    
    1. Include the Raygui header file in your C code:
    #include "raylib.h"
    #include "raygui.h"
    
    1. Add the panel, button, and text field code using the Raygui functions in your main function:
    int main()
    {
        const int screenWidth = 800;
        const int screenHeight = 600;
    
        InitWindow(screenWidth, screenHeight, "Raylib + Raygui Example");
    
        char myTextField[20] = { 0 };
    
        while (!WindowShouldClose())
        {
            BeginDrawing();
            ClearBackground(RAYWHITE);
    
            // Draw panel
            GuiGroupBox((Rectangle) { 100, 100, 200, 150 }, "My Panel");
    
            // Draw button
            if (GuiButton((Rectangle) { 120, 130, 160, 40 }, "Click Me"))
            {
                // Button action code here
                printf("Button clicked!\n");
            }
    
            // Draw text field
            GuiTextBox((Rectangle) { 120, 180, 160, 40 }, myTextField, 20, false);
    
            EndDrawing();
        }
    
        CloseWindow();
    
        return 0;
    }
    
    1. Compile and run your project to see the panel with a button and a text field using Raygui version 4.0 with Raylib.

    By following these updated steps, you should be able to successfully integrate Raygui version 4.0 with Raylib in your C project and use its functionality in your application.

  9. User

    But how to build when i don't use cmake and use gcc and windows .bat files?

  10. Assistant

    If you are not using CMake and prefer to compile your project using GCC and Windows batch files, you can create a simple batch file to compile your project. Here's an example of how you can compile a Raylib project that uses Raygui with GCC and a batch file:

    1. Create a new batch file (e.g., build.bat) in the same directory as your source files.

    2. Open the batch file in a text editor and add the following commands to compile your project:

    @echo off
    
    set RAYLIB_DIR=C:\path\to\raylib
    set SOURCE_FILES=main.c
    set OUTPUT_FILE=MyRaylibProject.exe
    
    gcc -o %OUTPUT_FILE% %SOURCE_FILES% -I%RAYLIB_DIR%/src -L%RAYLIB_DIR%/src -lraylib -lopengl32 -lgdi32 -lwinmm -Wl,-subsystem,windows
    

    Replace C:\path\to\raylib with the path to your Raylib library directory, main.c with your main source file, and MyRaylibProject.exe with the name you want for the output executable.

    1. Save the batch file.

    2. Open a command prompt and navigate to the directory where your batch file and source files are located.

    3. Run the batch file by typing its name (build.bat) and pressing Enter.

    This batch file will compile your project using GCC, linking the Raylib library and other necessary libraries, and generate the executable file. You can then run the executable to see your Raylib project with Raygui in action.

    Make sure you have GCC set up and properly configured on your system to compile C code. Additionally, ensure that you have the necessary dependencies for Raylib and Raygui installed and accessible in your project directory.

    By following these steps, you can build your Raylib project with Raygui using GCC and Windows batch files without using CMake.