Building a Versatile Image Class

Custom Alignment Scaling Mode

Is it possible to implement a ScalingMode that maintains aspect ratio but aligns the image to a specific corner or edge?

Abstract art representing computer programming

Yes, it's definitely possible to implement a ScalingMode that maintains aspect ratio while aligning the image to a specific corner or edge. This is a great feature to add more flexibility to our Image class. Let's call this new mode AlignedContain.

To implement this, we'll need to make several modifications to our Image class. First, let's define an Alignment enum to specify the desired alignment:

enum class Alignment {
  TopLeft, TopCenter, TopRight,
  CenterLeft, Center, CenterRight,
  BottomLeft, BottomCenter, BottomRight
};

We'll need to add a new member variable to store the alignment:

class Image {
private:
  // ... other members ...
  Alignment mAlignment{Alignment::Center};
  // ... other members ...
};

Now, let's modify our MatchAspectRatio() function to handle the alignment:

SDL_Rect Image::MatchAspectRatio(
  const SDL_Rect& Source,
  const SDL_Rect& Target) const {
  float TargetRatio{ Target.w
    / static_cast<float>(Target.h) };
  float SourceRatio{ Source.w
    / static_cast<float>(Source.h) };

  SDL_Rect ReturnValue = Source;

  if (SourceRatio < TargetRatio) {
    ReturnValue.h =
      static_cast<int>(Source.w / TargetRatio);
  } else {
    ReturnValue.w =
      static_cast<int>(Source.h * TargetRatio);
  }

  switch (mAlignment) {
    case Alignment::TopLeft:
      ReturnValue.x = Source.x;
      ReturnValue.y = Source.y;
      break;
    case Alignment::TopCenter:
      ReturnValue.x = Source.x
        + (Source.w - ReturnValue.w) / 2;
      ReturnValue.y = Source.y;
      break;
    // ... (other alignment cases)
  }

  return ReturnValue;
}

Add a method to set the alignment:

void Image::SetAlignment(Alignment Align) {
  mAlignment = Align;
  SetDestinationRectangle(mRequestedDestRectangle);
}

With these changes, we can now use the SetAlignment() method to maintain aspect ratio while aligning the image to a specific corner or edge. Here's an example of how to use it:

Image MyImage{
  "example.png",
  SrcRect, DestRect,
  ScalingMode::Contain
};
MyImage.SetAlignment(Alignment::TopLeft);

This implementation gives users fine-grained control over image positioning while still maintaining aspect ratio, making it easier to create complex layouts in games or other graphical applications.

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

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 51 Lessons
  • 100+ Code Samples
  • 91% 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