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

Author Topic: SFML: DrawTexture(string id, sf::RenderWindow pWindow); function?  (Read 2171 times)

0 Members and 1 Guest are viewing this topic.

DecoratorFawn82

  • Newbie
  • *
  • Posts: 10
    • View Profile
Hi! I've tried to create a function that can draw a SFML sprite directly onto the screen. The problem is that when I where going to use this function I didn't know that you couldn't pass a sf::RenderWindow to the pWindow parameter in that function. The compiler just generated a bunch of errors :(.

This is how the function looks like and how I've thought it could work. 

// Draw's the texture onto the screen
void TextureManger::DrawTexture(string id, sf::RenderWindow pWindow)
{
    pWindow.clear(sf::Color::Black);
    pWindow.draw(this->getSprite[id]);
    pWindow.display();
}

But here is how I thought if using it which maybe were a bad idea :(

The window is of course a sf::RenderWindow.
DrawTexture("image", window);

First before I realized that I were using a sf::RenderWindow. I used a sf::Window. But when
I where going to create the function the sf::Window had no .clear(<COLOR>), .display() functions.
So I were forced to use a sf::RenderWindow instead.

Is it anyone who can help me out with this problem? Or have a solution to a function that can solve this problem? Thanks for help if so! :)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: SFML: DrawTexture(string id, sf::RenderWindow pWindow); function?
« Reply #1 on: January 18, 2014, 03:50:37 pm »
The problem is that you try to copy the window (pass per value) but rendertargets have deleted copy constructors (inherits from sf::NonCopyable).

So you have 2options, pass a pointer of the window or (better) pass a reference of the window to the function.

void TextureManger::DrawTexture(string id, sf::RenderTarget &pWindow)
{
    pWindow.clear(sf::Color::Black);
    pWindow.draw(this->getSprite[id]);
    pWindow.display();
}
 

So you can use this function like you want to.
DrawTexture("image", window);

 btw you should use sf::RenderTarget& as the parameter type because then you can pass any RenderTarget, RenderWindow or RenderTextur, to the function, because their are inheriting from RenderTarget.


AlexAUT

DecoratorFawn82

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: SFML: DrawTexture(string id, sf::RenderWindow pWindow); function?
« Reply #2 on: January 18, 2014, 04:38:20 pm »
Thanks! That's useful information but I had to use a sf::RenderWindow& pWindow instead of a sf::RenderTarget& pWindow

Because the sf::RenderTarget didn't provide a display() function. So I needed to use a sf::RenderWindow& pWindow instead. But thanks for your help! :)

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: SFML: DrawTexture(string id, sf::RenderWindow pWindow); function?
« Reply #3 on: January 18, 2014, 04:54:28 pm »
Because the sf::RenderTarget didn't provide a display() function. So I needed to use a sf::RenderWindow& pWindow instead.

I don't know anything about your codedesign, but I would ask myself: "Is it really the job of such a draw function to call display?" I would answer it with no and call display somewhere else, because what if I want to draw another texture/sprite? (btw you should only call dispaly once each frame).


AlexAUT