Sample.inl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "Application.h"
  23. #include "Console.h"
  24. #include "DebugHud.h"
  25. #include "Engine.h"
  26. #include "FileSystem.h"
  27. #include "Graphics.h"
  28. #include "InputEvents.h"
  29. #include "Renderer.h"
  30. #include "ResourceCache.h"
  31. #include "Sprite.h"
  32. #include "Texture2D.h"
  33. #include "Timer.h"
  34. #include "UI.h"
  35. #include "XMLFile.h"
  36. Sample::Sample(Context* context) :
  37. Application(context)
  38. {
  39. }
  40. void Sample::Setup()
  41. {
  42. // Modify engine startup parameters
  43. engineParameters_["WindowTitle"] = GetTypeName();
  44. engineParameters_["LogName"] = GetTypeName() + ".log";
  45. engineParameters_["FullScreen"] = false;
  46. engineParameters_["Headless"] = false;
  47. }
  48. void Sample::Start()
  49. {
  50. // Create logo
  51. CreateLogo();
  52. // Set custom window Title & Icon
  53. SetWindowTitleAndIcon();
  54. // Create console and debug HUD
  55. CreateConsoleAndDebugHud();
  56. // Subscribe key down event
  57. SubscribeToEvent(E_KEYDOWN, HANDLER(Sample, HandleKeyDown));
  58. }
  59. void Sample::SetLogoVisible(bool enable)
  60. {
  61. if (logoSprite_)
  62. logoSprite_->SetVisible(enable);
  63. }
  64. void Sample::CreateLogo()
  65. {
  66. // Get logo texture
  67. ResourceCache* cache = GetSubsystem<ResourceCache>();
  68. Texture2D* logoTexture = cache->GetResource<Texture2D>("Textures/LogoLarge.png");
  69. if (!logoTexture)
  70. return;
  71. // Create logo sprite and add to the UI layout
  72. UI* ui = GetSubsystem<UI>();
  73. logoSprite_ = ui->GetRoot()->CreateChild<Sprite>();
  74. // Set logo sprite texture
  75. logoSprite_->SetTexture(logoTexture);
  76. int textureWidth = logoTexture->GetWidth();
  77. int textureHeight = logoTexture->GetHeight();
  78. // Set logo sprite scale
  79. logoSprite_->SetScale(256.0f / textureWidth);
  80. // Set logo sprite size
  81. logoSprite_->SetSize(textureWidth, textureHeight);
  82. // Set logo sprite hot spot
  83. logoSprite_->SetHotSpot(0, textureHeight);
  84. // Set logo sprite alignment
  85. logoSprite_->SetAlignment(HA_LEFT, VA_BOTTOM);
  86. // Make logo not fully opaque to show the scene underneath
  87. logoSprite_->SetOpacity(0.75f);
  88. // Set a low priority for the logo so that other UI elements can be drawn on top
  89. logoSprite_->SetPriority(-100);
  90. }
  91. void Sample::SetWindowTitleAndIcon()
  92. {
  93. ResourceCache* cache = GetSubsystem<ResourceCache>();
  94. Graphics* graphics = GetSubsystem<Graphics>();
  95. Image* icon = cache->GetResource<Image>("Textures/UrhoIcon.png");
  96. graphics->SetWindowIcon(icon);
  97. graphics->SetWindowTitle("Urho3D Sample");
  98. }
  99. void Sample::CreateConsoleAndDebugHud()
  100. {
  101. // Get default style
  102. ResourceCache* cache = GetSubsystem<ResourceCache>();
  103. XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  104. // Create console
  105. Console* console = engine_->CreateConsole();
  106. console->SetDefaultStyle(xmlFile);
  107. // Create debug HUD.
  108. DebugHud* debugHud = engine_->CreateDebugHud();
  109. debugHud->SetDefaultStyle(xmlFile);
  110. }
  111. void Sample::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  112. {
  113. using namespace KeyDown;
  114. int key = eventData[P_KEY].GetInt();
  115. // Close console (if open) or exit when ESC is pressed
  116. if (key == KEY_ESC)
  117. {
  118. Console* console = GetSubsystem<Console>();
  119. if (console->IsVisible())
  120. console->SetVisible(false);
  121. else
  122. engine_->Exit();
  123. }
  124. // Toggle console with F1
  125. else if (key == KEY_F1)
  126. GetSubsystem<Console>()->Toggle();
  127. // Toggle debug HUD with F2
  128. else if (key == KEY_F2)
  129. GetSubsystem<DebugHud>()->ToggleAll();
  130. // Common rendering quality controls, only when UI has no focused element
  131. else if (!GetSubsystem<UI>()->GetFocusElement())
  132. {
  133. Renderer* renderer = GetSubsystem<Renderer>();
  134. // Texture quality
  135. if (key == '1')
  136. {
  137. int quality = renderer->GetTextureQuality();
  138. ++quality;
  139. if (quality > QUALITY_HIGH)
  140. quality = QUALITY_LOW;
  141. renderer->SetTextureQuality(quality);
  142. }
  143. // Material quality
  144. else if (key == '2')
  145. {
  146. int quality = renderer->GetMaterialQuality();
  147. ++quality;
  148. if (quality > QUALITY_HIGH)
  149. quality = QUALITY_LOW;
  150. renderer->SetMaterialQuality(quality);
  151. }
  152. // Specular lighting
  153. else if (key == '3')
  154. renderer->SetSpecularLighting(!renderer->GetSpecularLighting());
  155. // Shadow rendering
  156. else if (key == '4')
  157. renderer->SetDrawShadows(!renderer->GetDrawShadows());
  158. // Shadow map resolution
  159. else if (key == '5')
  160. {
  161. int shadowMapSize = renderer->GetShadowMapSize();
  162. shadowMapSize *= 2;
  163. if (shadowMapSize > 2048)
  164. shadowMapSize = 512;
  165. renderer->SetShadowMapSize(shadowMapSize);
  166. }
  167. // Shadow depth and filtering quality
  168. else if (key == '6')
  169. {
  170. int quality = renderer->GetShadowQuality();
  171. ++quality;
  172. if (quality > SHADOWQUALITY_HIGH_24BIT)
  173. quality = SHADOWQUALITY_LOW_16BIT;
  174. renderer->SetShadowQuality(quality);
  175. }
  176. // Occlusion culling
  177. else if (key == '7')
  178. {
  179. bool occlusion = renderer->GetMaxOccluderTriangles() > 0;
  180. occlusion = !occlusion;
  181. renderer->SetMaxOccluderTriangles(occlusion ? 5000 : 0);
  182. }
  183. // Instancing
  184. else if (key == '8')
  185. renderer->SetDynamicInstancing(!renderer->GetDynamicInstancing());
  186. // Take screenshot
  187. else if (key == '9')
  188. {
  189. Graphics* graphics = GetSubsystem<Graphics>();
  190. Image screenshot(context_);
  191. graphics->TakeScreenShot(screenshot);
  192. // Here we save in the Data folder with date and time appended
  193. screenshot.SavePNG(GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Screenshot_" +
  194. Time::GetTimeStamp().Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  195. }
  196. }
  197. }