Skip to content

Commit

Permalink
Removed public interface: Merged NativeContextHandle into NativeHandle.
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBanana committed Sep 4, 2023
1 parent f0e2b9b commit 08bdb68
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 87 deletions.
6 changes: 0 additions & 6 deletions include/LLGL/Platform/Android/AndroidNativeHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ struct NativeHandle
ANativeWindow* window;
};

//! Android native context handle structure.
struct NativeContextHandle
{
ANativeWindow* parentWindow;
};


} // /namespace LLGL

Expand Down
6 changes: 0 additions & 6 deletions include/LLGL/Platform/IOS/IOSNativeHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ struct NativeHandle
UIView* view;
};

//! iOS native context handle structure.
struct NativeContextHandle
{
UIView* parentView;
};


} // /namespace LLGL

Expand Down
15 changes: 8 additions & 7 deletions include/LLGL/Platform/Linux/LinuxNativeHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ namespace LLGL
//! Linux native handle structure.
struct NativeHandle
{
//! X11 display connection.
::Display* display;

//! X11 window object.
::Window window;
::XVisualInfo* visual;
};

//! Linux native context handle structure.
struct NativeContextHandle
{
::Display* display;
::Window parentWindow;
//! X11 visual information.
::XVisualInfo* visual;

//! X11 colormap object. Used internally by the OpenGL backend.
::Colormap colorMap;

//! X11 screen index.
int screen;
};

Expand Down
6 changes: 0 additions & 6 deletions include/LLGL/Platform/MacOS/MacOSNativeHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ struct NativeHandle
NSWindow* window;
};

//! MacOS native context handle structure.
struct NativeContextHandle
{
NSWindow* parentWindow;
};


} // /namespace LLGL

Expand Down
11 changes: 2 additions & 9 deletions include/LLGL/Platform/Win32/Win32NativeHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,14 @@ namespace LLGL
/**
\brief Win32 native handle structure.
\remarks This must be a POD (Plain-Old-Data) structure, so no default initialization is provided!
\see Surface::GetNativeHandle
\see WindowDescriptor::windowContext
*/
struct NativeHandle
{
HWND window;
};

/**
\brief Win32 native context handle structure.
\remarks This must be a POD (Plain-Old-Data) structure, so no default initialization is provided!
*/
struct NativeContextHandle
{
HWND parentWindow;
};


} // /namespace LLGL

Expand Down
2 changes: 1 addition & 1 deletion include/LLGL/SwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class LLGL_EXPORT SwapChain : public RenderTarget
\param[in] size Specifies the surface content size. This is only used if \c surface is null.
Otherwise, the size is determined by the content size of the specified surface (i.e. with the Surface::GetContentSize function).
\param[in] fullscreen Specifies whether to put the surface into fullscreen mode.
\param[in] windowContext Optional pointer to a NativeContextHandle structure. This is only used for desktop platforms.
\param[in] windowContext Optional pointer to a NativeHandle structure. This is only used for desktop platforms.
\see WindowDescriptor::windowContext
\see Surface::GetContentSize
\see SwitchFullscreenMode
Expand Down
14 changes: 11 additions & 3 deletions include/LLGL/WindowFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,20 @@ struct WindowDescriptor
\code
#include <LLGL/Platform/NativeHandle.h>
//...
LLGL::NativeContextHandle handle;
//handle.parentWindow = ...
windowDesc.windowContext = reinterpret_cast<const void*>(&handle);
LLGL::NativeHandle myParentWindowHandle;
myParentWindow->GetNativeHandle(&myParentWindowHandle, sizeof(myParentWindowHandle));
windowDesc.windowContext = &myParentWindowHandle;
windowDesc.windowContextSize = sizeof(myParentWindowHandle);
\endcode
*/
const void* windowContext = nullptr;

/**
\brief Specifies the size (in bytes) of the data type windowContext points to.
\remarks If windowContext is non-null, this must be equal to <code>sizeof(LLGL::NativeHandle)</code>.
\see windowContext
*/
std::size_t windowContextSize = 0;
};

/**
Expand Down
6 changes: 3 additions & 3 deletions sources/Platform/Linux/LinuxWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ void LinuxWindow::OnProcessEvents()
void LinuxWindow::OpenWindow()
{
/* Get native context handle */
auto nativeHandle = reinterpret_cast<const NativeContextHandle*>(desc_.windowContext);
if (nativeHandle)
const NativeHandle* nativeHandle = reinterpret_cast<const NativeHandle*>(desc_.windowContext);
if (nativeHandle != nullptr && desc_.windowContextSize == sizeof(NativeHandle))
{
/* Get X11 display from context handle */
display_ = nativeHandle->display;
Expand All @@ -219,7 +219,7 @@ void LinuxWindow::OpenWindow()
throw std::runtime_error("failed to open X11 display");

/* Setup common parameters for window creation */
::Window rootWnd = (nativeHandle != nullptr ? nativeHandle->parentWindow : DefaultRootWindow(display_));
::Window rootWnd = (nativeHandle != nullptr ? nativeHandle->window : DefaultRootWindow(display_));
int screen = (nativeHandle != nullptr ? nativeHandle->screen : DefaultScreen(display_));
::Visual* visual = (nativeHandle != nullptr ? nativeHandle->visual->visual : DefaultVisual(display_, screen));
int depth = (nativeHandle != nullptr ? nativeHandle->visual->depth : DefaultDepth(display_, screen));
Expand Down
6 changes: 1 addition & 5 deletions sources/Platform/MacOS/MacOSCompatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@
/* Define MacOS version specific macros */

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
# define LLGL_MACOS_AUTORELEASEPOOLBLOCK_SUPPORTED
#endif

#ifdef LLGL_MACOS_AUTORELEASEPOOLBLOCK_SUPPORTED
# define LLGL_MACOS_AUTORELEASEPOOL_OPEN @autoreleasepool {
# define LLGL_MACOS_AUTORELEASEPOOL_CLOSE }
#else
# define LLGL_MACOS_AUTORELEASEPOOL_OPEN NSAutoreleasePool* autoreleasePool = [[NSAutoreleasePool alloc] init];
# define LLGL_MACOS_AUTORELEASEPOOL_CLOSE [autoreleasePool release];
#endif // /LLGL_MACOS_AUTORELEASEPOOLBLOCK_SUPPORTED
#endif // /MAC_OS_X_VERSION_10_7

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12

Expand Down
67 changes: 49 additions & 18 deletions sources/Platform/MacOS/MacOSWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ static NSUInteger GetNSWindowStyleMask(const WindowDescriptor& desc)
wndDelegate_ { CreateNSWindowDelegate(desc) },
wnd_ { CreateNSWindow(desc) }
{
if (!desc.centered)
SetPosition(desc.position);
}

MacOSWindow::~MacOSWindow()
Expand Down Expand Up @@ -128,22 +126,40 @@ static NSUInteger GetNSWindowStyleMask(const WindowDescriptor& desc)
return GetSize(true);
}

void MacOSWindow::SetPosition(const Offset2D& position)
static void SetRelativeNSWindowPosition(NSWindow* wnd, const Offset2D& position, NSWindow* parentWnd = nullptr)
{
/* Get visible screen size (without dock and menu bar) */
NSScreen* screen = [NSScreen mainScreen];
if (parentWnd != nullptr)
{
CGPoint parentWndOrigin = parentWnd.frame.origin;

NSSize frameSize = [screen frame].size;
NSRect visibleFrame = [screen visibleFrame];
/* Set window position (inverse Y coordinate due to different coordinate space between Windows and MacOS) */
CGFloat x = static_cast<CGFloat>(position.x);
CGFloat y = [[parentWnd contentView] frame].size.height - static_cast<CGFloat>(position.y);

/* Calculate menu bar height */
CGFloat menuBarHeight = frameSize.height - visibleFrame.size.height - visibleFrame.origin.y;
[wnd setFrameTopLeftPoint:NSMakePoint(parentWndOrigin.x + x, parentWndOrigin.y + y)];
}
else
{
/* Get visible screen size (without dock and menu bar) */
NSScreen* screen = [NSScreen mainScreen];

/* Set window position (inverse Y coordinate due to different coordinate space between Windows and MacOS) */
CGFloat x = (CGFloat)position.x;
CGFloat y = frameSize.height - menuBarHeight - (CGFloat)position.y;
NSSize screenFrameSize = [screen frame].size;
NSRect screenVisibleFrame = [screen visibleFrame];

/* Calculate menu bar height */
CGFloat menuBarHeight = screenFrameSize.height - screenVisibleFrame.size.height - screenVisibleFrame.origin.y;

/* Set window position (inverse Y coordinate due to different coordinate space between Windows and MacOS) */
CGFloat x = static_cast<CGFloat>(position.x);
CGFloat y = screenFrameSize.height - menuBarHeight - static_cast<CGFloat>(position.y);

[wnd setFrameTopLeftPoint:NSMakePoint(x, y)];
}
}

[wnd_ setFrameTopLeftPoint:NSMakePoint(x, y)];
void MacOSWindow::SetPosition(const Offset2D& position)
{
SetRelativeNSWindowPosition(wnd_, position);
}

Offset2D MacOSWindow::GetPosition() const
Expand Down Expand Up @@ -288,18 +304,33 @@ static NSUInteger GetNSWindowStyleMask(const WindowDescriptor& desc)
[wnd setDelegate:wndDelegate_];
[wnd setAcceptsMouseMovedEvents:YES];
[wnd setTitle:ToNSString(desc.title.c_str())];
[wnd makeKeyAndOrderFront:nil];

/* Center window in the middle of the screen */
if (desc.centered)
[wnd center];

#if 0
/* Set window collection behavior for resize events */
if (desc.resizable)
[wnd setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary | NSWindowCollectionBehaviorManaged];
#endif

if (desc.windowContext != nullptr && desc.windowContextSize == sizeof(NativeHandle))
{
/* Add to parent window if specified */
if (NSWindow* parentWnd = reinterpret_cast<const NativeHandle*>(desc.windowContext)->window)
{
[parentWnd addChildWindow:wnd ordered:NSWindowAbove];
if (!desc.centered)
SetRelativeNSWindowPosition(wnd, desc.position, parentWnd);
}
}
else
{
/* Move this window to the front of the screen list and center if requested */
[wnd makeKeyAndOrderFront:nil];
if (desc.centered)
[wnd center];
else
SetRelativeNSWindowPosition(wnd, desc.position);
}

/* Show window */
if (desc.visible)
[wnd setIsVisible:YES];
Expand Down
2 changes: 1 addition & 1 deletion sources/Platform/MacOS/MacOSWindowDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace LLGL { class MacOSWindow; }
BOOL fullscreenMode_;
}

- (nonnull instancetype)initWithWindow:(LLGL::MacOSWindow*)window isResizable:(BOOL)resizable;
- (nonnull instancetype)initWithWindow:(nonnull LLGL::MacOSWindow*)window isResizable:(BOOL)resizable;
- (void)makeResizable:(BOOL)resizable;

- (BOOL)popResizeSignal;
Expand Down
2 changes: 1 addition & 1 deletion sources/Platform/MacOS/MacOSWindowDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@implementation MacOSWindowDelegate

- (nonnull instancetype)initWithWindow:(LLGL::MacOSWindow*)window isResizable:(BOOL)resizable
- (nonnull instancetype)initWithWindow:(nonnull LLGL::MacOSWindow*)window isResizable:(BOOL)resizable
{
self = [super init];

Expand Down
40 changes: 25 additions & 15 deletions sources/Platform/Win32/Win32Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ static DWORD GetWindowStyle(const WindowDescriptor& desc)
{
DWORD style = (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);

if (desc.windowContext != nullptr && reinterpret_cast<const NativeContextHandle*>(desc.windowContext)->parentWindow != 0)
const bool hasWindowContext =
(
desc.windowContext != nullptr &&
desc.windowContextSize == sizeof(NativeHandle) &&
reinterpret_cast<const NativeHandle*>(desc.windowContext)->window != 0
);

if (hasWindowContext)
style |= WS_CHILD;
else if (desc.borderless)
style |= WS_POPUP;
Expand Down Expand Up @@ -120,8 +127,7 @@ std::unique_ptr<Window> Window::Create(const WindowDescriptor& desc)
}

Win32Window::Win32Window(const WindowDescriptor& desc) :
contextHandle_ { 0 },
wnd_ { CreateWindowHandle(desc) }
wnd_ { CreateWindowHandle(desc) }
{
}

Expand Down Expand Up @@ -268,7 +274,11 @@ WindowDescriptor Win32Window::GetDesc() const
desc.acceptDropFiles = ((windowFlags & WM_DROPFILES) != 0);
desc.centered = (centerPoint.x == desc.position.x && centerPoint.y == desc.position.y);

desc.windowContext = (contextHandle_.parentWindow != 0 ? (&contextHandle_) : nullptr);
if (parentWnd_ != nullptr)
{
desc.windowContext = &parentWnd_;
desc.windowContextSize = sizeof(parentWnd_);
}

return desc;
}
Expand Down Expand Up @@ -357,6 +367,14 @@ void Win32Window::OnProcessEvents()
* ======= Private: =======
*/

static HWND GetNativeWin32ParentWindow(const void* nativeHandle, std::size_t nativeHandleSize)
{
if (nativeHandle != nullptr && nativeHandleSize == sizeof(NativeHandle))
return reinterpret_cast<const NativeHandle*>(nativeHandle)->window;
else
return nullptr;
}

HWND Win32Window::CreateWindowHandle(const WindowDescriptor& desc)
{
auto windowClass = Win32WindowClass::Instance();
Expand All @@ -365,16 +383,8 @@ HWND Win32Window::CreateWindowHandle(const WindowDescriptor& desc)
auto appearance = GetWindowAppearance(desc);

/* Get parent window */
HWND parentWnd = HWND_DESKTOP;

if (auto nativeContext = reinterpret_cast<const NativeContextHandle*>(desc.windowContext))
{
if (nativeContext->parentWindow)
{
parentWnd = nativeContext->parentWindow;
contextHandle_ = *nativeContext;
}
}
parentWnd_ = GetNativeWin32ParentWindow(desc.windowContext, desc.windowContextSize);
HWND parentWndOrDesktop = (parentWnd_ != nullptr ? parentWnd_ : HWND_DESKTOP);

#ifdef UNICODE
auto title = desc.title.to_utf16();
Expand All @@ -391,7 +401,7 @@ HWND Win32Window::CreateWindowHandle(const WindowDescriptor& desc)
appearance.position.y,
static_cast<int>(appearance.size.width),
static_cast<int>(appearance.size.height),
parentWnd,
parentWndOrDesktop,
nullptr,
GetModuleHandle(nullptr),
nullptr
Expand Down
4 changes: 2 additions & 2 deletions sources/Platform/Win32/Win32Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class Win32Window final : public Window
private:

WindowDescriptor desc_;
NativeContextHandle contextHandle_; // Must be initialized before "wnd_" member!
HWND wnd_ = nullptr;
HWND parentWnd_ = nullptr;
HWND wnd_ = nullptr;

};

Expand Down
2 changes: 1 addition & 1 deletion sources/Renderer/OpenGL/GLSwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ GLSwapChain::GLSwapChain(
#ifdef LLGL_OS_LINUX

/* Set up surface for the swap-chain and pass native context handle */
NativeContextHandle windowContext;
NativeHandle windowContext = {};
ChooseGLXVisualAndGetX11WindowContext(pixelFormat, windowContext);
SetOrCreateSurface(surface, desc.resolution, desc.fullscreen, &windowContext);

Expand Down
2 changes: 1 addition & 1 deletion sources/Renderer/OpenGL/GLSwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class GLSwapChain final : public SwapChain
bool SetSwapInterval(int swapInterval);

#ifdef __linux__
void ChooseGLXVisualAndGetX11WindowContext(GLPixelFormat& pixelFormat, NativeContextHandle& windowContext);
void ChooseGLXVisualAndGetX11WindowContext(GLPixelFormat& pixelFormat, NativeHandle& windowContext);
#endif

private:
Expand Down
Loading

0 comments on commit 08bdb68

Please sign in to comment.