From 3f61214571a2a3d950692ef6692d8314ef5eae00 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Mon, 26 May 2014 15:31:45 +0200 Subject: [PATCH] Improved OS X implementation It makes sure the notifications sent to SFOpenGLView are only from its window. --- src/SFML/Window/OSX/SFOpenGLView.h | 10 ++++ src/SFML/Window/OSX/SFOpenGLView.mm | 61 +++++++++++++---------- src/SFML/Window/OSX/SFViewController.mm | 2 + src/SFML/Window/OSX/SFWindowController.mm | 2 + 4 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/SFML/Window/OSX/SFOpenGLView.h b/src/SFML/Window/OSX/SFOpenGLView.h index 20518948d..f65da562b 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 deda683a3..d08c63ca9 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 71004addd..05e2cccf8 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 b4ddc3e06..4f6473c94 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; }