• 2D shoot 'em up
SDL2 turn-based strategy tutorial
Water Closet ported to PlayStation Vita
The Legend of Edgar 1.35
SDL2 Rogue tutorial
— 2D platformer tutorial —
Note: this tutorial series builds upon the ones that came before it. If you aren't familiar with the previous tutorials, or the prior ones of this series, you should read those first.
This tutorial will explain how to add and control a character on a basic 2D map. Unpack the code and then type make to build. Once compiling is finished type ./ppp05 to run the code.
A 1280 x 720 window will open, with a series of coloured squares displayed over a light blue background. Pete, the player character, will drop to the ground from the top-left of the screen. He can be controlled using A and D to move him left and right, and using I to jump. Pressing Space will reset Pete to the starting point. If you're not fond of these controls, they can be easily changed in player.c. Black squares with white outlines are entities that Pete can stand on. Moving platforms will carry Pete around if he stands on them, and push him out of the way if he attempts to block them. Close the window by clicking on the window's close button.
Inspecting the code
Adding moving platforms is not tricky; they're just entities that move back and forth between two points. The hardest part of dealing with moving platforms is dealing with the collision response, as we need to move an entity a platform comes into contact with around. To start with, we'll add a new flag to defs.h:
The EF_PUSH flag will be used to tell moving platforms that they carry entities and push them around. We also need to add some new variables to the Entity struct:
We've added a tick function pointer so that we can drive the logic of our moving platforms. This will be called once per frame when we process our entities. The riding variable will point to an entity that another entity is currently sitting on. These are both used in the doEntities function in entities.c, which we'll made some updates to:
As before, we're looping through all the entities in the world, but as well as moving them we're also calling their tick function if it's been assigned. In this tutorial only the moving platforms will make use of them. We're then looping through all the entities again, only this time we're checking to see if they're currently riding on something. If the entity is, we're updating its x variable and then calling a new function called push. This function simply calls our moveToWorld and moveToEntities:
As can be seen, the entity being pushed will simply act as though its moving normally, with all the relevant collision checks against the world, etc. being made. We're not calling move this time because move does things like apply gravity, which we've already done. The move function itself has also been updated:
We're once again testing if the current entity is riding something. If it is and the thing it's riding is moving down the screen (dy > 0), we want to set the rider's dy to that of the thing it's riding, plus 1. Of course, this could have consequences for our game. It would mean that Pete (or any other entity does so) wouldn't be able to jump since this code would also override the jump movement. To overcome this, we simply state that if the player jumps, they are no longer riding anything:
We've only got one more function to update to allow platforms to carry entities and push them around. This will be done in the moveToEntities function:
Most things stay the same, but we're now making some additional checks. If the current entity has hit something solid and we're moving down the screen, we're telling the entity that it is on the ground and also now riding on the thing it made contact with. This will mean that, according to the second loop of doEntities, the entity will now start moving with the platform. Go back and look at the doEntities function again and you'll see it in action.
However, if the entity that has been touched isn't solid (other's flags do not contain EF_SOLID) things become interesting. If we've got the EF_PUSH flag set we're going to move the other thing out of the way:
This is done by adding the current entity's dx to the other's x, and then calling push. In effect, we're treating the other entity as though it is trying to move in the same direction as the pusher. As we're doing this for both dx and dy, a platform will now push things about. Note that while this works well for a platform pushing an entity up, it works less well when moving down. This is due to the speed of gravity vs the speed of the platform, which is why we have we added those new lines in the move function. Without these, an entity sitting on a platform moving down will appear to hop and bounce: falling, stopping, falling, stopping, etc. This can be seen in action by commenting out:
With that done, we just need to create the moving platforms. We'll do this by updating the addEntFromLine function:
In it, we're now calling separate functions to create the blocks and platforms. We'll look at the initPlatform function, as the initBlock function was more or less covered in the previous tutorial (it has been shifted into block.c). The initPlatform function lives in a file called platform.c:
We create an entity and then read all the relevant details of our platform from the line that was passed over to the function. For the platform, we read in the start x and y (sx and sy), and end x and y (ex and ey). We assign the current x and y to the start x and y, and then assign the tick function pointer. Finally, we load the platform's texture and assign its flags. Note that we're assigning the EF_PUSH flag to the platform. This is important for our platform to behave as expected: it should push Pete (and other entities) around when it encounters them.
The last thing to look at is the tick code for the platform:
The tick function controls the movement of the platform. Essentially, the platform is told to move to the end if it's at the start, and vice versa. So that we don't overshoot the start or end, we test if the absolute of the platform's x minus its start x is less than the speed it's moving (we also do this for the y). The direction of the platform is reversed if both of these are true. We call calcSlope to work out the direction the platform needs to move in, passing over the entity's dx and dy variables, and then multiplying the result by PLATFORM speed.
And that's it! We now have a platform game with a number of usable features. All that's missing is the pizza for Pete to collect, and some music and sounds effects. We'll put these finishing touches in during the final tutorial.
The source code for all parts of this tutorial (including assets) is available for purchase:
It is also available as part of the SDL2 tutorial bundle:
If you do not wish to create an itch.io account, you can also purchase the tutorial bundle using PayPal, and then download the tutorials directly from the main tutorials page.