![]() | |
PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Quest game tutorial
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (44) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (18) • 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. |
— Simple 2D quest game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction In the last part, we put together the beginnings of our town, which consisted of just the ground and the surrounding wall. It's time now to fill it with some buildings, that we will be able to visit. We'll be adding some doors, too, that will mark the entrance. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./quest08 to run the code. As before, you can move around with the WASD control scheme. Enter the buildings by walking through the doors. The doors are removed permanently by this, to show that we've entered the place at least once. As with the overworld, the layout will be different each time the game is run. When you're finished, close the window to exit. Inspecting the code Once again, the bulk of our focus will be on townGen.c. Before we get there, we need to make some updates to structs.h: First, we've added a new struct called Building:
This struct represents the building itself. It will be expanded our in future. For now, it contains just the a field to hold the `bounds` of the building. The building itself is also part of a linked list. Next, we've updated Town:
We've now storing our building data linked list in the Town itself (as buildingHead). Now, over to townGen.c. First, we've updated generateTown:
We're making calls to two new functions: addBuildings and addDoors. We'll look at addBuildings first:
What this function does is randomly generate a number of buildings, and adds them to the town map. First, we randomly determine the number of buildings the town will have (`n`). We then use a for-loop to generate that number of buildings. Each Building's `bounds` will have a random width and height between 7 and 16 tiles (including the walls), and will be placed in a random part of the town. For each building we create, we make a call to a function called isOverlapping (we'll see this in a bit), to check if it is okay to add our building at the desired location. Once done, the building is added to the Town's linked list. Once all our buildings have been created, we loop through our linked list of Buildings, and update the Town's `map` `data`. We'll use the Building's `bounds` to fill in the rectanglar area occupied. Much like when we created the Town's ground and surrounding walls, the edges of the building will be converted into TT_WALL, while the internal area will become TT_FLOOR. Pretty simple. Next, let's look at isOverlapping:
isOverlapping does as the name suggests - it checks whether the building we're about to add is overlapping with any other buildings we've already created. For this purpose, we're also ensuring that the building is within the bounds of the town. To ensure that there is space for us to navigate the town, we're padding the bounds of the buildings by 3 before testing the overlap. This allows us room to walk between them. As with our "road" building on the overworld, we're increasing the space by this much to make things a bit more comfortable, rather than have a one tile gap between things. With our buildings placed, we now need to add the ability to enter them; right now, we have solid walls all around. We'll add the building entry points via addDoors:
Much like when we added the entrance to the Town itself, we're randomly picking a point on each building's exterior wall. As we pick a point, we're staying away from the corners, so that the entrance is a little more towards the middle of the building (once again, so that things don't look odd). With our point chosen, we're converting the wall tile into a ground tile, and then adding the Door entity at the location (`x` and `y`). That's it for townGen.c. We've got a buildings constructed, placed correctly, and our entrances added. Before we finish up, let's look at door.c, to see how our Door entity works. Starting with initDoor:
Just a regular entity init function, that sets a `texture` and a `touch` function:
The `touch` function, as mentioned before, destroys the door completely when the player come into contact with it! Yep, we're smashing down the doors to get a good look inside. The owner of the building probably won't be too happy about that. We're done! Our town is now looking a little more like a town, with buildings and doors. But where are the people? This isn't a ghost town (although that would be quite fun, too). We should add some, that the player can interact with. So, in the next part we'll be adding in our NPCs, that the player can talk to. 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 |