Jelajahi Sumber

Added Sample base class for samples, adapted from Aster Jian's code.

Lasse Öörni 12 tahun lalu
induk
melakukan
5136dcab3f

+ 1 - 1
Engine/Engine/Application.cpp

@@ -34,7 +34,7 @@ Application::Application(Context* context) :
 {
     engineParameters_ = Engine::ParseParameters(GetArguments());
 
-    // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point.
+    // Create the Engine, but do not initialize it yet. Subsystems except Graphics & Renderer are registered at this point
     engine_ = new Engine(context);
 }
 

+ 1 - 1
Engine/Engine/Application.h

@@ -46,7 +46,7 @@ public:
     /// Setup before engine initialization. This is a chance to eg. modify the engine parameters. Return nonzero to terminate with an error exit code without initializing the engine.
     virtual int Setup() { return 0; }
 
-    /// Startup after engine initialization and before running the main loop. Return nonzero to terminate with an error exit code without running the main loop.
+    /// Setup after engine initialization and before running the main loop. Return nonzero to terminate with an error exit code without running the main loop.
     virtual int Start() { return 0; }
 
     /// Cleanup after the main loop. Return the exit code for the application.

+ 1 - 1
Extras/CharacterDemo/CharacterDemo.h

@@ -43,7 +43,7 @@ public:
     /// Construct.
     CharacterDemo(Context* context);
     
-    /// Startup after engine initialization and before running the main loop.
+    /// Setup after engine initialization and before running the main loop.
     virtual int Start();
     
 private:

+ 8 - 13
Samples/01_HelloWorld/HelloWorld.cpp

@@ -36,17 +36,15 @@
 DEFINE_APPLICATION_MAIN(HelloWorld)
 
 HelloWorld::HelloWorld(Context* context) :
-    Application(context),
-    cache_(GetSubsystem<ResourceCache>())
+    Sample(context)
 {
-    // Modify engine startup parameters
-    engineParameters_["WindowTitle"] = GetTypeName();
-    engineParameters_["LogName"]     = GetTypeName() + ".log";
-    engineParameters_["FullScreen"]  = false;
 }
 
 int HelloWorld::Start()
 {
+    // Execute base class startup
+    Sample::Start();
+
     // Create "Hello World" Text
     CreateText();
 
@@ -59,6 +57,8 @@ int HelloWorld::Start()
 
 void HelloWorld::CreateText()
 {
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+
     // Construct new Text object
     SharedPtr<Text> helloText(new Text(context_));
 
@@ -66,7 +66,7 @@ void HelloWorld::CreateText()
     helloText->SetText("Hello World from Urho3D!");
 
     // Set font and text color
-    helloText->SetFont(cache_->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
+    helloText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 30);
     helloText->SetColor(Color(0.0f, 1.0f, 0.0f));
 
     // Align Text center-screen
@@ -85,10 +85,5 @@ void HelloWorld::SubscribeToEvents()
 
 void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
 {
-    // Determine whether the escape key was pressed...
-    if (GetSubsystem<Input>()->GetKeyPress(KEY_ESC))
-    {
-        // ...if so, request Engine exit
-        engine_->Exit();
-    }
+    // Do nothing for now, could be extended to eg. animate the display
 }

+ 4 - 8
Samples/01_HelloWorld/HelloWorld.h

@@ -22,8 +22,7 @@
 
 #pragma once
 
-#include "Application.h"
-#include "ResourceCache.h"
+#include "Sample.h"
 
 // All Urho3D classes reside in namespace Urho3D
 using namespace Urho3D;
@@ -33,16 +32,16 @@ using namespace Urho3D;
 ///     - Initialization of the Urho3D engine;
 ///     - Adding a Text element to the graphical user interface;
 ///     - Subscribing to and handling of update events;
-class HelloWorld : public Application
+class HelloWorld : public Sample
 {
     // Mandatory when deriving from Object, enables type information
     OBJECT(HelloWorld)
 
 public:
-    /// Construct. Note that Engine is not yet initialized at this point, so must defer actual startup for later.
+    /// Construct.
     HelloWorld(Context* context);
 
-    /// Startup after engine initialization.
+    /// Setup after engine initialization and before running the main loop.
     virtual int Start();
 
 private:
@@ -55,7 +54,4 @@ private:
 
     /// Callback method invoked when a logic update event is dispatched.
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
-
-    /// Pointer to ResourceCache instance for loading the used Font.
-    SharedPtr<ResourceCache> cache_;
 };

+ 1 - 1
Samples/CMakeLists.txt

@@ -2,7 +2,7 @@
 include_directories(${CMAKE_CURRENT_SOURCE_DIR})
 
 # Include common to all samples
-set (H_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Run.h")
+set (H_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Sample.h" "${CMAKE_CURRENT_SOURCE_DIR}/Sample.inl")
 
 # Add samples
 add_subdirectory(01_HelloWorld)

+ 70 - 0
Samples/Sample.h

@@ -0,0 +1,70 @@
+//
+// 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.
+//
+
+#pragma once
+
+#include "Application.h"
+#include "Sprite.h"
+
+// All Urho3D classes reside in namespace Urho3D
+using namespace Urho3D;
+
+/// Sample class, as framework for all samples.
+///    - Initialization of the Urho3D engine (in Application class)
+///    - Modify engine parameters for windowed mode and to show the class name as title
+///    - Create Urho3D logo at screen;
+///    - Create Console and Debug HUD, and use F1 and F2 key to toggle them;
+///    - Handle Esc key down to hide Console or exit application;
+class Sample : public Application
+{
+    // Mandatory when deriving from Object, enables type information
+    OBJECT(Sample)
+
+public:
+    /// Construct.
+    Sample(Context* context);
+
+    /// Setup before engine initialization. Modifies the engine parameters.
+    virtual int Setup();
+
+    /// Setup after engine initialization. Creates the logo, console & debug HUD.
+    virtual int Start();
+
+    /// Control logo visibility.
+    void SetLogoVisible(bool enable);
+
+protected:
+    /// Logo sprite.
+    SharedPtr<Sprite> logoSprite_;
+
+private:
+    /// Create logo.
+    void CreateLogo();
+
+    /// Create console and debug HUD.
+    void CreateConsoleAndDebugHud();
+
+    /// Handle key down event.
+    void HandleKeyDown(StringHash eventType, VariantMap& eventData);
+};
+
+#include "Sample.inl"

+ 138 - 0
Samples/Sample.inl

@@ -0,0 +1,138 @@
+//
+// 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 "ResourceCache.h"
+#include "Texture2D.h"
+#include "UI.h"
+#include "XMLFile.h"
+
+Sample::Sample(Context* context) :
+    Application(context)
+{
+}
+
+int Sample::Setup()
+{
+    // Modify engine startup parameters
+    engineParameters_["WindowTitle"] = GetTypeName();
+    engineParameters_["LogName"]     = GetTypeName() + ".log";
+    engineParameters_["FullScreen"]  = false;
+
+    // Go on to engine initialization
+    return EXIT_SUCCESS;
+}
+
+int Sample::Start()
+{
+    // Create logo
+    CreateLogo();
+
+    // Create console and debug HUD
+    CreateConsoleAndDebugHud();
+
+    // Subscribe key down event
+    SubscribeToEvent(E_KEYDOWN, HANDLER(Sample, HandleKeyDown));
+
+    // Go on to main loop
+    return EXIT_SUCCESS;
+}
+
+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);
+}
+
+void Sample::CreateConsoleAndDebugHud()
+{
+    // Get default style
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
+    if (!xmlFile)
+        return;
+
+    // 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();
+    if (key == KEY_ESC)
+    {
+        if (!GetSubsystem<UI>()->GetFocusElement())
+            engine_->Exit();
+        else
+            GetSubsystem<Console>()->SetVisible(false);
+    }
+
+    // Toggle console when F1 down.
+    if (key == KEY_F1)
+        GetSubsystem<Console>()->Toggle();
+    
+    // Toggle debug HUD when F2 down.
+    if (key == KEY_F2)
+        GetSubsystem<DebugHud>()->ToggleAll();
+}