AtomicPlayer.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <Atomic/Atomic.h>
  23. #include <Atomic/Engine/Engine.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/IO/Log.h>
  26. #include <Atomic/Core/Main.h>
  27. #include <Atomic/Core/ProcessUtils.h>
  28. #include <Atomic/Resource/ResourceCache.h>
  29. #include <Atomic/Resource/ResourceEvents.h>
  30. // Move me
  31. #include <Atomic/Environment/Environment.h>
  32. #include <Atomic/Javascript/Javascript.h>
  33. #include "AtomicPlayer.h"
  34. #include <Atomic/DebugNew.h>
  35. #include <Atomic/UI/TBUI.h>
  36. #ifdef EMSCRIPTEN
  37. #include "emscripten.h"
  38. class EmscriptenApp
  39. {
  40. public:
  41. Atomic::SharedPtr<Atomic::Context> context_;
  42. Atomic::SharedPtr<AtomicPlayer> application_;
  43. static EmscriptenApp* sInstance_;
  44. EmscriptenApp() : context_(new Atomic::Context()), application_(new AtomicPlayer(context_))
  45. {
  46. sInstance_ = this;
  47. }
  48. };
  49. EmscriptenApp* EmscriptenApp::sInstance_ = NULL;
  50. static void RunFrame()
  51. {
  52. Engine* engine = EmscriptenApp::sInstance_->application_->GetSubsystem<Engine>();
  53. if (engine->IsInitialized())
  54. engine->RunFrame();
  55. else
  56. printf("ENGINE NOT INITIALIZED\n");
  57. }
  58. int main(int argc, char** argv)
  59. {
  60. Atomic::ParseArguments(argc, argv);
  61. // leak
  62. new EmscriptenApp();
  63. EmscriptenApp::sInstance_->application_->Run();
  64. emscripten_set_main_loop(RunFrame, 0, 1);
  65. /*
  66. int firefox = EM_ASM_INT ( return ((navigator.userAgent.toLowerCase().indexOf('firefox') > -1) ? 1 : 0), 0);
  67. if (firefox)
  68. emscripten_set_main_loop(RunFrame, 0, 1);
  69. else
  70. emscripten_set_main_loop(RunFrame, 60, 1);
  71. */
  72. return 0;
  73. }
  74. #else
  75. DEFINE_APPLICATION_MAIN(AtomicPlayer);
  76. #endif
  77. // fixme
  78. static JSVM* vm = NULL;
  79. static Javascript* javascript = NULL;
  80. AtomicPlayer::AtomicPlayer(Context* context) :
  81. Application(context)
  82. {
  83. RegisterEnvironmenttLibrary(context_);
  84. }
  85. void AtomicPlayer::Setup()
  86. {
  87. FileSystem* filesystem = GetSubsystem<FileSystem>();
  88. #ifdef EMSCRIPTEN
  89. engineParameters_["WindowWidth"] = 1280;
  90. engineParameters_["WindowHeight"] = 720;
  91. engineParameters_["FullScreen"] = false;
  92. #endif
  93. engineParameters_["WindowTitle"] = "AtomicPlayer";
  94. #if (ATOMIC_PLATFORM_ANDROID)
  95. engineParameters_["FullScreen"] = true;
  96. engineParameters_["ResourcePaths"] = "CoreData;Data;AtomicResources";
  97. #else
  98. engineParameters_["ResourcePaths"] = "AtomicResources";
  99. engineParameters_["FullScreen"] = false;
  100. engineParameters_["WindowWidth"] = 1280;
  101. engineParameters_["WindowHeight"] = 720;
  102. #endif
  103. #if ATOMIC_PLATFORM_WINDOWS
  104. engineParameters_["ResourcePrefixPath"] = "AtomicPlayer_Resources";
  105. #elif ATOMIC_PLATFORM_ANDROID
  106. //engineParameters_["ResourcePrefixPath"] = "assets";
  107. #elif ATOMIC_PLATFORM_OSX
  108. engineParameters_["ResourcePrefixPath"] = "../Resources";
  109. #endif
  110. // Use the script file name as the base name for the log file
  111. engineParameters_["LogName"] = filesystem->GetAppPreferencesDir("AtomicPlayer", "Logs") + "AtomicPlayer.log";
  112. }
  113. void AtomicPlayer::Start()
  114. {
  115. // Instantiate and register the Javascript subsystem
  116. javascript = new Javascript(context_);
  117. context_->RegisterSubsystem(javascript);
  118. vm = javascript->InstantiateVM("MainVM");
  119. vm->InitJSContext();
  120. vm->SetModuleSearchPath("Modules");
  121. if (!vm->ExecuteMain())
  122. {
  123. ErrorExit("Error executing Scripts/main.js");
  124. }
  125. return;
  126. }
  127. void AtomicPlayer::Stop()
  128. {
  129. context_->RemoveSubsystem<Javascript>();
  130. }
  131. void AtomicPlayer::HandleScriptReloadStarted(StringHash eventType, VariantMap& eventData)
  132. {
  133. }
  134. void AtomicPlayer::HandleScriptReloadFinished(StringHash eventType, VariantMap& eventData)
  135. {
  136. }
  137. void AtomicPlayer::HandleScriptReloadFailed(StringHash eventType, VariantMap& eventData)
  138. {
  139. ErrorExit();
  140. }