include/cinder/app/Renderer.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2012, The Cinder Project, All rights reserved.
00003 
00004  This code is intended for use with the Cinder C++ library: http://libcinder.org
00005 
00006  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00007  the following conditions are met:
00008 
00009     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00010     the following disclaimer.
00011     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00012     the following disclaimer in the documentation and/or other materials provided with the distribution.
00013 
00014  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00015  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00016  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00017  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00018  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00019  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00020  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00021  POSSIBILITY OF SUCH DAMAGE.
00022 */
00023 
00024 #pragma once
00025 
00026 #include "cinder/Cinder.h"
00027 #include "cinder/gl/gl.h"  // necessary to give GLee the jump on Cocoa.h
00028 #include "cinder/Surface.h"
00029 #include "cinder/Display.h"
00030 
00031 #if defined( CINDER_MAC )
00032     #include <ApplicationServices/ApplicationServices.h>
00033     #include <CoreFoundation/CoreFoundation.h>
00034     #if defined __OBJC__
00035         @class AppImplCocoaRendererQuartz;
00036         @class AppImplCocoaRendererGl;
00037         @class NSOpenGLContext;
00038         @class NSView;
00039     #else
00040         class AppImplCocoaRendererQuartz;
00041         class AppImplCocoaRendererGl;
00042         class NSOpenGLContext;
00043         class NSView;
00044     #endif
00045     typedef struct _CGLContextObject       *CGLContextObj;
00046     typedef struct _CGLPixelFormatObject   *CGLPixelFormatObj;
00047 #elif defined( CINDER_COCOA_TOUCH )
00048     #if defined __OBJC__
00049         typedef struct CGContext * CGContextRef;
00050         @class AppImplCocoaTouchRendererGl;
00051         @class AppImplCocoaTouchRendererQuartz;
00052         @class UIView;
00053         @class EAGLContext;
00054     #else
00055         typedef struct CGContext * CGContextRef;
00056         class AppImplCocoaTouchRendererGl;
00057         class AppImplCocoaTouchRendererQuartz;
00058         class UIView;
00059         class EAGLContext;
00060     #endif
00061 #endif
00062 
00063 
00064 namespace cinder { namespace app {
00065 
00066 class App;
00067 
00068 typedef std::shared_ptr<class Renderer>     RendererRef;
00069 class Renderer {
00070  public:
00071     virtual ~Renderer() {};
00072     
00073     virtual RendererRef clone() const = 0;
00074     
00075 #if defined( CINDER_COCOA )
00076     #if defined( CINDER_MAC )
00077         virtual void    setup( App *aApp, CGRect frame, NSView *cinderView, RendererRef sharedRenderer, bool retinaEnabled ) = 0;
00078         virtual CGContextRef            getCgContext() { throw; } // the default behavior is failure
00079         virtual CGLContextObj           getCglContext() { throw; } // the default behavior is failure
00080         virtual CGLPixelFormatObj       getCglPixelFormat() { throw; } // the default behavior is failure
00081     #elif defined( CINDER_COCOA_TOUCH )
00082         virtual void    setup( App *aApp, const Area &frame, UIView *cinderView, RendererRef sharedRenderer ) = 0;
00083         virtual bool    isEaglLayer() const { return false; }
00084     #endif
00085 
00086 
00087     virtual void    setFrameSize( int width, int height ) {}        
00088 
00089 #elif defined( CINDER_MSW )
00090     virtual void setup( App *aApp, HWND wnd, HDC dc, RendererRef sharedRenderer ) = 0;
00091 
00092     virtual void prepareToggleFullScreen() {}
00093     virtual void finishToggleFullScreen() {}
00094     virtual void kill() {}
00095 
00096     virtual HWND                getHwnd() = 0;
00097     virtual HDC                 getDc() { return NULL; }
00098 #endif
00099 
00100     virtual Surface copyWindowSurface( const Area &area ) = 0;
00101 
00102     virtual void startDraw() {}
00103     virtual void finishDraw() {}        
00104     virtual void makeCurrentContext() {}
00105     virtual void defaultResize() {}
00106 
00107  protected:
00108     Renderer() : mApp( 0 ) {}
00109     Renderer( const Renderer &renderer );
00110 
00111     App         *mApp;
00112 };
00113 
00114 typedef std::shared_ptr<class RendererGl>   RendererGlRef;
00115 class RendererGl : public Renderer {
00116   public:
00117 #if defined( CINDER_COCOA_TOUCH )
00118     RendererGl( int aAntiAliasing = AA_MSAA_4 );
00119 #else
00120     RendererGl( int aAntiAliasing = AA_MSAA_16 );
00121 #endif
00122     ~RendererGl();
00123 
00124     static RendererGlRef    create( int antiAliasing = AA_MSAA_16 ) { return RendererGlRef( new RendererGl( antiAliasing ) ); }
00125     virtual RendererRef     clone() const { return RendererGlRef( new RendererGl( *this ) ); }
00126  
00127 #if defined( CINDER_COCOA )
00128     #if defined( CINDER_MAC )
00129         virtual void setup( App *aApp, CGRect frame, NSView *cinderView, RendererRef sharedRenderer, bool retinaEnabled );
00130         virtual CGLContextObj           getCglContext();
00131         virtual CGLPixelFormatObj       getCglPixelFormat();
00132         virtual NSOpenGLContext*        getNsOpenGlContext();       
00133     #elif defined( CINDER_COCOA_TOUCH )
00134         virtual void    setup( App *aApp, const Area &frame, UIView *cinderView, RendererRef sharedRenderer );
00135         virtual bool    isEaglLayer() const { return true; }
00136         EAGLContext*    getEaglContext() const;
00137     #endif
00138     virtual void    setFrameSize( int width, int height );
00139 #elif defined( CINDER_MSW )
00140     virtual void    setup( App *aApp, HWND wnd, HDC dc, RendererRef sharedRenderer );
00141     virtual void    kill();
00142     virtual HWND    getHwnd() { return mWnd; }
00143     virtual void    prepareToggleFullScreen();
00144     virtual void    finishToggleFullScreen();
00145 #endif
00146 
00147     enum    { AA_NONE = 0, AA_MSAA_2, AA_MSAA_4, AA_MSAA_6, AA_MSAA_8, AA_MSAA_16, AA_MSAA_32 };
00148     static const int    sAntiAliasingSamples[];
00149     void                setAntiAliasing( int aAntiAliasing );
00150     int                 getAntiAliasing() const { return mAntiAliasing; }
00151 
00152     virtual void    startDraw();
00153     virtual void    finishDraw();
00154     virtual void    defaultResize();
00155     virtual void    makeCurrentContext();
00156     virtual Surface copyWindowSurface( const Area &area );
00157     
00158  protected:
00159     RendererGl( const RendererGl &renderer );
00160 
00161     int         mAntiAliasing;
00162 #if defined( CINDER_MAC )
00163     AppImplCocoaRendererGl      *mImpl;
00164 #elif defined( CINDER_COCOA_TOUCH )
00165     AppImplCocoaTouchRendererGl *mImpl;
00166 #elif defined( CINDER_MSW )
00167     class AppImplMswRendererGl  *mImpl;
00168     HWND                        mWnd;
00169     friend class                AppImplMswRendererGl;
00170 #endif
00171 };
00172 
00173 typedef std::shared_ptr<class Renderer2d>   Renderer2dRef;
00174 #if defined( CINDER_COCOA )
00175 class Renderer2d : public Renderer {
00176   public:
00177     Renderer2d();
00178     
00179     static Renderer2dRef    create() { return Renderer2dRef( new Renderer2d() ); }
00180     virtual RendererRef     clone() const { return Renderer2dRef( new Renderer2d( *this ) ); }
00181 
00182     #if defined( CINDER_COCOA_TOUCH )
00183         virtual void setup( App *aApp, const Area &frame, UIView *cinderView, RendererRef sharedRenderer );
00184     #else
00185         ~Renderer2d();
00186         virtual void setup( App *aApp, CGRect frame, NSView *cinderView, RendererRef sharedRenderer, bool retinaEnabled );
00187     #endif
00188 
00189     virtual CGContextRef            getCgContext();
00190 
00191     virtual void startDraw();
00192     virtual void finishDraw();
00193     virtual void defaultResize();
00194     virtual void makeCurrentContext();  
00195     virtual void setFrameSize( int width, int height );
00196     virtual Surface copyWindowSurface( const Area &area );
00197     
00198   protected:
00199     Renderer2d( const Renderer2d &renderer );
00200 
00201 #if defined( CINDER_MAC )
00202     AppImplCocoaRendererQuartz      *mImpl;
00203 #else
00204     AppImplCocoaTouchRendererQuartz *mImpl;
00205 #endif
00206     CGContextRef                    mCGContext;
00207 };
00208 
00209 #elif defined( CINDER_MSW )
00210 
00211 class Renderer2d : public Renderer {
00212  public:
00213     Renderer2d( bool doubleBuffer = true ); 
00214  
00215     static Renderer2dRef    create( bool doubleBuffer = true ) { return Renderer2dRef( new Renderer2d( doubleBuffer ) ); }
00216     virtual RendererRef     clone() const { return Renderer2dRef( new Renderer2d( *this ) ); }
00217     
00218     virtual void setup( App *aApp, HWND wnd, HDC dc, RendererRef sharedRenderer );
00219     virtual void kill();
00220     
00221     virtual HWND    getHwnd() { return mWnd; }
00222     virtual HDC     getDc();
00223 
00224     virtual void    prepareToggleFullScreen();
00225     virtual void    finishToggleFullScreen();
00226 
00227     virtual void startDraw() override;
00228     virtual void finishDraw() override;
00229     virtual void defaultResize() override;
00230     virtual Surface copyWindowSurface( const Area &area );
00231     
00232  protected:
00233     Renderer2d( const Renderer2d &renderer );
00234  
00235     class AppImplMswRendererGdi *mImpl;
00236 
00237     bool            mDoubleBuffer;
00238     HWND            mWnd;
00239     HDC             mDC;
00240 };
00241 
00242 #endif
00243 
00244 } } // namespace cinder::app