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

Author Topic: Screenshot  (Read 4394 times)

0 Members and 1 Guest are viewing this topic.

Overlord

  • Newbie
  • *
  • Posts: 5
    • View Profile
Screenshot
« on: September 23, 2011, 12:32:11 pm »
I started learning SFML from SFMLCoder, and noticed that SFML interface changed, so I switched Image with Texture. This is lesson 2, so I didn't get very far :). The program worked, but I couldn't get a screenshot, so I googled, and found out, that I need an sf::Image and Window.Capture(). The program compiles, and runs, but it says that screenshot failed to save. My guess is that screenshot image is empty, but I am really new to SFML and I finished chapter 13 in Prata's book, so I am new to C++, too. I'd appreciate if someone took a look at the code and told me what to change and why. Sry if this has been asked 1000 times already.


Code: [Select]

#include <SFML/Graphics.hpp>

int main()
{

    sf::VideoMode VMode(800, 600, 32);//Create Window
    sf::RenderWindow Window(VMode, "Empty Window");

    sf::Texture Image;//Create image
    if (!Image.LoadFromFile("cake.png"))//Add a meme
        return 1;


    sf::Sprite Sprite2;//Make a sprite
    Sprite2.SetTexture(Image);//Adjust it as I want
    Sprite2.SetPosition(0.0f, 0.0f);
    Sprite2.SetScale(0.0f, 0.0f);
    Sprite2.Resize((float)VMode.Width, (float)VMode.Height);
    Sprite2.SetColor(sf::Color::Red);//Paint it with blood!!!!

    sf::Image Screenshot;//Image to store the screenshot

    while (Window.IsOpened())//If it is open
    {
        sf::Event Event;
        while (Window.PollEvent(Event))
        {
            switch (Event.Type)
            {
            case sf::Event::Closed://Check if it has been closed
                Window.Close();
                break;
            default:
                break;
            }
        }

        Window.Clear(sf::Color(0,255,127));//Color it

        Window.Draw(Sprite2);//Draw the sprite
        Window.Display();//Display the window
        Screenshot = Window.Capture();//Store screen in image

    }

    Screenshot.SaveToFile("sc.png");//Save screenshot
    return 0;
}
Errare humanum est. => I am really really human.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Screenshot
« Reply #1 on: September 23, 2011, 12:35:14 pm »
Here is what happens:
Code: [Select]
Windows.Close();
...
Screenshot = Window.Capture(); // empty because window is closed
...
Screenshot.SaveToFile("sc.png"); // write empty image

It makes no sense to overwrite the screenshot image every frame but only save it after the window is closed.
Laurent Gomila - SFML developer

Overlord

  • Newbie
  • *
  • Posts: 5
    • View Profile
Screenshot
« Reply #2 on: September 23, 2011, 12:45:16 pm »
Thx for a fast reply. Just moved Window.Capture() a bit above and it worked. Thx for help.
Errare humanum est. => I am really really human.

 

anything