Sign in to follow this  

[C++] Cross-Platform Window

This topic is 2848 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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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;

//header
struct WindowData
{
#ifdef WINDOWS
HWND targetWindow;
#else
XWindow *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.cpp
void createContext(WindowData &windowData)
{
// do stuff with targetWindow here
}




//XWin.cpp
void 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 2848 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this