PC Games

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

Android Games

Number Blocks
Match 3 Warriors


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
Medals (Achievements)
2D turn-based strategy game
SDL 1 tutorials (outdated)

Latest Updates

SDL2 turn-based strategy tutorial
Thu, 14th April 2022

Water Closet ported to PlayStation Vita
Tue, 4th January 2022

The Legend of Edgar 1.35
Sat, 1st January 2022

Achievements tutorial
Thu, 2nd December 2021

SDL2 Rogue tutorial
Thu, 30th September 2021

All Updates »


android (3)
battle-for-the-solar-system (9)
blob-wars (9)
brexit (1)
code (6)
edgar (7)
games (39)
lasagne-monsters (1)
making-of (5)
match3 (1)
numberblocksonline (1)
orb (2)
site (1)
tanx (4)
three-guys (3)
three-guys-apocalypse (3)
tutorials (10)
water-closet (4)


The Honour of the Knights (First Edition) (The Battle for the Solar System)

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 an Imperial nation's civil war than either the Confederation Stellar Navy or the government are willing to let on.

Click here to learn more and read an extract!

« Back to tutorial listing

— 2D Shoot 'Em Up Tutorial —
Part 2: Drawing the player


Note: this tutorial builds upon the ones that came before it. If you aren't familiar with the previous tutorials in this series you should read those first.

In the first tutorial, we learned how to open a window in SDL2. We're going to take another small step, but an essential one - loading and drawing graphics. This tutorial will show you how to achieve both in SDL2. Unpack the code and then type make to build. Once compiling is finished type ./shooter02 to run the code.

A 1280 x 720 window will open, coloured a light shade of blue. A spaceship sprite will also be shown, as in the screenshot above. Close the window by clicking on the window's close button.

Inspecting the code

We're going to build upon the code that we wrote in the first tutorial. If we first take a look at initSDL in init.c we'll notice that a new line has been added:


This line initializes SDL Image, to allow us to load PNGs and JPGs, to be used as textures. Next, we'll take a look at the changes in draw.c. There are two new functions available: loadTexture and blit. These two functions will load an image and display one respectively. We'll start with the loadTexture function.

SDL_Texture *loadTexture(char *filename)
	SDL_Texture *texture;


	texture = IMG_LoadTexture(app.renderer, filename);

	return texture;

This function is fairly simple. It calls the SDL Image function IMG_LoadTexture to load an image and return it as a texture. We pass over two parameters: the renderer that we're using and the filename of the image we want to load. It's possible to create a texture in SDL by first creating or loading a surface and then converting it into a texture, but this function does all the work for us in one call. Next, we have the blit function.

void blit(SDL_Texture *texture, int x, int y)
	SDL_Rect dest;

	dest.x = x;
	dest.y = y;
	SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h);

	SDL_RenderCopy(app.renderer, texture, NULL, &dest);

The blit function simply draws the specified texture on screen at the specified x and y coordinates. The actual drawing code is performed by an SDL function called SDL_RenderCopy that takes four parameters. The first is our trusty renderer and the second is the texture we want to draw. The next two parameters are the rectangular regions of the src texture and the target renderer. We pass NULL as the src rectangle to tell SDL to copy the entire texture. The dest rectangle is the x and y coordinates where we want to draw our texture, with its width and height being the width and height of the source texture.

The reason for allowing us to specify the source and dest rectangles is because we might have a large texture (such as a sprite atlas) that we only want to copy a portion of. The dest width and height would allow us to resize the source texture at the destination if we wanted to. For now, we'll keep things 1:1.

We've added a new object to structs.h. This Entity struct is going to be used to represent the player. Right now, it just holds the x and y coordinates, and the texture that it will use.

typedef struct {
	int x;
	int y;
	SDL_Texture *texture;
} Entity;

Finally, let's turn our attention to main.c. There have been a few changes here, but again nothing major or complicated.

int main(int argc, char *argv[])
	memset(&app, 0, sizeof(App));
	memset(&player, 0, sizeof(Entity));


	player.x = 100;
	player.y = 100;
	player.texture = loadTexture("gfx/player.png");


	while (1)


		blit(player.texture, player.x, player.y);



	return 0;

We now have an Entity declaration called player (this variable is defined in main.h for now). We assign the player its x and y coordinates and also call our loadTexture function to grab the texture to use. Finally, in the while loop, we call our blit function, passing the player's texture and coordinates. We put this draw call between the prepareScene and presentScene, so that it gets drawn after the scene has been cleared but not before the scene is presented. Everything else stays the same.


  • Place the ship in a different location on the screen.
  • Double the size of the player's ship when it's drawn (have a look at the blit function).


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:


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.

SDL2_Tutorials.tar.gz 56.76MB 23rd April 2022

Click here to see the list of files in the archive

Mobile site