Application.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /******************************************************************************
  2. Use 'App' to setup initial application parameters inside 'InitPre' function.
  3. /******************************************************************************/
  4. enum APP_FLAG // Application Flags
  5. {
  6. APP_NO_TITLE_BAR =1<< 0, // application window will have no title bar [Supported Platforms: Windows, Linux]
  7. APP_NO_CLOSE =1<< 1, // application will have close box on the window disabled, and won't be closed by Alt+F4
  8. APP_MINIMIZABLE =1<< 2, // apllication will have minimize box on the window
  9. APP_MAXIMIZABLE =1<< 3, // apllication will have maximize box on the window
  10. APP_RESIZABLE =1<< 4, // application window can be resized by dragging the window edges/corners
  11. APP_HIDDEN =1<< 5, // application window will be initially hidden
  12. APP_FULL_TOGGLE =1<< 6, // display can be altered with Alt+Enter keys (windowed/fullscreen toggle), Alt+Shift+Enter can also be used to switch into fullscreen with resolution taken from the window size
  13. APP_WORK_IN_BACKGROUND =1<< 7, // application will work also in background (when not focused)
  14. APP_ON_RUN_EXIT =1<< 8, // if the application is already running in another instance, then new instance will not be created, but instead the existing instance window will be activated
  15. APP_ON_RUN_WAIT =1<< 9, // if the application is already running in another instance, then wait until it exits and then continue
  16. APP_MEM_LEAKS =1<<10, // error will be shown at program exit if memory leaks were found, use this only for debugging as it may slow down the application [Supported Platforms: Windows]
  17. APP_NO_PAUSE_ON_WINDOW_MOVE_SIZE =1<<11, // application will not pause when the main window is being moved or resized [Supported Platforms: Windows, other platforms don't need this]
  18. APP_AUTO_FREE_IMAGE_OPEN_GL_ES_DATA=1<<12, // this flag is used only for OpenGL ES, it specifies that all Images will automatically call 'freeOpenGLESData' after loading their data, the method frees the software copy of the GPU data which increases available memory, however after calling this method the data can no longer be accessed on the CPU (can no longer be locked or saved to file), you can enable this option for release version of your game to reduce its memory usage
  19. APP_AUTO_FREE_MESH_OPEN_GL_ES_DATA =1<<13, // this flag is used only for OpenGL ES, it specifies that all Meshes will automatically call 'freeOpenGLESData' after loading their data, the method frees the software copy of the GPU data which increases available memory, however after calling this method the data can no longer be accessed on the CPU (can no longer be locked or saved to file), you can enable this option for release version of your game to reduce its memory usage
  20. APP_AUTO_FREE_PHYS_BODY_HELPER_DATA=1<<14, // 'PhysBody' objects will automatically call their 'freeHelperData' method after loading from file, this will free up some memory, however it will disable saving the 'PhysBody' to file, or converting it to 'MeshBase', you can enable this option for release version of your game to reduce its memory usage
  21. APP_ALLOW_NO_GPU =1<<15, // allow application to run if the graphics driver does not meet the minimum requirements, this mode is recommended for server applications where GPU can be limited or not available. This flag works differently for different API's : if in DX9 the graphics failed to initialize then application will run but with graphics disabled, if in DX10+ the graphics failed to initialize then application will run and will display graphics but using "WARP - Windows Advanced Rasterization Platform" which uses CPU for graphics, in order to ensure maximum CPU performance on servers you may want to create the application window small - less pixels to draw = smaller CPU usage, or minimize the application window at startup = completely disables any drawing/rendering, in OpenGL graphics will always be disabled when using this flag
  22. APP_ALLOW_NO_XDISPLAY =1<<16, // this flag is only for Linux: allow application to run if the X Window System failed to initialize or is not available
  23. APP_EXIT_IMMEDIATELY =1<<17, // if this flag is specified then the application will exit immediately after 'InitPre' function
  24. APP_BREAKPOINT_ON_ERROR =1<<18, // if program encounters an error and 'Exit' function is called, then breakpoint will be forced by calling the 'Break' during the 'Exit' function, allowing you to enter debug mode, use this option only for debugging
  25. APP_CALLSTACK_ON_ERROR =1<<19, // if program encounters an error and 'Exit' function is called, then current call stack will be included in the error message [Supported Platforms: Windows]
  26. APP_WEB_DISABLE_AUTO_RESIZE =1<<20, // Web applications by default will auto resize the canvas to cover the entire browser window client area, to disable this behavior you can enable this option [Supported Platforms: Web]
  27. APP_FADE_OUT =1<<21, // if enabled then application window and all sounds will smoothly fade out at application close, available only on Desktop platforms
  28. APP_AUTO_FREE_OPEN_GL_ES_DATA=APP_AUTO_FREE_IMAGE_OPEN_GL_ES_DATA|APP_AUTO_FREE_MESH_OPEN_GL_ES_DATA,
  29. };
  30. /******************************************************************************/
  31. #if EE_PRIVATE
  32. // !! these enums must be equal to "EsenthelActivity.java" !!
  33. #endif
  34. enum AWAKE_MODE : Byte
  35. {
  36. AWAKE_OFF , // the system and screen can go to sleep
  37. AWAKE_SYSTEM, // prevent the system from going to sleep, however the screen can go to sleep
  38. AWAKE_SCREEN, // prevent the system and screen from going to sleep, as long as the App is active or has APP_WORK_IN_BACKGROUND enabled
  39. };
  40. /******************************************************************************/
  41. struct Application // Application Settings
  42. {
  43. Int x, y ; // initial position (-1..1) used to specify window position on the desktop at the creation of application, default=(-1, 1)
  44. UInt flag ; // APP_FLAG
  45. Int active_wait, // amount of milliseconds the application should wait before making 'Update' calls when in active mode , -1=unlimited (app will wait until event occurs ), 0=instant (app will keep calling 'Update' continuously), >0=wait (app will wait specified time until event occurs before making 'Update' calls), default=0. It's recommended to use this instead of manually waiting with 'Time.wait', because this method allows app to resume instantly when event occurs , unlike 'Time.wait' which waits without stopping.
  46. background_wait; // amount of milliseconds the application should wait before making 'Update' calls when in background mode and with APP_WORK_IN_BACKGROUND enabled, -1=unlimited (app will wait until it's activated), 0=instant (app will keep calling 'Update' continuously), >0=wait (app will wait specified time until activated before making 'Update' calls), default=0. It's recommended to use this instead of manually waiting with 'Time.wait', because this method allows app to resume instantly when it gets activated, unlike 'Time.wait' which waits without stopping.
  47. void (*receive_data)(CPtr data, Int size, Ptr hwnd_sender ); // pointer to custom function called whan the application has received binary data sent using 'WindowSendData' function, the application may not access 'data' memory after the callback function returns, 'hwnd_sender'=hwnd window identifier of the sender, default=null
  48. void (*save_state )( ); // pointer to custom function called when application is being put into background or will be terminated, in this function you should save current state of data which you may want to restore at next application startup, this function is used only on mobile platforms where the operating system may close the application for any reason, default=null
  49. void (*paused )( ); // pointer to custom function called when application is being paused (lost focus), default=null
  50. void (*resumed )( ); // pointer to custom function called when application is being resumed (gained focus), default=null
  51. void (*drop )(Memc<Str> &names, GuiObj *focus_obj, C Vec2 &screen_pos); // pointer to custom function called when a file is Drag and Dropped on the application window, 'names'=list of file names being drag and dropped, 'focus_obj'=gui object at which elements are being dropped, 'screen_pos'=screen position at which dropping occurs, default=null
  52. void (*quit )( ); // pointer to custom function called when the application was requested to quit (for example by pressing Alt+F4 or clicking on the "x-close" window button), if this member is different than null, then the application will not exit automatically, manual exiting can be called inside custom 'quit' function (for example by activating 'StateExit' State), this member is ignored when the application was created with APP_NO_CLOSE flag, default=null, this is not supported on Windows Universal Apps
  53. void (*exit )(CChar *error ); // pointer to custom function called when the application has encountered an error and is about to force terminating the application, typically this occurs inside 'Exit' function, default=null
  54. void (*low_memory )( ); // pointer to custom function called when the application has received low memory notification from the system, inside it you can release any cached memory, default=null
  55. void (*notification)(Notification &notification, Bool selected ); // pointer to custom function called when the application notification was selected or dismissed. If the callback is specified, then this notification is not automatically removed, you should call 'notification.remove' to remove it. If no callback is specified, then 'notification.remove' is called automatically.
  56. void (*sleep )(Bool sleep ); // pointer to custom function called when the device is going to sleep.
  57. // get / set
  58. Application& name (C Str &name ); // set application name
  59. C Str& name ( )C {return _name ;} // get application name
  60. C Str& exe ( )C {return _exe ;} // get executable path and name
  61. C Str& cmdLine ( )C {return _cmd_line ;} // get command line parameters
  62. Application& lang (LANG_TYPE lang ); // set application language, some engine messages rely on this value, changing language triggers 'GuiObj.setText' for all gui objects, default=EN
  63. LANG_TYPE lang ( )C {return _lang ;} // get application language, some engine messages rely on this value, changing language triggers 'GuiObj.setText' for all gui objects, default=EN
  64. Bool elevated ( )C {return _elevated ;} // get application process elevation (true if has administrator rights)
  65. UInt parentProcessID ( )C; // get application parent process ID
  66. UInt processID ( )C {return _process_id ;} // get application process ID
  67. UIntPtr threadID ( )C {return _thread_id ;} // get application main thread ID
  68. Ptr hwnd ( )C {return _hwnd ;} // get application window handle in the OS (this can be casted to HWND object)
  69. C RectI& desktopArea ( )C {return _desktop_area ;} // get available desktop area (not covered by windows taskbar or other desktop toolbars)
  70. C VecI2& desktop ( )C {return _desktop_size ;} // get screen size at the moment of application start (desktop size )
  71. Int desktopW ( )C {return _desktop_size.x;} // get screen width at the moment of application start (desktop width )
  72. Int desktopH ( )C {return _desktop_size.y;} // get screen height at the moment of application start (desktop height)
  73. Bool active ( )C {return _active ;} // if application is active (its window is focused)
  74. Bool minimized ( )C; // if application is minimized
  75. Bool maximized ( )C; // if application is maximized
  76. Bool closed ( )C {return _closed ;} // if application has finished closing, this is enabled at the very end of application life cycle, right before all global C++ destructors being called
  77. DIR_ENUM orientation ( )C; // get device orientation, this is valid for mobile devices which support accelerometers, for those devices the method will return one of the following orientation: DIR_UP (default), DIR_DOWN (rotated down), DIR_LEFT (rotated left), DIR_RIGHT (rotated right), if the device doesn't support accelerometers then DIR_UP is returned
  78. Bool mainThread ( )C; // if current thread is the main thread
  79. Application& icon (C Image &icon); // set custom application icon
  80. Application& stayAwake (AWAKE_MODE mode); // set preventing the operating system from going to sleep
  81. C Str& backgroundText( )C {return _back_text ;} // get text displayed on the Status Bar Notification when App is running in Background mode on Android, default="Running in background"
  82. Application& backgroundText(C Str &text ); // set text displayed on the Status Bar Notification when App is running in Background mode on Android, default="Running in background"
  83. // operations
  84. Bool renameSelf (C Str &dest); // rename application executable file to 'dest' location, false on fail
  85. void deleteSelfAtExit( ); // notify that the exe should delete itself at application exit
  86. void close ( ); // request application to be closed, if 'App.quit' was specified then it will be called instead
  87. // advanced
  88. void coInitialize(UInt dwCoInit); // this method is for Windows only, it allows to change initialization of the Windows COM library. By default the engine already initializes the COM library using "CoInitialize(null)" system function (which is called before 'InitPre' function), however if you require to initialize the COM library using 'CoInitializeEx' system function with custom settings, then you must call this method ('coInitialize') with 'dwCoInit' parameter which you would normally use for 'CoInitializeEx'. The engine will reinitialize the COM library and any interfaces it previously obtained. You should not attempt to manually uninitialize the COM library by using 'CoUninitialize' on the main thread, as the engine already manages initialization and uninitialization of the library. 'CoUninitialize' will be automatically called by the engine at the end of the application, after 'Shut' function.
  89. #if EE_PRIVATE
  90. static Bool Fullscreen();
  91. void del ();
  92. Bool create0();
  93. Bool create1();
  94. Bool create ();
  95. void update ();
  96. void loop ();
  97. #if WINDOWS_OLD
  98. HWND Hwnd()C {return HWND(_hwnd);}
  99. #elif WINDOWS_NEW
  100. Windows::UI::Core::CoreWindow^& Hwnd() {return reinterpret_cast<Windows::UI::Core::CoreWindow^&>(_hwnd);}
  101. void loopUntil(Bool &finished, Bool wait=false); // this should be called only on the main thread
  102. static void ExecuteRecordedEvents();
  103. #elif MAC
  104. NSWindow* Hwnd()C {return (NSWindow*)_hwnd ;}
  105. #elif LINUX
  106. XWindow Hwnd()C {return XWindow(_hwnd);}
  107. void setWindowFlags(Bool force_resizable=false);
  108. #endif
  109. void setActive(Bool active);
  110. Bool testInstance ();
  111. void windowCreate ();
  112. void windowDel ();
  113. void windowMsg ();
  114. void deleteSelf ();
  115. void detectMemLeaks();
  116. void showError (CChar *error);
  117. void lowMemory ();
  118. #endif
  119. #if !EE_PRIVATE
  120. private:
  121. #endif
  122. Bool _active, _initialized, _minimized, _maximized, _close, _closed, _del_self_at_exit, _elevated;
  123. #if WINDOWS_NEW
  124. Bool _loop;
  125. #endif
  126. AWAKE_MODE _stay_awake;
  127. DIR_ENUM _orientation;
  128. LANG_TYPE _lang;
  129. Int _mem_leaks;
  130. mutable UInt _parent_process_id;
  131. UInt _process_id;
  132. UIntPtr _thread_id;
  133. Ptr _hwnd;
  134. VecI2 _window_pos, _window_size, _window_resized, _desktop_size;
  135. RectI _desktop_area, _bound, _bound_maximized;
  136. Str _exe, _name, _cmd_line, _back_text;
  137. #if WINDOWS_OLD
  138. UInt _style_window, _style_window_maximized, _style_full;
  139. #if EE_PRIVATE
  140. HINSTANCE _hinstance;
  141. #else
  142. Ptr _hinstance;
  143. #endif
  144. #endif
  145. #if WINDOWS
  146. #if EE_PRIVATE
  147. HICON _icon;
  148. #else
  149. Ptr _icon;
  150. #endif
  151. #else
  152. Image _icon;
  153. #endif
  154. ThreadSafeCallbacks _callbacks;
  155. Application();
  156. }extern
  157. App; // Application Settings
  158. /******************************************************************************/
  159. void StartEEManually(Ptr dll_module_instance); // this function imitates "WinMain" function, and should be called only in a DLL based application to start the engine manually
  160. void LoadEmbeddedPaks(Cipher *cipher); // load PAK files embedded in Application executable file, you can call this inside 'InitPre' function, 'cipher'=Cipher used for encrypting PAK files
  161. void SupportCompressBC7 (); // call this inside 'InitPre' function to add support for compressing IMAGE_BC7 format , using this will however make your executable file bigger
  162. void SupportCompressETC (); // call this inside 'InitPre' function to add support for compressing IMAGE_ETC formats , using this will however make your executable file bigger
  163. void SupportCompressPVRTC(); // call this inside 'InitPre' function to add support for compressing IMAGE_PVRTC formats on Desktop platforms, using this will however make your executable file bigger
  164. void SupportCompressAll (); // call this inside 'InitPre' function to add support for compressing all formats , using this will however make your executable file bigger
  165. /******************************************************************************/
  166. // Multi Language Text, returns one of the few translations provided depending on current application language
  167. inline Str MLT(C Str &english ) {return english;}
  168. inline Str MLT(C Str &english, LANG_TYPE l0, C Str &t0 ) {if(App.lang()==l0)return t0; return english;}
  169. inline Str MLT(C Str &english, LANG_TYPE l0, C Str &t0, LANG_TYPE l1, C Str &t1 ) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; return english;}
  170. inline Str MLT(C Str &english, LANG_TYPE l0, C Str &t0, LANG_TYPE l1, C Str &t1, LANG_TYPE l2, C Str &t2 ) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; if(App.lang()==l2)return t2; return english;}
  171. inline Str MLT(C Str &english, LANG_TYPE l0, C Str &t0, LANG_TYPE l1, C Str &t1, LANG_TYPE l2, C Str &t2, LANG_TYPE l3, C Str &t3 ) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; if(App.lang()==l2)return t2; if(App.lang()==l3)return t3; return english;}
  172. inline Str MLT(C Str &english, LANG_TYPE l0, C Str &t0, LANG_TYPE l1, C Str &t1, LANG_TYPE l2, C Str &t2, LANG_TYPE l3, C Str &t3, LANG_TYPE l4, C Str &t4) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; if(App.lang()==l2)return t2; if(App.lang()==l3)return t3; if(App.lang()==l4)return t4; return english;}
  173. // Multi Language Text Constant, returns one of the few translations provided depending on current application language
  174. inline CChar* MLTC(CChar* english ) {return english;}
  175. inline CChar* MLTC(CChar* english, LANG_TYPE l0, CChar* t0 ) {if(App.lang()==l0)return t0; return english;}
  176. inline CChar* MLTC(CChar* english, LANG_TYPE l0, CChar* t0, LANG_TYPE l1, CChar* t1 ) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; return english;}
  177. inline CChar* MLTC(CChar* english, LANG_TYPE l0, CChar* t0, LANG_TYPE l1, CChar* t1, LANG_TYPE l2, CChar* t2 ) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; if(App.lang()==l2)return t2; return english;}
  178. inline CChar* MLTC(CChar* english, LANG_TYPE l0, CChar* t0, LANG_TYPE l1, CChar* t1, LANG_TYPE l2, CChar* t2, LANG_TYPE l3, CChar* t3 ) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; if(App.lang()==l2)return t2; if(App.lang()==l3)return t3; return english;}
  179. inline CChar* MLTC(CChar* english, LANG_TYPE l0, CChar* t0, LANG_TYPE l1, CChar* t1, LANG_TYPE l2, CChar* t2, LANG_TYPE l3, CChar* t3, LANG_TYPE l4, CChar* t4) {if(App.lang()==l0)return t0; if(App.lang()==l1)return t1; if(App.lang()==l2)return t2; if(App.lang()==l3)return t3; if(App.lang()==l4)return t4; return english;}
  180. /******************************************************************************/
  181. #if EE_PRIVATE
  182. extern Bool LogInit;
  183. #endif
  184. /******************************************************************************/