In this lesson, we'll dive into image positioning and cropping techniques. Here's what we'll cover:
As a starting point, we’ll be building upon the basic application loop and surface-blitting concepts we covered in previous lessons:
#include <SDL.h>
#include "Image.h"
class Window {
public:
Window() {
SDLWindow = SDL_CreateWindow(
"My Program", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 600, 300, 0);
}
void Render() {
SDL_FillRect(
GetSurface(), nullptr, SDL_MapRGB(
GetSurface()->format, 50, 50, 50
)
);
}
void Update() {
SDL_UpdateWindowSurface(SDLWindow);
}
SDL_Surface* GetSurface() {
return SDL_GetWindowSurface(SDLWindow);
}
~Window() { SDL_DestroyWindow(SDLWindow); }
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
private:
SDL_Window* SDLWindow;
};
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Event Event;
bool shouldQuit{false};
Window GameWindow;
Image ExampleImg{"example.bmp"};
while (!shouldQuit) {
while (SDL_PollEvent(&Event)) {
if (Event.type == SDL_QUIT) {
shouldQuit = true;
}
}
GameWindow.Render();
ExampleImg.Render(GameWindow.GetSurface());
GameWindow.Update();
}
SDL_Quit();
return 0;
}
#pragma once
#include <iostream>
#include <SDL.h>
#include <string>
class Image {
public:
Image(std::string File)
: ImageSurface{SDL_LoadBMP(File.c_str())} {
if (!ImageSurface) {
std::cout << "Failed to load image: "
<< File << ":\n" << SDL_GetError();
}
}
void Render(SDL_Surface* DestinationSurface) {
SDL_BlitSurface(
ImageSurface, nullptr,
DestinationSurface, nullptr
);
}
~Image() {
if (ImageSurface) {
SDL_FreeSurface(ImageSurface);
}
}
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
private:
SDL_Surface* ImageSurface{nullptr};
};
It relies on an image called example.bmp
being in the same location as our executable. The image we’re using in these examples is available here.
Our program currently renders the following window:
Most of our work in this lesson will be based on the 2nd and 4th arguments we’re passing to the Render()
 method:
// Image.h
class Image {
public:
// ...
void Render(SDL_Surface* DestinationSurface) {
SDL_BlitSurface(
ImageSurface,
nullptr, // Source rectangle
DestinationSurface,
nullptr // Destination rectangle
);
}
// ...
};
These two arguments are pointers to SDL_Rect
objects representing a source rectangle and destination rectangle respectively. We’ll focus on the source rectangle for now.
When we pass a nullptr
as the second argument, SDL_BlitSurface()
will copy as much of the source surface as possible. If the destination surface is big enough to contain the entire source surface, then the entire source will be blitted.
To blit only a portion of the image, we’d define an SDL_Rect
representing the rectangular area we’re interested in. SDL_Rect
is a simple struct with 4Â members:
x
- how far the rectangle is from the left edgey
- how far the rectangle is from the top edgew
- how wide the rectangle ish
- how tall the rectangle isWe can select the entire surface by setting:
x
to 0
y
to 0
w
to the surface’s width (available through the w
member on SDL_Surface
)h
to the surface’s height (available through the h
member on SDL_Surface
)This creates behavior equivalent to before, where we passed nullptr
as the source rectangle:
// Image.h
class Image {
public:
// ...
void Render(SDL_Surface* DestinationSurface) {
SourceRectangle(
ImageSurface, &SourceRectangle,
DestinationSurface, nullptr
);
}
// ...
private:
SDL_Surface* ImageSurface;
SDL_Rect SourceRectangle{
0, 0, ImageSurface->w, ImageSurface->h
};
};
Let’s demonstrate how we can update our SourceRectangle
to cause or Render()
method to blit only a portion of the image:
To make our rectangle smaller, we reduce the w
and h
 values:
// Image.h
class Image {
// ...
private:
SDL_Surface* ImageSurface;
SDL_Rect SourceRectangle{
0, 0,
ImageSurface->w - 50,
ImageSurface->h - 100
};
};
Our output now looks like this:
Let’s move our SourceRectangle
to the bottom right of our image:
Our SDL_Rect
is currently 50 pixels narrower and 100 pixels shorter than our image so, to move it to the bottom right edge, we use those values for the x
and y
 positions:
// Image.h
class Image {
// ...
private:
SDL_Surface* ImageSurface;
SDL_Rect SourceRectangle{
50, 100,
ImageSurface->w - 50,
ImageSurface->h - 100
};
};
Our output looks like this:
As a final example, let’s crop from all edges. We’ll exclude 50 pixels from the left and right edges, and 100 pixels from the top and bottom edges:
Our SourceRectangle
looks like this:
// Image.h
class Image {
// ...
private:
SDL_Surface* ImageSurface;
SDL_Rect SourceRectangle{
50, 100,
ImageSurface->w - 50,
ImageSurface->w - 100,
ImageSurface->h - 100
ImageSurface->w - 200,
};
};
Our output now looks like this:
The fourth argument to SDL_BlitSurface()
is an SDL_Rect
representing where we want the image to be blitted on our destination surface. The x
and y
properties specify where we want the image positioned within the destination surface.
The w
and h
are used a little differently in the context of a destination rectangle. We’ll set them to 0
for now, and talk about them in more detail later.
To replicate the default behavior of SDL_BlitSurface()
, we’d set the x
and y
values to 0
, too. This places the image at the top left edge of the surface:
// Image.h
class Image {
public:
// ...
void Render(SDL_Surface* DestinationSurface) {
SDL_BlitSurface(
ImageSurface, &SourceRectangle,
DestinationSurface, &DestinationRectangle
);
}
// ...
private:
SDL_Surface* ImageSurface;
SDL_Rect SourceRectangle{
0, 0, ImageSurface->w, ImageSurface->h};
SDL_Rect DestinationRectangle{
0, 0, 0, 0};
};
To move the image away from the left edge, we’d update the x
and y
components of the destination rectangle accordingly:
// Image.h
#pragma once
#include <iostream>
#include <SDL.h>
#include <string>
class Image {
//...
private:
SDL_Surface* ImageSurface;
SDL_Rect SourceRectangle{
0, 0, ImageSurface->w, ImageSurface->h};
SDL_Rect DestinationRectangle{
50, 100, 0, 0};
};
As we can see in the previous examples, we sometimes copy fewer pixels than our source rectangle contains. Clipping occurs when SDL needs to adjust what's being rendered. This happens in two main scenarios:
Source clipping: When the source rectangle extends beyond the boundaries of the source image. In this case, SDL2 only renders the part of the rectangle that's within the image.
Destination clipping: When the image (or part of it) would be rendered outside the boundaries of the destination surface. This can happen if:
In both cases, SDL automatically adjusts the rendering to show only the visible portion of the image.
If we need to detect and react to clipping, we can examine our destination rectangle after the call to SDL_BlitSurface()
. Whilst SDL_BlitSurface()
receives the source rectangle as a const SDL_Rect*
, the destination rectangle parameter is simply an SDL_Rect*
.
This indicates that SDL_BlitSurface()
is going to modify our destination rectangle, and that’s indeed the case. Specifically, SDL_BlitSurface()
updates the w
and h
properties of the destination rectangle with the dimensions of the area that was blitted, after clipping:
// Image.h
class Image {
public:
// ...
void Render(SDL_Surface* DestinationSurface) {
SDL_BlitSurface(
ImageSurface, &SourceRectangle,
DestinationSurface, &DestinationRectangle
);
std::cout << "\nDestination Width: "
<< DestinationRectangle.w
<< ", Height:"
<< DestinationRectangle.h;
}
// ...
};
Destination Width: 512, Height:200
Destination Width: 512, Height:200
Destination Width: 512, Height:200
...
We can compare the sizes of our source and destination rectangles to understand if clipping is happening:
// Image.h
class Image {
public:
// ...
void Render(SDL_Surface* DestinationSurface) {
SDL_BlitSurface(
ImageSurface, &SourceRectangle,
DestinationSurface, &DestinationRectangle
);
if (DestinationRectangle.w !=
SourceRectangle.w) {
std::cout << "Clipping Horizontally\n";
}
if (DestinationRectangle.h !=
SourceRectangle.h) {
std::cout << "Clipping Vertically\n";
}
}
// ...
};
Clipping Vertically
Clipping Vertically
Clipping Vertically
...
Whilst SDL can handle source and destination rectangles being invalid in this way, it’s generally not something we want to rely on. Proactively taking steps to keep our object’s state accurate makes adding more powerful capabilities.
For example, adding more capabilities to this Image
class may want to use the SourceRectangle
, and that becomes more complex if we can’t assume the SourceRectangle
is accurate.
Later in this chapter, we’ll extend our Image
class to add this and additional capabilities.
In this lesson, we've explored several key concepts for manipulating images in SDL2:
SDL_Rect
structures to control which parts of an image are rendered (source) and where they appear on screen (destination).x
and y
coordinates.These techniques form the foundation for more advanced image manipulation in SDL2. Later in this section, we’ll use this knowledge to expand our Image
class with new capabilities. First, though, we’ll learn some more topics that will influence how we design that API.
In the next lesson, we’ll see how we can control the size of our rendering, by blitting a larger or smaller copy of our image onto the destination surface.
Learn to precisely control image display using source and destination rectangles.
Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library
Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library
Free, unlimited access