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 Firmware HF-Tech's chips have changed the world. Embedded into the heads of over 90% of the world's population, they have cured autism, dementia, provided intelligence boosts, and helped to ease some of the more mundane tasks in life. Daniel Blair, hacker and Workshop member, however is not convinced that everything is as rosy as it seems. But is he looking in all the wrong places..? |
— Making a 2D split screen game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction It's finally time for us to allow the player to configure the type of game they want to play, such as deciding how many lives each player should have. This step in making our game is all about widgets and configuration, and will be done within the new title.c file (which will further be expanded upon in the next part). Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./versus16 to run the code. You will see three text options: Start, Options, and Quit. As with the Options menu, use the arrow keys and the Enter key to navigate. Choosing Start will display a screen like the one above. Use the arrow keys to navigate the options and change the settings. Once you are finished, choose Go! to start the game properly. The game will play as normal, now using the win conditions set in the menu. Pressing Escape to choose an option to return to the title, and change the setup if you desire. Once you're finished, close the window to exit, or Choose Quit from the main title options. Inspecting the code Our game options will be (almost) the first thing we see when the game starts. They will form part of the title screen, and so we'll partly implement our title screen in this part. We'll do it properly in the next part. As already stated, all our game options are is a collection of widgets that allow us to set certain conditions. The values will be plucked from these widgets upon starting the game. It shouldn't be too tough to understand. So, over to title.c, where the vast majority of the updates live. First up, we have to initTitle function:
First, we want to set up our widgets. We do this via a call to setupWidgets. We're using a control variable here called wasInit to ensure this only happens once (we'll be using this code block for other setup bits later). Next, we want to make sure we highlight the Start widget, and then set another variable called `show` to SHOW_TITLE, so that we know what we want to process and draw. We can see show in use, as we move over to `logic`:
Here, we're deciding which of our widgets to process, based on the value of `show`. This doesn't require any further explaination! Over next, to `draw`:
As with `logic`, we're deciding which widgets we want to draw, based on the value of `show`. When in SHOW_START state, we're also drawing some text at the top of the screen saying "GAME SETTINGS". Again, a very simple function. Now, we're going to look at the functions used by the widgets themselves. These are all very standard, so we'll go over them quickly. Starting with `start`:
This function is invoked when we select the Start menu option. We update `show` to SHOW_START, to display the game options, and then set the active widget as "Go". The `back` function follows:
Rather like `start`, but we're returning to display the main title widgets. We're also highlighting the "Start" option. Next, we have the `options` function:
Here, we're just preparing to show our options menu, rather like in zone.c. `quit` follows:
Nothing much to say here..! Those are the simple function done, so now we can look at the more interesting one. We'll start with the `go` function:
This looks complicated, but it's not at all. This function is responsible for configuring the game conditions for the players. We start by settings our game's conditions to be a random zone, and all unlimited targets. Next, we grab the value from the "zone" SelectWidget, test if the value is not "Random", and call atoi on the value, to convert it from a string into a number. The values in the zone's SelectWidget are "1", "2", "3", "4", "5", and "Random", so this will result us getting a numerical value. We set Game's zoneNum to this value, to tell the game which zone we wish to play (and continue playing). We repeat this logic for our Game's livesLimit, scoreLimit, and timeLimit, testing that the associated widget's value is not "Unlimited" in these cases. For our aliens, we test if the SelectWidget value is "On", and set Game's `aliens` flag from the result of the strcmp. Finally, we save our configuration, so the players don't need to choose their favourite settings each time, and then start the game by calling initZone. Not overly complicated, but it does contain a few consideration. The returnFromOptions function follows:
This is called when we return from our options screen. There's not much that needs describing here, so we'll move onto the setupWidget function:
We're mostly setting the actions for various widgets here, using the functions we looked at earlier. Of note is the call to setGameplayWidgetValue that is used for lives, time, score, and zone. This is where things get a bit trickier, as we'll see:
What this function does is sets the SelectWidget's value, based on the input value. If `value` is not -1, we'll make use of the setSelectWidgetValue function (in widgets.c) to set the SelectWidget's value, based on the `text` supplied - this function basically loops through all the values in the SelectWidget, searching for a text match. `text` will contain "1", "2", etc. If the value is -1, we'll set the SelectWidget's value to the last one available. We're making an assumption here that the last option in our SelectWidget will be "Unlimited", "Random", etc., rather than an actual unknown value, such as "1", "2", "5", etc. This is basically some widget management, to correctly show the player the selected values. That's it for title.c. We're almost done with this part. Just two more changes to look at. Moving across to options.c, we've made a change to `quit`:
Now, instead of calling exit, we're saving our configuration, clearing down the zone data, and returning to the title screen. Finally, we've updated main.c:
We're now calling initTitle. Another part done! We're now able to configure our game before playing, quit out of it, re-configure, and play again. Our settings are also saved, so we don't need to keep setting things up when we start the game. Our game is very nearly finished. What we're missing is a funky title screen, to add a bit of glam. So, in our penultimate part, we'll be adding in the title screen! 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 |