Sample.inl 6.2 KB

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