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 What we're going to look at now is an optimisation step. As stated earlier, most modern computers won't choke even a little on our game; it's not all that complex. However, there is still potential for our performance to take a huge hit on larger levels. This part might also come in useful to those who might want to make a different type of game, using the concepts laid out in this tutorial. It might be fun to make an exploration game, with the area made up entirely of polygons. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./versus12 to run the code. You will see a window open like the one above, with each player on either side of our zone. Use the default controls (or, at your option, copy the config.json file from a previous tutorial, to use that - remember to exit the game before copying the replacement file). Play the game as normal. There isn't anything new to actually see here (there is no debug information displayed, unlike in the screenshots), so once you're finished, close the window to exit. Inspecting the code The idea of a spatial grid probably sounds like an enormously complex beast, but it certainly is not. As we'll see, it simply involves adding our entities to cells, and returning those entries as we query the grid. There are some gotchas involved, but we'll discuss those as we come to them. Let's get going. To begin with, we've updated defs.h:
MAX_WORLD_TRIANGLES is the maximum number of triangles we can add in our world. Next, it's over to structs.h, where we've added a new data type:
SpatialGridCell is a type to hold the data for a single "cell" in our Spatial Grid. numTris is the toal number of triangles in the cell. triCapacity is the maximum number of triangles it can currently hold, and `triangles` is an array of Triangles. This array can be resized according to our needs. For those that have been through the other tutorials involves quadtrees (Gunner and Santa), this will look quite familiar. Next, we've made a small change to Triangle:
We've added in an `id` field here. This hold the id of the triangle, that we can increment as we add more triangles. With that done, we can look at the code for our Spatial Grid, in spatialGrid.c. Before we do that, let's have a look at what we're going to do first.
Above is a visual representation of our spatial grid. Consider each grid cell (outlined in red). The rectangular area occupied by each triangle will be added to every cell it crosses. Consider the red highlighted region below the player. The two triangles that make up the horizontal plain will be added to the five highlighted cells. Anything that requests the triangles that occupy any one of those cells will get back those two triangles, and nothing more. This will mean that if we then want to check which triangles our player can come into contact with, we will be fetching all triangles in the cells highlighted in green (based on the collision radius we create for our entities). This will be just the two triangles in the top-left, since their square area will fall into that region. Everything else is ignored. We'll see how this works as we go through our spatial grid. We'll also talk about how we filter out duplicates (one of the gotchas). So, give to spatialGrid.c, where we'll start with initSpatialGrid:
Here, we're setting up our spatial grid. Our grid is, as one might expect, a multidimensional array of SpatialGridCells. As our grid is of a fixed size (GRID_SIZE is defined as 50 in spatialGrid.c), we're going to loop through our spatial grid array, and zero all the memory. Now, over to addTriToSpatialGrid, where something more interesting is going on:
This function adds a Triangle (`t`) to our spatial grid. We start by calculating the rectangular area occupied by the triangle, by working out its maximum and minimum `x` and `y` point values (assigned to `x1`, `y1`, `x2`, and `y2`). With the values worked out, we call a function named clipAddBounds, that will ensure our rectangular areas are constrained to our grid - we'll see this function in a little while. With our cell points known, we set up a loop to add the triangle to all the cells it should belong to. As with our Quadtree implementation, we'll increase the size of the SpatialGridCell to hold more triangles if it is currently at capacity (via increaseTriCapacity). Nothing mindblowing there! Next up, we have getAllTrisFromSpatialGrid:
As the name suggests, this function fetches Triangles from our spatial grid, for a given rectangular area: `x`, `y`, width (`w`), and height (`h`). These are added to an array we pass into the function named `candidates` (again, like our quadtree fetching!). We start by setting a variable called cIndex to 0. cIndex is our current index in our candidates array. Next, we zero our `candidates` array (expected to be of size MAX_WORLD_TRIANGLES), as well as an array called `added`. The `added` array is very important, as it is what we'll use to filter out duplicate Triangles in a very efficient manner. As each of our Triangles has an id, we'll use this as the index in the array to find out if a Triangle has been added. As this is random access within an array, it is very fast! Our array is also a uint8_t, which takes up very little memory. Next, we prepare our fetch loop. We'll call a function named clipFetchBounds, that will convert our world-space into grid-space, and ensure it is constrained correctly. We'll then loop from `x1` to `x2` and `y1` to `y2` through our grid cells, grabbing the Triangles from the affected cell, and adding them to our `candidates` list. As we do this, we test if the value in our `added` array at the index of the Triangle's `id` is set, and only add the Triangle to `candidates` if not. We then set the value in `added`, to mark it as such. Our return list now contains 100% unique Triangles. So, in summary, for a given world-space area, we're converting to grid-space, and building a unique list of all the Triangle that are contained within. That's it for adding and fetching Triangles to our spatial grid. As you can see, it's a pretty simple process. We'll finish off the other functions in spatialGrid.c now, starting with increaseTriCapacity:
As expected, all this does is increase the size of the `triangles` array the specified SpatialGridCell (`cell`). We saw a similar function in the Gunner tutorial, with its quadtree. Over now to clipAddBounds:
As we learned earlier, this function is responsible for converting world-space coordinates into grid-space coordinates. We ensure that the values of `x1`, `y1`, `x2`, and `y2` don't exceed our grid bounds. CELL_WIDTH and CELL_HEIGHT are calculated in spatialGrid.c:
At most, our spatial grid will be 5 screens wide and high (ZONE_WIDTH and ZONE_HEIGHT). That's quite big, and none of the zones in our game are anywhere near as big as that! We can see that our CELL_WIDTH and CELL_HEIGHT is based on the zone being split into 50 cells horizontal and vertical. Base on a SCREEN_WIDTH of 1600 and SCREEN_HEIGHT of 900, this gives us cells of 160 x 90 pixels. clipFetchBounds acts much the same, but takes a width (`w`) and height (`h`) to work with, as part of its calcuation:
`x1` and `y1 will already have been set to world-space prior to being passed into the function, leaving us only to convert the rest. That's all for spatialGrid.c for now. In Part 2, we'll be adding support for entities. Let's move on now to the remainder of the updates. First, over to world.c, where we've updated initWorld:
We're setting a new variable here call numTriangles to 0. This will be used to track the number of triangles we have so far created for our world. Next, we've updated loadWorld:
After loading our world, we loop through all the triangles in our list and add them to our spatial grid, via a call to addTriToSpatialGrid. Simple! Now for drawWorld. Once again, those who have followed the quadtree tutorial will be right at home with the following change:
Here, we're calling getAllTrisFromSpatialGrid, passing over the camera's `x` and `y` position as the top-left coordinates, and half the screen width, and its full height, as the width and height of the rectangle we want to grab our triangles for. These are placed into the `candidates` Triangle array that we pass over to the function. Next, we loop through all the candidate Triangles, stopping when we either hit a NULL or our counter (`i`) reached MAX_WORLD_TRIANGLES. Everything else remains the same. So, we'll only pull back the Triangles that fit into the rectangle of our current camera view, and render just those. This is much better than rendering the entire world view. Over next to spawnTriangle:
Now, when calling this method, we're checking if we've not exceeded our limit of MAX_WORLD_TRIANGLES. Then, when creating a Triangle, we're assigning its `id` to the next in the sequence of numTriangles. That's our updates to world.c concluded. From here on, we're just going to be looking mostly at the updates we've made elsewhere, to make use of our spatial grid. To begin with, let's look at the change to entities.c, and the touchWorld function:
As with rendering our world, when it comes to testing the collisions of the entities against the world, we're making use of the getAllTrisFromSpatialGrid function. For our entities, we're going to pass across a rectangle of fixed size. Here, ENTITY_WORLD_RADIUS is defined as 32. We're then looping through all the Triangles, as before. We are doing much the same in the touchWorld function in bullets.c:
Here, we're using a fixed size of 8 for BULLET_WORLD_RADIUS. While we're here, we've also made small change to drawBullets, to improve the rendering:
Now, before calling drawModel, we're testing if the bullet is visible to the camera that is making the call. We do this by testing a rectangular area that the bullet will occupy, and then checking if that rectangle is on screen. A small, micro optimisation. Over to pods.c, where we've tweaked canAddPod:
Once again, we're using the getAllTrisFromSpatialGrid function here, so that we don't test every Triangle in the world, when we only need to examine a small area. Before we finish up, let's look at another small optimisation, over in particles.c, in the drawParticles function:
As with our bullets, we're going to test if the Particle is inside the camera view before drawing it. With all that done, it's over to zone.c, for the final step. We just need up tweak initZone:
With a call to initSpatialGrid, we're all good to go! That wasn't too bad, was it? In fact, it was probably a lot easier than one first imagined. The only tricky bit about using a spatial grid is ensuring that we don't return the same Triangle more than once. There were a number of way we could've handled this, but I feel that using an array to determine if the Triangle is already being returned is the best bet. The worst approach would be to loop through our array, to see if the Triangle exists in the return list, before adding it. That would lead to a considerable performance hit. In the next part of our spatial grid implementation, we'll be adding in the entites. They, too, can be subject to a large number of collision checks, and so we want to reduce these as much as possible. As with our Triangles, our entities will also use an id / array combination. 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 |