PC Games

Orb
Lasagne Monsters
Three Guys Apocalypse
Water Closet
Blob Wars : Attrition
The Legend of Edgar
TBFTSS: The Pandoran War
Three Guys
Blob Wars : Blob and Conquer
Blob Wars : Metal Blob Solid
Project: Starfighter
TANX Squadron

Tutorials

2D shoot 'em up
2D top-down shooter
2D platform game
Sprite atlas tutorial
Working with TTF fonts
2D adventure game
Widget tutorial
2D shoot 'em up sequel
2D run and gun
Roguelike
Medals (Achievements)
2D turn-based strategy game
2D isometric game
2D map editor
2D mission-based shoot 'em up
2D Santa game
2D split screen game
2D quest game
SDL 1 tutorials (outdated)

Latest Updates

SDL2 Quest game tutorial
Wed, 7th May 2025

SDL2 Versus game tutorial
Wed, 20th March 2024

Download keys for SDL2 tutorials on itch.io
Sat, 16th March 2024

The Legend of Edgar 1.37
Mon, 1st January 2024

SDL2 Santa game tutorial 🎅
Thu, 23rd November 2023

All Updates »

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.

Click here to learn more and read an extract!

« Back to tutorial listing

— Simple 2D quest game —
Part 8: Towns, part 3

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:


struct Building
{
	BoundingBox  bounds;
	Building    *next;
};

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:


typedef struct
{
	Entity   *entity;
	Map       map;
	SDL_Point entrance;
	Building  buildingHead;
} 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:


void generateTown(Entity *e)
{
	Town *t;

	// snipped

	addBuildings(t);

	addDoors(t);

	// snipped
}

We're making calls to two new functions: addBuildings and addDoors. We'll look at addBuildings first:


static void addBuildings(Town *t)
{
	int          n, i, x, y;
	Building    *b, *prev;
	BoundingBox *bb;

	n = MIN_TOWN_BUILDINGS + rand() % (MAX_TOWN_BUILDINGS - MIN_TOWN_BUILDINGS);

	prev = &t->buildingHead;

	for (i = 0; i < n; i++)
	{
		b = malloc(sizeof(Building));
		memset(b, 0, sizeof(Building));

		bb = &b->bounds;

		do
		{
			bb->x1 = rand() % t->map.width;
			bb->y1 = rand() % t->map.height;
			bb->x2 = bb->x1 + (7 + rand() % 10);
			bb->y2 = bb->y1 + (7 + rand() % 10);
		} while (isOverlapping(bb, t));

		prev->next = b;
		prev = b;
	}

	for (b = t->buildingHead.next; b != NULL; b = b->next)
	{
		bb = &b->bounds;

		for (x = bb->x1; x <= bb->x2; x++)
		{
			for (y = bb->y1; y <= bb->y2; y++)
			{
				if (x == bb->x1 || y == bb->y1 || x == bb->x2 || y == bb->y2)
				{
					t->map.data[x][y] = TT_WALL;
				}
				else
				{
					t->map.data[x][y] = TT_FLOOR;
				}
			}
		}
	}
}

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:


static int isOverlapping(BoundingBox *bb1, Town *t)
{
	Building    *b;
	BoundingBox *bb2;
	int          w1, h1, w2, h2;

	w1 = bb1->x2 - bb1->x1;
	h1 = bb1->y2 - bb1->y1;

	if (!(bb1->x1 >= 3 && bb1->y1 >= 3 && bb1->x2 < t->map.width - 3 && bb1->y2 < t->map.height - 3))
	{
		return 1;
	}

	for (b = t->buildingHead.next; b != NULL; b = b->next)
	{
		bb2 = &b->bounds;

		w2 = (bb2->x2 - bb2->x1) + 6;
		h2 = (bb2->y2 - bb2->y1) + 6;

		if (collision(bb1->x1, bb1->y1, w1, h1, bb2->x1 - 3, bb2->y1 - 3, w2, h2))
		{
			return 1;
		}
	}

	return 0;
}

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:


static void addDoors(Town *t)
{
	int          x, y, w, h;
	Building    *b;
	BoundingBox *bb;
	Entity      *e;

	for (b = t->buildingHead.next; b != NULL; b = b->next)
	{
		bb = &b->bounds;
		w = bb->x2 - bb->x1;
		h = bb->y2 - bb->y1;

		switch (rand() % 4)
		{
			case 0:
				x = bb->x1 + 2 + rand() % (w - 4);
				y = bb->y1;
				break;

			case 1:
				x = bb->x1 + 2 + rand() % (w - 4);
				y = bb->y2;
				break;

			case 2:
				x = bb->x1;
				y = bb->y1 + 2 + rand() % (h - 4);
				break;

			default:
				x = bb->x2;
				y = bb->y1 + 2 + rand() % (h - 4);
				break;
		}

		t->map.data[x][y] = TT_GROUND;

		e = initEntity("Door");
		e->x = x;
		e->y = y;
	}
}

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:


void initDoor(Entity *e)
{
	e->type = ET_DOOR;
	e->solid = 0;
	e->texture = getAtlasImage("gfx/entities/door.png", 1);

	e->touch = touch;
}

Just a regular entity init function, that sets a `texture` and a `touch` function:


static void touch(Entity *self, Entity *other)
{
	if (other == game.map->player)
	{
		self->dead = 1;
	}
}

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

Mobile site