include/cinder/app/AppBasic.h
Go to the documentation of this file.
00001 /*
00002  Copyright (c) 2010, The Barbarian Group
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
00006  the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and
00009     the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
00011     the following disclaimer in the documentation and/or other materials provided with the distribution.
00012 
00013  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
00014  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00015  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00016  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00017  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00018  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00019  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00020  POSSIBILITY OF SUCH DAMAGE.
00021 */
00022 
00023 #pragma once
00024 
00025 #include "cinder/app/App.h"
00026 #include "cinder/Display.h"
00027 #include "cinder/Function.h"
00028 
00029 #if defined( CINDER_MAC )
00030     #include <OpenGL/CGLTypes.h>
00031     #ifdef __OBJC__
00032         @class AppImplCocoaBasic;
00033     #else
00034         class AppImplCocoaBasic;
00035     #endif
00036 #endif
00037 
00038 #include "cinder/app/TouchEvent.h"
00039 
00040 namespace cinder { namespace app {
00041 
00042 class AppBasic : public App {
00043  public:
00044     class Settings : public App::Settings {
00045      public:
00046         Settings();
00047 
00048         void    setShouldQuit ( bool aShouldQuit = true );
00049         void    setFullScreen( bool aFullScreen = true );
00050         void    setResizable( bool aResizable = true );
00051 
00053         Display*    getDisplay() const { return mDisplay; }
00054         void        setDisplay( std::shared_ptr<Display> aDisplay );
00055 
00056 #if defined( CINDER_MAC )
00057 
00058         void    enableSecondaryDisplayBlanking( bool enable = false ) { mEnableSecondaryDisplayBlanking = enable; }
00060         bool    isSecondaryDisplayBlankingEnabled() const { return mEnableSecondaryDisplayBlanking; }   
00061 #endif
00062         
00064         void        enableMultiTouch( bool enable = true ) { mEnableMultiTouch = enable; }
00066         bool        isMultiTouchEnabled() const { return mEnableMultiTouch; }
00067 
00068      private:
00069         bool        mEnableMultiTouch;
00070 #if defined( CINDER_MAC )
00071         bool        mEnableSecondaryDisplayBlanking;
00072 #endif
00073         Display     *mDisplay;
00074     };
00075 
00076  public:
00077     AppBasic();
00078     virtual ~AppBasic();
00079 
00080     virtual void        prepareSettings( Settings *settings ) {}
00081 
00083     virtual void        touchesBegan( TouchEvent event ) {}
00085     virtual void        touchesMoved( TouchEvent event ) {}
00087     virtual void        touchesEnded( TouchEvent event ) {}
00089     const std::vector<TouchEvent::Touch>&   getActiveTouches() const { return mActiveTouches; }
00090 
00092     CallbackId      registerTouchesBegan( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesBegan.registerCb( callback ); }
00094     template<typename T>
00095     CallbackId      registerTouchesBegan( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesBegan.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); }
00097     void            unregisterTouchesBegan( CallbackId id ) { mCallbacksTouchesBegan.unregisterCb( id ); }
00098 
00100     CallbackId      registerTouchesMoved( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesMoved.registerCb( callback ); }
00102     template<typename T>
00103     CallbackId      registerTouchesMoved( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesMoved.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); }
00105     void            unregisterTouchesMoved( CallbackId id ) { mCallbacksTouchesMoved.unregisterCb( id ); }
00106 
00108     CallbackId      registerTouchesEnded( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesEnded.registerCb( callback ); }
00110     template<typename T>
00111     CallbackId      registerTouchesEnded( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesEnded.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); }
00113     void            unregisterTouchesEnded( CallbackId id ) { mCallbacksTouchesEnded.unregisterCb( id ); }
00114 
00115 
00117     virtual int     getWindowWidth() const;
00119     void            setWindowWidth( int windowWidth );
00121     virtual int     getWindowHeight() const;
00123     void            setWindowHeight( int windowHeight );
00125     void            setWindowSize( int windowWidth, int windowHeight );
00126 
00128     virtual Vec2i   getWindowPos() const;
00129 
00130     using App::setWindowPos;
00132     virtual void    setWindowPos( const Vec2i &windowPos );
00133     
00135     virtual float       getFrameRate() const;
00137     virtual void        setFrameRate( float frameRate );
00139     virtual bool        isFullScreen() const;
00141     virtual void        setFullScreen( bool fullScreen );
00142 
00144     virtual bool        isBorderless() const;
00146     virtual void        setBorderless( bool borderless = true );
00148     virtual bool        isAlwaysOnTop() const;
00150     virtual void        setAlwaysOnTop( bool alwaysOnTop = true );
00151 
00152 
00154     Vec2i               getMousePos() const;
00156     void                hideCursor();
00158     void                showCursor();
00159 
00160     const Settings&     getSettings() const { return mSettings; }
00161     const Display&      getDisplay();
00162 
00164     virtual void        quit();
00165 
00167     const std::vector<std::string>&     getArgs() const { return mCommandLineArgs; }
00168 
00170     virtual fs::path    getAppPath();
00171 
00172     // DO NOT CALL - should be private but aren't for esoteric reasons
00174     // Internal handlers - these are called into by AppImpl's. If you are calling one of these, you have likely strayed far off the path.
00175 #if defined( CINDER_MAC )
00176     void        privateSetImpl__( AppImplCocoaBasic *aImpl );   
00177 #endif
00178     void        privateTouchesBegan__( const TouchEvent &event );
00179     void        privateTouchesMoved__( const TouchEvent &event );
00180     void        privateTouchesEnded__( const TouchEvent &event );
00181     void        privateSetActiveTouches__( const std::vector<TouchEvent::Touch> &touches ) { mActiveTouches = touches; }
00182     
00183 #if defined( CINDER_MSW )
00184     virtual bool        getsWindowsPaintEvents() { return true; }
00185 #endif
00186 
00187     
00189     static AppBasic*    get()   { return sInstance; }
00190 
00192     // These are called by application instantation macros and are only used in the launch process
00193     static void     prepareLaunch() { App::prepareLaunch(); }
00194 #if defined( CINDER_MSW )
00195     static void     executeLaunch( AppBasic *app, class Renderer *renderer, const char *title );
00196 #elif defined( CINDER_MAC )
00197     static void     executeLaunch( AppBasic *app, class Renderer *renderer, const char *title, int argc, char * const argv[] ) { sInstance = app; App::executeLaunch( app, renderer, title, argc, argv ); }
00198 #endif
00199     static void     cleanupLaunch() { App::cleanupLaunch(); }
00200     
00201     virtual void    launch( const char *title, int argc, char * const argv[] );
00203 
00205     virtual void    privateResize__( const ResizeEvent &event );
00207 
00208  private:
00209  
00210     static AppBasic*    sInstance;
00211 
00212     CallbackMgr<bool (TouchEvent)>      mCallbacksTouchesBegan, mCallbacksTouchesMoved, mCallbacksTouchesEnded;
00213 
00214 #if defined( CINDER_MAC )
00215     AppImplCocoaBasic           *mImpl;
00216 #elif defined( CINDER_MSW )
00217     class AppImplMswBasic   *mImpl;
00218     friend class AppImplMswBasic;
00219 #endif
00220     
00221     std::vector<std::string>    mCommandLineArgs;
00222 
00223     std::vector<TouchEvent::Touch>      mActiveTouches; // list of currently active touches
00224 
00225     Settings        mSettings;
00226 };
00227 
00228 } } // namespace cinder::app
00229 
00230 // App-instantiation macros
00231 
00232 #if defined( CINDER_MAC )
00233     #define CINDER_APP_BASIC( APP, RENDERER )                               \
00234     int main( int argc, char * const argv[] ) {                             \
00235         cinder::app::AppBasic::prepareLaunch();                             \
00236         cinder::app::AppBasic *app = new APP;                               \
00237         cinder::app::Renderer *ren = new RENDERER;                          \
00238         cinder::app::AppBasic::executeLaunch( app, ren, #APP, argc, argv ); \
00239         cinder::app::AppBasic::cleanupLaunch();                             \
00240         return 0;                                                           \
00241     }
00242 #elif defined( CINDER_MSW )
00243     #define CINDER_APP_BASIC( APP, RENDERER )                                                       \
00244     int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {  \
00245         cinder::app::AppBasic::prepareLaunch();                                                     \
00246         cinder::app::AppBasic *app = new APP;                                                       \
00247         cinder::app::Renderer *ren = new RENDERER;                                                  \
00248         cinder::app::AppBasic::executeLaunch( app, ren, #APP );                                     \
00249         cinder::app::AppBasic::cleanupLaunch();                                                     \
00250         return 0;                                                                                   \
00251     }
00252 #endif