Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Get image subrect  (Read 2317 times)

0 Members and 1 Guest are viewing this topic.

Richy19

  • Full Member
  • ***
  • Posts: 190
    • View Profile
Get image subrect
« on: July 08, 2011, 03:37:34 am »
Hi, in my game I am creating an animation class,
The class will take in a spritesheet and on request return an image from that sheet.

However I am unsure as to how I can get part of the main image.
I did think of having a sprite which has the main image and then setting its subrect to what I want and then getting the sprites image, but from what I read this would just return the original image so I am not sure as to how I could do this.

I am also unsure of how I can get a sprites position after moving it.
If I use sprite::move then IIRC it moves the sprite to an offset from its original position, but when using getPosition this just returns its original position, so is there a way to get its current position?
Or would i just have to not use move and do something like:
sprite.setPosition(sprite.getPos().x +5, sprite.getPos().y -3);

JAssange

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Get image subrect
« Reply #1 on: July 08, 2011, 03:53:12 am »
You just set the subrect and then draw, it'll draw only the subrect.

Haikarainen

  • Guest
Get image subrect
« Reply #2 on: July 11, 2011, 03:21:10 pm »
Create a Tile class wich consists of:

Code: [Select]
sf::Image* image; // pointer to image
sf::IntRect subrect; // the subrect to be drawn
void Draw(sf::RenderWindow* w, sf::Vector2f p ){
   sf::Sprite tmp;
   tmp.SetImage(*image);
   tmp.SetSubRect(subrect);
   w->Draw(tmp);
}


Then on your animation class, just have an internal position, and when you draw your current frame:

Code: [Select]
this->CurrentFrame->Draw(renderwindow, this->Position)

That way you separate the sprite's position-functions, and you can create your own :)

 

anything