SFML community forums

Help => Window => Topic started by: Asbestbrezel on December 28, 2013, 10:01:55 pm

Title: [c++] give the Render-Window the Focus
Post by: Asbestbrezel on December 28, 2013, 10:01:55 pm
Hello.

I create my RenderWindow in the Constructor of my CGame-Class, which instance is created after an Input of the lives, the players shall get.

Now, the Problem is, under Windows (no Problem under Linux <3 ) the new created RenderWindow looses the focus, right after it was created. Apparently because i used cin before, if i avoid the console-input, everything works fine.

#include "CGame.hpp"
#include <iostream>

using namespace std;

int main()
{

        int lifes=0;
        do
        {
                badInput=false;
                cout << "How much Lifes do all the players get? (1 to 20):";
                cin >> lifes;
                if (cin.fail() || cin.bad())
                {
                        cin.clear();
                        while (cin.get() != '\n');
                        cout << "Bad Input, my Dear" << endl;
                        badInput=true;
                }
                if (lifes < 1 || lifes > 20)
                {
                        cout << "No Sir, wrong Number!" << endl;
                        badInput=true;
                }
        } while (badInput);
       


        CGame game(4, 2, lifes);


        game.run();

return 0;
}

CGame::CGame(int players, int humanPlayers, int lifes)
: _timePerFrame(sf::seconds(1.f/60.f))
, _window(sf::VideoMode::getDesktopMode(), "Kosmosgeballer beta - v0.1", sf::Style::Fullscreen)
, _view(sf::FloatRect(0.f,0.f,1920.f,1080.f))
{
    _gameOver=false;
    _humanPlayerCount=humanPlayers;
    _backgroundTexture.loadFromFile("Media/Textures/Background2.png");
    _backgroundImage.setTexture(_backgroundTexture);

    _window.setView(_view);

    if ((_window.getSize().x / _window.getSize().y) <= (16.f/9.f))
        _view.setViewport(sf::FloatRect(0.f, ((_window.getSize().y/2.f)-((_window.getSize().x/(16.f/9.f))/2.f))/_window.getSize().y, 1.f, (_window.getSize().x/(16.f/9.f))/_window.getSize().y));
    else _view.setViewport(sf::FloatRect(((_window.getSize().x/2.f)-((_window.getSize().y*(16.f/9.f))/2.f))/_window.getSize().x, 0.f, (_window.getSize().y*(16.f/9.f))/_window.getSize().x, 1.f));

    for (int i=0; i<players; i++)
    {
        _playerlist.emplace_back (_window, i, lifes);
        _playerlist.back().reset(_playerlist, 0);
        if (i >= humanPlayers) _mutterlist.emplace_back (i, _playerlist);
    }
}

What can i do, to prevent the Window from loosing the focus or how can i give it back to it?

Thanks for your Help.
Title: Re: [c++] give the Render-Window the Focus
Post by: eXpl0it3r on December 29, 2013, 12:53:57 am
Currently there's no way to absolutely track or force focus. For your specific problem you'd essentially need a way to force a focus to the window, because regardless whether it works on your Linux system, the decision whether or not the window gets focus is being made by the window manager, which can vary, especially on Linux.
But keep in mind, you're not alone! There have already been similar requests, e.g. here (http://en.sfml-dev.org/forums/index.php?topic=13658.0) or here (http://en.sfml-dev.org/forums/index.php?topic=6623.0). Nobody however took their time to open a ticket on the issue tracker (as far as I know) - so feel free to do so.

There are only two solutions I see at the moment:
Title: Re: [c++] give the Render-Window the Focus
Post by: Asbestbrezel on December 29, 2013, 03:47:16 pm
But keep in mind, you're not alone! There have already been similar requests, e.g. here (http://en.sfml-dev.org/forums/index.php?topic=13658.0) or here (http://en.sfml-dev.org/forums/index.php?topic=6623.0). Nobody however took their time to open a ticket on the issue tracker (as far as I know) - so feel free to do so.

Done!  ;)