Browse Source

Moved CharacterDemo to the samples.
Use forward declarations in the samples.

Lasse Öörni 12 years ago
parent
commit
04b00263b0
46 changed files with 270 additions and 202 deletions
  1. 0 2
      Docs/GettingStarted.dox
  2. 0 2
      Source/CMakeLists.txt
  3. 5 11
      Source/Extras/Readme.txt
  4. 1 1
      Source/Samples/02_HelloGUI/HelloGUI.cpp
  5. 6 1
      Source/Samples/02_HelloGUI/HelloGUI.h
  6. 1 1
      Source/Samples/03_Sprites/Sprites.cpp
  7. 5 2
      Source/Samples/03_Sprites/Sprites.h
  8. 1 1
      Source/Samples/04_StaticScene/StaticScene.cpp
  9. 6 2
      Source/Samples/04_StaticScene/StaticScene.h
  10. 1 1
      Source/Samples/05_AnimatingScene/AnimatingScene.cpp
  11. 6 2
      Source/Samples/05_AnimatingScene/AnimatingScene.h
  12. 2 0
      Source/Samples/05_AnimatingScene/Rotator.cpp
  13. 2 0
      Source/Samples/06_SkeletalAnimation/Mover.cpp
  14. 0 1
      Source/Samples/06_SkeletalAnimation/Mover.h
  15. 1 1
      Source/Samples/06_SkeletalAnimation/SkeletalAnimation.cpp
  16. 6 1
      Source/Samples/06_SkeletalAnimation/SkeletalAnimation.h
  17. 1 1
      Source/Samples/07_Billboards/Billboards.cpp
  18. 6 2
      Source/Samples/07_Billboards/Billboards.h
  19. 1 1
      Source/Samples/08_Decals/Decals.cpp
  20. 2 2
      Source/Samples/08_Decals/Decals.h
  21. 1 2
      Source/Samples/09_MultipleViewports/MultipleViewports.cpp
  22. 6 2
      Source/Samples/09_MultipleViewports/MultipleViewports.h
  23. 1 1
      Source/Samples/10_RenderToTexture/RenderToTexture.cpp
  24. 6 2
      Source/Samples/10_RenderToTexture/RenderToTexture.h
  25. 2 0
      Source/Samples/10_RenderToTexture/Rotator.cpp
  26. 1 1
      Source/Samples/11_Physics/Physics.cpp
  27. 6 2
      Source/Samples/11_Physics/Physics.h
  28. 1 1
      Source/Samples/12_PhysicsStressTest/PhysicsStressTest.cpp
  29. 6 2
      Source/Samples/12_PhysicsStressTest/PhysicsStressTest.h
  30. 2 0
      Source/Samples/13_Ragdolls/CreateRagdoll.cpp
  31. 0 1
      Source/Samples/13_Ragdolls/CreateRagdoll.h
  32. 1 1
      Source/Samples/13_Ragdolls/Ragdolls.cpp
  33. 6 2
      Source/Samples/13_Ragdolls/Ragdolls.h
  34. 3 1
      Source/Samples/14_SoundEffects/SoundEffects.cpp
  35. 7 3
      Source/Samples/14_SoundEffects/SoundEffects.h
  36. 1 1
      Source/Samples/15_Navigation/Navigation.cpp
  37. 2 2
      Source/Samples/15_Navigation/Navigation.h
  38. 0 1
      Source/Samples/16_Chat/Chat.cpp
  39. 6 11
      Source/Samples/17_SceneReplication/SceneReplication.cpp
  40. 1 2
      Source/Samples/17_SceneReplication/SceneReplication.h
  41. 32 37
      Source/Samples/18_CharacterDemo/CMakeLists.txt
  42. 0 0
      Source/Samples/18_CharacterDemo/Character.cpp
  43. 1 0
      Source/Samples/18_CharacterDemo/Character.h
  44. 110 83
      Source/Samples/18_CharacterDemo/CharacterDemo.cpp
  45. 14 9
      Source/Samples/18_CharacterDemo/CharacterDemo.h
  46. 1 0
      Source/Samples/CMakeLists.txt

+ 0 - 2
Docs/GettingStarted.dox

@@ -774,8 +774,6 @@ void HelloWorld::HandleUpdate(StringHash eventType, VariantMap& eventData)
 
 Now you should be ready to compile HelloWorld.cpp. The resulting executable will be placed in the Bin directory. It should be substantially smaller than the Urho3D application due to leaving out the scripting functionality.
 
-For a more complex C++ example, check out CharacterDemo in the Extras directory, which creates the same static scene as the TestScene script example, and demonstrates a 1st/3rd person controllable character. To enable it in the build, uncomment from the bottom of the root CMakeLists.txt.
-
 
 \page EditorInstructions Editor instructions
 

+ 0 - 2
Source/CMakeLists.txt

@@ -143,10 +143,8 @@ else ()
         endif ()
         
         # Urho3D extras
-        # Todo: CharacterDemo will be moved to Samples
         if (ENABLE_EXTRAS)
             add_subdirectory (Extras/OgreBatchConverter)
-            add_subdirectory (Extras/CharacterDemo)
         endif ()
     endif ()
     

+ 5 - 11
Source/Extras/Readme.txt

@@ -1,8 +1,8 @@
 OgreBatchConverter
 
 - Contributed by Carlo Carollo. Converts multiple Ogre .mesh.xml files (also from 
-  subdirectories) by invoking the OgreImporter tool.
-- Enable in build by uncommenting at the bottom of the Urho3D root CMakeLists.txt
+  subdirectories) by invoking the OgreImporter tool. Use the CMake option
+  ENABLE_EXTRAS to include in the build.
 
 OgreMaxscriptExport
 
@@ -10,14 +10,8 @@ OgreMaxscriptExport
   Exporter from the Ogre SDK that will import Ogre .mesh.xml files (for feeding
   into OgreImporter) and materials in Urho3D .xml format.
 
-CharacterDemo
-
-- A moving character example, with 1st/3rd person camera. Creates the same scene as
-  the TestScene script application.
-- Enable in build by uncommenting at the bottom of the Urho3D root CMakeLists.txt
-
 LuaScript
 
-- Lua bindings for Urho3D. Contributed by Aster Jian. Uncomment ENABLE_LUA near
-  the bottom of Urho3D's root CMakeLists.txt to enable.
-  
+- Lua bindings for Urho3D. Contributed by Aster Jian. Use the CMake option
+  ENABLE_LUA to include in the build.
+

+ 1 - 1
Source/Samples/02_HelloGUI/HelloGUI.cpp

@@ -29,12 +29,12 @@
 #include "Text.h"
 #include "UI.h"
 #include "UIEvents.h"
+#include "Window.h"
 
 #include "HelloGUI.h"
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(HelloGUI)
 
 HelloGUI::HelloGUI(Context* context) :

+ 6 - 1
Source/Samples/02_HelloGUI/HelloGUI.h

@@ -23,7 +23,12 @@
 #pragma once
 
 #include "Sample.h"
-#include "Window.h"
+
+// Do forward declarations for the engine classes we have as member variables
+namespace Urho3D
+{
+    class Window;
+}
 
 using namespace Urho3D;
 

+ 1 - 1
Source/Samples/03_Sprites/Sprites.cpp

@@ -24,6 +24,7 @@
 #include "Engine.h"
 #include "Graphics.h"
 #include "ResourceCache.h"
+#include "Sprite.h"
 #include "Texture2D.h"
 #include "UI.h"
 
@@ -37,7 +38,6 @@ static const unsigned NUM_SPRITES = 100;
 // Custom variable identifier for storing sprite velocity within the UI element
 static const ShortStringHash VAR_VELOCITY("Velocity");
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(Sprites)
 
 Sprites::Sprites(Context* context) :

+ 5 - 2
Source/Samples/03_Sprites/Sprites.h

@@ -23,9 +23,12 @@
 #pragma once
 
 #include "Sample.h"
-#include "Sprite.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Sprite;
+}
+
 using namespace Urho3D;
 
 /// Moving sprites example.

+ 1 - 1
Source/Samples/04_StaticScene/StaticScene.cpp

@@ -31,6 +31,7 @@
 #include "Octree.h"
 #include "Renderer.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -39,7 +40,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(StaticScene)
 
 StaticScene::StaticScene(Context* context) :

+ 6 - 2
Source/Samples/04_StaticScene/StaticScene.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Static 3D scene example.

+ 1 - 1
Source/Samples/05_AnimatingScene/AnimatingScene.cpp

@@ -32,6 +32,7 @@
 #include "Renderer.h"
 #include "ResourceCache.h"
 #include "Rotator.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -41,7 +42,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(AnimatingScene)
 
 AnimatingScene::AnimatingScene(Context* context) :

+ 6 - 2
Source/Samples/05_AnimatingScene/AnimatingScene.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Animating 3D scene example.

+ 2 - 0
Source/Samples/05_AnimatingScene/Rotator.cpp

@@ -24,6 +24,8 @@
 #include "Scene.h"
 #include "SceneEvents.h"
 
+#include "DebugNew.h"
+
 Rotator::Rotator(Context* context) :
     Component(context),
     rotationSpeed_(Vector3::ZERO)

+ 2 - 0
Source/Samples/06_SkeletalAnimation/Mover.cpp

@@ -26,6 +26,8 @@
 #include "Scene.h"
 #include "SceneEvents.h"
 
+#include "DebugNew.h"
+
 Mover::Mover(Context* context) :
     Component(context),
     moveSpeed_(0.0f),

+ 0 - 1
Source/Samples/06_SkeletalAnimation/Mover.h

@@ -24,7 +24,6 @@
 
 #include "Component.h"
 
-// All Urho3D classes reside in namespace Urho3D
 using namespace Urho3D;
 
 /// Custom component for moving the animated model and rotating at area edges.

+ 1 - 1
Source/Samples/06_SkeletalAnimation/SkeletalAnimation.cpp

@@ -37,6 +37,7 @@
 #include "Octree.h"
 #include "Renderer.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "Text.h"
 #include "UI.h"
 #include "Zone.h"
@@ -45,7 +46,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(SkeletalAnimation)
 
 SkeletalAnimation::SkeletalAnimation(Context* context) :

+ 6 - 1
Source/Samples/06_SkeletalAnimation/SkeletalAnimation.h

@@ -23,7 +23,12 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
+
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
 
 // All Urho3D classes reside in namespace Urho3D
 using namespace Urho3D;

+ 1 - 1
Source/Samples/07_Billboards/Billboards.cpp

@@ -34,6 +34,7 @@
 #include "Octree.h"
 #include "Renderer.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -43,7 +44,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(Billboards)
 
 Billboards::Billboards(Context* context) :

+ 6 - 2
Source/Samples/07_Billboards/Billboards.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Billboard example.

+ 1 - 1
Source/Samples/08_Decals/Decals.cpp

@@ -35,6 +35,7 @@
 #include "Octree.h"
 #include "Renderer.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -45,7 +46,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(Decals)
 
 Decals::Decals(Context* context) :

+ 2 - 2
Source/Samples/08_Decals/Decals.h

@@ -23,14 +23,14 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
 namespace Urho3D
 {
     class Drawable;
+    class Node;
+    class Scene;
 }
 
-// All Urho3D classes reside in namespace Urho3D
 using namespace Urho3D;
 
 /// Decals example.

+ 1 - 2
Source/Samples/09_MultipleViewports/MultipleViewports.cpp

@@ -35,6 +35,7 @@
 #include "Renderer.h"
 #include "RenderPath.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -45,7 +46,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(MultipleViewports)
 
 MultipleViewports::MultipleViewports(Context* context) :
@@ -183,7 +183,6 @@ void MultipleViewports::CreateInstructions()
     instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
 }
 
-
 void MultipleViewports::SetupViewports()
 {
     Graphics* graphics = GetSubsystem<Graphics>();

+ 6 - 2
Source/Samples/09_MultipleViewports/MultipleViewports.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Multiple viewports example.

+ 1 - 1
Source/Samples/10_RenderToTexture/RenderToTexture.cpp

@@ -33,6 +33,7 @@
 #include "RenderSurface.h"
 #include "ResourceCache.h"
 #include "Rotator.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Technique.h"
 #include "Text.h"
@@ -44,7 +45,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(RenderToTexture)
 
 RenderToTexture::RenderToTexture(Context* context) :

+ 6 - 2
Source/Samples/10_RenderToTexture/RenderToTexture.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Render to texture example

+ 2 - 0
Source/Samples/10_RenderToTexture/Rotator.cpp

@@ -24,6 +24,8 @@
 #include "Scene.h"
 #include "SceneEvents.h"
 
+#include "DebugNew.h"
+
 Rotator::Rotator(Context* context) :
     Component(context),
     rotationSpeed_(Vector3::ZERO)

+ 1 - 1
Source/Samples/11_Physics/Physics.cpp

@@ -38,6 +38,7 @@
 #include "Renderer.h"
 #include "ResourceCache.h"
 #include "RigidBody.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -47,7 +48,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(Physics)
 
 Physics::Physics(Context* context) :

+ 6 - 2
Source/Samples/11_Physics/Physics.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Physics example.

+ 1 - 1
Source/Samples/12_PhysicsStressTest/PhysicsStressTest.cpp

@@ -38,6 +38,7 @@
 #include "Renderer.h"
 #include "ResourceCache.h"
 #include "RigidBody.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -47,7 +48,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(PhysicsStressTest)
 
 PhysicsStressTest::PhysicsStressTest(Context* context) :

+ 6 - 2
Source/Samples/12_PhysicsStressTest/PhysicsStressTest.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Physics stress test example.

+ 2 - 0
Source/Samples/13_Ragdolls/CreateRagdoll.cpp

@@ -27,6 +27,8 @@
 #include "RigidBody.h"
 #include "PhysicsEvents.h"
 
+#include "DebugNew.h"
+
 CreateRagdoll::CreateRagdoll(Context* context) :
     Component(context)
 {

+ 0 - 1
Source/Samples/13_Ragdolls/CreateRagdoll.h

@@ -25,7 +25,6 @@
 #include "CollisionShape.h"
 #include "Constraint.h"
 
-// All Urho3D classes reside in namespace Urho3D
 using namespace Urho3D;
 
 /// Custom component that creates a ragdoll upon collision.

+ 1 - 1
Source/Samples/13_Ragdolls/Ragdolls.cpp

@@ -40,6 +40,7 @@
 #include "Renderer.h"
 #include "ResourceCache.h"
 #include "RigidBody.h"
+#include "Scene.h"
 #include "Text.h"
 #include "UI.h"
 #include "Zone.h"
@@ -48,7 +49,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(Ragdolls)
 
 Ragdolls::Ragdolls(Context* context) :

+ 6 - 2
Source/Samples/13_Ragdolls/Ragdolls.h

@@ -23,9 +23,13 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
-// All Urho3D classes reside in namespace Urho3D
+namespace Urho3D
+{
+    class Node;
+    class Scene;
+}
+
 using namespace Urho3D;
 
 /// Ragdoll example.

+ 3 - 1
Source/Samples/14_SoundEffects/SoundEffects.cpp

@@ -21,13 +21,16 @@
 //
 
 #include "Audio.h"
+#include "Button.h"
 #include "Engine.h"
 #include "Font.h"
 #include "Input.h"
 #include "Log.h"
 #include "ResourceCache.h"
 #include "Scene.h"
+#include "Slider.h"
 #include "Sound.h"
+#include "SoundSource.h"
 #include "Text.h"
 #include "UI.h"
 #include "UIEvents.h"
@@ -52,7 +55,6 @@ static String soundResourceNames[] = {
     "Sounds/Powerup.wav"
 };
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(SoundEffects)
 
 SoundEffects::SoundEffects(Context* context) :

+ 7 - 3
Source/Samples/14_SoundEffects/SoundEffects.h

@@ -22,10 +22,14 @@
 
 #pragma once
 
-#include "Button.h"
 #include "Sample.h"
-#include "Slider.h"
-#include "SoundSource.h"
+
+namespace Urho3D
+{
+    class Button;
+    class Scene;
+    class Slider;
+}
 
 using namespace Urho3D;
 

+ 1 - 1
Source/Samples/15_Navigation/Navigation.cpp

@@ -36,6 +36,7 @@
 #include "Octree.h"
 #include "Renderer.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -46,7 +47,6 @@
 
 #include "DebugNew.h"
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(Navigation)
 
 Navigation::Navigation(Context* context) :

+ 2 - 2
Source/Samples/15_Navigation/Navigation.h

@@ -23,14 +23,14 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
 namespace Urho3D
 {
     class Drawable;
+    class Node;
+    class Scene;
 }
 
-// All Urho3D classes reside in namespace Urho3D
 using namespace Urho3D;
 
 /// Navigation example.

+ 0 - 1
Source/Samples/16_Chat/Chat.cpp

@@ -55,7 +55,6 @@ const int MSG_CHAT = 32;
 // UDP port we will use
 const unsigned short CHAT_SERVER_PORT = 2345;
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(Chat)
 
 Chat::Chat(Context* context) :

+ 6 - 11
Source/Samples/17_SceneReplication/SceneReplication.cpp

@@ -44,6 +44,7 @@
 #include "Renderer.h"
 #include "RigidBody.h"
 #include "ResourceCache.h"
+#include "Scene.h"
 #include "StaticModel.h"
 #include "Text.h"
 #include "UI.h"
@@ -68,7 +69,6 @@ static const unsigned CTRL_BACK = 2;
 static const unsigned CTRL_LEFT = 4;
 static const unsigned CTRL_RIGHT = 8;
 
-// Expands to this example's entry-point
 DEFINE_APPLICATION_MAIN(SceneReplication)
 
 SceneReplication::SceneReplication(Context* context) :
@@ -355,21 +355,16 @@ void SceneReplication::HandlePhysicsPreStep(StringHash eventType, VariantMap& ev
         Input* input = GetSubsystem<Input>();
         Controls controls;
         
-        // Assume buttons are not pressed, copy yaw
-        controls.buttons_ = 0;
+        // Copy mouse yaw
         controls.yaw_ = yaw_;
         
         // Only apply WASD controls if there is no focused UI element
         if (!ui->GetFocusElement())
         {
-            if (input->GetKeyDown('W'))
-                controls.buttons_ |= CTRL_FORWARD;
-            if (input->GetKeyDown('S'))
-                controls.buttons_ |= CTRL_BACK;
-            if (input->GetKeyDown('A'))
-                controls.buttons_ |= CTRL_LEFT;
-            if (input->GetKeyDown('D'))
-                controls.buttons_ |= CTRL_RIGHT;
+            controls.Set(CTRL_FORWARD, input->GetKeyDown('W'));
+            controls.Set(CTRL_BACK, input->GetKeyDown('S'));
+            controls.Set(CTRL_LEFT, input->GetKeyDown('A'));
+            controls.Set(CTRL_RIGHT, input->GetKeyDown('D'));
         }
         
         serverConnection->SetControls(controls);

+ 1 - 2
Source/Samples/17_SceneReplication/SceneReplication.h

@@ -23,17 +23,16 @@
 #pragma once
 
 #include "Sample.h"
-#include "Scene.h"
 
 namespace Urho3D
 {
     class Button;
     class Connection;
+    class Scene;
     class Text;
     class UIElement;
 }
 
-// All Urho3D classes reside in namespace Urho3D
 using namespace Urho3D;
 
 /// Scene network replication example.

+ 32 - 37
Source/Extras/CharacterDemo/CMakeLists.txt → Source/Samples/18_CharacterDemo/CMakeLists.txt

@@ -1,37 +1,32 @@
-#
-# 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.
-#
-
-# Define target name
-set (TARGET_NAME CharacterDemo)
-
-# Define source files
-file (GLOB CPP_FILES *.cpp)
-file (GLOB H_FILES *.h)
-set (SOURCE_FILES ${CPP_FILES} ${H_FILES})
-
-# Define dependency libs
-set (LIBS ../../Engine/Container ../../Engine/Core ../../Engine/Engine ../../Engine/Graphics ../../Engine/Input ../../Engine/IO ../../Engine/Math 
-    ../../Engine/Network ../../Engine/Physics ../../Engine/Resource ../../Engine/Scene ../../Engine/UI)
-set (INCLUDE_DIRS_ONLY ../../ThirdParty/Bullet/src)
-
-# Setup target with resource copying
-setup_main_executable ()
+#
+# 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.
+#
+
+# Define target name
+set (TARGET_NAME 18_CharacterDemo)
+
+# Define source files
+file (GLOB CPP_FILES *.cpp)
+file (GLOB H_FILES *.h)
+set (SOURCE_FILES ${CPP_FILES} ${H_FILES} ${COMMON_SAMPLE_H_FILES})
+
+# Setup target with resource copying
+setup_main_executable ()

+ 0 - 0
Source/Extras/CharacterDemo/Character.cpp → Source/Samples/18_CharacterDemo/Character.cpp


+ 1 - 0
Source/Extras/CharacterDemo/Character.h → Source/Samples/18_CharacterDemo/Character.h

@@ -42,6 +42,7 @@ const float JUMP_FORCE = 7.0f;
 const float YAW_SENSITIVITY = 0.1f;
 const float INAIR_THRESHOLD_TIME = 0.1f;
 
+/// Character component, responsible for physical movement according to controls, as well as animation.
 class Character : public Component
 {
     OBJECT(Character)

+ 110 - 83
Source/Extras/CharacterDemo/CharacterDemo.cpp → Source/Samples/18_CharacterDemo/CharacterDemo.cpp

@@ -5,7 +5,6 @@
 #include "CollisionShape.h"
 #include "Controls.h"
 #include "CoreEvents.h"
-#include "DebugRenderer.h"
 #include "Engine.h"
 #include "Font.h"
 #include "Input.h"
@@ -34,8 +33,7 @@ const float CAMERA_MAX_DIST = 5.0f;
 DEFINE_APPLICATION_MAIN(CharacterDemo)
 
 CharacterDemo::CharacterDemo(Context* context) :
-    Application(context),
-    cache_(GetSubsystem<ResourceCache>()),
+    Sample(context),
     firstPerson_(false)
 {
     // Register factory for the Character component so it can be created via CreateComponent
@@ -44,26 +42,38 @@ CharacterDemo::CharacterDemo(Context* context) :
 
 void CharacterDemo::Start()
 {
+    // Execute base class startup
+    Sample::Start();
+    
+    // Create static scene content
     CreateScene();
+    
+    // Create the controllable character
     CreateCharacter();
+    
+    // Create the UI content
+    CreateInstructions();
+    
+    // Subscribe to necessary events
     SubscribeToEvents();
 }
 
 void CharacterDemo::CreateScene()
 {
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    
     scene_ = new Scene(context_);
     
     // Create scene subsystem components
     scene_->CreateComponent<Octree>();
     scene_->CreateComponent<PhysicsWorld>();
-    scene_->CreateComponent<DebugRenderer>();
     
     // Create camera and define viewport. Camera does not necessarily have to belong to the scene
     cameraNode_ = new Node(context_);
     Camera* camera = cameraNode_->CreateComponent<Camera>();
     GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
     
-    // Create static scene content, same as in TestScene.as
+    // Create static scene content. First create a zone for ambient lighting and fog control
     Node* zoneNode = scene_->CreateChild("Zone");
     Zone* zone = zoneNode->CreateComponent<Zone>();
     zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
@@ -72,97 +82,85 @@ void CharacterDemo::CreateScene()
     zone->SetFogEnd(300.0f);
     zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
     
-    {
-        Node* lightNode = scene_->CreateChild("GlobalLight");
-        lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f));
-        
-        Light* light = lightNode->CreateComponent<Light>();
-        light->SetLightType(LIGHT_DIRECTIONAL);
-        light->SetCastShadows(true);
-        light->SetShadowBias(BiasParameters(0.0001f, 0.5f));
-        light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));
-        light->SetSpecularIntensity(0.5f);
-    }
+    // Create a directional light with cascaded shadow mapping
+    Node* lightNode = scene_->CreateChild("DirectionalLight");
+    lightNode->SetDirection(Vector3(0.3f, -0.5f, 0.425f));
+    Light* light = lightNode->CreateComponent<Light>();
+    light->SetLightType(LIGHT_DIRECTIONAL);
+    light->SetCastShadows(true);
+    light->SetShadowBias(BiasParameters(0.0001f, 0.5f));
+    light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));
+    light->SetSpecularIntensity(0.5f);
 
-    {
-        Node* objectNode = scene_->CreateChild("Floor");
-        objectNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f));
-        objectNode->SetScale(Vector3(200.0f, 1.0f, 200.0f));
-        
-        StaticModel* object = objectNode->CreateComponent<StaticModel>();
-        object->SetModel(cache_->GetResource<Model>("Models/Box.mdl"));
-        object->SetMaterial(cache_->GetResource<Material>("Materials/Stone.xml"));
-        object->SetOccluder(true);
-        
-        RigidBody* body = objectNode->CreateComponent<RigidBody>();
-        body->SetCollisionLayer(2);
-        CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
-        shape->SetBox(Vector3::ONE);
-    }
+    // Create the floor object
+    Node* floorNode = scene_->CreateChild("Floor");
+    floorNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f));
+    floorNode->SetScale(Vector3(200.0f, 1.0f, 200.0f));
+    StaticModel* object = floorNode->CreateComponent<StaticModel>();
+    object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
+    object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
+    
+    RigidBody* body = floorNode->CreateComponent<RigidBody>();
+    // Use collision layer bit 2 to mark world scenery. This is what we will raycast against to prevent camera from going
+    // inside geometry
+    body->SetCollisionLayer(2);
+    CollisionShape* shape = floorNode->CreateComponent<CollisionShape>();
+    shape->SetBox(Vector3::ONE);
     
-    for (unsigned i = 0; i < 50; ++i)
+    // Create mushrooms of varying sizes
+    const unsigned NUM_MUSHROOMS = 60;
+    for (unsigned i = 0; i < NUM_MUSHROOMS; ++i)
     {
-        Node* objectNode = scene_->CreateChild("Box");
-        objectNode->SetPosition(Vector3(Random() * 180.0f - 90.0f, 1.0f, Random() * 180.0f - 90.0f));
-        objectNode->SetScale(2.0f);
-        
+        Node* objectNode = scene_->CreateChild("Mushroom");
+        objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, 0.0f, Random(180.0f) - 90.0f));
+        objectNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
+        objectNode->SetScale(2.0f + Random(5.0f));
         StaticModel* object = objectNode->CreateComponent<StaticModel>();
-        object->SetModel(cache_->GetResource<Model>("Models/Box.mdl"));
-        object->SetMaterial(cache_->GetResource<Material>("Materials/Stone.xml"));
+        object->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
+        object->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
         object->SetCastShadows(true);
         
         RigidBody* body = objectNode->CreateComponent<RigidBody>();
         body->SetCollisionLayer(2);
         CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
-        shape->SetBox(Vector3::ONE);
+        shape->SetTriangleMesh(object->GetModel(), 0);
     }
     
-    for (unsigned i = 0; i < 10; ++i)
+    // Create movable boxes. Let them fall from the sky at first
+    const unsigned NUM_SMALL_BOXES = 100;
+    for (unsigned i = 0; i < NUM_SMALL_BOXES; ++i)
     {
-        Node* objectNode = scene_->CreateChild("Box");
-        objectNode->SetPosition(Vector3(Random() * 180.0f - 90.0f, 10.0f, Random() * 180.0f - 90.0f));
-        objectNode->SetScale(20);
+        float scale = Random(2.0f) + 0.5f;
         
+        Node* objectNode = scene_->CreateChild("Box");
+        objectNode->SetPosition(Vector3(Random(180.0f) - 90.0f, Random(10.0f) + 10.0f, Random(180.0f) - 90.0f));
+        objectNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
+        objectNode->SetScale(scale);
         StaticModel* object = objectNode->CreateComponent<StaticModel>();
-        object->SetModel(cache_->GetResource<Model>("Models/Box.mdl"));
-        object->SetMaterial(cache_->GetResource<Material>("Materials/Stone.xml"));
+        object->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
+        object->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
         object->SetCastShadows(true);
-        object->SetOccluder(true);
         
         RigidBody* body = objectNode->CreateComponent<RigidBody>();
         body->SetCollisionLayer(2);
+        // Bigger boxes will be heavier and harder to move
+        body->SetMass(scale * 2.0f);
         CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
         shape->SetBox(Vector3::ONE);
     }
-
-    for (unsigned i = 0; i < 50; ++i)
-    {
-        Node* objectNode = scene_->CreateChild("Mushroom");
-        objectNode->SetPosition(Vector3(Random() * 180.0f - 90.0f, 0.0f, Random() * 180.0f - 90.0f));
-        objectNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
-        objectNode->SetScale(5.0f);
-        
-        StaticModel* object = objectNode->CreateComponent<StaticModel>();
-        object->SetModel(cache_->GetResource<Model>("Models/Mushroom.mdl"));
-        object->SetMaterial(cache_->GetResource<Material>("Materials/Mushroom.xml"));
-        object->SetCastShadows(true);
-        
-        RigidBody* body = objectNode->CreateComponent<RigidBody>();
-        body->SetCollisionLayer(2);
-        CollisionShape* shape = objectNode->CreateComponent<CollisionShape>();
-        shape->SetTriangleMesh(object->GetModel(), 0);
-    }
 }
 
 void CharacterDemo::CreateCharacter()
 {
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    
     Node* objectNode = scene_->CreateChild("Jack");
     objectNode->SetPosition(Vector3(0.0f, 1.0f, 0.0f));
     
     // Create the rendering component + animation controller
     AnimatedModel* object = objectNode->CreateComponent<AnimatedModel>();
-    object->SetModel(cache_->GetResource<Model>("Models/Jack.mdl"));
-    object->SetMaterial(cache_->GetResource<Material>("Materials/Jack.xml"));
+    object->SetModel(cache->GetResource<Model>("Models/Jack.mdl"));
+    object->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml"));
     object->SetCastShadows(true);
     objectNode->CreateComponent<AnimationController>();
     
@@ -188,9 +186,33 @@ void CharacterDemo::CreateCharacter()
     character_ = objectNode->CreateComponent<Character>();
 }
 
+void CharacterDemo::CreateInstructions()
+{
+    ResourceCache* cache = GetSubsystem<ResourceCache>();
+    UI* ui = GetSubsystem<UI>();
+    
+    // Construct new Text object, set string to display and font to use
+    Text* instructionText = ui->GetRoot()->CreateChild<Text>();
+    instructionText->SetText(
+        "Use WASD keys and mouse to move\n"
+        "Space to jump, F to toggle 1st/3rd person"
+    );
+    instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
+    // The text has multiple rows. Center them in relation to each other
+    instructionText->SetTextAlignment(HA_CENTER);
+    
+    // Position the text relative to the screen center
+    instructionText->SetHorizontalAlignment(HA_CENTER);
+    instructionText->SetVerticalAlignment(VA_CENTER);
+    instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
+}
+
 void CharacterDemo::SubscribeToEvents()
 {
+    // Subscribe to Update event for setting the character controls before physics simulation
     SubscribeToEvent(E_UPDATE, HANDLER(CharacterDemo, HandleUpdate));
+    
+    // Subscribe to PostUpdate event for updating the camera position after physics simulation
     SubscribeToEvent(E_POSTUPDATE, HANDLER(CharacterDemo, HandlePostUpdate));
 }
 
@@ -201,26 +223,31 @@ void CharacterDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
     float timeStep = eventData[P_TIMESTEP].GetFloat();
     Input* input = GetSubsystem<Input>();
 
-    if (input->GetKeyDown(KEY_ESC))
-        engine_->Exit();
-    
-    if (input->GetKeyPress('F'))
-        firstPerson_ = !firstPerson_;
-    
     if (character_)
     {
-        // Get movement controls and assign them to the character logic component
-        character_->controls_.Set(CTRL_UP, input->GetKeyDown('W'));
-        character_->controls_.Set(CTRL_DOWN, input->GetKeyDown('S'));
-        character_->controls_.Set(CTRL_LEFT, input->GetKeyDown('A'));
-        character_->controls_.Set(CTRL_RIGHT, input->GetKeyDown('D'));
-        character_->controls_.Set(CTRL_JUMP, input->GetKeyDown(KEY_SPACE));
+        UI* ui = GetSubsystem<UI>();
         
-        // Add character yaw & pitch from the mouse motion
-        character_->controls_.yaw_ += (float)input->GetMouseMoveX() * YAW_SENSITIVITY;
-        character_->controls_.pitch_ += (float)input->GetMouseMoveY() * YAW_SENSITIVITY;
-        // Limit pitch
-        character_->controls_.pitch_ = Clamp(character_->controls_.pitch_, -80.0f, 80.0f);
+        // Get movement controls and assign them to the character logic component. If UI has a focused element, clear controls
+        if (!ui->GetFocusElement())
+        {
+            character_->controls_.Set(CTRL_UP, input->GetKeyDown('W'));
+            character_->controls_.Set(CTRL_DOWN, input->GetKeyDown('S'));
+            character_->controls_.Set(CTRL_LEFT, input->GetKeyDown('A'));
+            character_->controls_.Set(CTRL_RIGHT, input->GetKeyDown('D'));
+            character_->controls_.Set(CTRL_JUMP, input->GetKeyDown(KEY_SPACE));
+            
+            // Add character yaw & pitch from the mouse motion
+            character_->controls_.yaw_ += (float)input->GetMouseMoveX() * YAW_SENSITIVITY;
+            character_->controls_.pitch_ += (float)input->GetMouseMoveY() * YAW_SENSITIVITY;
+            // Limit pitch
+            character_->controls_.pitch_ = Clamp(character_->controls_.pitch_, -80.0f, 80.0f);
+            
+            // Switch between 1st and 3rd person
+            if (input->GetKeyPress('F'))
+                firstPerson_ = !firstPerson_;
+        }
+        else
+            character_->controls_.Set(CTRL_UP | CTRL_DOWN | CTRL_LEFT | CTRL_RIGHT | CTRL_JUMP, false);
         
         // Set rotation already here so that it's updated every rendering frame instead of every physics frame
         character_->GetNode()->SetRotation(Quaternion(character_->controls_.yaw_, Vector3::UP));

+ 14 - 9
Source/Extras/CharacterDemo/CharacterDemo.h → Source/Samples/18_CharacterDemo/CharacterDemo.h

@@ -22,20 +22,25 @@
 
 #pragma once
 
-#include "Application.h"
-
-using namespace Urho3D;
+#include "Sample.h"
 
 namespace Urho3D
 {
     class Node;
-    class ResourceCache;
     class Scene;
 }
 
-class Character;
+using namespace Urho3D;
 
-class CharacterDemo : public Application
+class Character;
+
+/// Moving character example.
+/// This sample demonstrates:
+///     - Controlling a humanoid character through physics;
+///     - Driving animations using the AnimationController component;
+///     - Implementing 1st and 3rd person cameras, using raycasts to avoid the 3rd person camera
+///       clipping into scenery
+class CharacterDemo : public Sample
 {
     OBJECT(CharacterDemo);
 
@@ -51,17 +56,17 @@ private:
     void CreateScene();
     /// Create controllable character.
     void CreateCharacter();
+    /// Construct an instruction text to the UI.
+    void CreateInstructions();
     /// Subscribe to necessary events.
     void SubscribeToEvents();
-    /// Handle application update. Set controls to character & check global keys.
+    /// Handle application update. Set controls to character.
     void HandleUpdate(StringHash eventType, VariantMap& eventData);
     /// Handle application post-update. Update camera position after character has moved.
     void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
     
     /// Scene.
     SharedPtr<Scene> scene_;
-    /// Resource cache subsystem, stored here for convenience.
-    SharedPtr<ResourceCache> cache_;
     /// Camera scene node.
     SharedPtr<Node> cameraNode_;
     /// The controllable character.

+ 1 - 0
Source/Samples/CMakeLists.txt

@@ -59,3 +59,4 @@ add_subdirectory (14_SoundEffects)
 add_subdirectory (15_Navigation)
 add_subdirectory (16_Chat)
 add_subdirectory (17_SceneReplication)
+add_subdirectory (18_CharacterDemo)