Selaa lähdekoodia

Added timestep smoothing to Engine. By default the previous 2 frames' timesteps are averaged.

Lasse Öörni 12 vuotta sitten
vanhempi
sitoutus
db3bf3fbf9

+ 59 - 79
Docs/LuaScriptAPI.dox

@@ -27,7 +27,7 @@ namespace Urho3D
 - void ErrorExit(const String message = String::EMPTY, int exitCode = EXIT_FAILURE)
 - void OpenConsoleWindow()
 - void PrintLine(const String str, bool error = false)
-- string* GetArguments()
+- const Vector<String>& GetArguments()
 - String GetConsoleInput()
 - String GetPlatform()
 - unsigned GetNumPhysicalCPUs()
@@ -716,47 +716,6 @@ Properties:
 - float farDistance
 - float rolloffFactor
 
-### Vector : VectorBase
-
-Methods:
-
-- TOLUA_TEMPLATE_BIND(T, String)
-- Vector()
-- Vector(const Vector<T>& vector)
-- ~Vector()
-- Vector<T> operator + (const T& rhs) const
-- Vector<T> operator + (const Vector<T>& rhs) const
-- bool operator == (const Vector<T>& rhs) const
-- T& operator [] (unsigned index)
-- const T& operator [] (unsigned index) const
-- T& At(unsigned index)
-- const T& At(unsigned index) const
-- void Push(const T& value)
-- void Push(const Vector<T>& vector)
-- void Pop()
-- void Insert(unsigned pos, const T& value)
-- void Insert(unsigned pos, const Vector<T>& vector)
-- void Erase(unsigned pos, unsigned length = 1)
-- bool Remove(const T& value)
-- void Clear()
-- void Resize(unsigned newSize)
-- void Reserve(unsigned newCapacity)
-- void Compact()
-- bool Contains(const T& value) const
-- T& Front()
-- const T& Front() const
-- T& Back()
-- const T& Back() const
-- unsigned Size() const
-- unsigned Capacity() const
-- bool Empty() const
-
-Properties:
-
-- unsigned size (readonly)
-- unsigned capacity (readonly)
-- bool empty (readonly)
-
 ### PODVector
 
 Methods:
@@ -804,7 +763,7 @@ Methods:
 
 - Object* GetEventSender() const
 - EventHandler* GetEventHandler() const
-- const String& GetTypeName(ShortStringHash type) const
+- const String GetTypeName(ShortStringHash type) const
 
 ### Object : RefCounted
 
@@ -812,8 +771,8 @@ Methods:
 
 - ShortStringHash GetType() const
 - ShortStringHash GetBaseType() const
-- const String& GetTypeName() const
-- const String& GetCategory() const
+- const String GetTypeName() const
+- const String GetCategory() const
 - void SendEvent(const String eventName, VariantMap* eventData = 0)
 
 Properties:
@@ -907,7 +866,7 @@ Methods:
 - bool operator == (const Vector4& rhs) const
 - bool operator == (const Quaternion& rhs) const
 - bool operator == (const Color& rhs) const
-- bool operator == (const String& rhs) const
+- bool operator == (const String rhs) const
 - bool operator == (const ResourceRef& rhs) const
 - bool operator == (const ResourceRefList& rhs) const
 - bool operator == (const IntRect& rhs) const
@@ -942,7 +901,7 @@ Methods:
 - const Vector4& GetVector4() const
 - const Quaternion& GetQuaternion() const
 - const Color& GetColor() const
-- const String& GetString() const
+- const String GetString() const
 - VectorBuffer GetBuffer() const
 - const ResourceRef& GetResourceRef() const
 - const ResourceRefList& GetResourceRefList() const
@@ -996,7 +955,7 @@ Methods:
 - const Vector4& GetVector4(const String key)
 - const Quaternion& GetQuaternion(const String key)
 - const Color& GetColor(const String key)
-- const String& GetString(const String key)
+- const String GetString(const String key)
 - VectorBuffer GetBuffer(const String key)
 - const ResourceRef& GetResourceRef(const String key)
 - const ResourceRefList& GetResourceRefList(const String key)
@@ -1021,7 +980,7 @@ Methods:
 - unsigned GetNumRows() const
 - unsigned GetNumHistoryRows() const
 - unsigned GetHistoryPosition() const
-- const String& GetHistoryRow(unsigned index) const
+- const String GetHistoryRow(unsigned index) const
 
 Properties:
 
@@ -1078,6 +1037,7 @@ Methods:
 - void SetMinFps(int fps)
 - void SetMaxFps(int fps)
 - void SetMaxInactiveFps(int fps)
+- void SetTimeStepSmoothing(int frames)
 - void SetPauseMinimized(bool enable)
 - void SetAutoExit(bool enable)
 - void Exit()
@@ -1087,6 +1047,7 @@ Methods:
 - int GetMinFps() const
 - int GetMaxFps() const
 - int GetMaxInactiveFps() const
+- int GetTimeStepSmoothing() const
 - bool GetPauseMinimized() const
 - bool GetAutoExit() const
 - bool IsInitialized() const
@@ -1098,6 +1059,7 @@ Properties:
 - int minFps
 - int maxFps
 - int maxInactiveFps
+- int timeStepSmoothing
 - bool pauseMinimized
 - bool autoExit
 - bool initialized (readonly)
@@ -1162,7 +1124,7 @@ Properties:
 
 Methods:
 
-- const String& GetAnimationName() const
+- const String GetAnimationName() const
 - StringHash GetAnimationNameHash() const
 - float GetLength() const
 - unsigned GetNumTracks() const
@@ -1222,7 +1184,7 @@ Methods:
 - bool IsFadingOut(const String name) const
 - unsigned char GetLayer(const String name) const
 - Bone* GetStartBone(const String name) const
-- const String& GetStartBoneName(const String name) const
+- const String GetStartBoneName(const String name) const
 - float GetTime(const String name) const
 - float GetWeight(const String name) const
 - bool IsLooped(const String name) const
@@ -1541,7 +1503,7 @@ Methods:
 - bool TakeScreenShot(Image& destImage)
 - bool IsInitialized() const
 - void* GetExternalWindow() const
-- const String& GetWindowTitle() const
+- const String GetWindowTitle() const
 - IntVector2 GetWindowPosition() const
 - int GetWidth() const
 - int GetHeight() const
@@ -2446,7 +2408,7 @@ Methods:
 - unsigned GetNumTouches() const
 - TouchState* GetTouch(unsigned index) const
 - unsigned GetNumJoysticks() const
-- const String& GetJoystickName(unsigned index) const
+- const String GetJoystickName(unsigned index) const
 - JoystickState* GetJoystick(unsigned index)
 - bool GetToggleFullscreen() const
 - bool IsMouseVisible() const
@@ -2472,8 +2434,9 @@ Properties:
 
 Methods:
 
+- VectorBuffer Read(unsigned size)
 - unsigned Seek(unsigned position)
-- const String& GetName() const
+- const String GetName() const
 - unsigned GetChecksum()
 - unsigned GetPosition() const
 - unsigned GetSize() const
@@ -2537,8 +2500,9 @@ Methods:
 - bool IsOpen() const
 - void* GetHandle() const
 - bool IsPackaged() const
+- VectorBuffer Read(unsigned size)
 - unsigned Seek(unsigned position)
-- const String& GetName() const
+- const String GetName() const
 - unsigned GetChecksum()
 - unsigned GetPosition() const
 - unsigned GetSize() const
@@ -2576,6 +2540,7 @@ Methods:
 - unsigned ReadVLE()
 - unsigned ReadNetID()
 - String ReadLine()
+- unsigned Write(const VectorBuffer& buffer)
 - bool WriteInt(int value)
 - bool WriteShort(short value)
 - bool WriteByte(signed char value)
@@ -2628,6 +2593,7 @@ Methods:
 - bool SetCurrentDir(const String pathName)
 - bool CreateDir(const String pathName)
 - int SystemCommand(const String commandLine)
+- int SystemRun(const String fileName, const Vector<String>& arguments)
 - bool SystemOpen(const String fileName, const String mode = String::EMPTY)
 - bool Copy(const String srcFileName, const String destFileName)
 - bool Rename(const String srcFileName, const String destFileName)
@@ -2639,7 +2605,7 @@ Methods:
 - unsigned GetLastModifiedTime(const String fileName) const
 - bool FileExists(const String fileName) const
 - bool DirExists(const String pathName) const
-- Vector<String> ScanDir(const String pathName, const String filter, unsigned flags, bool recursive) const
+- const Vector<String>& ScanDir(const String pathName, const String filter, unsigned flags, bool recursive) const
 - String GetProgramDir() const
 - String GetUserDocumentsDir() const
 
@@ -2686,7 +2652,7 @@ Methods:
 - bool Exists(const String fileName) const
 - const PackageEntry* GetEntry(const String fileName) const
 - const HashMap<String, PackageEntry>& GetEntries() const
-- const String& GetName() const
+- const String GetName() const
 - StringHash GetNameHash() const
 - unsigned GetNumFiles() const
 - unsigned GetTotalSize() const
@@ -2706,6 +2672,7 @@ Properties:
 
 Methods:
 
+- unsigned Write(const VectorBuffer& buffer)
 - bool WriteInt(int value)
 - bool WriteShort(short value)
 - bool WriteByte(signed char value)
@@ -2754,8 +2721,8 @@ Methods:
 - void SubscribeToEvent(void* sender, const String eventName, const String functionName)
 - void UnsubscribeFromEvent(void* sender, const String eventName)
 - void UnsubscribeFromEvents(void* sender)
-- const String& GetScriptFileName() const
-- const String& GetScriptObjectType() const
+- const String GetScriptFileName() const
+- const String GetScriptObjectType() const
 
 Properties:
 
@@ -3590,7 +3557,7 @@ Methods:
 - unsigned short GetPort() const
 - String ToString() const
 - unsigned GetNumDownloads() const
-- const String& GetDownloadName() const
+- const String GetDownloadName() const
 - float GetDownloadProgress() const
 
 Properties:
@@ -3649,11 +3616,12 @@ Methods:
 - void UnregisterRemoteEvent(const String eventType)
 - void UnregisterAllRemoteEvents()
 - void SetPackageCacheDir(const String path)
+- SharedPtr<HttpRequest> MakeHttpRequest(const String& url, const String& verb = String::EMPTY, const Vector<String>& headers = Vector<String>(), const String& postData = String::EMPTY)
 - int GetUpdateFps() const
 - Connection* GetServerConnection() const
 - bool IsServerRunning() const
 - bool CheckRemoteEvent(StringHash eventType) const
-- const String& GetPackageCacheDir() const
+- const String GetPackageCacheDir() const
 
 Properties:
 
@@ -3990,7 +3958,7 @@ Properties:
 
 Methods:
 
-- const String& GetName() const
+- const String GetName() const
 - StringHash GetNameHash() const
 - unsigned GetMemoryUse() const
 
@@ -4015,7 +3983,7 @@ Methods:
 - unsigned GetMemoryBudget(ShortStringHash type) const
 - unsigned GetMemoryUse(ShortStringHash type) const
 - unsigned GetTotalMemoryUse() const
-- const String& GetResourceName(StringHash nameHash) const
+- const String GetResourceName(StringHash nameHash) const
 - String GetResourceFileName(const String name) const
 - bool GetAutoReloadResources() const
 
@@ -4160,7 +4128,7 @@ Methods:
 - int GetScriptObject() const
 - int GetScriptObject(const String scriptObjectType) const
 - unsigned GetID() const
-- const String& GetName() const
+- const String GetName() const
 - StringHash GetNameHash() const
 - Node* GetParent() const
 - Scene* GetScene() const
@@ -4256,13 +4224,13 @@ Methods:
 - bool IsUpdateEnabled() const
 - bool IsAsyncLoading() const
 - float GetAsyncProgress() const
-- const String& GetFileName() const
+- const String GetFileName() const
 - unsigned GetChecksum() const
 - float GetTimeScale() const
 - float GetElapsedTime() const
 - float GetSmoothingConstant() const
 - float GetSnapThreshold() const
-- const String& GetVarName(ShortStringHash hash) const
+- const String GetVarName(ShortStringHash hash) const
 - void Update(float timeStep)
 - void BeginThreadedUpdate()
 - void EndThreadedUpdate()
@@ -4417,11 +4385,12 @@ Methods:
 - void SetResizePopup(bool enable)
 - unsigned GetNumItems() const
 - UIElement* GetItem(unsigned index) const
+- const PODVector<UIElement*>& GetItems() const
 - unsigned GetSelection() const
 - UIElement* GetSelectedItem() const
 - ListView* GetListView() const
 - UIElement* GetPlaceholder() const
-- const String& GetPlaceholderText() const
+- const String GetPlaceholderText() const
 - bool GetResizePopup() const
 - void SetSelectionAttr(unsigned index)
 
@@ -4456,6 +4425,7 @@ Methods:
 - void SetButtonTexts(const String okText, const String cancelText)
 - void SetPath(const String path)
 - void SetFileName(const String fileName)
+- void SetFilters(const Vector<String>& filters, unsigned defaultIndex)
 - void SetDirectoryMode(bool enable)
 - void UpdateElements()
 - XMLFile* GetDefaultStyle() const
@@ -4468,10 +4438,10 @@ Methods:
 - Button* GetOKButton() const
 - Button* GetCancelButton() const
 - Button* GetCloseButton() const
-- const String& GetTitle() const
-- const String& GetPath() const
-- const String& GetFileName() const
-- const String& GetFilter() const
+- const String GetTitle() const
+- const String GetPath() const
+- const String GetFileName() const
+- const String GetFilter() const
 - unsigned GetFilterIndex() const
 - bool GetDirectoryMode() const
 
@@ -4513,7 +4483,7 @@ Methods:
 - void SetCursorMovable(bool enable)
 - void SetTextSelectable(bool enable)
 - void SetTextCopyable(bool enable)
-- const String& GetText() const
+- const String GetText() const
 - unsigned GetCursorPosition() const
 - float GetCursorBlinkRate() const
 - unsigned GetMaxLength() const
@@ -4549,6 +4519,7 @@ Methods:
 - void RemoveItem(unsigned index)
 - void RemoveAllItems()
 - void SetSelection(unsigned index)
+- void SetSelections(const PODVector<unsigned>& indices)
 - void AddSelection(unsigned index)
 - void RemoveSelection(unsigned index)
 - void ToggleSelection(unsigned index)
@@ -4563,9 +4534,12 @@ Methods:
 - void ToggleExpand(unsigned index, bool recursive = false)
 - unsigned GetNumItems() const
 - UIElement* GetItem(unsigned index) const
+- const PODVector<UIElement*>& GetItems() const
 - unsigned FindItem(UIElement* item) const
 - unsigned GetSelection() const
+- const PODVector<unsigned>& GetSelections() const
 - UIElement* GetSelectedItem() const
+- const PODVector<UIElement*>& GetSelectedItems() const
 - bool IsSelected(unsigned index) const
 - bool IsExpanded(unsigned index) const
 - HighlightMode GetHighlightMode() const
@@ -4766,7 +4740,7 @@ Methods:
 - void SetEffectColor(const Color& effectColor)
 - Font* GetFont() const
 - int GetFontSize() const
-- const String& GetText() const
+- const String GetText() const
 - HorizontalAlignment GetTextAlignment() const
 - float GetRowSpacing() const
 - bool GetWordwrap() const
@@ -4778,6 +4752,11 @@ Methods:
 - const Color& GetEffectColor() const
 - int GetRowHeight() const
 - unsigned GetNumRows() const
+- const PODVector<int>& GetRowWidths() const
+- const PODVector<IntVector2>& GetCharPositions() const
+- const PODVector<IntVector2>& GetCharSizes() const
+- void SetEffectDepthBias(float bias)
+- float GetEffectDepthBias() const
 - void SetFontAttr(ResourceRef value)
 - ResourceRef GetFontAttr() const
 
@@ -4827,7 +4806,7 @@ Methods:
 - Font* GetFont() const
 - Material* GetMaterial() const
 - int GetFontSize() const
-- const String& GetText() const
+- const String GetText() const
 - HorizontalAlignment GetTextAlignment() const
 - HorizontalAlignment GetHorizontalAlignment() const
 - VerticalAlignment GetVerticalAlignment() const
@@ -4839,6 +4818,7 @@ Methods:
 - int GetWidth() const
 - int GetRowHeight() const
 - unsigned GetNumRows() const
+- const PODVector<int>& GetRowWidths() const
 - const Color& GetColor(Corner corner) const
 - float GetOpacity() const
 - bool GetFaceCamera() const
@@ -4894,12 +4874,12 @@ Methods:
 - UIElement* GetRoot() const
 - UIElement* GetRootModalElement() const
 - Cursor* GetCursor() const
+- IntVector2 GetCursorPosition() const
 - UIElement* GetElementAt(const IntVector2& position, bool enabledOnly = true)
 - UIElement* GetElementAt(int x, int y, bool enabledOnly = true)
 - UIElement* GetFocusElement() const
 - UIElement* GetFrontElement() const
-- IntVector2 GetCursorPosition() const
-- const String& GetClipBoardText() const
+- const String GetClipBoardText() const
 - float GetDoubleClickInterval() const
 - int GetMaxFontTextureSize() const
 - bool IsNonFocusedMouseWheel() const
@@ -4913,9 +4893,9 @@ Properties:
 - UIElement* root (readonly)
 - UIElement* rootModalElement (readonly)
 - Cursor* cursor
+- IntVector2 cursorPosition (readonly)
 - UIElement* focusElement (readonly)
 - UIElement* frontElement (readonly)
-- IntVector2 cursorPosition (readonly)
 - String& clipBoardText
 - float doubleClickInterval
 - int maxFontTextureSize
@@ -5001,7 +4981,7 @@ Methods:
 - void SetInternal(bool enable)
 - void SetTraversalMode(TraversalMode traversalMode)
 - void SetElementEventSender(bool flag)
-- const String& GetName() const
+- const String GetName() const
 - const IntVector2& GetPosition() const
 - const IntVector2& GetSize() const
 - int GetWidth() const
@@ -5038,7 +5018,7 @@ Methods:
 - bool HasColorGradient() const
 - FocusMode GetFocusMode() const
 - unsigned GetDragDropMode() const
-- const String& GetAppliedStyle() const
+- const String GetAppliedStyle() const
 - XMLFile* GetDefaultStyle(bool recursiveUp = true) const
 - LayoutMode GetLayoutMode() const
 - int GetLayoutSpacing() const

+ 1 - 0
Docs/ScriptAPI.dox

@@ -7229,6 +7229,7 @@ Properties:
 - String category (readonly)
 - int minFps
 - int maxFps
+- int timeStepSmoothing
 - int maxInactiveFps
 - bool pauseMinimized
 - bool autoExit

+ 14 - 1
Source/Engine/Engine/Engine.cpp

@@ -81,6 +81,7 @@ extern const char* logLevelPrefixes[];
 Engine::Engine(Context* context) :
     Object(context),
     timeStep_(0.0f),
+    timeStepSmoothing_(2),
     minFps_(10),
     #if defined(ANDROID) || defined(IOS) || defined(RASPI)
     maxFps_(60),
@@ -383,6 +384,11 @@ DebugHud* Engine::CreateDebugHud()
     return debugHud;
 }
 
+void Engine::SetTimeStepSmoothing(int frames)
+{
+    timeStepSmoothing_ = Clamp(frames, 1, 10);
+}
+
 void Engine::SetMinFps(int fps)
 {
     minFps_ = Max(fps, 0);
@@ -581,7 +587,14 @@ void Engine::ApplyFrameLimit()
             elapsed = targetMin;
     }
     
-    timeStep_ = elapsed / 1000000.0f;
+    // Perform timestep smoothing
+    timeStep_ = 0.0f;
+    lastTimeSteps_.Push(elapsed / 1000000.0f);
+    if (lastTimeSteps_.Size() > timeStepSmoothing_)
+        lastTimeSteps_.Erase(0);
+    for (unsigned i = 0; i < lastTimeSteps_.Size(); ++i)
+        timeStep_ += lastTimeSteps_[i];
+    timeStep_ /= lastTimeSteps_.Size();
 }
 
 VariantMap Engine::ParseParameters(const Vector<String>& arguments)

+ 8 - 0
Source/Engine/Engine/Engine.h

@@ -56,6 +56,8 @@ public:
     void SetMaxFps(int fps);
     /// Set maximum frames per second when the application does not have input focus.
     void SetMaxInactiveFps(int fps);
+    /// Set how many frames to average for timestep smoothing. Default is 2. 1 disables smoothing.
+    void SetTimeStepSmoothing(int frames);
     /// Set whether to pause update events and audio when minimized.
     void SetPauseMinimized(bool enable);
     /// Set whether to exit automatically on exit request (window close button.)
@@ -75,6 +77,8 @@ public:
     int GetMaxFps() const { return maxFps_; }
     /// Return the maximum frames per second when the application does not have input focus.
     int GetMaxInactiveFps() const { return maxInactiveFps_; }
+    /// Return how many frames to average for timestep smoothing.
+    int GetTimeStepSmoothing() const { return timeStepSmoothing_; }
     /// Return whether to pause update events and audio when minimized.
     bool GetPauseMinimized() const { return pauseMinimized_; }
     /// Return whether to exit automatically on exit request.
@@ -108,8 +112,12 @@ private:
     
     /// Frame update timer.
     HiresTimer frameTimer_;
+    /// Previous timesteps for smoothing.
+    PODVector<float> lastTimeSteps_;
     /// Next frame timestep in seconds.
     float timeStep_;
+    /// How many frames to average for the smoothed timestep.
+    unsigned timeStepSmoothing_;
     /// Minimum frames per second.
     unsigned minFps_;
     /// Maximum frames per second.

+ 2 - 0
Source/Engine/Script/EngineAPI.cpp

@@ -109,6 +109,8 @@ static void RegisterEngine(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Engine", "int get_minFps() const", asMETHOD(Engine, GetMinFps), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void set_maxFps(int)", asMETHOD(Engine, SetMaxFps), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "int get_maxFps() const", asMETHOD(Engine, GetMaxFps), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Engine", "void set_timeStepSmoothing(int)", asMETHOD(Engine, SetTimeStepSmoothing), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Engine", "int get_timeStepSmoothing() const", asMETHOD(Engine, GetTimeStepSmoothing), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void set_maxInactiveFps(int)", asMETHOD(Engine, SetMaxInactiveFps), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "int get_maxInactiveFps() const", asMETHOD(Engine, GetMaxInactiveFps), asCALL_THISCALL);
     engine->RegisterObjectMethod("Engine", "void set_pauseMinimized(bool)", asMETHOD(Engine, SetPauseMinimized), asCALL_THISCALL);

+ 5 - 2
Source/Extras/LuaScript/pkgs/Engine/Engine.pkg

@@ -6,10 +6,11 @@ class Engine : public Object
     
     Console* CreateConsole();
     DebugHud* CreateDebugHud();
-    
+
     void SetMinFps(int fps);
     void SetMaxFps(int fps);
     void SetMaxInactiveFps(int fps);
+    void SetTimeStepSmoothing(int frames);
     void SetPauseMinimized(bool enable);
     void SetAutoExit(bool enable);
     void Exit();
@@ -20,15 +21,17 @@ class Engine : public Object
     int GetMinFps() const;
     int GetMaxFps() const;
     int GetMaxInactiveFps() const;
+    int GetTimeStepSmoothing() const;
     bool GetPauseMinimized() const;
     bool GetAutoExit() const;
     bool IsInitialized() const;
     bool IsExiting() const;
     bool IsHeadless() const;
-    
+
     tolua_property__get_set int minFps;
     tolua_property__get_set int maxFps;
     tolua_property__get_set int maxInactiveFps;
+    tolua_property__get_set int timeStepSmoothing;
     tolua_property__get_set bool pauseMinimized;
     tolua_property__get_set bool autoExit;
     tolua_readonly tolua_property__is_set bool initialized;