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

Author Topic: sf::RenderWindow draw image  (Read 25926 times)

0 Members and 1 Guest are viewing this topic.

Munchor

  • Newbie
  • *
  • Posts: 16
    • View Profile
sf::RenderWindow draw image
« on: August 30, 2012, 08:40:57 pm »
I'm not being able to draw an sf::Image to a sf::RenderWindow.

Code: [Select]
PlayState.cpp: In member function ‘virtual void PlayState::render()’:
PlayState.cpp:30:34: error: no matching function for call to ‘sf::RenderWindow::draw(sf::Image&)’
PlayState.cpp:30:34: note: candidates are:
In file included from /usr/include/SFML/Graphics/RenderTexture.hpp:33:0,
                 from /usr/include/SFML/Graphics.hpp:39,
                 from PlayState.cpp:3:
/usr/include/SFML/Graphics/RenderTarget.hpp:186:10: note: void sf::RenderTarget::draw(const sf::Drawable&, const sf::RenderStates&)
/usr/include/SFML/Graphics/RenderTarget.hpp:186:10: note:   no known conversion for argument 1 from ‘sf::Image’ to ‘const sf::Drawable&’
/usr/include/SFML/Graphics/RenderTarget.hpp:197:10: note: void sf::RenderTarget::draw(const sf::Vertex*, unsigned int, sf::PrimitiveType, const sf::RenderStates&)
/usr/include/SFML/Graphics/RenderTarget.hpp:197:10: note:   candidate expects 4 arguments, 1 provided
make: *** [all] Error 1

void PlayState::render() {
  game->window.clear();
  game->window.draw(player->image);
  game->window.display();
}

That is my code. First of all, isn't sf::Image an sf::Drawable? Secondly, how would I go about passing the sf::RenderStates?

Thank you.






Oh wait, just read this, it all make sense now :)

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: sf::RenderWindow draw image
« Reply #1 on: August 30, 2012, 08:51:53 pm »
Hello,

You have to load the sf::Image in a sf::Texture (to make it simple, a sf::Texture is a picture living on the graphic card, but sf::Image is an array of pixels (image in RAM) ). Then, Create a sf::Sprite and set its texture to the newly created sf::Texture.

//It's an example, you have to keep the sf::Texture and sf::Sprite as members of your class
sf::Texture texture;
texture.loadFromImage(player->image);
sf::Sprite sprite;
sprite.setTexture(texture, true);

//Then, in PlayState::render()
game->window.draw(sprite);

EDIT : The tutorial you've read is for 1.6. In this version, sf::Texture doesn't exists, sf::Sprite used only sf::Image. Now on 2.0, you have to use sf::Texture.

 

anything