Browse Source

Ported the first sample to AngelScript.

Lasse Öörni 12 years ago
parent
commit
5094541394

+ 51 - 0
Bin/Data/Scripts/01_HelloWorld.as

@@ -0,0 +1,51 @@
+// This first example, maintaining tradition, prints a "Hello World" message.
+// Furthermore it shows:
+//     - Using the Sample utility functions as a base for the application;
+//     - Adding a Text element to the graphical user interface;
+//     - Subscribing to and handling of update events;
+
+#include "Utilities/Sample.as"
+
+void Start()
+{
+    // Execute the common startup for samples
+    SampleStart();
+    
+    // Create "Hello World" Text
+    CreateText();
+
+    // Finally, hook-up this HelloWorld instance to handle update events
+    SubscribeToEvents();
+}
+
+void CreateText()
+{
+    // Construct new Text object
+    Text@ helloText = Text();
+
+    // Set String to display
+    helloText.text = "Hello World from Urho3D!";
+
+    // Set font and text color
+    helloText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 30);
+    helloText.color = Color(0.0f, 1.0f, 0.0f);
+
+    // Align Text center-screen
+    helloText.horizontalAlignment = HA_CENTER;
+    helloText.verticalAlignment = VA_CENTER;
+
+    // Add Text instance to the UI root element
+    ui.root.AddChild(helloText);
+}
+
+void SubscribeToEvents()
+{
+    // Subscribe HandleUpdate() function for processing update events
+    SubscribeToEvent("Update", "HandleUpdate");
+}
+
+void HandleUpdate(StringHash eventType, VariantMap& eventData)
+{
+    // Do nothing for now, could be extended to eg. animate the display
+}
+

+ 163 - 0
Bin/Data/Scripts/Utilities/Sample.as

@@ -0,0 +1,163 @@
+// Common sample initialization as a framework for all samples.
+//    - Create Urho3D logo at screen;
+//    - Create Console and Debug HUD, and use F1 and F2 key to toggle them;
+//    - Toggle rendering options from the keys 1-8;
+//    - Handle Esc key down to hide Console or exit application;
+
+Sprite@ logoSprite;
+
+void SampleStart()
+{
+    // Create logo
+    CreateLogo();
+
+    // Create console and debug HUD
+    CreateConsoleAndDebugHud();
+
+    // Subscribe key down event
+    SubscribeToEvent("KeyDown", "HandleKeyDown");
+}
+
+void SetLogoVisible(bool enable)
+{
+    if (logoSprite !is null)
+        logoSprite.visible = enable;
+}
+
+
+void CreateLogo()
+{
+    // Get logo texture
+    Texture2D@ logoTexture = cache.GetResource("Texture2D", "Textures/LogoLarge.png");
+    if (logoTexture is null)
+        return;
+
+    // Create logo sprite and add to the UI layout
+    logoSprite = ui.root.CreateChild("Sprite");
+
+    // Set logo sprite texture
+    logoSprite.texture = logoTexture;
+
+    int textureWidth = logoTexture.width;
+    int textureHeight = logoTexture.height;
+
+    // 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.opacity = 0.75f;
+    
+    // Set a low priority for the logo so that other UI elements can be drawn on top
+    logoSprite.priority = -100;
+}
+
+void CreateConsoleAndDebugHud()
+{
+    // Get default style
+    XMLFile@ xmlFile = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
+    if (xmlFile is null)
+        return;
+
+    // Create console
+    Console@ console = engine.CreateConsole();
+    console.defaultStyle = xmlFile;
+
+    // Create debug HUD
+    DebugHud@ debugHud = engine.CreateDebugHud();
+    debugHud.defaultStyle = xmlFile;
+}
+
+void HandleKeyDown(StringHash eventType, VariantMap& eventData)
+{
+    int key = eventData["Key"].GetInt();
+
+    // Close console (if open) or exit when ESC is pressed
+    if (key == KEY_ESC)
+    {
+        if (!console.visible)
+            engine.Exit();
+        else
+            console.visible = false;
+    }
+
+    // Toggle console with F1
+    if (key == KEY_F1)
+        console.Toggle();
+    
+    // Toggle debug HUD with F2
+    if (key == KEY_F2)
+        debugHud.ToggleAll();
+    
+    // Common rendering quality controls, only when UI has no focused element
+    if (ui.focusElement is null)
+    {
+        // Texture quality
+        if (key == '1')
+        {
+            int quality = renderer.textureQuality;
+            ++quality;
+            if (quality > QUALITY_HIGH)
+                quality = QUALITY_LOW;
+            renderer.textureQuality = quality;
+        }
+        
+        // Material quality
+        if (key == '2')
+        {
+            int quality = renderer.materialQuality;
+            ++quality;
+            if (quality > QUALITY_HIGH)
+                quality = QUALITY_LOW;
+            renderer.materialQuality = quality;
+        }
+        
+        // Specular lighting
+        if (key == '3')
+            renderer.specularLighting = !renderer.specularLighting;
+        
+        // Shadow rendering
+        if (key == '4')
+            renderer.drawShadows = !renderer.drawShadows;
+
+        // Shadow map resolution
+        if (key == '5')
+        {
+            int shadowMapSize = renderer.shadowMapSize;
+            shadowMapSize *= 2;
+            if (shadowMapSize > 2048)
+                shadowMapSize = 512;
+            renderer.shadowMapSize = shadowMapSize;
+        }
+        
+        // Shadow depth and filtering quality
+        if (key == '6')
+        {
+            int quality = renderer.shadowQuality;
+            ++quality;
+            if (quality > SHADOWQUALITY_HIGH_24BIT)
+                quality = SHADOWQUALITY_LOW_16BIT;
+            renderer.shadowQuality = quality;
+        }
+        
+        // Occlusion culling
+        if (key == '7')
+        {
+            bool occlusion = renderer.maxOccluderTriangles > 0;
+            occlusion = !occlusion;
+            renderer.maxOccluderTriangles = occlusion ? 5000 : 0;
+        }
+        
+        // Instancing
+        if (key == '8')
+            renderer.dynamicInstancing = !renderer.dynamicInstancing;
+    }
+}

+ 1 - 1
Source/Samples/01_HelloWorld/HelloWorld.h

@@ -29,7 +29,7 @@ using namespace Urho3D;
 
 
 /// This first example, maintaining tradition, prints a "Hello World" message.
 /// This first example, maintaining tradition, prints a "Hello World" message.
 /// Furthermore it shows:
 /// Furthermore it shows:
-///     - Initialization of the Urho3D engine;
+///     - Using the Sample / Application classes, which initialize the Urho3D engine and run the main loop;
 ///     - Adding a Text element to the graphical user interface;
 ///     - Adding a Text element to the graphical user interface;
 ///     - Subscribing to and handling of update events;
 ///     - Subscribing to and handling of update events;
 class HelloWorld : public Sample
 class HelloWorld : public Sample

+ 1 - 0
Source/Samples/Sample.h

@@ -33,6 +33,7 @@ using namespace Urho3D;
 ///    - Modify engine parameters for windowed mode and to show the class name as title
 ///    - Modify engine parameters for windowed mode and to show the class name as title
 ///    - Create Urho3D logo at screen;
 ///    - Create Urho3D logo at screen;
 ///    - Create Console and Debug HUD, and use F1 and F2 key to toggle them;
 ///    - Create Console and Debug HUD, and use F1 and F2 key to toggle them;
+///    - Toggle rendering options from the keys 1-8;
 ///    - Handle Esc key down to hide Console or exit application;
 ///    - Handle Esc key down to hide Console or exit application;
 class Sample : public Application
 class Sample : public Application
 {
 {