| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- //
- // Copyright (c) 2008-2013 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Application.h"
- #include "Console.h"
- #include "DebugHud.h"
- #include "Engine.h"
- #include "InputEvents.h"
- #include "Renderer.h"
- #include "ResourceCache.h"
- #include "Sprite.h"
- #include "Texture2D.h"
- #include "UI.h"
- #include "XMLFile.h"
- Sample::Sample(Context* context) :
- Application(context)
- {
- }
- void Sample::Setup()
- {
- // Modify engine startup parameters
- engineParameters_["WindowTitle"] = GetTypeName();
- engineParameters_["LogName"] = GetTypeName() + ".log";
- engineParameters_["FullScreen"] = false;
- engineParameters_["Headless"] = false;
- }
- void Sample::Start()
- {
- // Create logo
- CreateLogo();
- // Create console and debug HUD
- CreateConsoleAndDebugHud();
- // Subscribe key down event
- SubscribeToEvent(E_KEYDOWN, HANDLER(Sample, HandleKeyDown));
- }
- void Sample::SetLogoVisible(bool enable)
- {
- if (logoSprite_)
- logoSprite_->SetVisible(enable);
- }
- void Sample::CreateLogo()
- {
- // Get logo texture
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- Texture2D* logoTexture = cache->GetResource<Texture2D>("Textures/LogoLarge.png");
- if (!logoTexture)
- return;
- // Create logo sprite and add to the UI layout
- UI* ui = GetSubsystem<UI>();
- logoSprite_ = ui->GetRoot()->CreateChild<Sprite>();
- // Set logo sprite texture
- logoSprite_->SetTexture(logoTexture);
- int textureWidth = logoTexture->GetWidth();
- int textureHeight = logoTexture->GetHeight();
- // Set logo sprite scale
- logoSprite_->SetScale(256.0f / textureWidth);
- // Set logo sprite size
- logoSprite_->SetSize(textureWidth, textureHeight);
- // Set logo sprite hot spot
- logoSprite_->SetHotSpot(0, textureHeight);
- // Set logo sprite alignment
- logoSprite_->SetAlignment(HA_LEFT, VA_BOTTOM);
-
- // Make logo not fully opaque to show the scene underneath
- logoSprite_->SetOpacity(0.75f);
-
- // Set a low priority for the logo so that other UI elements can be drawn on top
- logoSprite_->SetPriority(-100);
- }
- void Sample::CreateConsoleAndDebugHud()
- {
- // Get default style
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
- // Create console
- Console* console = engine_->CreateConsole();
- console->SetDefaultStyle(xmlFile);
- // Create debug HUD.
- DebugHud* debugHud = engine_->CreateDebugHud();
- debugHud->SetDefaultStyle(xmlFile);
- }
- void Sample::HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- using namespace KeyDown;
- int key = eventData[P_KEY].GetInt();
- // Close console (if open) or exit when ESC is pressed
- if (key == KEY_ESC)
- {
- Console* console = GetSubsystem<Console>();
- if (!console->IsVisible())
- engine_->Exit();
- else
- console->SetVisible(false);
- }
- // Toggle console with F1
- else if (key == KEY_F1)
- GetSubsystem<Console>()->Toggle();
-
- // Toggle debug HUD with F2
- else if (key == KEY_F2)
- GetSubsystem<DebugHud>()->ToggleAll();
-
- // Common rendering quality controls, only when UI has no focused element
- else if (!GetSubsystem<UI>()->GetFocusElement())
- {
- Renderer* renderer = GetSubsystem<Renderer>();
-
- // Texture quality
- if (key == '1')
- {
- int quality = renderer->GetTextureQuality();
- ++quality;
- if (quality > QUALITY_HIGH)
- quality = QUALITY_LOW;
- renderer->SetTextureQuality(quality);
- }
-
- // Material quality
- else if (key == '2')
- {
- int quality = renderer->GetMaterialQuality();
- ++quality;
- if (quality > QUALITY_HIGH)
- quality = QUALITY_LOW;
- renderer->SetMaterialQuality(quality);
- }
-
- // Specular lighting
- else if (key == '3')
- renderer->SetSpecularLighting(!renderer->GetSpecularLighting());
-
- // Shadow rendering
- else if (key == '4')
- renderer->SetDrawShadows(!renderer->GetDrawShadows());
-
- // Shadow map resolution
- else if (key == '5')
- {
- int shadowMapSize = renderer->GetShadowMapSize();
- shadowMapSize *= 2;
- if (shadowMapSize > 2048)
- shadowMapSize = 512;
- renderer->SetShadowMapSize(shadowMapSize);
- }
-
- // Shadow depth and filtering quality
- else if (key == '6')
- {
- int quality = renderer->GetShadowQuality();
- ++quality;
- if (quality > SHADOWQUALITY_HIGH_24BIT)
- quality = SHADOWQUALITY_LOW_16BIT;
- renderer->SetShadowQuality(quality);
- }
-
- // Occlusion culling
- else if (key == '7')
- {
- bool occlusion = renderer->GetMaxOccluderTriangles() > 0;
- occlusion = !occlusion;
- renderer->SetMaxOccluderTriangles(occlusion ? 5000 : 0);
- }
-
- // Instancing
- else if (key == '8')
- renderer->SetDynamicInstancing(!renderer->GetDynamicInstancing());
- }
- }
|