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();
}
}