Raylib: Game Development Basics


Raylib is a C++ library intended to help with game development. You can use it to make all kinds of games and you are even allowed to sell those games. Today, I will be showing you the very basics of its use. Do note that I'm not going to cover how to install it. For that, I suggest you to watch this video.
Setting Up raylib

And, if you do not like the default editor provided with it then you can learn how to configure Visual Studio for its use in the same video.

For this tutorial I am assuming that you have a basic understanding of Programming and can write code in C or C++ language. But, If that is not the case then I will be making a tutorial on that later on. For now, you can check out my article about how to start learning programming here.

First you have to create a new file and save it as a C or C++ file. I am going to save mine as 'main.cpp'. You can save the file anywhere on your computer as long as it can be accessed be the editor. You don't have to save it in the install directory of raylib. At the start of the file, we are going to include the raylib library like this.

Next we will need to write a function. If you have previously worked with this language, then you should know what those strange words mean. If not, then you can ignore them for now. Now, your file should look like this. 'return 0' at the end should be written as such. It is not particularly important for game programming but if have worked on this language previously then you will know why it is there.

Next we are going to write some lines that will tell the computer how to start a window for our little game. First write the following piece of code inside our main function. We are saying that the window about to be created should have a width of 800 pixels and a height of 650 pixels. Pixels are the units in which computers measure the screen space.

After that we tell the computer to start a window with the above size and with the words "Placeholder Title" written on it title bar.
We specify that the 60 frames should be rendered on the screen every second.

Now we will begin the game loop. A game loop is a sort of repetitive cycle within which a game runs. All video games are made in this way. Let us write the following code. Note the first line says WindowShouldClose() but the exclamation mark before that negates it. So, we are telling the computer that while window is not closing, it should do certain things.

The last line of the loop tells the computer to close the window as you can probably guess.

Inside the loop, we are making call to the raylib library functions BeginDrawing() and EndDrawing(). They work in the same way as I talked about the game loop previously. Raylib has this way of whatever it starts explicitly it also ends it explicitly. But, other Game creation tools may not be like that.

Next we tell the computer to clear the previous background and cover it with the specified color. I must mention that raylib a number of predefined colors that are mentioned in the raylib cheatsheet which you can find here. Once you gain enough understanding of this liberary, you can even define your own colors.

And, that is it. After saving these changes, you can run this small program. If you are using the default editor provided with raylib then pressing F6 will run your code. and you will see a result like the following. 

Here is the complete code for this example.

Comments

Popular Posts