Game Development with raylib C++ — Day #2
In the last article Download and Installation, we downloaded and installed both Visual Studio Code and the raylib library. In this article, together, we create a simple program that has a circle following the mouse pointer.
Create a main file
With the workspace open, (see the previous article), let’s create a new file by clicking on the New File button. Name this file main.cpp and press enter.
Noticed how the main file has the C++ icon. It detected since by the file extension, which is a source file for C++ code.
With C++ programs, the entry point when it starts is the main function. So lets code this function. In the main.cpp file area, type the following code:
To create a game window, we need to include the raylib library and use the InitWindow function.
The InitWindow function takes the width of the window in pixels, height, and the title of the window. The code above creates a window that is 800x600 and titled My First Program.
To run our code, press F5 or Run > Start Debugging. It will compile the code and execute the program that brings up a window. Did you see it! It did appear for an instance. The problem is the program is doing exactly what you instructed it to do. It initialised a window with the properties you supplied then the next line of code, it exits of the program.
We need to make use of a while loop and have the application running till we press the close button on the window or press ESC. Type the following:
Lets briefly go over some of these new functions.
SetTargetFPS() — this sets the maximum frame rate per second. Each system has different processing power so what we are doing here is stating what the maximum frame rate we want this program to run in.
WindowShouldClose() — this will allow the program to properly close without having to STOP our debugger to end the program. It lets you make use of the Windows close button or it listens out for the ESC key press.
BeginDrawing() and EndDrawing () — between these functions is where the text and shapes are drawn to the window canvas area, others known as the Setup and teardown.
ClearBackground() — clears the canvas (in the window) to the colour specified. This is a must to avoid screen flickering.
CloseWindow() — This closes the window safely. In the background it turns off all the services that supported the game running.
Run the program now and you will see the window filled with a black colour and you can either press the window close button or press the ESC key.
Drawing a circle
Now that we have a window setup, lets now move on to drawing a circle to the window.
raylib has a number of functions for drawing a circle as you can see from the image below.
We are going to use the DrawCircle function that takes both the x and y positions, radius, and colour parameters. Let's draw a blue circle in the centre of the screen.
Getting mouse inputs
With a blue circle drawn to the screen, lets get the mouse position and have the circle go where the mouse goes. Lets use a different function, the DrawCircleV function that accepts a Vector2 parameter since the GetMousePosition function returns a Vector2. Here is the full code
There you go, in this program, we have a Window that shows a blue circle (ball) where the mouse pointer is position.
The full source code is available on GitHub at https://github.com/JSeneque/cpp-raylib-01-myfirstprogram
In the next article, Game class and Game Loop, I will show you how to create a Game class that encapsulates the core game loop, which makes games programs different from the other programs writing in C++.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.
I am available for your next chart-topping game!