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

Author Topic: Problem: returning control to command line after closing window  (Read 4267 times)

0 Members and 1 Guest are viewing this topic.

2767210869

  • Newbie
  • *
  • Posts: 3
    • View Profile
EDIT: I had originally posted this in the Window forum. But I moved it after realising that I'm using the Graphics package (not the Window package) and I think that it is more likely that there is a general solution to this kind of problem.

I'm trying to write a program for manually recording some data. Some of the entry is in the command line with a basic menu system, but I'm using SFML for pop-up windows to record location data.
The problem is that when the window closes, the cursor doesn't return to the command line automatically. I have to manually move the mouse and click on the terminal before being able to enter keyboard input for the menu system on the command line. Is there a way so that this happens automatically?

I haven't been able to find any results on this topic by searching Google or this forum.
Any help or guidance would be greatly appreciated.

This is a simplified version that demonstrates the essence of the menu system and pop-up window.
#include <SFML/Graphics.hpp>
#include <iostream>

int locationCoordinates[2] = {0};

void locationWindow()
    {
    sf::RenderWindow window(sf::VideoMode(800, 600), "");
    window.setFramerateLimit(60);
    window.requestFocus();
   
    while (window.isOpen())
        {
        sf::Event event;
       
        while(window.pollEvent(event))
            {
            if (event.type == sf::Event::Closed)
                window.close();
            else if (event.type == event.MouseButtonPressed)
                {
                sf::Vector2i location = sf::Mouse::getPosition(window);
                locationCoordinates[0] = location.x;
                locationCoordinates[1] = location.y;
                window.close();
                }
            }
        window.clear(sf::Color::White);
        window.display();
        }
    }

int main()
    {
    bool done = false;
    int input = 0;
       
    while (done == false)
        {
        std::cout << "1. Locate" << std::endl;
        std::cout << "2. Exit" << std::endl;
        std::cin >> input;
        if (input == 1)
            {
            locationWindow();
            std::cout << locationCoordinates[0] << ", " << locationCoordinates[0] << std::endl;
            }
        else if (input == 2)
            done = true;
        }
       
    return 0;
    }
 

Details: SFML 2.4.2, Mac OS Sierra 10.12.6, Xcode 8.3.3

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Problem: returning control to command line after closing window
« Reply #1 on: August 26, 2017, 07:00:02 pm »
So if I understand correctly, you want window focus to revert back to the terminal after the window closes?
If the terminal was the previously focused window, I'm surprised it doesn't.

Regardless, this is going to be OS dependent.

General process would be to:
    1. Get a handle to the terminal window.
    2. Request for the focus move to the terminal window whenever you close the SFML window.

2767210869

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Problem: returning control to command line after closing window
« Reply #2 on: August 26, 2017, 08:08:59 pm »
Thanks for replying, you understand correctly. I, too, was surprised it doesn't.

I don't understand how to proceed with your two-step process.
Does 'get a handle to the terminal window' refer to a SFML function/process or a more general C/++ function/process?
I would really appreciate any more help you could give me (e.g. specific search terms or documentation)?

Hapax

  • Hero Member
  • *****
  • Posts: 3381
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem: returning control to command line after closing window
« Reply #3 on: August 27, 2017, 01:30:05 am »
SFML doesn't provide any control over a console window that is not created by it. Not automatically, anyway (I suppose it's possible for SFML to control the window if passed its handle?) Does SFML even know that the console window exists? ;)

As you said, it looks like you will need OS-specific code for this task. No idea what the Mac code would be (it seems you're using Mac) but I suppose it would be something equivalent to this Windows function.

One alternative option would be to create the window manually. That way you can control it as necessary.
In fact, if all you want is console window-style functionality, you may wish to consider using an SFML window and use Selba Ward's Console Screen, which creates a graphical console-style screen.

I also tested your example code. It (on Windows) returned to the console window automatically but only if the window was just closed using the close button whereas closing via taskbar stopped the console window being the previous window... :(
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*