/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/



#ifndef OSGVIEWER_PIXELBUFFERCOCOA
#define OSGVIEWER_PIXELBUFFERCOCOA 1

#ifdef __APPLE__

#include <osg/GraphicsContext>
#include <osgViewer/Export>


#ifdef __OBJC__
@class NSOpenGLContext;
#else
class NSOpenGLContext;
#endif

namespace osgViewer
{

class OSGVIEWER_EXPORT PixelBufferCocoa : public osg::GraphicsContext
{
    public:
		struct Implementation;

        PixelBufferCocoa(osg::GraphicsContext::Traits* traits):
			_valid(false),
			_initialized(false),
            _realized(false),
            _context(NULL)
		{
            _traits = traits;

            init();
            
            if (valid())
            {
                setState( new osg::State );
                getState()->setGraphicsContext(this);

                if (_traits.valid() && _traits->sharedContext)
                {
                    getState()->setContextID( _traits->sharedContext->getState()->getContextID() );
                    incrementContextIDUsageCount( getState()->getContextID() );   
                }
                else
                {
                    getState()->setContextID( osg::GraphicsContext::createNewContextID() );
                }

            }
        }
    
        virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const PixelBufferCocoa*>(object)!=0; }
        virtual const char* libraryName() const { return "osgViewer"; }
        virtual const char* className() const { return "PixelBufferCarbon"; }

        virtual bool valid() const { return _valid; }

        /** Realise the GraphicsContext.*/
        virtual bool realizeImplementation();

        /** Return true if the graphics context has been realised and is ready to use.*/
        virtual bool isRealizedImplementation() const { return _realized; }

        /** Close the graphics context.*/
        virtual void closeImplementation();

        /** Make this graphics context current.*/
        virtual bool makeCurrentImplementation();

        /** Make this graphics context current with specified read context implementation. */
        virtual bool makeContextCurrentImplementation(osg::GraphicsContext* readContext);

        /** Release the graphics context.*/
        virtual bool releaseContextImplementation();

        /** Bind the graphics context to associated texture implementation.*/
        virtual void bindPBufferToTextureImplementation( GLenum buffer );

        /** Swap the front and back buffers.*/
        virtual void swapBuffersImplementation();
        
         NSOpenGLContext* getContext() { return _context; }
    public:
    

    protected:
		

    
        ~PixelBufferCocoa();
    
        void init();

        bool            _valid;
        bool            _initialized;
        bool            _realized;  
		NSOpenGLContext* _context;
};

}

#endif

#endif
