Sample.inl 6.2 KB

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