diff --git a/src/SFML/Window/OSX/SFOpenGLView.h b/src/SFML/Window/OSX/SFOpenGLView.h index 20518948..f65da562 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.h +++ b/src/SFML/Window/OSX/SFOpenGLView.h @@ -69,6 +69,8 @@ namespace sf { /// NB: -initWithFrame: is also implemented to default isFullscreen to NO /// in case SFOpenGLView is created with the standard message. /// +/// To finish the initialization -finishInit should be called too. +/// /// \param frameRect dimension of the view /// \param isFullscreen fullscreen flag /// @@ -77,6 +79,14 @@ namespace sf { //////////////////////////////////////////////////////////// -(id)initWithFrame:(NSRect)frameRect fullscreen:(BOOL)isFullscreen; +//////////////////////////////////////////////////////////// +/// \brief Finish the creation of the SFML OpenGL view +/// +/// This method should be called after the view was added to a window +/// +//////////////////////////////////////////////////////////// +-(void)finishInit; + //////////////////////////////////////////////////////////// /// \brief Apply the given requester to the view /// diff --git a/src/SFML/Window/OSX/SFOpenGLView.mm b/src/SFML/Window/OSX/SFOpenGLView.mm index deda683a..d08c63ca 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.mm +++ b/src/SFML/Window/OSX/SFOpenGLView.mm @@ -160,31 +160,7 @@ BOOL isValidTextUnicode(NSEvent* event); [self addTrackingArea:m_trackingArea]; m_fullscreen = isFullscreen; - [self updateScaleFactor]; - - // Register for window focus events - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(windowDidBecomeKey:) - name:NSWindowDidBecomeKeyNotification - object:[self window]]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(windowDidResignKey:) - name:NSWindowDidResignKeyNotification - object:[self window]]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(windowDidResignKey:) - name:NSWindowWillCloseNotification - object:[self window]]; - - // Register for changed screen and changed screen's profile events - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(updateScaleFactor) - name:NSWindowDidChangeScreenNotification - object:[self window]]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(updateScaleFactor) - name:NSWindowDidChangeScreenProfileNotification - object:[self window]]; + m_scaleFactor = 1.0; // Default value; it will be updated in finishInit // Create a hidden text view for parsing key down event properly m_silentResponder = [[SFSilentResponder alloc] init]; @@ -193,12 +169,47 @@ BOOL isValidTextUnicode(NSEvent* event); // Request high resolution on high DPI displays [self setWantsBestResolutionOpenGLSurface:YES]; + + // At that point, the view isn't attached to a window. We defer the rest of + // the initialization process to later. } return self; } +//////////////////////////////////////////////////////// +-(void)finishInit +{ + // Register for window focus events + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowDidBecomeKey:) + name:NSWindowDidBecomeKeyNotification + object:[self window]]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowDidResignKey:) + name:NSWindowDidResignKeyNotification + object:[self window]]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(windowDidResignKey:) + name:NSWindowWillCloseNotification + object:[self window]]; + + // Register for changed screen and changed screen's profile events + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updateScaleFactor) + name:NSWindowDidChangeScreenNotification + object:[self window]]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(updateScaleFactor) + name:NSWindowDidChangeScreenProfileNotification + object:[self window]]; + + // Now that we have a window, set up correctly the scale factor + [self updateScaleFactor]; +} + + //////////////////////////////////////////////////////// -(void)setRequesterTo:(sf::priv::WindowImplCocoa*)requester { diff --git a/src/SFML/Window/OSX/SFViewController.mm b/src/SFML/Window/OSX/SFViewController.mm index 71004add..05e2cccf 100644 --- a/src/SFML/Window/OSX/SFViewController.mm +++ b/src/SFML/Window/OSX/SFViewController.mm @@ -71,6 +71,8 @@ [m_view addSubview:m_oglView]; [m_oglView setAutoresizingMask:[m_view autoresizingMask]]; + + [m_oglView finishInit]; } return self; diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index b4ddc3e0..4f6473c9 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -149,6 +149,8 @@ [self setupFullscreenViewWithMode:mode]; else [self setupWindowWithMode:mode andStyle:style]; + + [m_oglView finishInit]; } return self; }