Loading
Lesson 01
Courses / Build a Game with SDL - Make a Labyrinth with C++ from Scratch - Beginner Tutorial
Exploring Game Development in C++ with SDL (libsdl / SDL2) – Beginner Tutorial from Scratch

This lesson picks up from an initial exploration of Simple DirectMedia Layer (SDL), a library commonly used to develop video games.

The library header files for development were installed using apt in Ubuntu.

The function SDL_Init was used to initialize SDL. You can retrieve the errors emitted by SDL using the function SDL_GetError. You might find it useful to use the function SDL_Log for logging, the act of recording events that took place in the program.

One of the very first things you do is create a window to hold the game screen. That can be done with the SDL_CreateWindow function. Some of the arguments it takes are the title of the window, its position, and its dimensions.

Many of the functions from SDL return a pointer, so it is common to check if anything went wrong by comparing the return value to NULL, as was done with the result of SDL_CreateWindow.

The surface of the window can be retrieved using the function SDL_GetWindowSurface. You just need to pass in the pointer to the SDL_Window struct.

You can paint a filled rectangle on a surface using the SDL_FillRect function. It takes the pointer to the surface you want to paint, the pointer to the rectangle where you want to paint, and the color. You can pass NULL instead of SDL_Rect* to paint the whole surface. For the color, you can map to the required Uint32 using the function SDL_MapRGB.

The rectangle that was painted over the window surface will not appear to the user until the function SDL_UpdateWindowSurface is called.

A call to SDL_Delay was used to halt the execution of the program for a few milliseconds so the user could see something happen before it quit.

When you allocate resources such as an SDL_Window, it is important to remember to deallocate them. In this case, we used SDL_DestroyWindow to deallocate the memory used by the window and its surface.

Before the program quits, it is also important to make a call to close SDL. That is, using SDL_Quit().

Based on that initial exploration of libsdl, the lesson goes on to show how to paint a filled rectangle on a specific area of the window surface. An SDL_Rect is defined and its address is passed as the second argument to the SDL_FillRect function.

As part of more practice, concentric filled rectangles are painted at the center of the window surface.

A small square is used to represent the player on the screen. Its position is determined by an SDL_Rect structure. Its color is determined by a Uint32 variable whose value is mapped using a call to SDL_MapRGB().

We also learn to handle player input by polling events with the function SDL_PollEvent. Simply create a variable of type SDL_Event. Then pass the address of that variable to the SDL_PollEvent function. Keep calling that function as the condition of a loop, so long as the return value is not zero, meaning there is still some input to process.

We check if the event is a call to quit the program or if the event was a keyboard event where the key was pressed down.

The SDL_QUIT event type signals that we need to end the game loop and stop the execution of the program.

The SDL_KEYDOWN event type tells us we should look into player keyboard input. we check what key was pressed using the value from event.key.keysym.sym. For example, If the value is SDLK_RIGHT, it means the right-arrow key was pressed down. We then update the player position rectangle structure accordingly, based on that input signal.

We improve the player input with collision detection at the borders of the screen. That is, we cannot allow the player to walk outside the screen boundaries. That is done with simple if statement checks on the next position coordinate requested by the user.

By the end of the lesson, we have a screen with concentric filled rectangles. A small square, representing the player, can move across that screen with the keyboard arrow keys.

Did you like the lesson? 😆👍
Consider a donation to support our work: