PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (43) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (17) • water-closet (4) Books A North-South Divide For over a hundred years, messenger Duncan has wandered the world, searching for the missing pieces of an amulet that will rid him of his curse; a curse that has burdened him with an extreme intolerance of the cold, an unnaturally long life, and the despair of watching all he knew and loved become lost to the ravages of time. But now, Duncan is close to the end of his long quest. |
— Making a 2D split screen game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction Our game is more or less finished now. In this final part, we'll be adding in a handful of little features, as well as sound and music. None of these things are required for our game, and are really just quality of life updates. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./versus18 to run the code. Play the game as normal, enjoying the sound and music, the messages when collecting items and scoring points, and the new countdown timer when a match begins. Once you're finished, close the window to exit, or Choose Quit from the main title options. Inspecting the code Adding in all our finishing touches requires us to hop into almost every file in our game. We'll be skipping over the sound and music code, as this has been seen many times before. We'll instead concentrate on the things unique to our game. Starting first with structs.h:
We've added in a new struct to define our information message when we collect items, etc. InfoText will hold this data. `text` is the message text; `health`, how long the text lives for; `position`, where it is in our zone; `color`, the colour of the text. Let's now look at infoText.c, where we've added in all our functions for handling the information text. Nothing here will really come as a surprise. Starting with initInfoText:
We're setting up a linked list here, using `head` and `tail`, static variables in infoText.c. Next up, we have addInfoText:
This function creates an InfoText item and adds it to our linked list. `text` is the text we want to display. `position`, where the text should appear, and `r`, `g`, `b` are its colour values. Again, we're simply creating an InfoText object and setting all its values. All our InfoText objects will live for just 1 second (FPS). Next up, we have doInfoText:
We're just processing the InfoText objects here. For each interation, we're decreasing the `health` of the InfoText, and also decreasing its position's `y` value. This will make the text move up the screen while it is still alive, just for aesthetic purposes, of course. Our InfoText will be deleted if its `health` falls to 0 or less. drawInfoText follows:
This function accepts a camera position as an argument (as an SDL_FPoint). As one might expect, we're adjusting the rendering position of the text based on the camera location. With that known (`x` and `y` as the draw position coordinates), we call drawText, passing over the InfoText's `text` and `color` data. We'll be drawing the text centered horizontally. Finally, we have clearInfoText:
We're just emptying our linked list here, to delete all the InfoText. Great! We've defined our InfoText. There was nothing there that was difficult to work with or understand. In summary, we're just drawing some text on screen, that will exist for a second, drawn in the colour of our choosing. Let's look at some examples of how we're using it. First, over to pod.c, where we've updated the `touch` function:
Now, when a Pod is collected by a Player, we'll call addInfoText, to display some information about what the Pod contained (points, health, etc). Notice that we're using different colours, based on the type of Pod that was collected. Likewise, we've updated takeDamage in aliens.c:
We're displaying the number of points that the Player earned for defeating an alien. Now to zone.c, where we've made some changes to accommodate our InfoText, and also added in some other quality of life updates. First to initZone:
First, we've added the call to initInfoText. Next, we've added in a new static variable called gameStartTimer, that we're setting to 2 seconds. This is responsible for displaying our countdown timer when a match first starts. We've also added in a new `state` called ZS_GAME_START, that we're setting here. If we jump down to `logic` next, we can see this being put to use:
We've added ZS_GAME_START to our `state` switch statement. All this does is decrease the value of gameStartTimer, switching `state` to ZS_IN_PROGRESS once it falls to 0 or less. We've also accounted for the existance of gameStartTimer in our main game processing. We want gameStartTimer to be 0 or less, as well as gameOverTimer to be greater than -2 seconds in order for our game to process. What this means is that while gameStartTimer is positive, the game won't process any logic, and everything will be frozen in place. We've also added doInfoText to our game processing clause. Our logic changes are done. Now we can move over to the rendering changes. Starting with `draw`:
For each of our players, we're now calling drawStars and drawInfoText, passing over the camera (`c`) to each. This is the reason why we added a camera position to drawStars for our title screen, since we were going to extend the usage into the game itself during these finishing touches. We've also added in a call to a new function named drawGameStart, that will render our countdown timer. Once again, we're doing this only if we're not displaying the game options, since all that text being drawn over other text would result in an unreadable mess! Over to drawGameStart:
This is where we render the "Get Ready ..." text that shows up when the match is starting. We test that gameStartTimer is greater than 0 before darkening the screen, and rendering some text, including the Zone number, and the countdown timer. There's nothing more to add here. And finally, we just have to do some clean up of our infoText, in clearZone:
The call to clearInfoText ensures we don't have any rogue info text hanging around when we're done with this zone. Our game is complete! We now have a two player, split screen game that can be configured to be played in a number of different ways, and supports keyboard and joystick controls for both players. We've also learned how to use SDL2's geometry API to create triangles and render them in batches. On top of that, a spatial grid partitioning system was detailed. Where to do go from here? Well, we could create a number of split screen games based on what we've learned here. Perhaps a 2D split screen platformer, or some sort of real time strategy game. We could even make the game cooperative, instead of competitive. Or maybe the game could have its multiplayer component removed, and instead turned into a single player game, where a spaceship explores an alien world, all built from triangles. Our spatial grid would certainly work well for something like that. As with all the other tutorials, the possibilities are endless. Purchase The source code for all parts of this tutorial (including assets) is available for purchase, as part of the SDL2 tutorials bundle: From itch.io |