Urho3DPlayer.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Copyright (c) 2008-2016 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. #ifdef URHO3D_ANGELSCRIPT
  23. #include <Urho3D/AngelScript/ScriptFile.h>
  24. #include <Urho3D/AngelScript/Script.h>
  25. #endif
  26. #include <Urho3D/Core/Main.h>
  27. #include <Urho3D/Engine/Engine.h>
  28. #include <Urho3D/IO/FileSystem.h>
  29. #include <Urho3D/IO/Log.h>
  30. #ifdef URHO3D_LUA
  31. #include <Urho3D/LuaScript/LuaScript.h>
  32. #endif
  33. #include <Urho3D/Resource/ResourceCache.h>
  34. #include <Urho3D/Resource/ResourceEvents.h>
  35. #include "Urho3DPlayer.h"
  36. #include <Urho3D/DebugNew.h>
  37. URHO3D_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. // Construct a search path to find the resource prefix with two entries:
  114. // The first entry is an empty path which will be substituted with program/bin directory -- this entry is for binary when it is still in build tree
  115. // The second and third entries are possible relative paths from the installed program/bin directory to the asset directory -- these entries are for binary when it is in the Urho3D SDK installation location
  116. if (!engineParameters_.Contains("ResourcePrefixPaths"))
  117. engineParameters_["ResourcePrefixPaths"] = ";../share/Resources;../share/Urho3D/Resources";
  118. }
  119. void Urho3DPlayer::Start()
  120. {
  121. String extension = GetExtension(scriptFileName_);
  122. if (extension != ".lua" && extension != ".luc")
  123. {
  124. #ifdef URHO3D_ANGELSCRIPT
  125. // Instantiate and register the AngelScript subsystem
  126. context_->RegisterSubsystem(new Script(context_));
  127. // Hold a shared pointer to the script file to make sure it is not unloaded during runtime
  128. scriptFile_ = GetSubsystem<ResourceCache>()->GetResource<ScriptFile>(scriptFileName_);
  129. /// \hack If we are running the editor, also instantiate Lua subsystem to enable editing Lua ScriptInstances
  130. #ifdef URHO3D_LUA
  131. if (scriptFileName_.Contains("Editor.as", false))
  132. context_->RegisterSubsystem(new LuaScript(context_));
  133. #endif
  134. // If script loading is successful, proceed to main loop
  135. if (scriptFile_ && scriptFile_->Execute("void Start()"))
  136. {
  137. // Subscribe to script's reload event to allow live-reload of the application
  138. SubscribeToEvent(scriptFile_, E_RELOADSTARTED, URHO3D_HANDLER(Urho3DPlayer, HandleScriptReloadStarted));
  139. SubscribeToEvent(scriptFile_, E_RELOADFINISHED, URHO3D_HANDLER(Urho3DPlayer, HandleScriptReloadFinished));
  140. SubscribeToEvent(scriptFile_, E_RELOADFAILED, URHO3D_HANDLER(Urho3DPlayer, HandleScriptReloadFailed));
  141. return;
  142. }
  143. #else
  144. ErrorExit("AngelScript is not enabled!");
  145. return;
  146. #endif
  147. }
  148. else
  149. {
  150. #ifdef URHO3D_LUA
  151. // Instantiate and register the Lua script subsystem
  152. LuaScript* luaScript = new LuaScript(context_);
  153. context_->RegisterSubsystem(luaScript);
  154. // If script loading is successful, proceed to main loop
  155. if (luaScript->ExecuteFile(scriptFileName_))
  156. {
  157. luaScript->ExecuteFunction("Start");
  158. return;
  159. }
  160. #else
  161. ErrorExit("Lua is not enabled!");
  162. return;
  163. #endif
  164. }
  165. // The script was not successfully loaded. Show the last error message and do not run the main loop
  166. ErrorExit();
  167. }
  168. void Urho3DPlayer::Stop()
  169. {
  170. #ifdef URHO3D_ANGELSCRIPT
  171. if (scriptFile_)
  172. {
  173. // Execute the optional stop function
  174. if (scriptFile_->GetFunction("void Stop()"))
  175. scriptFile_->Execute("void Stop()");
  176. }
  177. #else
  178. if (false)
  179. {
  180. }
  181. #endif
  182. #ifdef URHO3D_LUA
  183. else
  184. {
  185. LuaScript* luaScript = GetSubsystem<LuaScript>();
  186. if (luaScript && luaScript->GetFunction("Stop", true))
  187. luaScript->ExecuteFunction("Stop");
  188. }
  189. #endif
  190. }
  191. void Urho3DPlayer::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  192. {
  193. #ifdef URHO3D_ANGELSCRIPT
  194. if (scriptFile_->GetFunction("void Stop()"))
  195. scriptFile_->Execute("void Stop()");
  196. #endif
  197. }
  198. void Urho3DPlayer::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  199. {
  200. #ifdef URHO3D_ANGELSCRIPT
  201. // Restart the script application after reload
  202. if (!scriptFile_->Execute("void Start()"))
  203. {
  204. scriptFile_.Reset();
  205. ErrorExit();
  206. }
  207. #endif
  208. }
  209. void Urho3DPlayer::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  210. {
  211. #ifdef URHO3D_ANGELSCRIPT
  212. scriptFile_.Reset();
  213. ErrorExit();
  214. #endif
  215. }