[#1] Make a C++ Game With Raylib: Setting Up Workspace

by Yot360 in Circuits > Software

3342 Views, 0 Favorites, 0 Comments

[#1] Make a C++ Game With Raylib: Setting Up Workspace

raylib_logo_card.png

In this instructable you'll learn how to setup a workspace to work with raylib, a C game library, to use with C++.

Supplies

For this instructable you won't need much, simply a computer and an internet connection.

C/C++ Compiler

To start coding in C/C++ you'll need a compiler, it's the piece of software that compiles your code into a computer-readable executable.


Windows:

If you're on windows, things are a bit harder, first download the compiler here w64devkit GitHub. Unzip the contents anywhere. Inside is w64devkit.exe, which launches a console window with the environment configured and ready to go. It is the easiest way to enter the development environment, and requires no system changes.

No that this is downloaded, you'll be able to run compiler commands... by opening the w64devkit.exe.


Linux:

If you're on Linux, things are easier, just use your package manager:

Arch:

sudo pacman -S base-devel

Debian:

sudo apt install g++ gcc

I can't do all distros else the list will be really long.


You can now try to see if it works by running:

gcc --version && g++ -- version

Compiling Raylib

Now that we have a compiler, we can compile raylib.

First, download its source code on GitHub https://github.com/raysan5/raylib/archive/refs/heads/master.zip. Extract the .zip into a folder. Then enter the src folder with w64devkit if on windows or with terminal if on Linux, with :

cd path/to/raylib/extracted/folder/src

After cd-ing to raylib directory, compile it with:

make PLATFORM=PLATFORM_DESKTOP

It'll take a bit of time, and you'll be left with a file named "libraylib.a" that you'll need to include in your projects.

If on Linux you can also do:

sudo make install

to install it directly on your system.

Creating a Project

Now that you have everything set-up, you can create your project.

1 - Make a new folder with your game name.

2 - Create an /lib and /include folder inside your project's folder.

3 - Copy "libraylib.a" previously compiled inside the /lib folder and "src/raylib.h" from raylib folder to your /include folder.

4 - Create a "main.cpp" file and enter the following code inside:

/*******************************************************************************************
*
*   raylib [core] example - Basic window
*
*   Welcome to raylib!
*
*   To test examples, just press F6 and execute raylib_compile_execute script
*   Note that compiled executable is placed in the same folder as .c file
*
*   You can find all basic examples on C:\raylib\raylib\examples folder or
*   raylib official webpage: www.raylib.com
*
*   Enjoy using raylib. :)
*
*   This example has been created using raylib 1.0 (www.raylib.com)
*   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
*   Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#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
    {
        // Update
        //----------------------------------------------------------------------------------
        // TODO: Update your variables here
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    CloseWindow();        // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

This is a basic window with raylib. Compile it with:

g++ main.cpp -lraylib -lGL -lm -lpthread -ldl -lrt -lX11

on Linux, and:

g++ main.cpp -Iinclude -Llib -lraylib -lopengl32 -lgdi32 -lwinmm


Finally run your program with:

./a.out


You won't be able to use a raylib compiled on windows on Linux.

Finished

Well done! You made your first "Game" using C++ and raylib!

Don't forget to leave a comment if you need support, and drop a like on my instructable if you find it useful!

Follow me on Twitter : Yot360

Check my website: yot-dev.com