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

Author Topic: [c++] give the Render-Window the Focus  (Read 2983 times)

0 Members and 1 Guest are viewing this topic.

Asbestbrezel

  • Newbie
  • *
  • Posts: 2
    • View Profile
[c++] give the Render-Window the Focus
« 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.
« Last Edit: December 29, 2013, 01:38:28 pm by Asbestbrezel »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: [c++] give the Render-Window the Focus
« Reply #1 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 or here. 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:
  • Use a different way for user input (recommended anyways).
  • Call the W32 API directly.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Asbestbrezel

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [c++] give the Render-Window the Focus
« Reply #2 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 or here. 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!  ;)