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

Author Topic: Taking a Screenshot (Issue)  (Read 2977 times)

0 Members and 1 Guest are viewing this topic.

SickReferenceBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Taking a Screenshot (Issue)
« on: March 31, 2016, 12:31:55 am »
Hello, I am using SFML to create a random weapon generator, for the time being it just makes wooden planks, it works pretty well and I am very happy with how easy it is to do the basic stuff I'm doing with SFML, my only issue is screenshots, I've tried my best to make this work but to no avail.

When the user presses the 'Z' key, a variable called Take_Screenshot becomes true and then during the generation of the weapon it saves the screenshot. The problem is that when you press 'Z' the weapon has already been generated. So it instead saves a screenshot for the next weapon instead because only when you press enter does the screenshot code get run.

So I've tried moving the screenshot code outside of the Generate_Item, but the problem is that the screen gets cleared before then, I've tried a great deal of things and none of them resulted in an instant screenshot, I feel like perhaps it's just a case of logic and I'm missing something but I can't for the life of me figure it out,

Any help would be appreciated!

    while (window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Return)
                {
                    Generate_Item = 1;
                }
                else if(event.key.code == sf::Keyboard::Z)
                {
                    Take_ScreenShot = 1;
                }
            }
        }

        if(Generate_Item == 1)
        {
            Generate_Item = 0;
            window.clear(sf::Color::Black);

            GenerateMaterial();
            window.draw(MaterialSprite);

            int Condition_Amount = 5;
            Condition_Amount = rand()%Condition_Amount;

            if(Condition_Amount == 0)
                Condition_Amount = 5;
            else
                Condition_Amount = 4;

            Condition_Amount = rand()%Condition_Amount;

            if(Condition_Amount >= 1)
            {
                while(Condition_Amount != 0)
                {
                    Condition_Amount --;
                    GenerateCondition();
                    window.draw(ConditionSprite);
                }
            }

            text.setString("        PROCEDURAL WEAPON GENERATION!");
            text.setCharacterSize(25);

            text1.setString(" (" + Weapon_Name + ")");
            text1.setPosition(100, 85);
            text1.setCharacterSize(15);

            if(Weapon_Dura == 0)
                text2.setString(" (" + Wep_Damage + " DMG / BROKEN / $" + Wep_Value + ")");
            else
                text2.setString(" (" + Wep_Damage + " DMG / " + Wep_Dura + " DURA / $" + Wep_Value + ")");

            text2.setPosition(140, 105);
            text2.setCharacterSize(15);

            text3.setString(Weapon_Description);
            text3.setPosition(120, 125);
            text3.setCharacterSize(15);

             text4.setString("Press enter to generate a new weapon\nPress Z to save weapon as image");
            text4.setPosition(150, 355);
            text4.setCharacterSize(15);

            window.draw(text1);
            window.draw(text2);
            window.draw(text3);

            if(Take_ScreenShot == 1)
            {
                sf::Image screenshot = window.capture();
                screenshot.saveToFile("screenshots/" + Weapon_Name + ".png");
            }

            window.draw(text4);
            window.draw(text);
            window.display();
        }
    }
« Last Edit: March 31, 2016, 12:33:37 am by SickReferenceBro »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10936
    • View Profile
    • development blog
    • Email
AW: Taking a Screenshot (Issue)
« Reply #1 on: March 31, 2016, 01:52:33 am »
First you need to decide when a screenshot can be made and what should be captured.

Next you could render the item to a render texture, that way the item can kept alive till the next generation, plus it doesn't matter what else is on the screen.

Finally you call copyToImage on the texture and save it to disc.

Also keep in mind, the drawn objects get only visible once you call display(), so the screenshot taking in the middle of the draw code, has no effect on the captured window content.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SickReferenceBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Taking a Screenshot (Issue)
« Reply #2 on: March 31, 2016, 04:16:36 am »
Hello eXpl0it3r, thank you for replying so quickly!

I want the screenshot to be taken when the user presses 'z' and I want it to take a screenshot of the currently generated image, everything on screen aside from "text4" and "text"

I'm not entirely sure I understand the steps I need to take,

my screenshots come out fine, but I can't get the code to take a screenshot of the current weapon, it's always the next weapon, I want to get it to instantly save the current weapon as a screenshot

I can provide the entire source if nececarry, but I am very confused and have tried shuffling the code around numerous ways for the last two days, the only difference is the kind of errors I run into.

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Taking a Screenshot (Issue)
« Reply #3 on: March 31, 2016, 06:40:58 am »
If you want to take the screenshot instantly, it probably shouldn't be dependent on whether Generate_Item is true (and it is dependent in the code you supplied.)

SickReferenceBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Taking a Screenshot (Issue)
« Reply #4 on: March 31, 2016, 12:14:52 pm »
Hey Cire, this is true. My only problem is that Generate_Item needs to exist, If I just left the code within generate item out in the loop it's in it would generate an items nonstop and my whole code wouldnt work

I can't figure out how to both have a continual one at a time weapon generation system while being able to take screenshots

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10936
    • View Profile
    • development blog
    • Email
Re: Taking a Screenshot (Issue)
« Reply #5 on: March 31, 2016, 01:26:40 pm »
Did you think about my idea regarding render texture?

I'm still not sure, if you're just stuck with the logic. If you need to check another case, just use another variable. Like item_generation_in_progress and item_generated or something.
« Last Edit: March 31, 2016, 01:29:07 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/

SickReferenceBro

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Taking a Screenshot (Issue)
« Reply #6 on: March 31, 2016, 07:55:50 pm »
Hello eXpl0it3r, I'm not sure I follow how your instructions with the logic.

And embarrassingly I don't understand what you mean by render texture, I'm pretty new to SFML

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10936
    • View Profile
    • development blog
    • Email
Re: Taking a Screenshot (Issue)
« Reply #7 on: April 01, 2016, 12:20:46 am »
I've replied to your PM with an example on how to integrate a render texture into your code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/