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

Author Topic: Displaying sub-images?  (Read 5464 times)

0 Members and 1 Guest are viewing this topic.

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Displaying sub-images?
« on: August 23, 2012, 08:13:17 pm »
I have an image file where there is more than one 'sub-image' in the file (like a reel of sub-images). Is it possible in SFML, to load up the image file and only display the individual parts of the image? Is so, how do I do that?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Displaying sub-images?
« Reply #1 on: August 23, 2012, 08:16:41 pm »
You can assign the texture to a spirte and then use the setTextureRect function to define your 'sub-image'. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Displaying sub-images?
« Reply #2 on: August 23, 2012, 08:20:48 pm »
You can assign the texture to a spirte and then use the setTextureRect function to define your 'sub-image'. ;)

Okay, so I use sf::Texture for that sort of thing? All I needed to know, thank you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Displaying sub-images?
« Reply #3 on: August 23, 2012, 08:24:17 pm »
Okay, so I use sf::Texture for that sort of thing?
Yes, because a sprite can only hold a texture... ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Displaying sub-images?
« Reply #4 on: August 23, 2012, 08:48:44 pm »
Okay, so I use sf::Texture for that sort of thing?
Yes, because a sprite can only hold a texture... ;)

Or in this case, it can also hold a reference to sf::Image (.SetImage()...).

But I see no 'setTextureRect' in the documentation, or anything for that matter:
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php


Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Displaying sub-images?
« Reply #5 on: August 23, 2012, 08:53:20 pm »
Maybe I'm being naive. I want to load the entire image into memory, but treat it as individual images in memory, like what you'd get in a Java graphics environment?

Texture only seems to be able to load only one portion of the image from file - which means there's going to be a file-accessing bottleneck if I have to load up different portions of the image from file (yeah sure, trivial access times, but what's the improvement between loading 8+ separate image files rather than accessing the same file 8 times with additional calculation?).


Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Displaying sub-images?
« Reply #6 on: August 23, 2012, 09:13:22 pm »
Quote
Texture only seems to be able to load only one portion of the image from file
Where did you get that idea from? sf::Texture::loadFromFile() loads the whole and complete image into memory. I think there's a bit of confusion about the usage of sf::Image, sf::Texture and sf::Sprite. You should definately look that up. (setTextureRect is a member of sf::Sprite btw)

You could always create multiple instances of sf::Sprite that get their information from the same sf::Texture but use different TextureRects. You could as well change the Rect of a single sf::Sprite after drawing and then draw again with the new Rect, and so on. (I've never used any of the 2 methods, but both should work according to my understanding of it.)

Is that what you're talking about?
« Last Edit: August 23, 2012, 09:15:58 pm by Perde »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Displaying sub-images?
« Reply #7 on: August 23, 2012, 09:17:16 pm »
Being able to read is an advantage... ::)

You can assign the texture to a spirte and then use the setTextureRect function to define your 'sub-image'. ;)

Or in this case, it can also hold a reference to sf::Image (.SetImage()...).
Don't start to mix up SFML 1.6 and SFML 2.0. Since you've never said which version you're using (which you should have) I assumed SFML 2.0. And a sf::Sprite in SFML 2 can only hold a reference to a sf::Texture.

Texture only seems to be able to load only one portion of the image from file - which means there's going to be a file-accessing bottleneck if I have to load up different portions of the image from file (yeah sure, trivial access times, but what's the improvement between loading 8+ separate image files rather than accessing the same file 8 times with additional calculation?).
I've no idea what you're talking about. You load the texture once (loadFromFile) and then it's on the GPUs memory. Then you pass a reference to that texture to your sprite and then set the texture rect (on the sprite), which will 'cut-out' the wanted image. This is the most efficent way to do it.
If you don't like it you can also load the image into a sf::Image and then transform only a portion of the image to multiple textures.
« Last Edit: August 23, 2012, 09:18:54 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Displaying sub-images?
« Reply #8 on: August 23, 2012, 09:37:28 pm »
Okay, got it. 1.6 as 2.0 is still set as a 'future' release (I know I can get it now but I'll wait until it's all ready first). Confusion there.

Quote
Texture only seems to be able to load only one portion of the image from file
Where did you get that idea from? sf::Texture::loadFromFile() loads the whole and complete image into memory. I think there's a bit of confusion about the usage of sf::Image, sf::Texture and sf::Sprite. You should definately look that up. (setTextureRect is a member of sf::Sprite btw)

You could always create multiple instances of sf::Sprite that get their information from the same sf::Texture but use different TextureRects. You could as well change the Rect of a single sf::Sprite after drawing and then draw again with the new Rect, and so on. (I've never used any of the 2 methods, but both should work according to my understanding of it.)

Is that what you're talking about?

I think?

But it's not under Sprite either:
http://www.sfml-dev.org/documentation/1.6/classsf_1_1Sprite.php

"SetSubRect (const IntRect &SubRect)" Maybe?

I'm not daft about how to go about it. I'm daft as to what function does it. In Java it was something like 'SetView' for their own version of the images (change the view of the image made sense to me).
« Last Edit: August 23, 2012, 09:39:06 pm by Joshua Flynn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Displaying sub-images?
« Reply #9 on: August 23, 2012, 09:43:09 pm »
SetSubRect in SFML 1.6 (don't forget there are tutorials available)
setTextureRect in SFML 2
Laurent Gomila - SFML developer

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Displaying sub-images?
« Reply #10 on: August 23, 2012, 09:53:29 pm »
But it's not under Sprite either:
http://www.sfml-dev.org/documentation/1.6/classsf_1_1Sprite.php

Laurent already answered that one. I was refering to 2.0 simply because you linked to it's doc in a previous post.
By the way, as a bloody beginner myself I advise you to stick to 2.0 even though it's "just" a RC for now. Once you wrap your mind around the basics it's absolutely simple to use.

 

anything