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

Author Topic: If with window.draw  (Read 2576 times)

0 Members and 2 Guests are viewing this topic.

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
If with window.draw
« 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.

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: If with window.draw
« Reply #1 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();
 

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: If with window.draw
« Reply #2 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.

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: If with window.draw
« Reply #3 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 ;)

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: If with window.draw
« Reply #4 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!!!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: If with window.draw
« Reply #5 on: August 30, 2015, 10:51:54 pm »
I have fixed the problem
How? Your solution could help future visitors with the same problem.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: If with window.draw
« Reply #6 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: If with window.draw
« Reply #7 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything