Cropping and Positioning Images

Learn to precisely control image display using source and destination rectangles.

Ryan McCombe
Updated

In this lesson, we'll dive into image positioning and cropping techniques. Here's what we'll cover:

  • Source and Destination Rectangles: Learn how to control which parts of an image are rendered and where they appear on screen.
  • Cropping: Discover how to display only specific portions of an image.
  • Positioning: Learn how to place images precisely where we want them on our destination surface.
  • Clipping: Understand what happens when images don't fit entirely on screen and how to detect it.

As a starting point, we'll be building upon the basic application loop and surface-blitting concepts we covered in previous lessons:

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:

Source and Destination Rectangles

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 edge
  • y - how far the rectangle is from the top edge
  • w - how wide the rectangle is
  • h - how tall the rectangle is

We 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
  };
};

Cropping

Let's demonstrate how we can update our SourceRectangle to cause our 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:

Positioning

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};
};

Clipping

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:

  • The destination position would place the image partially or fully off-screen.
  • The image is larger than the destination surface.

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.

Summary

In this lesson, we've explored several key concepts for manipulating images in SDL2:

  • Source and Destination Rectangles: We learned how to use SDL_Rect structures to control which parts of an image are rendered (source) and where they appear on screen (destination).
  • Cropping: We practiced selecting specific portions of an image by adjusting the source rectangle's dimensions and position.
  • Positioning: We discovered how to place images at different locations on the screen by modifying the destination rectangle's x and y coordinates.
  • Clipping: We understood how SDL2 handles situations where images don't fit entirely on the screen and learned to detect when clipping occurs.

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.

Next Lesson
Lesson 29 of 129

Image Scaling and Aspect Ratios

Learn techniques for scaling images and working with aspect ratios

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Centering an Image in SDL2
How do I move an image to the center of the screen using SDL2?
Detecting Image Clipping in SDL2
How do I check if an image is being clipped when rendered?
Image Rotation in SDL2 with Blitting
Is it possible to rotate an image using SDL2's blitting functions?
Rendering Multiple Instances of an Image in SDL2
How can I display the same image multiple times in different positions on the screen?
Using Negative Coordinates in SDL2
What happens if I set negative values for x and y in the destination rectangle?
Creating a Slideshow Effect in SDL2
How do I create a simple slideshow effect by changing the source rectangle?
Fitting an Image to Window in SDL2
How do I ensure an image always fits within the window, regardless of its size?
Or Ask your Own Question
Purchase the course to ask your own questions