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

Author Topic: Capturing window content  (Read 4622 times)

0 Members and 2 Guests are viewing this topic.

Skorpion

  • Newbie
  • *
  • Posts: 28
    • View Profile
Capturing window content
« on: December 22, 2015, 11:52:36 pm »
Hi all!

I'm having a problem, maybe an easy to solve one, nevertheless I can't find any working solution.

I want to capture whole window content, pass it to another method and display as background. At first I tought about
sf::Image
class so I created an object and tried to get what's on the screen. But even though one cycle of rendering stuff from the game passed all I got captured was the loading screen. I started some research and found out it's not the best option, because it's slow. OK then, I wrote Sleep(5000); over the capturing call and it didn't helped at all. But then I used more recommended code which is
sf::Texture::update();
Now it's worse, because I get that error:
Assertion failed: x + window.getSize().x <= m_size.x, file D:\sfml-release\_Sources\SFML\src\SFML\Graphics\Texture.cpp, line 407
Fine. There were thousands of errors in my life, aint one like that. I googled it and I found it might be some problem with including *.dll files to my project. So I checked all of them, for debug as well as release version and didn't find any mistake. Now I'm desperate, this is my code:
if (!cut_scene)
{
        Texture tex;
        tex.update(window);
        cut_scenes.run(tex);
        cut_scene = true;
}
When I change the line with updating texture with window content to loading to the texture a file it works perfectly.

I'm sorry, if this error is easy to fix but I really googled much and found nothing helpful. I'm also sorry for my english, which is probably awful.

Thank you in advance for help.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Capturing window content
« Reply #1 on: December 23, 2015, 12:19:19 am »
I think sf::RenderTexture is what you want.
It's like drawing things on a texture instead of a window.

Skorpion

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Capturing window content
« Reply #2 on: December 23, 2015, 12:53:04 am »
Thank you for your fast reply!

I tried sf::RenderTexture and now no error occures. Athough nothing is being displayed. This is how my code looks like now:
if (!cut_scene)
{
    RenderTexture texture;
    texture.clear();
    texture.setView(view);
    texture.draw(stuff_1);
    texture.draw(stuff_2);
    texture.draw(stuff_3);
    texture.display();
    cut_scenes.run(texture);
    cut_scene = true;
}

The declaration of run() method:
cut_scenes.run(RenderTexture &);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Capturing window content
« Reply #3 on: December 23, 2015, 07:59:23 am »
What about properly reading the doc/tutorial first? ;)

What's missing from your two pieces of code is the call to actually create the texture/render-texture. You're capturing/rendering to an invalid texture in both cases.
Laurent Gomila - SFML developer

Skorpion

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Capturing window content
« Reply #4 on: December 23, 2015, 04:36:44 pm »
Huh, seems logical. For some reasons I didn't thought about creating one before drawing to it. I just treated it like sf::Texture, where there is no need to pass any size of what we want to store in it, like calling sf::Texture::loadFromFile() method. Thank you :) now it works perfectly.

For all, who could have any problem like that in the future, you need to call sf::RenderTexture::create() for sf::RenderTexture object before drawing to it:
if (!cut_scene)
{
    RenderTexture texture;
    texture.create(500, 500);    // This line is NECESARY, you should change the values of course ;)
    texture.clear();
    texture.setView(view);
    texture.draw(stuff_1);
    texture.draw(stuff_2);
    texture.draw(stuff_3);
    texture.display();
    cut_scenes.run(texture);
    cut_scene = true;
}

Thank you again, you gave me another lesson about SFML :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Capturing window content
« Reply #5 on: December 23, 2015, 07:04:22 pm »
Quote
I just treated it like sf::Texture, where there is no need to pass any size of what we want to store in it, like calling sf::Texture::loadFromFile() method
You would also need to call texture.create(width, height) in this case, since you don't load it from an existing image.
Laurent Gomila - SFML developer

Skorpion

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Capturing window content
« Reply #6 on: December 23, 2015, 07:31:44 pm »
I just thought loadFromFile = draw in some way.