SFML community forums

Help => Graphics => Topic started by: DraGun on August 07, 2015, 04:32:11 pm

Title: window.draw() does not work inside of 'if' statements
Post by: DraGun on August 07, 2015, 04:32:11 pm
As the title says, whenever I try to use window.draw() inside of an 'if' or 'else' it won't draw. It's something I've had to work around, but now I really just want it to work. Any ideas as to why it doesn't work?
Title: Re: window.draw() does not work inside of 'if' statements
Post by: zsbzsb on August 07, 2015, 04:37:08 pm
So let me get this straight, you think we have done something to the draw() function that causes it to fail inside a branch statement? Even if this was remotely possible try to think of a reason that we would do something like that, because it makes no sense at all.

What you need to do is write a complete and minimal example that shows your code and what you are doing wrong so someone can point out what you did wrong.
Title: Re: window.draw() does not work inside of 'if' statements
Post by: DraGun on August 07, 2015, 04:42:30 pm
So let me get this straight, you think I'm assuming that it's your fault that it's broken? Even if that was remotely possible, that doesn't make any sense at all? Why would I just start attacking you? Oh wait, I wasn't.

What you need to do, is not be a complete and total jerk, and realize that I'm asking for help, not starting an attack campaign.
Title: Re: window.draw() does not work inside of 'if' statements
Post by: kitteh-warrior on August 07, 2015, 04:49:03 pm
Maybe if you supplied a minimal and complete example, someone could help you. ::)
Title: Re: window.draw() does not work inside of 'if' statements
Post by: zsbzsb on August 07, 2015, 04:51:32 pm
Sorry for being sarcastic, but when you write a post that basically says the following....

(click to show/hide)

doesn't work purely on the fact that there is a branch statement makes no sense whatsoever. So if you believe (according to your post) that any branch statement causes drawing to fail then you clearly did something wrong and are asking for a 'jerk' response. But as I already said, unless you provide code that fails to work with branching statements we can't do anything to help you.

Oh, and for the record: I wasn't attacking you but merely pointing out your flawed reasoning. Following your post you assumed that whenever draw() is used within an 'if' or 'else' statement (branching) that the draw would just fail purely based on the fact it was used with a branch statement.

https://en.wikipedia.org/wiki/Deductive_fallacy
Title: Re: window.draw() does not work inside of 'if' statements
Post by: Hapax on August 07, 2015, 04:52:25 pm
My 'guess' would be you're doing this:
if (something)
    window.draw(someObject)
else
    window.draw(someOtherObject)
// ...
window.clear();

Looks like zsbzsb beat me to referencing the clear, draw, display...

The above mistake commonly happens when people draw things based on events.
Title: Re: window.draw() does not work inside of 'if' statements
Post by: DraGun on August 07, 2015, 04:59:36 pm
Well, I figured the first post made it obvious. It's a simple problem, not some complex custom code. The examples you've given is what I'm doing. If you want something specific here:


if (searching == true)
     window.draw(searchText);

 

Whether or not the variable is true, it doesn't draw the text, or rectangle, or sprite, or whatever. My code clears every frame. The trick I've been forced to use to get around this problem is just move the object off screen. It would be a lot simpler if the 'if' statements would work, and would save a couple hundred lines of code.
Title: Re: window.draw() does not work inside of 'if' statements
Post by: Hapax on August 07, 2015, 05:06:43 pm
The examples you've given is what I'm doing.
So, you've fixed the problem now?

if (searching == true)
     window.draw(searchText);
Whether or not the variable is true, it doesn't draw the text
That's not true. If searching is true, it will draw searchText.
Is it possible that you've mistyped and the code you use is: if (searching = true)
This is assignment. To avoid this error (if you find you do it often), you could try it the other way around e.g. if (true == searching) or just use
if (searching)
Title: Re: window.draw() does not work inside of 'if' statements
Post by: zsbzsb on August 07, 2015, 05:07:34 pm
What you need to do is write a complete and minimal example that shows your code and what you are doing wrong so someone can point out what you did wrong.

Maybe if you supplied a minimal and complete example, someone could help you. ::)

READ ME: (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368

(click to show/hide)