diff --git a/src/SFML/Window/CMakeLists.txt b/src/SFML/Window/CMakeLists.txt index 9e942344b..2b02eb9d3 100644 --- a/src/SFML/Window/CMakeLists.txt +++ b/src/SFML/Window/CMakeLists.txt @@ -68,6 +68,8 @@ else() # MACOSX ${SRCROOT}/OSX/SFWindow.m ${SRCROOT}/OSX/SFWindowController.h ${SRCROOT}/OSX/SFWindowController.mm + ${SRCROOT}/OSX/SFViewController.h + ${SRCROOT}/OSX/SFViewController.mm ${SRCROOT}/OSX/VideoModeImpl.cpp ${SRCROOT}/OSX/WindowImplCocoa.hpp ${SRCROOT}/OSX/WindowImplCocoa.mm diff --git a/src/SFML/Window/OSX/SFContext.mm b/src/SFML/Window/OSX/SFContext.mm index 2479dd97b..e46687454 100644 --- a/src/SFML/Window/OSX/SFContext.mm +++ b/src/SFML/Window/OSX/SFContext.mm @@ -55,7 +55,7 @@ SFContext::SFContext(SFContext* shared) myPool = [[NSAutoreleasePool alloc] init]; // Create the context - CreateContext(shared, 0, ContextSettings(0, 0, 0)); + CreateContext(shared, VideoMode::GetDesktopMode().BitsPerPixel, ContextSettings(0, 0, 0)); // Activate the context SetActive(true); @@ -83,6 +83,7 @@ SFContext::SFContext(SFContext* shared, const WindowImpl* owner, //////////////////////////////////////////////////////////// SFContext::~SFContext() { + [myContext clearDrawable]; [myContext release]; [myPool drain]; // [A] diff --git a/src/SFML/Window/OSX/SFViewController.h b/src/SFML/Window/OSX/SFViewController.h new file mode 100644 index 000000000..4ce6a85c6 --- /dev/null +++ b/src/SFML/Window/OSX/SFViewController.h @@ -0,0 +1,49 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#import + +//////////////////////////////////////////////////////////// +/// Predefine some classes +//////////////////////////////////////////////////////////// +@class SFOpenGLView; + +//////////////////////////////////////////////////////////// +/// Implementation of WindowImplDelegateProtocol for view managment. +/// +//////////////////////////////////////////////////////////// + +@interface SFViewController : NSObject { + NSView* myView; + SFOpenGLView* myOGLView; + sf::priv::WindowImplCocoa* myRequester; +} + +-(id)initWithView:(NSView *)view; + +@end diff --git a/src/SFML/Window/OSX/SFViewController.mm b/src/SFML/Window/OSX/SFViewController.mm new file mode 100644 index 000000000..6047fa46e --- /dev/null +++ b/src/SFML/Window/OSX/SFViewController.mm @@ -0,0 +1,241 @@ +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2011 Marco Antognini (antognini.marco@gmail.com), +// Laurent Gomila (laurent.gom@gmail.com), +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it freely, +// subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; +// you must not claim that you wrote the original software. +// If you use this software in a product, an acknowledgment +// in the product documentation would be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, +// and must not be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// +//////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// +#import +#import +#include +#include + +@implementation SFViewController + + +//////////////////////////////////////////////////////// +-(id)initWithView:(NSView *)view +{ + if ((self = [super init])) { + myRequester = 0; + + // Retain the view for our own use. + myView = [view retain]; + + if (myView == nil) { + + sf::Err() + << "No view was given to initWithWindow:." + << std::endl; + + return self; + } + + // Create the view. + NSRect frame = [myView frame]; + frame.origin.x = 0; + frame.origin.y = 0; + myOGLView = [[SFOpenGLView alloc] initWithFrame:frame]; + + if (myOGLView == nil) { + + sf::Err() + << "Could not create an instance of NSOpenGLView " + << "in (SFViewController -initWithView:)." + << std::endl; + + return self; + } + + // Set the (OGL) view to the view as its "content" view. + [myView addSubview:myOGLView]; + } + + return self; +} + + +//////////////////////////////////////////////////////// +-(void)dealloc +{ + [self closeWindow]; + + [myView release]; + [myOGLView release]; + + [super dealloc]; +} + + +//////////////////////////////////////////////////////// +-(void)setRequesterTo:(sf::priv::WindowImplCocoa *)requester +{ + // Forward to the view. + [myOGLView setRequesterTo:requester]; + myRequester = requester; +} + + +//////////////////////////////////////////////////////// +-(sf::WindowHandle)getSystemHandle +{ + return myView; +} + + +//////////////////////////////////////////////////////// +-(void)changeTitle:(NSString *)title +{ + sf::Err() << "Cannot change the title of the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)enableKeyRepeat +{ + [myOGLView enableKeyRepeat]; +} + + +//////////////////////////////////////////////////////// +-(void)disableKeyRepeat +{ + [myOGLView disableKeyRepeat]; +} + + +//////////////////////////////////////////////////////// +-(void)hideMouseCursor +{ + [NSCursor hide]; +} + + +//////////////////////////////////////////////////////// +-(void)showMouseCursor +{ + [NSCursor unhide]; +} + + +//////////////////////////////////////////////////////// +-(void)hideWindow +{ + [myView setHidden:YES]; +} + + +//////////////////////////////////////////////////////// +-(void)showWindow +{ + [myView setHidden:NO]; +} + + +//////////////////////////////////////////////////////// +-(void)closeWindow +{ + sf::Err() << "Cannot close the view." << std::endl; + [self setRequesterTo:0]; +} + + +//////////////////////////////////////////////////////// +-(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y +{ + if (myRequester == 0) return; + + // Create a SFML event. + myRequester->MouseMovedAt(x, y); + + // Flip for SFML window coordinate system + y = NSHeight([[myView window] frame]) - y; + + // Adjust for view reference instead of window + y -= NSHeight([[myView window] frame]) - NSHeight([myOGLView frame]); + + // Convert to screen coordinates + NSPoint screenCoord = [[myView window] convertBaseToScreen:NSMakePoint(x, y)]; + + // Flip screen coodinates + float const screenHeight = NSHeight([[[myView window] screen] frame]); + screenCoord.y = screenHeight - screenCoord.y; + + CGDirectDisplayID screenNumber = (CGDirectDisplayID)[[[[[myView window] screen] deviceDescription] valueForKey:@"NSScreenNumber"] intValue]; + + // Place the cursor. + CGDisplayMoveCursorToPoint(screenNumber, CGPointMake(screenCoord.x, screenCoord.y)); + /* + CGDisplayMoveCursorToPoint -- Discussion : + + No events are generated as a result of this move. + Points that lie outside the desktop are clipped to the desktop. + */ +} + + +////////////////////////////////////////////////////////. +-(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y +{ + sf::Err() << "Cannot move the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)resizeTo:(unsigned int)width by:(unsigned int)height +{ + NSRect frame = NSMakeRect([myView frame].origin.x, + [myView frame].origin.y, + width, + height); + + [myView setFrame:frame]; +} + + +//////////////////////////////////////////////////////// +-(void)setIconTo:(unsigned int)width + by:(unsigned int)height + with:(sf::Uint8 const *)pixels +{ + sf::Err() << "Cannot set an icon to the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)processEventWithBlockingMode:(BOOL)block +{ + sf::Err() << "Cannot process event from the view." << std::endl; +} + + +//////////////////////////////////////////////////////// +-(void)applyContext:(NSOpenGLContext *)context +{ + [myOGLView setOpenGLContext:context]; + [context setView:myOGLView]; +} + + +@end diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index dd6b81834..4aa87332b 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -88,14 +88,14 @@ sf::Err() << "Could not create an instance of NSOpenGLView " - << "in (SFWindowController -initWithMode:andStyle:)." + << "in (SFWindowController -initWithWindow:)." << std::endl; return self; } // Set the view to the window as its content view. - [myWindow setContentView:myOGLView]; + [[myWindow contentView] addSubview:myOGLView]; } return self; @@ -434,7 +434,7 @@ * for more information. */ sf::Err() - << "Cannot fetch event from a worker thread. (OS X limitation)" + << "Cannot fetch event from a worker thread. (OS X restriction)" << std::endl; return; diff --git a/src/SFML/Window/OSX/WindowImplCocoa.mm b/src/SFML/Window/OSX/WindowImplCocoa.mm index 86180c08b..61cfbb468 100644 --- a/src/SFML/Window/OSX/WindowImplCocoa.mm +++ b/src/SFML/Window/OSX/WindowImplCocoa.mm @@ -30,8 +30,7 @@ #include #import -//#import -#warning SFViewController not yet implemented. +#import #import namespace sf @@ -45,9 +44,6 @@ namespace priv //////////////////////////////////////////////////////////// WindowImplCocoa::WindowImplCocoa(WindowHandle handle) { - sf::Err() << "Not yet fully supported." << std::endl; -#warning WindowImplCocoa(WindowHandle handle) not yet fully implemented - SetUpPoolAndApplication(); // Treat the handle as it real type @@ -56,13 +52,21 @@ WindowImplCocoa::WindowImplCocoa(WindowHandle handle) // We have a window. myDelegate = [[SFWindowController alloc] initWithWindow:nsHandle]; + + // Don't forget to update our parent (that is, WindowImpl) size : + myWidth = [[nsHandle contentView] frame].size.width; + myHeight = [[nsHandle contentView] frame].size.height; - } /*else if ([nsHandle isKindOfClass:[NSView class]]) { + } else if ([nsHandle isKindOfClass:[NSView class]]) { // We have a view. myDelegate = [[SFViewController alloc] initWithView:nsHandle]; - } */ else { + // Don't forget to update our parent (that is, WindowImpl) size : + myWidth = [nsHandle frame].size.width; + myHeight = [nsHandle frame].size.height; + + } else { sf::Err() << "Cannot import this Window Handle because it is neither "