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 setFullScreenSize( int aFullScreenSizeX, int aFullScreenSizeY ); 00050 void setFullScreen( bool aFullScreen = true ); 00051 void setResizable( bool aResizable = true ); 00052 00053 int getFullScreenWidth() const { return mFullScreenSizeX; } 00054 int getFullScreenHeight() const { return mFullScreenSizeY; } 00055 00057 Display* getDisplay() const { return mDisplay; } 00058 void setDisplay( shared_ptr<Display> aDisplay ); 00059 00060 #if defined( CINDER_MAC ) 00061 00062 void enableSecondaryDisplayBlanking( bool enable = false ) { mEnableSecondaryDisplayBlanking = enable; } 00064 bool isSecondaryDisplayBlankingEnabled() const { return mEnableSecondaryDisplayBlanking; } 00065 #endif 00066 00068 void enableMultiTouch( bool enable = true ) { mEnableMultiTouch = enable; } 00070 bool isMultiTouchEnabled() const { return mEnableMultiTouch; } 00071 00072 private: 00073 bool mEnableMultiTouch; 00074 #if defined( CINDER_MAC ) 00075 bool mEnableSecondaryDisplayBlanking; 00076 #endif 00077 int mFullScreenSizeX, mFullScreenSizeY; 00078 Display *mDisplay; 00079 }; 00080 00081 public: 00082 AppBasic(); 00083 virtual ~AppBasic(); 00084 00085 virtual void prepareSettings( Settings *settings ) {} 00086 00088 virtual void touchesBegan( TouchEvent event ) {} 00090 virtual void touchesMoved( TouchEvent event ) {} 00092 virtual void touchesEnded( TouchEvent event ) {} 00094 const std::vector<TouchEvent::Touch>& getActiveTouches() const { return mActiveTouches; } 00095 00097 CallbackId registerTouchesBegan( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesBegan.registerCb( callback ); } 00099 template<typename T> 00100 CallbackId registerTouchesBegan( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesBegan.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); } 00102 void unregisterTouchesBegan( CallbackId id ) { mCallbacksTouchesBegan.unregisterCb( id ); } 00103 00105 CallbackId registerTouchesMoved( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesMoved.registerCb( callback ); } 00107 template<typename T> 00108 CallbackId registerTouchesMoved( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesMoved.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); } 00110 void unregisterTouchesMoved( CallbackId id ) { mCallbacksTouchesMoved.unregisterCb( id ); } 00111 00113 CallbackId registerTouchesEnded( std::function<bool (TouchEvent)> callback ) { return mCallbacksTouchesEnded.registerCb( callback ); } 00115 template<typename T> 00116 CallbackId registerTouchesEnded( T *obj, bool (T::*callback)(TouchEvent) ) { return mCallbacksTouchesEnded.registerCb( std::bind1st( std::mem_fun( callback ), obj ) ); } 00118 void unregisterTouchesEnded( CallbackId id ) { mCallbacksTouchesEnded.unregisterCb( id ); } 00119 00120 00122 virtual int getWindowWidth() const; 00124 void setWindowWidth( int windowWidth ); 00126 virtual int getWindowHeight() const; 00128 void setWindowHeight( int windowHeight ); 00130 void setWindowSize( int windowWidth, int windowHeight ); 00131 00133 virtual float getFrameRate() const; 00135 virtual void setFrameRate( float frameRate ); 00137 virtual bool isFullScreen() const; 00139 virtual void setFullScreen( bool fullScreen ); 00140 00142 Vec2i getMousePos() const; 00144 void hideCursor(); 00146 void showCursor(); 00147 00148 const Settings& getSettings() const { return mSettings; } 00149 const Display& getDisplay(); 00150 00152 virtual void quit(); 00153 00155 const std::vector<std::string>& getArgs() const { return mCommandLineArgs; } 00156 00158 virtual std::string getAppPath(); 00159 00160 // DO NOT CALL - should be private but aren't for esoteric reasons 00162 // Internal handlers - these are called into by AppImpl's. If you are calling one of these, you have likely strayed far off the path. 00163 #if defined( CINDER_MAC ) 00164 void privateSetImpl__( AppImplCocoaBasic *aImpl ); 00165 #endif 00166 void privateTouchesBegan__( const TouchEvent &event ); 00167 void privateTouchesMoved__( const TouchEvent &event ); 00168 void privateTouchesEnded__( const TouchEvent &event ); 00169 void privateSetActiveTouches__( const std::vector<TouchEvent::Touch> &touches ) { mActiveTouches = touches; } 00170 00171 #if defined( CINDER_MSW ) 00172 virtual bool getsWindowsPaintEvents() { return true; } 00173 #endif 00174 00175 00177 static AppBasic* get() { return sInstance; } 00178 00180 // These are called by application instantation macros and are only used in the launch process 00181 static void prepareLaunch() { App::prepareLaunch(); } 00182 #if defined( CINDER_MSW ) 00183 static void executeLaunch( AppBasic *app, class Renderer *renderer, const char *title ); 00184 #elif defined( CINDER_MAC ) 00185 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 ); } 00186 #endif 00187 static void cleanupLaunch() { App::cleanupLaunch(); } 00188 00189 virtual void launch( const char *title, int argc, char * const argv[] ); 00191 00193 virtual void privateResize__( const ResizeEvent &event ); 00195 00196 private: 00197 00198 static AppBasic* sInstance; 00199 00200 CallbackMgr<bool (TouchEvent)> mCallbacksTouchesBegan, mCallbacksTouchesMoved, mCallbacksTouchesEnded; 00201 00202 #if defined( CINDER_MAC ) 00203 AppImplCocoaBasic *mImpl; 00204 #elif defined( CINDER_MSW ) 00205 class AppImplMswBasic *mImpl; 00206 friend class AppImplMswBasic; 00207 #endif 00208 00209 std::vector<std::string> mCommandLineArgs; 00210 00211 std::vector<TouchEvent::Touch> mActiveTouches; // list of currently active touches 00212 00213 Settings mSettings; 00214 }; 00215 00216 } } // namespace cinder::app 00217 00218 // App-instantiation macros 00219 00220 #if defined( CINDER_MAC ) 00221 #define CINDER_APP_BASIC( APP, RENDERER ) \ 00222 int main( int argc, char * const argv[] ) { \ 00223 cinder::app::AppBasic::prepareLaunch(); \ 00224 cinder::app::AppBasic *app = new APP; \ 00225 cinder::app::Renderer *ren = new RENDERER; \ 00226 cinder::app::AppBasic::executeLaunch( app, ren, #APP, argc, argv ); \ 00227 cinder::app::AppBasic::cleanupLaunch(); \ 00228 return 0; \ 00229 } 00230 #elif defined( CINDER_MSW ) 00231 #define CINDER_APP_BASIC( APP, RENDERER ) \ 00232 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { \ 00233 cinder::app::AppBasic::prepareLaunch(); \ 00234 cinder::app::AppBasic *app = new APP; \ 00235 cinder::app::Renderer *ren = new RENDERER; \ 00236 cinder::app::AppBasic::executeLaunch( app, ren, #APP ); \ 00237 cinder::app::AppBasic::cleanupLaunch(); \ 00238 return 0; \ 00239 } 00240 #endif