diff --git a/src/SFML/Window/OSX/SFContext.hpp b/src/SFML/Window/OSX/SFContext.hpp index 19483335..c49e482f 100644 --- a/src/SFML/Window/OSX/SFContext.hpp +++ b/src/SFML/Window/OSX/SFContext.hpp @@ -42,10 +42,18 @@ typedef NSOpenGLContext* NSOpenGLContextRef; @class NSAutoreleasePool; typedef NSAutoreleasePool* NSAutoreleasePoolRef; +@class NSOpenGLView; +typedef NSOpenGLView* NSOpenGLViewRef; + +@class NSWindow; +typedef NSWindow* NSWindowRef; + #else // If C++ typedef void* NSOpenGLContextRef; typedef void* NSAutoreleasePoolRef; +typedef void* NSOpenGLViewRef; +typedef void* NSWindowRef; #endif @@ -72,14 +80,26 @@ public: //////////////////////////////////////////////////////////// /// \brief Create a new context attached to a window /// - /// \param shared Context to share the new one with (can be NULL) - /// \param owner Pointer to the owner window - /// \param bitsPerPixel Pixel depth (in bits per pixel) + /// \param shared Context to share the new one with /// \param settings Creation parameters + /// \param owner Pointer to the owner window + /// \param bitsPerPixel Pixel depth, in bits per pixel /// //////////////////////////////////////////////////////////// - SFContext(SFContext* shared, const WindowImpl* owner, - unsigned int bitsPerPixel, const ContextSettings& settings); + SFContext(SFContext* shared, const ContextSettings& settings, + const WindowImpl* owner, unsigned int bitsPerPixel); + + //////////////////////////////////////////////////////////// + /// \brief Create a new context that embeds its own rendering target + /// + /// \param shared Context to share the new one with + /// \param settings Creation parameters + /// \param width Back buffer width, in pixels + /// \param height Back buffer height, in pixels + /// + //////////////////////////////////////////////////////////// + SFContext(SFContext* shared, const ContextSettings& settings, + unsigned int width, unsigned int height); //////////////////////////////////////////////////////////// /// \brief Destructor @@ -133,8 +153,10 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - NSOpenGLContextRef myContext; ///< OpenGL context - NSAutoreleasePoolRef myPool; ///< Memory manager for this class. + NSOpenGLContextRef myContext; ///< OpenGL context. + NSAutoreleasePoolRef myPool; ///< Memory manager for this class. + NSOpenGLViewRef myView; ///< Only for offscreen context. + NSWindowRef myWindow; ///< Only for offscreen context. }; } // namespace priv diff --git a/src/SFML/Window/OSX/SFContext.mm b/src/SFML/Window/OSX/SFContext.mm index e4668745..54d43d15 100644 --- a/src/SFML/Window/OSX/SFContext.mm +++ b/src/SFML/Window/OSX/SFContext.mm @@ -51,6 +51,7 @@ namespace priv //////////////////////////////////////////////////////////// SFContext::SFContext(SFContext* shared) + : myView(0), myWindow(0) { myPool = [[NSAutoreleasePool alloc] init]; @@ -63,8 +64,9 @@ SFContext::SFContext(SFContext* shared) //////////////////////////////////////////////////////////// -SFContext::SFContext(SFContext* shared, const WindowImpl* owner, - unsigned int bitsPerPixel, const ContextSettings& settings) +SFContext::SFContext(SFContext* shared, const ContextSettings& settings, + const WindowImpl* owner, unsigned int bitsPerPixel) + : myView(0), myWindow(0) { myPool = [[NSAutoreleasePool alloc] init]; @@ -80,16 +82,43 @@ SFContext::SFContext(SFContext* shared, const WindowImpl* owner, } +SFContext::SFContext(SFContext* shared, const ContextSettings& settings, + unsigned int width, unsigned int height) + : myView(0), myWindow(0) +{ + // Ensure the process is setup in order to create a valid window. + WindowImplCocoa::SetUpProcess(); + + myPool = [[NSAutoreleasePool alloc] init]; + + // Create the context. + CreateContext(shared, VideoMode::GetDesktopMode().BitsPerPixel, settings); + + // Create a dummy window/view pair (hidden) and asign it our context. + myWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height) + styleMask:NSBorderlessWindowMask + backing:NSBackingStoreBuffered + defer:NO]; // Don't defer it! + myView = [[NSOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, width, height)]; + [myWindow setContentView:myView]; + [myView setOpenGLContext:myContext]; + [myContext setView:myView]; + + // Activate the context + SetActive(true); +} + //////////////////////////////////////////////////////////// SFContext::~SFContext() { [myContext clearDrawable]; [myContext release]; - [myPool drain]; // [A] - /* - * [A] : Produce sometimes "*** attempt to pop an unknown autorelease pool" - */ + [myView release]; // Might be nil but we don't care. + [myWindow release]; // Idem. + + [myPool drain]; // Produce sometimes "*** attempt to pop an unknown autorelease pool" + // This is not a real issue : http://stackoverflow.com/questions/3484888/nsautoreleasepool-question } @@ -132,8 +161,8 @@ void SFContext::CreateContext(SFContext* shared, attrs.reserve(20); // max attributs (estimation). // These casts are safe. C++ is much more strict than Obj-C. - attrs.push_back(NSOpenGLPFAClosestPolicy); + attrs.push_back(NSOpenGLPFAClosestPolicy); attrs.push_back(NSOpenGLPFADoubleBuffer); if (bitsPerPixel > 24) { @@ -162,7 +191,7 @@ void SFContext::CreateContext(SFContext* shared, // Create the pixel pormat. NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:&attrs[0]]; - if(pixFmt == nil) { + if (pixFmt == nil) { sf::Err() << "Error. Unable to find a suitable pixel format." << std::endl; return; } @@ -174,6 +203,10 @@ void SFContext::CreateContext(SFContext* shared, myContext = [[NSOpenGLContext alloc] initWithFormat:pixFmt shareContext:sharedContext]; + if (myContext == nil) { + sf::Err() << "Error. Unable to create the context." << std::endl; + } + // Free up. [pixFmt release];