• 13
• 14
• 27
• 9
• 9

# [C++] Cross-Platform Window

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

## Recommended Posts

Hey guys. I'm working on an engine and I want to bind a context to a window from two different classes. But it's cross-platform. So instead of having multiple functions, I want to somehow be able to send data to the createContext() function and have it decide what type of Window that is. I was thinking of doing it with void* but with Xlib there's XWindow and XDisplay which I need to parse. So instead of having createContext(HWND*) and createContext(XWindow*, XDisplay*), how would I do something like this?

##### Share on other sites
you can create a parent GenericRenderWindow and derive Win32Window, XWindow, GTKWindow, QTWindow.

Example of usage:

void RenderFunc() {world->Render(TimeLine::GetTime());}int WINAPI WinMain(...) {RenderWindow * wnd = new Win32Window("Engine Test - Win32Window", 800, 600, true /*fullscreen*/);return wnd->Loop(RenderFunc);}int main() {RenderWindow * wnd = new XWindow("Engine Test - XWindow", 800, 600, true);return wnd->Loop(RenderFunc);}

##### Share on other sites
I already have that kind of thing, I just want to keep the contexts seperate so I can have different renderers.

##### Share on other sites
well u can define a new struct like

enum E_WINDOW_TYPE
{
EWT_WINDOWS,
EWT_XWINDOW
};
struct SWindowData
{
virtual ~SWindowData(void){}
E_WINDOW_TYPE Type;
};

then

struct WindowsWindowData : public SWindowData
{
WindowsWindowData(void)
{
Type = EWT_WINDOWS;
}
HWND* Window;
};

struct XWindowWindowData : public SWindowData
{
XWindowWindowData(void)
{
Type = EWT_XWINDOW;
}
XWindow* Window;
XDisplay* Display;
};

void* createContext(SWindowData* data)
{
//now u can either decide by type what u want to cast or use dynamic_cast<>
switch(data->Type)
{
case EWT_WINDOWS:
//do windows stuff
break;
case EWT_XWINDOW:
//do xwindow stuff
break;
}
//or
if (dynamic_cast<XWindowWindowData*>(data))
//do xwindow stuff
else if (dynamic_cast<WindowsWindowData*>(data))
//do windows stuff

}

##### Share on other sites
That's exactly what's needed, but in a different manner.

##### Share on other sites
You don't need any of this, or at least you only need a limited subset; at compile time you know which windows types you want to create so you select then.

Example;
//headerstruct WindowData{#ifdef WINDOWSHWND targetWindow;#elseXWindow *window;XDisplay *display;}void createContext(WindowData &windowData);

Then two cpp files (or three if you have common setup which doesn't rely on the platform specific content of WindowData

//Win32.cppvoid createContext(WindowData &windowData){  // do stuff with targetWindow here}

//XWin.cppvoid createContext(WindowData &windowData){  // do stuff with window and display here}

Using your build system you select between them at compile time; in your Win32 build you compile the Win32 version and in your X build you compile the XWin version.

No runtime selection is required, nor given the differences does it make sense to do so, instead things are nicely packaged away in their own files with a 3rd 'common' file if needed.

(My own old windowing solution did basically this method; generic header files and common code in it own C++ file, platform specific stuff which implimented the functions from the header file in their own C++ file.)

##### Share on other sites
Yeah but it gets complicated when I want to add an SDL device as an alternative for support reasons.

##### Share on other sites
Then why not use SDL from the start? It handles all this for you anyway...

In short; why are you doing this? what do you NEED it to do?

No 'what if..' or 'I might..'; thats just hand wavy rubbish and comes under YAGNI.

What you are doing and above all WHY you are doing it.