Cropping and Positioning Images

Image Rotation in SDL2 with Blitting

Is it possible to rotate an image using SDL2's blitting functions?

Abstract art representing computer programming

Unfortunately, it's not possible to rotate an image using SDL2's basic blitting functions like SDL_BlitSurface(). These functions are designed for simple copying of rectangular areas from one surface to another, without any transformations like rotation.

However, SDL2 does provide other ways to achieve image rotation. The most common approach is to use SDL2's rendering API, which includes the SDL_RenderCopyEx() function. This function allows for rotation, scaling, and flipping of textures.

Here's an example of how you can rotate an image using SDL2's rendering API:

#include <SDL.h>

#include <iostream>

class RotatableImage {
public:
  RotatableImage(SDL_Renderer* renderer,
                 const char* file)
    : renderer{renderer}, texture{nullptr},
      angle{0.0} {
    SDL_Surface* surface = SDL_LoadBMP(file);
    if (!surface) {
      std::cerr << "Failed to load image: " <<
        SDL_GetError() << '\n';
      return;
    }

    texture = SDL_CreateTextureFromSurface(
      renderer, surface);
    SDL_FreeSurface(surface);

    if (!texture) {
      std::cerr << "Failed to create texture: "
        << SDL_GetError() << '\n';
    }
  }

  void Render(int x, int y) {
    if (!texture) return;

    SDL_Rect destRect{x, y, 100, 100};
    // Adjust size as needed
    SDL_RenderCopyEx(renderer, texture, nullptr,
                     &destRect, angle, nullptr,
                     SDL_FLIP_NONE);
  }

  void Rotate(double degrees) {
    angle += degrees;
    if (angle >= 360.0) angle -= 360.0;
    else if (angle < 0.0) angle += 360.0;
  }

  ~RotatableImage() {
    if (texture) SDL_DestroyTexture(texture);
  }

private:
  SDL_Renderer* renderer;
  SDL_Texture* texture;
  double angle;
};

int main() {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* window =
    SDL_CreateWindow("Rotating Image",
                     SDL_WINDOWPOS_UNDEFINED,
                     SDL_WINDOWPOS_UNDEFINED,
                     640, 480, 0);
  SDL_Renderer* renderer =
    SDL_CreateRenderer(window, -1,
                       SDL_RENDERER_ACCELERATED);

  RotatableImage img{renderer, "example.bmp"};

  for (int i = 0; i < 360; ++i) {
    SDL_SetRenderDrawColor(renderer, 255, 255,
                           255, 255);
    SDL_RenderClear(renderer);

    img.Rotate(1.0);
    // Rotate by 1 degree each frame
    img.Render(320, 240);
    // Render at center of 640x480 window

    SDL_RenderPresent(renderer);
    SDL_Delay(10);
    // 10ms delay to slow down rotation
  }

  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}

In this example, we've created a RotatableImage class that uses SDL2's rendering API. The key differences from our previous surface-based approach are:

  1. We create a texture from our loaded surface.
  2. We use SDL_RenderCopyEx() instead of SDL_BlitSurface().
  3. We keep track of the rotation angle.

The SDL_RenderCopyEx() function allows us to specify a rotation angle, which is what enables the rotation effect.

Note that this approach requires using SDL2's renderer, which is generally more flexible and performant than surface blitting, especially for operations like rotation.

Remember, when working with rotation, you might need to adjust your positioning logic to account for the image's pivot point and potential changes in size due to rotation.

This Question is from the Lesson:

Cropping and Positioning Images

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Cropping and Positioning Images

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

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved