Urho3DPlayer.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Copyright (c) 2008-2013 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 ENABLE_ANGELSCRIPT
  30. #include "ScriptFile.h"
  31. #include "Script.h"
  32. #endif
  33. #ifdef ENABLE_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. // On Android and iOS, read command line from a file as parameters can not otherwise be easily given
  46. #if defined(ANDROID) || defined(IOS)
  47. SharedPtr<File> commandFile(new File(context_, GetSubsystem<FileSystem>()->GetProgramDir() + "Data/CommandLine.txt",
  48. FILE_READ));
  49. String commandLine = commandFile->ReadLine();
  50. commandFile->Close();
  51. ParseArguments(commandLine, false);
  52. // Reparse engine startup parameters now
  53. engineParameters_ = Engine::ParseParameters(GetArguments());
  54. #endif
  55. // Check for script file name
  56. const Vector<String>& arguments = GetArguments();
  57. String scriptFileName;
  58. for (unsigned i = 0; i < arguments.Size(); ++i)
  59. {
  60. if (arguments[i][0] != '-')
  61. {
  62. scriptFileName_ = GetInternalPath(arguments[i]);
  63. break;
  64. }
  65. }
  66. // Show usage if not found
  67. if (scriptFileName_.Empty())
  68. {
  69. ErrorExit("Usage: Urho3DPlayer <scriptfile> [options]\n\n"
  70. "The script file should implement the function void Start() for initializing the "
  71. "application and subscribing to all necessary events, such as the frame update.\n"
  72. #ifndef WIN32
  73. "\nCommand line options:\n"
  74. "-x <res> Horizontal resolution\n"
  75. "-y <res> Vertical resolution\n"
  76. "-m <level> Enable hardware multisampling\n"
  77. "-v Enable vertical sync\n"
  78. "-t Enable triple buffering\n"
  79. "-w Start in windowed mode\n"
  80. "-s Enable resizing when in windowed mode\n"
  81. "-q Enable quiet mode which does not log to standard output stream\n"
  82. "-b <length> Sound buffer length in milliseconds\n"
  83. "-r <freq> Sound mixing frequency in Hz\n"
  84. "-p <paths> Resource path(s) to use, separated by semicolons\n"
  85. "-log <level> Change the log level, valid 'level' values are 'debug', 'info', 'warning', 'error'\n"
  86. "-borderless Borderless window mode\n"
  87. "-headless Headless mode. No application window will be created\n"
  88. "-prepass Use light pre-pass rendering\n"
  89. "-deferred Use deferred rendering\n"
  90. "-lqshadows Use low-quality (1-sample) shadow filtering\n"
  91. "-noshadows Disable shadow rendering\n"
  92. "-nolimit Disable frame limiter\n"
  93. "-nothreads Disable worker threads\n"
  94. "-nosound Disable sound output\n"
  95. "-noip Disable sound mixing interpolation\n"
  96. "-sm2 Force SM2.0 rendering\n"
  97. #endif
  98. );
  99. }
  100. }
  101. void Urho3DPlayer::Start()
  102. {
  103. String extension = GetExtension(scriptFileName_);
  104. if (extension != ".lua")
  105. {
  106. #ifdef ENABLE_ANGELSCRIPT
  107. // Instantiate and register the AngelScript subsystem
  108. context_->RegisterSubsystem(new Script(context_));
  109. // Hold a shared pointer to the script file to make sure it is not unloaded during runtime
  110. scriptFile_ = GetSubsystem<ResourceCache>()->GetResource<ScriptFile>(scriptFileName_);
  111. // If script loading is successful, proceed to main loop
  112. if (scriptFile_ && scriptFile_->Execute("void Start()"))
  113. {
  114. // Subscribe to script's reload event to allow live-reload of the application
  115. SubscribeToEvent(scriptFile_, E_RELOADSTARTED, HANDLER(Urho3DPlayer, HandleScriptReloadStarted));
  116. SubscribeToEvent(scriptFile_, E_RELOADFINISHED, HANDLER(Urho3DPlayer, HandleScriptReloadFinished));
  117. SubscribeToEvent(scriptFile_, E_RELOADFAILED, HANDLER(Urho3DPlayer, HandleScriptReloadFailed));
  118. return;
  119. }
  120. #else
  121. ErrorExit("AngelScript is not enabled!");
  122. #endif
  123. }
  124. else
  125. {
  126. #ifdef ENABLE_LUA
  127. // Instantiate and register the Lua script subsystem
  128. LuaScript* luaScript = new LuaScript(context_);
  129. context_->RegisterSubsystem(luaScript);
  130. // If script loading is successful, proceed to main loop
  131. if (luaScript->ExecuteFile(scriptFileName_))
  132. {
  133. luaScript->ExecuteFunction("Start");
  134. return;
  135. }
  136. #else
  137. ErrorExit("Lua is not enabled!");
  138. #endif
  139. }
  140. // The script was not successfully loaded. Show the last error message and do not run the main loop
  141. ErrorExit();
  142. }
  143. void Urho3DPlayer::Stop()
  144. {
  145. #ifdef ENABLE_ANGELSCRIPT
  146. if (scriptFile_)
  147. {
  148. // Execute the optional stop function
  149. if (scriptFile_->GetFunction("void Stop()"))
  150. scriptFile_->Execute("void Stop()");
  151. }
  152. #else
  153. if (false)
  154. {
  155. }
  156. #endif
  157. #ifdef ENABLE_LUA
  158. else
  159. {
  160. LuaScript* luaScript = GetSubsystem<LuaScript>();
  161. if (luaScript && luaScript->GetFunction("Stop", true))
  162. luaScript->ExecuteFunction("Stop");
  163. }
  164. #endif
  165. }
  166. void Urho3DPlayer::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  167. {
  168. #ifdef ENABLE_ANGELSCRIPT
  169. if (scriptFile_->GetFunction("void Stop()"))
  170. scriptFile_->Execute("void Stop()");
  171. #endif
  172. }
  173. void Urho3DPlayer::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  174. {
  175. #ifdef ENABLE_ANGELSCRIPT
  176. // Restart the script application after reload
  177. if (!scriptFile_->Execute("void Start()"))
  178. {
  179. scriptFile_.Reset();
  180. ErrorExit();
  181. }
  182. #endif
  183. }
  184. void Urho3DPlayer::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  185. {
  186. #ifdef ENABLE_ANGELSCRIPT
  187. scriptFile_.Reset();
  188. ErrorExit();
  189. #endif
  190. }