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 The Honour of the Knights (Second Edition) (Battle for the Solar System, #1) When starfighter pilot Simon Dodds is enrolled in a top secret military project, he and his wingmates begin to suspect that there is a lot more to the theft of a legendary battleship and the Mitikas Empire's civil war than the Helios Confederation is willing to let on. Somewhere out there the Pandoran army is gathering, preparing to bring ruin to all the galaxy... |
— An old-school isometric game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction This will be a short part, as we'll simply be adding in some sound and music. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./isometric12 to run the code. You will see a window open like the one above. Play the game as normal, and enjoy the music and sound effects. Note: there is no win screen. Once you're finished, close the window to exit. Inspecting the code With our game finished, let's throw in some sound effects and music, to complete the atmosphere. Note: since this is more of a demo for how to create an isometric world and view, there is no win state; the game will continue on even when all items have been collected. Adding in our sound and music is easy, so we'll just head right in. Starting with defs.h:
We've created a new enum set here. CH is short of channel, and will be used to determine the channel through which a sound will play. In order, we've created separate ones for walking (CH_WALK), collecting items (CH_ITEM), cleaning up spills (CH_SPILL), and activating a button (CH_BUTTON). We've also created an enum set for the sounds themselves:
These enums are used when loading and play sounds, as we'll see. If we turn to sound.c in system, we can see the loadSound function is loading our effects:
`sounds` is an array of SDL_mixer Mix_Chunks, static in sound.c. We're loading in a sound for each of our SND enums. With our sounds loaded and our enums set, playing sounds is simple. We already have a playSound function defined in sound.c, meaning all we need to do is call it from the approrpiate place, using the appropriate sound and channel values. In button.c, we're calling playSound whenever Purple Guy touches a button, via the `touch` function, passing over SND_BUTTON as the sound effect, and CH_BUTTON as the channel:
In glass.c, we're calling playSound when Purple Guy picks up an item, via `touch`, passing over SND_ITEM and CH_ITEM as the sound and channel, respectively:
The same occurs in knife.c, in its `touch`:
And also in redBox.c, in `touch`:
For the `touch` in spill.c, we're calling playSound with SND_SPILL and CH_SPILL, whenever Purple Guy touches a spill, and has water to do so:
Done. Nothing taxing, at all. We're just playing sounds to accompany the various interactions. Now for player.c, where we've updated doPlayer:
We've snipped a lot here, as the function is rather large, but have kept some code to act as context. Whenever the player moves, we're randomly playing a walk sound effect via the CH_WALK. Notice here that we're using rand() with SND_WALK_4, to pick a value between 0 and 3. This assumes that the walk sound effects we want to use are the first four in our list (that they are). Due to the delay in movement, it sounds like Purple Guy is running along quickly, but with some variation to his steps. The last sound effect we want to add is in iso.c, in the doISOObjects function:
As we know, whenever we enter a new zone, the drawing resets, and scene is recreated. Since this is controlled via a variable (drawTimer), we can test this to see if we want to play a sound. If the value of drawTimer is 0, we know that the scene is fresh, and hasn't drawn anything yet. We can therefore call playSound, passing over SND_TRANSITION. The channel we're using doesn't matter, so we're just passing over 0. Of course, we could always play this sound effect whenever Purple Guy moves into a new zone, via doPlayer, if we wished. Neither approach is wrong. The final thing we're going to do is play music. We do this in world.c, during initWorld:
Here, we've added calls to loadMusic, and then playMusic, to get our music playing. Both these functions are defined in sound.c. Passing 1 to playMusic means that it will loop forever. All done! Our little game is finished. More importantly, we've learned how to make a simple isometric engine, with decent sorting, tile picking, lighting, and other interactions. There is much more that could be added, but if one only wanted a simple adventure game, this is a great basis to work from. As previously stated, one could expand this into a huge game with multiple maps and zones. Hopefully, you will have found this very helpful. Purchase The source code for all parts of this tutorial (including assets) is available for purchase: From itch.io It is also available as part of the SDL2 tutorial bundle: |