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

Author Topic: Get win32 window handle (HWND) *SOLVED*  (Read 21402 times)

0 Members and 1 Guest are viewing this topic.

iiingo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« on: October 13, 2007, 05:11:11 pm »
I've been hacking a bit in the code now but i can't seem to figure out a nice clean way to extract the window handle from a sf::RenderWindow, can someone recommend a good way to do this?

I need the HWND for OIS ( Object Oriented Input System ).

Cheers!

/ iiingo

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get win32 window handle (HWND) *SOLVED*
« Reply #1 on: October 13, 2007, 05:40:49 pm »
You have to dive a little bit into the sf::Window class, and then into the sf::WindowImpl class. But it should be very easy to get the OS-specific window identifier. Each sf::WindowImpl derived class (one for each OS) stores its window id, so you should be able to return it with a virtual function. There is even a portable type for it : sf::WindowHandle.

But, why do you need OIS ? If something in OIS is better than in SFML I should really think about adding it.
Laurent Gomila - SFML developer

iiingo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #2 on: October 13, 2007, 06:04:50 pm »
Ok I solved it, and here's the portable solution:

First make a getter for the window handle that is already stored in
sf::priv::WindowImplWin32 as myHandle:

FILE: src\SFML\Window\Win32\WindowImplWin32.hpp:
Code: [Select]

public:
WindowHandle GetWindowHandle( void ) const;


FILE: src\SFML\Window\Win32\WindowImplWin32.cpp:
Code: [Select]

WindowHandle WindowImplWin32::GetWindowHandle( void ) const {
return myHandle;
}


Now to not break the Linux/OSX portability we must add this functionallity to the other window creation methods as well:

WindowImplCarbon
FILE: src\SFML\Window\OSX\WindowImplCarbon.hpp:
Code: [Select]

public:
WindowHandle GetWindowHandle( void ) const;


FILE: src\SFML\Window\OSX\WindowImplCarbon.cpp:
Code: [Select]

WindowHandle WindowImplCarbon::GetWindowHandle( void ) const {
return myHandle;
}


WindowImplX11
FILE: src\SFML\Window\Linux\WindowImplX11.hpp:
Code: [Select]

public:
WindowHandle GetWindowHandle( void ) const;


FILE: src\SFML\Window\Linux\WindowImplX11.cpp:
Code: [Select]

WindowHandle WindowImplX11::GetWindowHandle( void ) const {
return NULL; // Or is there some kind of WindowHandle on Linux?
}


Then change the interface sf::priv::WindowImpl so that we can access the window handle from sf::Window later on.
FILE: src\SFML\Window\WindowImpl.hpp
Code: [Select]

public:
virtual WindowHandle GetWindowHandle( void ) const  = 0;


Now add the getter to sf::Window that just calls the other getters:
FILE: src\SFML\Window\Window.hpp
Code: [Select]

WindowHandle GetWindowHandle() const;


FILE: src\SFML\Window\Window.cpp
Code: [Select]

WindowHandle Window::GetWindowHandle() const {
return myWindow->GetWindowHandle();
}


Well.. now it's possible to access the window handle from a sf::Window, but probably you'll want to be able to do sf::RenderWindow->GetWindowHandle() so lets do our last hack:
( sf::RenderWindow uses private inheritance from sf::Window so it will not be possible to just call GetWindowHandle() from the base class ).

FILE: src\SFML\Graphics\RenderWindow.hpp
Code: [Select]

public:
WindowHandle GetWindowHandle(void);


FILE: \src\SFML\Graphics\RenderWindow.cpp
Code: [Select]

WindowHandle RenderWindow::GetWindowHandle( void ) const {
return Window::GetWindowHandle();
}


That's it! now recompile sfml, if you're on Linux or Mac you must add the GetWindowHandle() method to the other window creation methods to get this code to work.

/ iiingo

iiingo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #3 on: October 13, 2007, 06:21:45 pm »
Quote from: "Laurent"
You have to dive a little bit into the sf::Window class, and then into the sf::WindowImpl class. But it should be very easy to get the OS-specific window identifier. Each sf::WindowImpl derived class (one for each OS) stores its window id, so you should be able to return it with a virtual function. There is even a portable type for it : sf::WindowHandle.

But, why do you need OIS ? If something in OIS is better than in SFML I should really think about adding it.


Thanks for the quick answer, you must have posted while I was writing my post :D.

Well.. the main reason is that i've been using OIS before and it's got all possible functionality i've ever needed besides for rumble for Xbox360 controllers but if i recall right only XInput handles that at the moment and that's probably not a concidence heh ;).

At first i didn't dig deep enough in SFML to notice that you're only missing a few defines to be able to use all keys either so i guess i could go with SFML's sf::Input too by only adding a few defs (i need all the keys on the keyboard for my network chat, the SHIFT keys for example) but now i've got OIS working anyways ^^.
Please add all the possible keys to:
Code: [Select]

Key::Code WindowImplWin32::VirtualKeyCodeToSF(WPARAM VirtualKey)

If you've got time :D.

Great design on the lib btw!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get win32 window handle (HWND) *SOLVED*
« Reply #4 on: October 14, 2007, 12:32:13 pm »
Your solution is the good one, except that you should return a sf::WindowHandle instead of HWND to keep portability.

I know some keys are missing, I'll fix it as soon as possible, that's not a problem ;)
Laurent Gomila - SFML developer

iiingo

  • Newbie
  • *
  • Posts: 4
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #5 on: October 14, 2007, 02:30:37 pm »
Quote from: "Laurent"
Your solution is the good one, except that you should return a sf::WindowHandle instead of HWND to keep portability.

I know some keys are missing, I'll fix it as soon as possible, that's not a problem ;)


I will edit my code and the post :D.

Great! Thanks for a very nice API btw, I will post my project in the projects section soon, it's a 4 player network coop shooter, the network code is almost done, the GUI system is almost done and the gameplay code is maby 40% done :D.

jorrit5477

  • Newbie
  • *
  • Posts: 2
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #6 on: April 28, 2009, 11:26:41 am »
Hi,

I am new to this forum, so at the very first I would like to say thank you for this wonderful and slim API. I am running some tests for an application currently at the start of development at the company I am working at.

I got a small framework build around SFML for handling events (frame events, window events and input events). Since I am already familiar with OIS for input and I like it, I would like to use the OIS library.

The solution mentioned above works really well, and since it is not breaking the API, could this be added to the library? If needed I could supply a patch for sfml2 (I am using this version). I noticed the WindowImpl class defines GetHandle, but it doesn't seem accessible through Window nor RenderWindow.

Cheers!
Jorrit

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get win32 window handle (HWND) *SOLVED*
« Reply #7 on: April 28, 2009, 01:59:02 pm »
You're right, it's not publicly accessible.
But I'm still not sure whether I should add it or not ;)
Laurent Gomila - SFML developer

jorrit5477

  • Newbie
  • *
  • Posts: 2
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #8 on: April 28, 2009, 06:29:38 pm »
It doesn't break anything, only a couple of extra lines in Window.hpp/cpp, so I think you should ;)

geolog

  • Newbie
  • *
  • Posts: 1
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #9 on: September 25, 2009, 05:53:05 am »
Sorry for bumping this up.

I am relatively new in the field, and is currently working on a similar project that requires the OIS to interact with SFML as well. However, the case is in Linux platform. Just wondering: if I am to implement such a feature, how should I exactly export the handle to OIS?

Any inspirations? Or am I on the wrong track? Thanks.  :)

petersvp

  • Newbie
  • *
  • Posts: 6
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #10 on: July 05, 2010, 10:19:02 pm »
Damn, this nasty not finished Alt+tab-while-fullscreen support...

I do NOT want to modify SFML!
And I can not make PROPERLY working alt-tab-screen-resulutuin-reset function. Alt+tab SHOULD reset the video mode for user to browse web and chat, and restoring game should set it back...

Radan

  • Newbie
  • *
  • Posts: 2
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #11 on: July 13, 2010, 11:15:32 pm »
Is there any chance this fix will get into SFML 2?

I would really like to use SFML for window management (as well as network and input) cause it cleans up my code nicely but I really need to extract the WindowHandle from it.

I am making a hobby game and I am using DirectX. At some point I plan to use OpenGL as well for non windows platforms but for DX I am forced to modify SFML source every time I upgrade the version. If this very simple functionality would be added in SFML 2.0 that would really open a new range of possibilities for application of sf:Window.

So, any chance?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get win32 window handle (HWND) *SOLVED*
« Reply #12 on: July 14, 2010, 11:13:46 am »
Quote
So, any chance?

Maybe... :wink:

But using SFML to create a DirectX window is not the best choice, almost everything in sf::Window is based on OpenGL.
Laurent Gomila - SFML developer

Radan

  • Newbie
  • *
  • Posts: 2
    • View Profile
Get win32 window handle (HWND) *SOLVED*
« Reply #13 on: July 14, 2010, 07:19:48 pm »
Quote from: "Laurent"
But using SFML to create a DirectX window is not the best choice, almost everything in sf::Window is based on OpenGL.



Is anybody already aware of any problems I might encounter that make this combination (D3D + SFML) unusable? Could I be taking a dead end path?


Basically, I did not start with SFML and then decide on DX instead of GL. I pretty much started with DX since I have much more experience with it and then discovered SFML. I really like the SFML library but I prefer D3D to OpenGL. I would just like to use sf::Window to clean up my window creation and manipulation code and I have always planned on keeping all of the rendering firmly in my own code, going straight to D3D/GL. Moreso since I plan to use all of the other SFML libraries (Input, Sound, Network) as the only solution for those segments.

Would that be ok or am I missing something from the picture?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get win32 window handle (HWND) *SOLVED*
« Reply #14 on: July 14, 2010, 08:52:25 pm »
This *should* work, but:
- your window will have an OpenGL context created for nothing
- the WindowSettings won't work
- UseVerticalSync will not work
- SetActive and Display mustn't be used

Moreover, I don't know if the fact that an OpenGL context is attached to the window may conflict with your D3D stuff.
Laurent Gomila - SFML developer