FeatureExamples.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, 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/Engine/Application.h>
  24. #include <Atomic/Graphics/Camera.h>
  25. #include <Atomic/Engine/Engine.h>
  26. #include <Atomic/IO/FileSystem.h>
  27. #include <Atomic/Graphics/Graphics.h>
  28. #include <Atomic/Input/Input.h>
  29. #include <Atomic/Input/InputEvents.h>
  30. #include <Atomic/Graphics/Renderer.h>
  31. #include <Atomic/Resource/ResourceCache.h>
  32. #include <Atomic/Scene/Scene.h>
  33. #include <Atomic/Scene/SceneEvents.h>
  34. #include <Atomic/Graphics/Texture2D.h>
  35. #include <Atomic/Core/Timer.h>
  36. #include <Atomic/UI/UI.h>
  37. #include <Atomic/UI/UIView.h>
  38. #include <Atomic/Resource/XMLFile.h>
  39. #include <Atomic/IO/Log.h>
  40. #include "FeatureExamples.h"
  41. #include "SampleSelector.h"
  42. ATOMIC_DEFINE_APPLICATION_MAIN(FeatureExamples)
  43. WeakPtr<UIView> FeatureExamples::uiView_;
  44. FeatureExamples::FeatureExamples(Context* context) :
  45. Application(context)
  46. {
  47. }
  48. void FeatureExamples::Setup()
  49. {
  50. // Modify engine startup parameters
  51. engineParameters_["WindowTitle"] = GetTypeName();
  52. engineParameters_["WindowWidth"] = 1280;
  53. engineParameters_["WindowHeight"] = 720;
  54. engineParameters_["LogName"] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("atomic", "logs") + GetTypeName() + ".log";
  55. engineParameters_["FullScreen"] = false;
  56. engineParameters_["Headless"] = false;
  57. engineParameters_["Sound"] = false;
  58. engineParameters_["ResourcePaths"] = "Data;PlayerData;CoreData";
  59. // Construct a search path to find the resource prefix with two entries:
  60. // 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
  61. // 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 Atomic SDK installation location
  62. if (!engineParameters_.Contains("ResourcePrefixPaths"))
  63. {
  64. // TODO: This is dependent on a source build
  65. String resourcePrefix = ToString("%s/Resources;%s/Submodules/AtomicExamples/FeatureExamples/CPlusPlus", ATOMIC_ROOT_SOURCE_DIR, ATOMIC_ROOT_SOURCE_DIR);
  66. engineParameters_["ResourcePrefixPaths"] = resourcePrefix;
  67. }
  68. }
  69. void FeatureExamples::Start()
  70. {
  71. UI* ui = GetSubsystem<UI>();
  72. ui->Initialize("DefaultUI/language/lng_en.tb.txt");
  73. ui->LoadDefaultPlayerSkin();
  74. // create new UIView
  75. uiView_ = new UIView(context_);
  76. // Set custom window Title & Icon
  77. SetWindowTitleAndIcon();
  78. new SampleSelector(context_);
  79. SubscribeToEvent(E_KEYDOWN, ATOMIC_HANDLER(FeatureExamples, HandleKeyDown));
  80. }
  81. void FeatureExamples::Stop()
  82. {
  83. engine_->DumpResources(true);
  84. }
  85. void FeatureExamples::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  86. {
  87. using namespace KeyDown;
  88. int key = eventData[P_KEY].GetInt();
  89. // Toggle console with F1
  90. if (key == KEY_F1)
  91. {
  92. GetSubsystem<UI>()->ToggleConsole();
  93. }
  94. // Toggle debug HUD with F2
  95. else if (key == KEY_F2)
  96. {
  97. GetSubsystem<UI>()->ToggleDebugHud();
  98. }
  99. }
  100. void FeatureExamples::SetWindowTitleAndIcon()
  101. {
  102. ResourceCache* cache = GetSubsystem<ResourceCache>();
  103. Graphics* graphics = GetSubsystem<Graphics>();
  104. Image* icon = cache->GetResource<Image>("Textures/UrhoIcon.png");
  105. graphics->SetWindowIcon(icon);
  106. graphics->SetWindowTitle("Atomic Sample");
  107. }