SFML community forums

Help => Window => Topic started by: kapesu8 on June 18, 2012, 06:43:09 pm

Title: How you are supposed to close the window correctly?
Post by: kapesu8 on June 18, 2012, 06:43:09 pm
Like if I close it straight from the console by closing the console it'll quit responding..
Title: Re: How you are supposed to close the window correctly?
Post by: eXpl0it3r on June 18, 2012, 07:18:46 pm
What are you talking about? :o ???

Also read this: ===== Read before posting ===== (http://en.sfml-dev.org/forums/index.php?topic=5559.0)

Just use what's suggested in all the examples (e.g. doc (http://www.sfml-dev.org/documentation/2.0/)):

 #include <SFML/Graphics.hpp>
 
 int main()
 {
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
 
     // Start the game loop
     while (window.isOpen())
     {
         // Process events
         sf::Event event;
         while (window.pollEvent(event))
         {
             // Close window : exit
             if (event.type == sf::Event::Closed)
                 window.close();
         }
 
         // Clear screen
         window.clear();
 
         // Update the window
         window.display();
     }
 
     return EXIT_SUCCESS;
 }
Title: Re: How you are supposed to close the window correctly?
Post by: kapesu8 on June 19, 2012, 06:39:34 pm
I mean that if I close the console manually it just stops responding.
Title: Re: How you are supposed to close the window correctly?
Post by: Laurent on June 19, 2012, 08:11:27 pm
Terminating a program by closing the console is not clean. It's like calling exit() in the middle of your program.
Title: Re: How you are supposed to close the window correctly?
Post by: kapesu8 on June 20, 2012, 01:10:17 pm
But that also happens when I close the sfml window by window.close() and jump out from the game loop..
Title: Re: How you are supposed to close the window correctly?
Post by: Laurent on June 20, 2012, 01:11:42 pm
And what happens after the game loop? If your program ends cleanly the window should disppear.
Title: Re: How you are supposed to close the window correctly?
Post by: eXpl0it3r on June 20, 2012, 06:05:04 pm
Everything works fine on my end.

I can call window.close(), std::exit() or return 0; anywhere in my code and the window closes and the application stops cleanly.

Also it could be helpfull if you specified which OS you're using.
Title: Re: How you are supposed to close the window correctly?
Post by: kapesu8 on June 20, 2012, 08:37:02 pm
I mean like I want to close the sfml window that I created and keep the console and manually close the console afterwards.
So currently it's like

main
start:
user input
variables and stuff
loop while the window is open
if the event is closed, close the window
other stuff
if the health (defined earlier) is under 0, close the window and jump to start
end the loop
end main

But if my code is something like (without sfml)
(Not so pseudo-code anymore)

int main() {
cin.get()
return 0;
}

And if I would close the console(by hand)before it reaches to return (so before I made the input) it would close normally.
That's the same thing I want to do with my sfml tests
Title: Re: How you are supposed to close the window correctly?
Post by: eXpl0it3r on June 20, 2012, 09:08:49 pm
Have you ever thought of using any of those formating buttons and dropdox boxes on top of the text editor? E.g. you could use the code tag to structure or highlight your pseudo or real code. ;)

Like I said in the first post, you should have given a better explenation in your first post. I mean read your top sentence, does it make any sense? ::)

Okay for you're problem just use std::cin.get() and be happy with it. :D
window.close() doesn't call std::exit() it's just a class that leaves it's scope and gets destroied in the process. But it doesn't jump randomly around thus killing the application 'flow'.

 #include <SFML/Graphics.hpp>
 #include <iostream>
 
 int main()
 {
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
 
     // Start the game loop
     while (window.isOpen())
     {
         // Process events
         sf::Event event;
         while (window.pollEvent(event))
         {
             // Close window : exit
             if (event.type == sf::Event::Closed)
                 window.close();
         }
 
         // Clear screen
         window.clear();
 
         // Update the window
         window.display();
     }
         
         // Do what ever you want, e.g. just wait for an input
         std::cin.get();
 
     return EXIT_SUCCESS;
 }

Btw this is something basic in C++. If you don't understand it, you should defently dig deeper into C++ by reading books. ;)
Title: Re: How you are supposed to close the window correctly?
Post by: kapesu8 on June 20, 2012, 09:24:36 pm
Ok so I want it to be something like this for example,

 #include <SFML/Graphics.hpp>
 #include <iostream>
 
 int main()
 {
     std::string Text;
     Start:
     std::cin >> Text;
     bool Retry(false);
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
     sf::Text(Text);
     // Start the game loop
     while (window.isOpen())
     {
         // Process events
         sf::Event event;
         while (window.pollEvent(event))
         {
             // Close window : exit
             if (event.type == sf::Event::Closed)
                 window.close();
         }
 
         // Clear screen
         window.clear();
         window.draw(Text);
         // Update the window
         window.display();
         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)){
         Retry = true;
         }
         if(Retry){
         window.close();
         goto Start;
         }
     }
     return EXIT_SUCCESS;
 }

But I want it to be possible to close the console at the input after I once pressed Escape
Title: Re: How you are supposed to close the window correctly?
Post by: eXpl0it3r on June 20, 2012, 09:56:37 pm
Dude DON'T USE GOTO! :o

Sorry for that, but seriously...  :-\

SFML consists out of individual modules and they do not always depend on each other. This is the case for sf::RenderWindow and sf::Keyboard. You don't need a window to catch keyboard inputs.

If I'm not completly wrong with all of this, you could just use:

 #include <SFML/Graphics.hpp>
 #include <iostream>
 
 int main()
 {
     // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
 
     // Start the game loop
     while (window.isOpen())
     {
         // Process events
         sf::Event event;
         while (window.pollEvent(event))
         {
             // Close window : exit
             if (event.type == sf::Event::Closed)
                 window.close();
         }
 
         // Clear screen
         window.clear();
 
         // Update the window
         window.display();
     }
     
     bool whatever = true;
     while(whatever)
     {
         // Do what ever you want
         if(sf::Keyboard::isPressed(sf::Keyboard::Escape))
             whatever = false;
     }
 
     return EXIT_SUCCESS;
 }
Title: Re: How you are supposed to close the window correctly?
Post by: kapesu8 on June 20, 2012, 10:39:42 pm
How would I get user input without getting the window to not responding state AKA pausing the loop..?
Title: Re: How you are supposed to close the window correctly?
Post by: eXpl0it3r on June 21, 2012, 02:20:43 am
Can't you express yourself better, I really don't get what you want...

You get user input with sf::Keyboard and sf::Mouse. ???
Title: Re: How you are supposed to close the window correctly?
Post by: Grimshaw on June 21, 2012, 02:50:27 am
Is there still people using goto with C++ these days? :oooooooo
Title: Re: How you are supposed to close the window correctly?
Post by: kapesu8 on June 21, 2012, 03:02:35 pm
I just want to get user input from the console in some points but I also want that during the user input you can close the console without getting it to not responding status.
Title: Re: How you are supposed to close the window correctly?
Post by: eXpl0it3r on June 22, 2012, 12:38:12 am
SFML doesn't provide a function to hide and show the console since this is a very OS specific task.
So you either hide it completly (you'd need to tell the linker about sfml-main(-d).lib), let stay in the background and don't hide it or write your own OS specific code (not recommended).

Non blocking input via the console doesn't work directly. You'd have to run the window and the console in seperate threads and then sync the messages between them.
Title: Re: How you are supposed to close the window correctly?
Post by: kapesu8 on June 22, 2012, 03:08:01 pm
What else than just linking sfml-main.lib would I need to do to hide it completely?
Title: Re: How you are supposed to close the window correctly?
Post by: Acrobat on June 22, 2012, 04:29:09 pm
subsystem : windows
preprocessor : change _CONSOLE to _WINDOWS
Title: Re: How you are supposed to close the window correctly?
Post by: Celtic Minstrel on June 22, 2012, 04:30:33 pm
To hide it completely and not use it at all, I think you'd need to change your build settings to tell it not to use a console. (Or perhaps what Acrobat posted, I dunno; not even sure what he means.) To just hide it temporarily... not sure, probably some Windows API call.
Title: Re: How you are supposed to close the window correctly?
Post by: Nexus on June 22, 2012, 04:49:16 pm
std::exit() [...] and the application stops cleanly.
Even if it seems to end cleanly (that is, no error message), std::exit() shouldn't be used in C++ programs because the standard doesn't require this function to perform any cleanup.

Quote from: C++ Standard 2003, ยง3.6.1/4
Calling the function
void exit(int);
declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence without
destroying any objects with automatic storage duration (12.4). If exit is called to end a program during
the destruction of an object with static storage duration, the program has undefined behavior.
Title: Re: How you are supposed to close the window correctly?
Post by: Celtic Minstrel on June 22, 2012, 04:53:30 pm
I only ever use std::exit (with non-zero argument) for exiting with an error, and not often for that; more often I use exceptions.