Urho3DPlayer.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Engine.h"
  23. #include "FileSystem.h"
  24. #include "Log.h"
  25. #include "Main.h"
  26. #include "ProcessUtils.h"
  27. #include "ResourceCache.h"
  28. #include "ResourceEvents.h"
  29. #ifdef URHO3D_ANGELSCRIPT
  30. #include "ScriptFile.h"
  31. #include "Script.h"
  32. #endif
  33. #ifdef URHO3D_LUA
  34. #include "LuaScript.h"
  35. #endif
  36. #include "Urho3DPlayer.h"
  37. #include "DebugNew.h"
  38. DEFINE_APPLICATION_MAIN(Urho3DPlayer);
  39. Urho3DPlayer::Urho3DPlayer(Context* context) :
  40. Application(context)
  41. {
  42. }
  43. void Urho3DPlayer::Setup()
  44. {
  45. FileSystem* filesystem = GetSubsystem<FileSystem>();
  46. // On Android and iOS, read command line from a file as parameters can not otherwise be easily given
  47. #if defined(ANDROID) || defined(IOS)
  48. SharedPtr<File> commandFile(new File(context_, filesystem->GetProgramDir() + "Data/CommandLine.txt",
  49. FILE_READ));
  50. String commandLine = commandFile->ReadLine();
  51. commandFile->Close();
  52. ParseArguments(commandLine, false);
  53. // Reparse engine startup parameters now
  54. engineParameters_ = Engine::ParseParameters(GetArguments());
  55. #endif
  56. // Check for script file name
  57. const Vector<String>& arguments = GetArguments();
  58. String scriptFileName;
  59. for (unsigned i = 0; i < arguments.Size(); ++i)
  60. {
  61. if (arguments[i][0] != '-')
  62. {
  63. scriptFileName_ = GetInternalPath(arguments[i]);
  64. break;
  65. }
  66. }
  67. // Show usage if not found
  68. if (scriptFileName_.Empty())
  69. {
  70. ErrorExit("Usage: Urho3DPlayer <scriptfile> [options]\n\n"
  71. "The script file should implement the function void Start() for initializing the "
  72. "application and subscribing to all necessary events, such as the frame update.\n"
  73. #ifndef WIN32
  74. "\nCommand line options:\n"
  75. "-x <res> Horizontal resolution\n"
  76. "-y <res> Vertical resolution\n"
  77. "-m <level> Enable hardware multisampling\n"
  78. "-v Enable vertical sync\n"
  79. "-t Enable triple buffering\n"
  80. "-w Start in windowed mode\n"
  81. "-s Enable resizing when in windowed mode\n"
  82. "-q Enable quiet mode which does not log to standard output stream\n"
  83. "-b <length> Sound buffer length in milliseconds\n"
  84. "-r <freq> Sound mixing frequency in Hz\n"
  85. "-p <paths> Resource path(s) to use, separated by semicolons\n"
  86. "-ap <paths> Autoload resource path(s) to use, seperated by semicolons\n"
  87. "-log <level> Change the log level, valid 'level' values are 'debug', 'info', 'warning', 'error'\n"
  88. "-ds <file> Dump used shader variations to a file for precaching\n"
  89. "-mq <level> Material quality level, default 2 (high)\n"
  90. "-tq <level> Texture quality level, default 2 (high)\n"
  91. "-tf <level> Texture filter mode, default 2 (trilinear)\n"
  92. "-af <level> Texture anisotropy level, default 4. Also sets anisotropic filter mode\n"
  93. "-flushgpu Flush GPU command queue each frame. Effective only on Direct3D9\n"
  94. "-borderless Borderless window mode\n"
  95. "-headless Headless mode. No application window will be created\n"
  96. "-landscape Use landscape orientations (iOS only, default)\n"
  97. "-portrait Use portrait orientations (iOS only)\n"
  98. "-prepass Use light pre-pass rendering\n"
  99. "-deferred Use deferred rendering\n"
  100. "-lqshadows Use low-quality (1-sample) shadow filtering\n"
  101. "-noshadows Disable shadow rendering\n"
  102. "-nolimit Disable frame limiter\n"
  103. "-nothreads Disable worker threads\n"
  104. "-nosound Disable sound output\n"
  105. "-noip Disable sound mixing interpolation\n"
  106. "-sm2 Force SM2.0 rendering\n"
  107. "-touch Touch emulation on desktop platform\n"
  108. #endif
  109. );
  110. }
  111. else
  112. {
  113. // Use the script file name as the base name for the log file
  114. engineParameters_["LogName"] = filesystem->GetAppPreferencesDir("urho3d", "logs") + GetFileNameAndExtension(scriptFileName_) + ".log";
  115. }
  116. }
  117. void Urho3DPlayer::Start()
  118. {
  119. String extension = GetExtension(scriptFileName_);
  120. if (extension != ".lua" && extension != ".luc")
  121. {
  122. #ifdef URHO3D_ANGELSCRIPT
  123. // Instantiate and register the AngelScript subsystem
  124. context_->RegisterSubsystem(new Script(context_));
  125. // Hold a shared pointer to the script file to make sure it is not unloaded during runtime
  126. scriptFile_ = GetSubsystem<ResourceCache>()->GetResource<ScriptFile>(scriptFileName_);
  127. /// \hack If we are running the editor, also instantiate Lua subsystem to enable editing Lua ScriptInstances
  128. #ifdef URHO3D_LUA
  129. if (scriptFileName_.Contains("Editor.as", false))
  130. context_->RegisterSubsystem(new LuaScript(context_));
  131. #endif
  132. // If script loading is successful, proceed to main loop
  133. if (scriptFile_ && scriptFile_->Execute("void Start()"))
  134. {
  135. // Subscribe to script's reload event to allow live-reload of the application
  136. SubscribeToEvent(scriptFile_, E_RELOADSTARTED, HANDLER(Urho3DPlayer, HandleScriptReloadStarted));
  137. SubscribeToEvent(scriptFile_, E_RELOADFINISHED, HANDLER(Urho3DPlayer, HandleScriptReloadFinished));
  138. SubscribeToEvent(scriptFile_, E_RELOADFAILED, HANDLER(Urho3DPlayer, HandleScriptReloadFailed));
  139. return;
  140. }
  141. #else
  142. ErrorExit("AngelScript is not enabled!");
  143. return;
  144. #endif
  145. }
  146. else
  147. {
  148. #ifdef URHO3D_LUA
  149. // Instantiate and register the Lua script subsystem
  150. LuaScript* luaScript = new LuaScript(context_);
  151. context_->RegisterSubsystem(luaScript);
  152. // If script loading is successful, proceed to main loop
  153. if (luaScript->ExecuteFile(scriptFileName_))
  154. {
  155. luaScript->ExecuteFunction("Start");
  156. return;
  157. }
  158. #else
  159. ErrorExit("Lua is not enabled!");
  160. return;
  161. #endif
  162. }
  163. // The script was not successfully loaded. Show the last error message and do not run the main loop
  164. ErrorExit();
  165. }
  166. void Urho3DPlayer::Stop()
  167. {
  168. #ifdef URHO3D_ANGELSCRIPT
  169. if (scriptFile_)
  170. {
  171. // Execute the optional stop function
  172. if (scriptFile_->GetFunction("void Stop()"))
  173. scriptFile_->Execute("void Stop()");
  174. }
  175. #else
  176. if (false)
  177. {
  178. }
  179. #endif
  180. #ifdef URHO3D_LUA
  181. else
  182. {
  183. LuaScript* luaScript = GetSubsystem<LuaScript>();
  184. if (luaScript && luaScript->GetFunction("Stop", true))
  185. luaScript->ExecuteFunction("Stop");
  186. }
  187. #endif
  188. }
  189. void Urho3DPlayer::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  190. {
  191. #ifdef URHO3D_ANGELSCRIPT
  192. if (scriptFile_->GetFunction("void Stop()"))
  193. scriptFile_->Execute("void Stop()");
  194. #endif
  195. }
  196. void Urho3DPlayer::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  197. {
  198. #ifdef URHO3D_ANGELSCRIPT
  199. // Restart the script application after reload
  200. if (!scriptFile_->Execute("void Start()"))
  201. {
  202. scriptFile_.Reset();
  203. ErrorExit();
  204. }
  205. #endif
  206. }
  207. void Urho3DPlayer::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  208. {
  209. #ifdef URHO3D_ANGELSCRIPT
  210. scriptFile_.Reset();
  211. ErrorExit();
  212. #endif
  213. }