Urho3DPlayer.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // Copyright (c) 2008-2015 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 <Urho3D/Core/Main.h>
  23. #include <Urho3D/Engine/Engine.h>
  24. #include <Urho3D/IO/FileSystem.h>
  25. #include <Urho3D/IO/Log.h>
  26. #include <Urho3D/Resource/ResourceCache.h>
  27. #include <Urho3D/Resource/ResourceEvents.h>
  28. #ifdef URHO3D_ANGELSCRIPT
  29. #include <Urho3D/Script/ScriptFile.h>
  30. #include <Urho3D/Script/Script.h>
  31. #endif
  32. #ifdef URHO3D_LUA
  33. #include <Urho3D/LuaScript/LuaScript.h>
  34. #endif
  35. #include "Urho3DPlayer.h"
  36. #include <Urho3D/DebugNew.h>
  37. DEFINE_APPLICATION_MAIN(Urho3DPlayer);
  38. Urho3DPlayer::Urho3DPlayer(Context* context) :
  39. Application(context)
  40. {
  41. }
  42. void Urho3DPlayer::Setup()
  43. {
  44. FileSystem* filesystem = GetSubsystem<FileSystem>();
  45. // Read command line from a file if no arguments given. This is primarily intended for mobile platforms.
  46. // Note that the command file name uses a hardcoded path that does not utilize the resource system
  47. // properly (including resource path prefix), as the resource system is not yet initialized at this point
  48. const String commandFileName = filesystem->GetProgramDir() + "Data/CommandLine.txt";
  49. if (GetArguments().Empty() && filesystem->FileExists(commandFileName))
  50. {
  51. SharedPtr<File> commandFile(new File(context_, commandFileName));
  52. String commandLine = commandFile->ReadLine();
  53. commandFile->Close();
  54. ParseArguments(commandLine, false);
  55. // Reparse engine startup parameters now
  56. engineParameters_ = Engine::ParseParameters(GetArguments());
  57. }
  58. // Check for script file name
  59. const Vector<String>& arguments = GetArguments();
  60. String scriptFileName;
  61. if (arguments.Size() && arguments[0][0] != '-')
  62. scriptFileName_ = GetInternalPath(arguments[0]);
  63. // Show usage if not found
  64. if (scriptFileName_.Empty())
  65. {
  66. ErrorExit("Usage: Urho3DPlayer <scriptfile> [options]\n\n"
  67. "The script file should implement the function void Start() for initializing the "
  68. "application and subscribing to all necessary events, such as the frame update.\n"
  69. #ifndef WIN32
  70. "\nCommand line options:\n"
  71. "-x <res> Horizontal resolution\n"
  72. "-y <res> Vertical resolution\n"
  73. "-m <level> Enable hardware multisampling\n"
  74. "-v Enable vertical sync\n"
  75. "-t Enable triple buffering\n"
  76. "-w Start in windowed mode\n"
  77. "-s Enable resizing when in windowed mode\n"
  78. "-q Enable quiet mode which does not log to standard output stream\n"
  79. "-b <length> Sound buffer length in milliseconds\n"
  80. "-r <freq> Sound mixing frequency in Hz\n"
  81. "-p <paths> Resource path(s) to use, separated by semicolons\n"
  82. "-ap <paths> Autoload resource path(s) to use, seperated by semicolons\n"
  83. "-log <level> Change the log level, valid 'level' values are 'debug', 'info', 'warning', 'error'\n"
  84. "-ds <file> Dump used shader variations to a file for precaching\n"
  85. "-mq <level> Material quality level, default 2 (high)\n"
  86. "-tq <level> Texture quality level, default 2 (high)\n"
  87. "-tf <level> Texture filter mode, default 2 (trilinear)\n"
  88. "-af <level> Texture anisotropy level, default 4. Also sets anisotropic filter mode\n"
  89. "-gl2 Force OpenGL 2 use even if OpenGL 3 is available\n"
  90. "-flushgpu Flush GPU command queue each frame. Effective only on Direct3D\n"
  91. "-borderless Borderless window mode\n"
  92. "-headless Headless mode. No application window will be created\n"
  93. "-landscape Use landscape orientations (iOS only, default)\n"
  94. "-portrait Use portrait orientations (iOS only)\n"
  95. "-prepass Use light pre-pass rendering\n"
  96. "-deferred Use deferred rendering\n"
  97. "-renderpath <name> Use the named renderpath (must enter full resource name)\n"
  98. "-lqshadows Use low-quality (1-sample) shadow filtering\n"
  99. "-noshadows Disable shadow rendering\n"
  100. "-nolimit Disable frame limiter\n"
  101. "-nothreads Disable worker threads\n"
  102. "-nosound Disable sound output\n"
  103. "-noip Disable sound mixing interpolation\n"
  104. "-touch Touch emulation on desktop platform\n"
  105. #endif
  106. );
  107. }
  108. else
  109. {
  110. // Use the script file name as the base name for the log file
  111. engineParameters_["LogName"] = filesystem->GetAppPreferencesDir("urho3d", "logs") + GetFileNameAndExtension(scriptFileName_) + ".log";
  112. }
  113. }
  114. void Urho3DPlayer::Start()
  115. {
  116. String extension = GetExtension(scriptFileName_);
  117. if (extension != ".lua" && extension != ".luc")
  118. {
  119. #ifdef URHO3D_ANGELSCRIPT
  120. // Instantiate and register the AngelScript subsystem
  121. context_->RegisterSubsystem(new Script(context_));
  122. // Hold a shared pointer to the script file to make sure it is not unloaded during runtime
  123. scriptFile_ = GetSubsystem<ResourceCache>()->GetResource<ScriptFile>(scriptFileName_);
  124. /// \hack If we are running the editor, also instantiate Lua subsystem to enable editing Lua ScriptInstances
  125. #ifdef URHO3D_LUA
  126. if (scriptFileName_.Contains("Editor.as", false))
  127. context_->RegisterSubsystem(new LuaScript(context_));
  128. #endif
  129. // If script loading is successful, proceed to main loop
  130. if (scriptFile_ && scriptFile_->Execute("void Start()"))
  131. {
  132. // Subscribe to script's reload event to allow live-reload of the application
  133. SubscribeToEvent(scriptFile_, E_RELOADSTARTED, HANDLER(Urho3DPlayer, HandleScriptReloadStarted));
  134. SubscribeToEvent(scriptFile_, E_RELOADFINISHED, HANDLER(Urho3DPlayer, HandleScriptReloadFinished));
  135. SubscribeToEvent(scriptFile_, E_RELOADFAILED, HANDLER(Urho3DPlayer, HandleScriptReloadFailed));
  136. return;
  137. }
  138. #else
  139. ErrorExit("AngelScript is not enabled!");
  140. return;
  141. #endif
  142. }
  143. else
  144. {
  145. #ifdef URHO3D_LUA
  146. // Instantiate and register the Lua script subsystem
  147. LuaScript* luaScript = new LuaScript(context_);
  148. context_->RegisterSubsystem(luaScript);
  149. // If script loading is successful, proceed to main loop
  150. if (luaScript->ExecuteFile(scriptFileName_))
  151. {
  152. luaScript->ExecuteFunction("Start");
  153. return;
  154. }
  155. #else
  156. ErrorExit("Lua is not enabled!");
  157. return;
  158. #endif
  159. }
  160. // The script was not successfully loaded. Show the last error message and do not run the main loop
  161. ErrorExit();
  162. }
  163. void Urho3DPlayer::Stop()
  164. {
  165. #ifdef URHO3D_ANGELSCRIPT
  166. if (scriptFile_)
  167. {
  168. // Execute the optional stop function
  169. if (scriptFile_->GetFunction("void Stop()"))
  170. scriptFile_->Execute("void Stop()");
  171. }
  172. #else
  173. if (false)
  174. {
  175. }
  176. #endif
  177. #ifdef URHO3D_LUA
  178. else
  179. {
  180. LuaScript* luaScript = GetSubsystem<LuaScript>();
  181. if (luaScript && luaScript->GetFunction("Stop", true))
  182. luaScript->ExecuteFunction("Stop");
  183. }
  184. #endif
  185. }
  186. void Urho3DPlayer::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  187. {
  188. #ifdef URHO3D_ANGELSCRIPT
  189. if (scriptFile_->GetFunction("void Stop()"))
  190. scriptFile_->Execute("void Stop()");
  191. #endif
  192. }
  193. void Urho3DPlayer::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  194. {
  195. #ifdef URHO3D_ANGELSCRIPT
  196. // Restart the script application after reload
  197. if (!scriptFile_->Execute("void Start()"))
  198. {
  199. scriptFile_.Reset();
  200. ErrorExit();
  201. }
  202. #endif
  203. }
  204. void Urho3DPlayer::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  205. {
  206. #ifdef URHO3D_ANGELSCRIPT
  207. scriptFile_.Reset();
  208. ErrorExit();
  209. #endif
  210. }