AtomicPlayer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Atomic.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Engine/EngineConfig.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include <Atomic/IO/Log.h>
  28. #include <Atomic/IO/IOEvents.h>
  29. #include <Atomic/Input/InputEvents.h>
  30. #include <Atomic/Core/Main.h>
  31. #include <Atomic/Core/ProcessUtils.h>
  32. #include <Atomic/Resource/ResourceCache.h>
  33. #include <Atomic/Resource/ResourceEvents.h>
  34. #include <Atomic/Script/ScriptSystem.h>
  35. #include <Atomic/UI/UI.h>
  36. // Move me
  37. #include <Atomic/Environment/Environment.h>
  38. #include <AtomicJS/Javascript/Javascript.h>
  39. #include <AtomicPlayer/Player.h>
  40. #include "AtomicPlayer.h"
  41. #include <Atomic/DebugNew.h>
  42. #ifdef __APPLE__
  43. #include <unistd.h>
  44. #endif
  45. ATOMIC_DEFINE_APPLICATION_MAIN(AtomicPlayer::AtomicPlayerApp)
  46. namespace AtomicPlayer
  47. {
  48. extern void jsapi_init_atomicplayer(JSVM* vm);
  49. AtomicPlayerApp::AtomicPlayerApp(Context* context) :
  50. Application(context)
  51. {
  52. }
  53. void AtomicPlayerApp::Setup()
  54. {
  55. #ifdef ATOMIC_3D
  56. RegisterEnvironmentLibrary(context_);
  57. #endif
  58. FileSystem* filesystem = GetSubsystem<FileSystem>();
  59. context_->RegisterSubsystem(new ScriptSystem(context_));
  60. // Read the engine configuration
  61. ReadEngineConfig();
  62. engineParameters_.InsertNew("WindowTitle", "AtomicPlayer");
  63. #if (ATOMIC_PLATFORM_ANDROID)
  64. engineParameters_.InsertNew("FullScreen", true);
  65. engineParameters_.InsertNew("ResourcePaths", "CoreData;PlayerData;Cache;AtomicResources");
  66. #elif ATOMIC_PLATFORM_WEB
  67. engineParameters_.InsertNew("FullScreen", false);
  68. engineParameters_.InsertNew("ResourcePaths", "AtomicResources");
  69. // engineParameters_.InsertNew("WindowWidth", 1280);
  70. // engineParameters_.InsertNew("WindowHeight", 720);
  71. #elif ATOMIC_PLATFORM_IOS
  72. engineParameters_.InsertNew("FullScreen", false);
  73. engineParameters_.InsertNew("ResourcePaths", "AtomicResources");
  74. #else
  75. engineParameters_.InsertNew("FullScreen", false);
  76. engineParameters_.InsertNew("WindowWidth", 1280);
  77. engineParameters_.InsertNew("WindowHeight", 720);
  78. engineParameters_.InsertNew("ResourcePaths", "AtomicResources");
  79. #endif
  80. #if ATOMIC_PLATFORM_WINDOWS || ATOMIC_PLATFORM_LINUX
  81. engineParameters_.InsertNew("WindowIcon", "Images/AtomicLogo32.png");
  82. engineParameters_.InsertNew("ResourcePrefixPaths", "AtomicPlayer_Resources");
  83. #elif ATOMIC_PLATFORM_ANDROID
  84. //engineParameters_.InsertNew("ResourcePrefixPaths"], "assets");
  85. #elif ATOMIC_PLATFORM_OSX
  86. engineParameters_.InsertNew("ResourcePrefixPaths", filesystem->GetProgramDir() + "../Resources");
  87. #endif
  88. const Vector<String>& arguments = GetArguments();
  89. for (unsigned i = 0; i < arguments.Size(); ++i)
  90. {
  91. if (arguments[i].Length() > 1)
  92. {
  93. String argument = arguments[i].ToLower();
  94. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  95. if (argument == "--log-std")
  96. {
  97. SubscribeToEvent(E_LOGMESSAGE, ATOMIC_HANDLER(AtomicPlayerApp, HandleLogMessage));
  98. }
  99. }
  100. }
  101. // Use the script file name as the base name for the log file
  102. engineParameters_.InsertNew("LogName", filesystem->GetAppPreferencesDir("AtomicPlayer", "Logs") + "AtomicPlayer.log");
  103. }
  104. void AtomicPlayerApp::Start()
  105. {
  106. Application::Start();
  107. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  108. // Instantiate and register the Javascript subsystem
  109. Javascript* javascript = new Javascript(context_);
  110. context_->RegisterSubsystem(javascript);
  111. vm_ = javascript->InstantiateVM("MainVM");
  112. vm_->InitJSContext();
  113. UI* ui = GetSubsystem<UI>();
  114. ui->Initialize("DefaultUI/language/lng_en.tb.txt");
  115. ui->LoadDefaultPlayerSkin();
  116. SubscribeToEvent(E_JSERROR, ATOMIC_HANDLER(AtomicPlayerApp, HandleJSError));
  117. vm_->SetModuleSearchPaths("Modules");
  118. // Instantiate and register the Player subsystem
  119. context_->RegisterSubsystem(new AtomicPlayer::Player(context_));
  120. AtomicPlayer::jsapi_init_atomicplayer(vm_);
  121. JSVM* vm = JSVM::GetJSVM(0);
  122. if (!vm->ExecuteMain())
  123. {
  124. SendEvent(E_EXITREQUESTED);
  125. }
  126. return;
  127. }
  128. void AtomicPlayerApp::Stop()
  129. {
  130. vm_ = 0;
  131. context_->RemoveSubsystem<Javascript>();
  132. // make sure JSVM is really down and no outstanding refs
  133. // as if not, will hold on engine subsystems, which is bad
  134. assert(!JSVM::GetJSVM(0));
  135. Application::Stop();
  136. }
  137. void AtomicPlayerApp::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  138. {
  139. }
  140. void AtomicPlayerApp::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  141. {
  142. }
  143. void AtomicPlayerApp::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  144. {
  145. ErrorExit();
  146. }
  147. void AtomicPlayerApp::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  148. {
  149. using namespace LogMessage;
  150. int level = eventData[P_LEVEL].GetInt();
  151. // The message may be multi-line, so split to rows in that case
  152. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  153. for (unsigned i = 0; i < rows.Size(); ++i)
  154. {
  155. if (level == LOG_ERROR)
  156. {
  157. fprintf(stderr, "%s\n", rows[i].CString());
  158. }
  159. else
  160. {
  161. fprintf(stdout, "%s\n", rows[i].CString());
  162. }
  163. }
  164. }
  165. void AtomicPlayerApp::HandleJSError(StringHash eventType, VariantMap& eventData)
  166. {
  167. using namespace JSError;
  168. //String errName = eventData[P_ERRORNAME].GetString();
  169. //String errStack = eventData[P_ERRORSTACK].GetString();
  170. String errMessage = eventData[P_ERRORMESSAGE].GetString();
  171. String errFilename = eventData[P_ERRORFILENAME].GetString();
  172. int errLineNumber = eventData[P_ERRORLINENUMBER].GetInt();
  173. String errorString = ToString("%s - %s - Line: %i",
  174. errFilename.CString(), errMessage.CString(), errLineNumber);
  175. }
  176. void AtomicPlayerApp::ReadEngineConfig()
  177. {
  178. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  179. #ifdef ATOMIC_PLATFORM_OSX
  180. String filename = fileSystem->GetProgramDir() + "../Resources/Settings/Engine.json";
  181. #else
  182. String filename = fileSystem->GetProgramDir() + "Settings/Engine.json";
  183. #endif
  184. if (!fileSystem->FileExists(filename))
  185. return;
  186. if (EngineConfig::LoadFromFile(context_, filename))
  187. {
  188. EngineConfig::ApplyConfig(engineParameters_);
  189. }
  190. }
  191. }