Jump to content
  • Advertisement
Sign in to follow this  
CTar

Method to get HWND from abstract window interface

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make my game engine as cross-platform as possible, therefore it is not acceptable to pass around HWNDs or other platform-specific types. I have therefore created an interface( iWindow ), then a factory returns a pointer to a iWindow object (which really is cWin32Window or some other derived type). The problem is that APIs like Direct3D expects an HWND object, which I cant provide since the iWindow interface doesn't know of HWNDs. Can anyone think of a way to get a HWND object, but still dont tell the client code about classes like cWin32Window? I thought it might be possible somehow with templates, something like this: template<typename T> virtual T& GetNativeWindow() = 0; // Called like this: TheWindow->GetNativeWindow<HWND>(); But you cant make templated functions pure-virtual.

Share this post


Link to post
Share on other sites
Advertisement
I don't know how windowing works under linux, but couldn't you have your window interface class be a template, like so?




template <class HandleType>
class IWindow
{
public:

virtual HandleType GetWindowHandle() = 0;
};

#ifdef WIN32
typedef IWindow<HWND> INativeWindow;
#endif

//Then do the same for linux


Share this post


Link to post
Share on other sites
The problem with that approach comes when you for example create a renderer interface, imagine this:


class iRenderer
{
public:
// Should we do this?
virtual eErrorCode Initialize(iWindow pWindow) = 0;
// Or this?
template<typename T>
virtual eErrorCode Initialize(iWindow<T> pWindow) = 0;
};




None of the above functions will work because in the first approach you need to specify the template argument and in the second approach you cant have virtual AND templated functions.

EDIT: Sorry I didn't read your post good enough, you could of course just do the following:
virtual eErrorCode Initialize(INativeWindow pWindow) = 0;

Share this post


Link to post
Share on other sites
You don't need to specify the type if you use a typedef, like this.


//In Window.h
#ifdef WIN32
typedef IWindow<HWND> INativeWindow;
#endif

//In Renderer.h
class IRenderer
{
public:

virtual eErrorCode Initialize(INativeWindow& pWindow) = 0;

};




There's no point in creating some kind of WindowsWindow, and LinuxWindow class for run-time polymorphism, because its not like your program compiled for windows is ever going to be run on linux.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!