SFML community forums

Help => General => Topic started by: two-tone- on September 17, 2013, 07:40:34 am

Title: Window refuses to draw if I don't include iostream + other issues (solved)
Post by: two-tone- on September 17, 2013, 07:40:34 am
OS: Win 7
IDE: CodeBlocks 12.11
Compiler: Defualt GCC (MinGW) for code blocks
SFML: 2.1

Well, I have two main issues.  I have a function where I set up the window and everything else called Start().  But for some reason if I don't include iostream that function is either skipped or the window is not drawn. Not sure which but I'm thinking it's the former.  Compiles just fine without iostream though

My other issue is that in Start() I have a while loop with another function in it.  Everything SFML related in that function seems to be ignored.

Compiles just fine, only run time errors are the two that I listed.


Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Ixrec on September 17, 2013, 07:49:54 am
For me it skips most of your code whether iostream is included or not, and the reason is fairly obvious as soon as I step through it with the debugger: gameState is never initialized, so the statement "if(gameState != Uninitialized) return;" always causes it to "return;", which makes the program exit before doing anything else.

Why not just define a constructor for the Game class instead of this weird Start() function that has to check if it's called twice?
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 08:11:32 am
Actually, oops.  That was a bit of left over code that I had not realized that I did not yet remove.  However, if I keep that but still include iostream it does not return.  This is rather odd. 

I did not run a debugger on it because I have yet to learn how to debug.  I'm still learning, if it was not yet obvious ^^;.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Ixrec on September 17, 2013, 08:48:25 am
The values of uninitialized variables are essentially random, so while "odd," it's not impossible.  It's just a reminder of why not to rely on undefined behavior.

If you're new to this you should consider getting an IDE of some kind like Visual Studio or Code::Blocks, since that way you get a built-in debugger and other stuff for free.
Edit: Turns out I can't read!
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 08:59:36 am
I'm already using CodeBlocks :P  My first post that started this thread states that :P

My issue is that I have no clue on how to use a debugger.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Ixrec on September 17, 2013, 09:33:32 am
This looks like a good start: http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 09:14:23 pm
That's a good way to start debugging in Code Blocks if you already know how. It doesn't tell you how to understand the output.

But enough of that.  My second problem still exists and I still have no idea as to why.  I know that the function is called as i can put a cout statement in it and it will print, but nothing SFML related works in there.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Ixrec on September 17, 2013, 09:29:48 pm
When I get rid of that line in start() the rest of the program does run and a window appears and everything.  It doesn't act like a normal window because there's no game loop and event loop, but the code that's actually there is doing what I would expect it to do.

I see that you've tried to simulate the game loop and event loop with these functions inside your class, but I don't think that's going to work.  I would ditch that design entirely and put those loops in main() where they belong.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 09:50:44 pm
There is an event loop in GameLoop().  Which is the function I have been talking about.  Why would  having a nested function like that keep all the SFML things in it from working?  If I can't have nested functions while using SFML then that seems like a serious flaw in SFML.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Ixrec on September 17, 2013, 09:55:04 pm
Of course you can have nested functions.  I'm only referring to the game loop and event loop (which aren't even supposed to be functions, by the way).  Everything else you can have inside classes.

And requiring those two loops to be out in main isn't much of a flaw or limitation because there's almost nothing else that main() should ever be doing in a windowed program.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Kojay on September 17, 2013, 10:28:28 pm
Line 9 in Game.cpp you 're creating a local mainwindow. Change it to

mainWindow.create(sf::VideoMode(800, 800), "Heros of Late - Extreme Pre Alpha");

There's nothing wrong with having the loops in class functions rather than main().

Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 10:46:11 pm
Thanks for the suggestion, but still nothing happens in the window.  It SHOULD be set to red and allow me to hit Escape to close the window when in the while loop that calls GameLoop() but it just sort of hangs.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Kojay on September 17, 2013, 10:55:00 pm
It is not a suggestion. Exactly what you describe happens when that line is changed - and the if statement above it commented out.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 11:05:25 pm
And I'm saying that I tried that and it still does not change anything.

I can't even move the window until Windows shows that it is being unresponsive.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Kojay on September 17, 2013, 11:07:20 pm
Are you using the exact sources that you posted here?
Did you clean and rebuild the project?
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 11:15:09 pm
The if statement at the beginning was removed, but other than that the sources are the exact same.  Yup, completely clean and rebuild.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: Ixrec on September 17, 2013, 11:30:14 pm
Kojay's right.  Taking the sources you posted, commenting out the uninitialized check and using create() makes the program work perfectly fine (it draws a red window I can actually move around and detects the escape key correctly).

Did you actually change Game::Start() to use create() as he suggested?
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: eXpl0it3r on September 17, 2013, 11:32:00 pm
Can you clarify what you mean with "But for some reason if I don't include iostream that function is either skipped or the window is not drawn."

What is "window" for you? And what do you understand with "draw".

As Kojay pointed out, the issue is with the locally created render window. Since it only gets created locally in the Start() function, all the clear/display function will get applied to a none existing window, thus nothing gets shown. Not sure what you did that it seemed to be still not working, but changing from
sf::RenderWindow mainWindow(sf::VideoMode(800, 800), "Heros of Late - Extreme Pre Alpha");
to
mainWindow.create(sf::VideoMode(800, 800), "Heros of Late - Extreme Pre Alpha");
should resolve the issue.

Also use the constructor for initializing variables.
Title: Re: Window refuses to draw if I don't include iostream + other issues
Post by: two-tone- on September 17, 2013, 11:44:14 pm
Ah ha, fixed it.  Simple misunderstanding on what Kojay said

I did

sf::RenderWindow mainWindow;
mainWindow.create(sf::VideoMode(800, 800), "Heros of Late - Extreme Pre Alpha");
    instead of just mainWindow.create(etc).  Oops ^^