include/cinder/app/AppBasic.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/app/App.h"
00028 #include "cinder/Display.h"
00029 #include "cinder/Function.h"
00030 
00031 #if defined( CINDER_MAC )
00032     #include <OpenGL/CGLTypes.h>
00033     #ifdef __OBJC__
00034         @class AppImplCocoaBasic;
00035         @class WindowImplBasicCocoa;
00036     #else
00037         class AppImplCocoaBasic;
00038         class WindowImplBasicCocoa;
00039     #endif
00040 #endif
00041 
00042 #include "cinder/app/TouchEvent.h"
00043 
00044 namespace cinder { namespace app {
00045 
00046 typedef signals::signal<bool (),BooleanAndEventCombiner>                EventSignalShouldQuit;
00047 
00048 class AppBasic : public App {
00049   public:
00050     class Settings : public App::Settings {
00051       public:
00052         Settings();
00053 
00054         void    setShouldQuit ( bool aShouldQuit = true );
00055         bool    isFullScreen() { return mDefaultWindowFormat.isFullScreen(); }
00056 
00058         bool    isResizable() const { return mDefaultWindowFormat.isResizable(); }
00060         void    setResizable( bool resizable = true ) { mDefaultWindowFormat.setResizable( resizable ); }
00062         bool    isBorderless() const { return mDefaultWindowFormat.isBorderless(); }
00064         void    setBorderless( bool borderless = true ) { mDefaultWindowFormat.setBorderless( borderless ); }
00066         bool    isAlwaysOnTop() const { return mDefaultWindowFormat.isAlwaysOnTop(); }
00068         void    setAlwaysOnTop( bool alwaysOnTop = true ) { mDefaultWindowFormat.setAlwaysOnTop( alwaysOnTop ); }
00069 
00071         DisplayRef  getDisplay() const { return mDefaultWindowFormat.getDisplay(); }
00073         void        setDisplay( DisplayRef display ) { mDefaultWindowFormat.setDisplay( display ); }
00074 
00075 #if defined( CINDER_MSW )
00076 
00077         void    enableConsoleWindow( bool enable = true ) { mEnableMswConsole = enable; }
00079         bool    isConsoleWindowEnabled() const { return mEnableMswConsole; }
00080 #endif
00081 
00083         void        enableQuitOnLastWindowClose( bool enable = true ) { mQuitOnLastWindowClose = enable; }
00085         bool        isQuitOnLastWindowCloseEnabled() const { return mQuitOnLastWindowClose; }
00086 
00087      private:
00088         bool        mQuitOnLastWindowClose;
00089 #if defined( CINDER_MSW )
00090         bool        mEnableMswConsole;
00091 #endif
00092     };
00093 
00094     // This is really just here to disambiguate app::WindowRef from the WindowRef found in QuickDraw (so that client code doesn't have to invoke cinder::app::WindowRef explicitly) 
00095     typedef std::shared_ptr<Window>     WindowRef;
00096 
00097  public:
00098     AppBasic();
00099     virtual ~AppBasic();
00100 
00101     virtual void        prepareSettings( Settings *settings ) {}
00102 
00104     EventSignalShouldQuit&  getSignalShouldQuit() { return mSignalShouldQuit; }
00105 
00107     WindowRef       createWindow( const Window::Format &format = Window::Format() );
00108     
00110     virtual float       getFrameRate() const;
00112     virtual void        setFrameRate( float frameRate );
00114     virtual void        disableFrameRate();
00116     virtual bool        isFrameRateEnabled() const; 
00117 
00119     void                hideCursor();
00121     void                showCursor();
00122 
00123     const Settings&     getSettings() const { return mSettings; }
00124 
00125 
00127     virtual void        quit();
00128 
00130     const std::vector<std::string>&     getArgs() const { return mCommandLineArgs; }
00131 
00133     virtual fs::path    getAppPath() const;
00134     
00136     virtual app::WindowRef  getWindow() const;
00138     virtual size_t          getNumWindows() const;
00140     virtual WindowRef       getWindowIndex( size_t index ) const;
00142     virtual WindowRef       getForegroundWindow() const;
00143 
00144     // DO NOT CALL - should be private but aren't for esoteric reasons
00146     // Internal handlers - these are called into by AppImpl's. If you are calling one of these, you have likely strayed far off the path.
00147 #if defined( CINDER_MAC )
00148     void        privateSetImpl__( AppImplCocoaBasic *aImpl );
00149 #endif
00150     bool        privateShouldQuit();
00151     
00152 #if defined( CINDER_MSW )
00153     virtual bool        getsWindowsPaintEvents() { return true; }
00154 #endif
00155 
00156     
00158     static AppBasic*    get()   { return sInstance; }
00159 
00161     // These are called by application instantation macros and are only used in the launch process
00162     static void     prepareLaunch() { App::prepareLaunch(); }
00163 #if defined( CINDER_MSW )
00164     static void     executeLaunch( AppBasic *app, RendererRef renderer, const char *title );
00165 #elif defined( CINDER_MAC )
00166     static void     executeLaunch( AppBasic *app, RendererRef renderer, const char *title, int argc, char * const argv[] ) { App::sInstance = sInstance = app; App::executeLaunch( app, renderer, title, argc, argv ); }
00167 #endif
00168     static void     cleanupLaunch() { App::cleanupLaunch(); }
00169     
00170     virtual void    launch( const char *title, int argc, char * const argv[] );
00172 
00173   protected:
00174     static AppBasic*    sInstance;
00175 
00176     EventSignalShouldQuit   mSignalShouldQuit;
00177 
00178 #if defined( CINDER_MAC )
00179     AppImplCocoaBasic           *mImpl;
00180 #elif defined( CINDER_MSW )
00181     class AppImplMswBasic   *mImpl;
00182     friend class AppImplMswBasic;
00183 #endif
00184     
00185     std::vector<std::string>    mCommandLineArgs;
00186 
00187     Settings        mSettings;
00188 };
00189 
00190 } } // namespace cinder::app
00191 
00192 // App-instantiation macros
00193 
00194 #if defined( CINDER_MAC )
00195     #define CINDER_APP_BASIC( APP, RENDERER )                               \
00196     int main( int argc, char * const argv[] ) {                             \
00197         cinder::app::AppBasic::prepareLaunch();                             \
00198         cinder::app::AppBasic *app = new APP;                               \
00199         cinder::app::RendererRef ren( new RENDERER );                       \
00200         cinder::app::AppBasic::executeLaunch( app, ren, #APP, argc, argv ); \
00201         cinder::app::AppBasic::cleanupLaunch();                             \
00202         return 0;                                                           \
00203     }
00204 #elif defined( CINDER_MSW )
00205     #define CINDER_APP_BASIC( APP, RENDERER )                                                       \
00206     int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {  \
00207         cinder::app::AppBasic::prepareLaunch();                                                     \
00208         cinder::app::AppBasic *app = new APP;                                                       \
00209         cinder::app::RendererRef ren( new RENDERER );                                               \
00210         cinder::app::AppBasic::executeLaunch( app, ren, #APP );                                     \
00211         cinder::app::AppBasic::cleanupLaunch();                                                     \
00212         return 0;                                                                                   \
00213     }
00214 #endif