SFML community forums

Help => Graphics => Topic started by: GAMINGBACON97 on August 30, 2015, 08:36:35 pm

Title: If with window.draw
Post by: GAMINGBACON97 on August 30, 2015, 08:36:35 pm
I'm having an issue, I'm trying to make an inventory screen, and when the player presses "I" I want the window to show me their inventory, and pause the game, I have done this:

         if (sf::Keyboard::isKeyPressed(sf::Keyboard::I)){
            window.draw(BAG);
         }
//BAG is inventory screen;
I get no compile errors, and my game runs, but, It just displays without me pressing "I", "I" does nothing.

P.S. I've only been learning SFML for about a week now, so please don't be angry if I made a newbish mistake.
Title: Re: If with window.draw
Post by: Mr_Blame on August 30, 2015, 08:50:24 pm
This is probably because after event processing(as i see your draw() function is in events if) you must have this line
window.clear()
Which clears whole framebuffer by filling it with color
Short explenation of what you are probably doing
//draw
Window.draw(sprite);

//clears the framebuffer by filling it with color (black default)
window.clear();

//and you get nothing displayed
Window.display();
 
Title: Re: If with window.draw
Post by: GAMINGBACON97 on August 30, 2015, 08:53:59 pm
      window.draw(Background);
      window.draw(playerImage);
      window.display();
      window.clear();
is the order I have them in now, and my if statement is farther up in my code, if need be I can post all my code, but it would problably be messy.
Title: Re: If with window.draw
Post by: Mr_Blame on August 30, 2015, 08:56:08 pm
He is the simplest and best code that you must have
window.clear();

//draw some epic stuff HERE

window.display();
 

Well this order MUST have any SFML app if don't want to have some weird behaviour ;)
Title: Re: If with window.draw
Post by: GAMINGBACON97 on August 30, 2015, 09:05:11 pm
I have fixed the problem, created another where is flashes for a milisecond, and fixed that with an int, and thank y'all for the fast responses, I expected to get a reply in a few hours, not a few minutes.
If I have any future issues, I will be sure to post to this forum because you guys ROCK!!!
Title: Re: If with window.draw
Post by: Hapax on August 30, 2015, 10:51:54 pm
I have fixed the problem
How? Your solution could help future visitors with the same problem.
Title: Re: If with window.draw
Post by: GAMINGBACON97 on August 31, 2015, 12:14:21 am
int pause4BAG = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::I)){
               pause4BAG = 1;
//this also opens up my inventory screen and other such things
   if (Slot1 == 1){
                  window.clear();
                  window.draw(BAG);
                  window.draw(item);
                  window.display();
               }
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
         pause4BAG = 0;
      }
if (pause4BAG == 0){
         window.clear();
         window.draw(Background);
         window.draw(playerImage);
         window.display();
      }
I could explain a bit more, but if you have any questions about my solutuion please feel free to email me.
Title: Re: If with window.draw
Post by: Hapax on August 31, 2015, 01:27:15 pm
Oh.
It looks like the problem you were having was that it doesn't stay open when you press I.
Assuming that you were drawing after clear and before display, the "inventory" should be shown as long as the key was held.

The way you are now using is (responding to key presses once, when they are pressed) is much more suited to events than real-time states.