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

Author Topic: How you are supposed to close the window correctly?  (Read 12264 times)

0 Members and 1 Guest are viewing this topic.

kapesu8

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
How you are supposed to close the window correctly?
« 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..

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How you are supposed to close the window correctly?
« Reply #1 on: June 18, 2012, 07:18:46 pm »
What are you talking about? :o ???

Also read this: ===== Read before posting =====

Just use what's suggested in all the examples (e.g. doc):

 #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;
 }
« Last Edit: June 18, 2012, 07:41:54 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kapesu8

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: How you are supposed to close the window correctly?
« Reply #2 on: June 19, 2012, 06:39:34 pm »
I mean that if I close the console manually it just stops responding.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How you are supposed to close the window correctly?
« Reply #3 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.
Laurent Gomila - SFML developer

kapesu8

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: How you are supposed to close the window correctly?
« Reply #4 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..

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How you are supposed to close the window correctly?
« Reply #5 on: June 20, 2012, 01:11:42 pm »
And what happens after the game loop? If your program ends cleanly the window should disppear.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How you are supposed to close the window correctly?
« Reply #6 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kapesu8

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: How you are supposed to close the window correctly?
« Reply #7 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How you are supposed to close the window correctly?
« Reply #8 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kapesu8

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: How you are supposed to close the window correctly?
« Reply #9 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How you are supposed to close the window correctly?
« Reply #10 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;
 }
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kapesu8

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: How you are supposed to close the window correctly?
« Reply #11 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..?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How you are supposed to close the window correctly?
« Reply #12 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. ???
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
Re: How you are supposed to close the window correctly?
« Reply #13 on: June 21, 2012, 02:50:27 am »
Is there still people using goto with C++ these days? :oooooooo

kapesu8

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: How you are supposed to close the window correctly?
« Reply #14 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.