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

Author Topic: Using WindowHandle to get window input  (Read 2931 times)

0 Members and 1 Guest are viewing this topic.

chisser98

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Using WindowHandle to get window input
« on: December 28, 2012, 04:30:14 pm »
Hey all,

I'm using SFML in a graphics engine as the window manager.  In my game engine, I get the WindowHandle from the graphics engine, and then use it to create another SFML window in the game engine.  I use this new window to handle events.  (Basically, I wanted the graphics engine to handle graphics, and the game engine to handle input).

Unfortunately, the new window doesn't seem to be receiving any events.  Also, if I call isOpen() on it, it returns false.  In the graphics engine, I can loop through the events on the window and it works fine.  However, if I don't loop through events in the graphics engine, and instead try to loop through events in the game engine, it doesn't seem to have any events there.

Here's what I call in my game engine:

Quote
...
sf::WindowHandle winHdl = window_->getWindowHandle(); // this gets and returns the sf::WindowHandle from my graphics engine
   
sf::Window sfmlWindow_(winHdl); // send the window handle to a new sfml window
...

In my 'handle events' method (which is called every time we go through the main game loop):

Quote
void Game::handleEvents() {
   sf::Event event;

   if (sfmlWindow_.isOpen() == true)
      std::cout << "IS OPEN: " << (sfmlWindow_.isOpen() == true) << std::endl;

   // while there are pending events...
   while (sfmlWindow_.pollEvent(event)) {
      std::cout << "event type: " << event.type << std::endl;
      
       // check the type of the event...
       switch (event.type) {
           // window closed
           case sf::Event::Closed:
               //window.close();
               break;
   
           // key pressed
           case sf::Event::KeyPressed:
               //...
               break;
   
           // we don't process other types of events
           default:
            receiveKeyboardEvent(event);
            receiveMouseEvent(event);
               break;
       }
   }
}

Anyone have any idea what could be going on?

Cheers

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Using WindowHandle to get window input
« Reply #1 on: December 28, 2012, 05:03:47 pm »
If you want the events then just pass the window reference.
Handle is supposed to be typedef of native os dependant handle type that you can use to work with os to create sfml's opengl context in already existing window using it's handle returned by some os function or do os specific magic with the sfml window using the handle that you pass to os functions.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Using WindowHandle to get window input
« Reply #2 on: December 28, 2012, 07:33:46 pm »
Please make use of the code=cpp tag!

And from where do you get the window handle? I mean is it an actual valid window handle or just some random numbers you came up with? ;)

A nice solution would also be to just pass the polled events to the input handler instead of the whole window.

Also a check like if(window.isOpen() == true) is completly nonsense, because you compare a boolean variable with another boolean variable, what you get in return is a boolean variable and since true is constant you'll get true if isOpen() returns true and false if isOpen() returns false, thus it's not needed and you can simply do a if(window.isOpen()).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

chisser98

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Using WindowHandle to get window input
« Reply #3 on: December 29, 2012, 02:23:27 am »
Thanks FRex, I just changed it to return a pointer to the sfml window.  Works fine now :)


edoroth

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Using WindowHandle to get window input
« Reply #4 on: June 11, 2013, 03:21:43 am »
How would you get this pointer to the sfml window? I'm not exactly sure how I should pass a reference to the window if I want to use it in another function besides the main...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Using WindowHandle to get window input
« Reply #5 on: June 11, 2013, 09:31:10 am »
void functionWithPointer(sf::RenderWindow* window);
void functionWithReference(sf::RenderWindow& window);

int main()
{
    sf::RenderWindow app(...);
    functionWithPointer(&app);
    functionWithReference(app);
}

But those are C++ basics. You should really learn C++ well enough before working with a library like SFML.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything