Method to get HWND from abstract window interface

Started by
2 comments, last by Oxyacetylene 18 years, 7 months ago
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.
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 WIN32typedef IWindow<HWND> INativeWindow;#endif//Then do the same for linux
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;
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.hclass 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.

This topic is closed to new replies.

Advertisement