Browse Source

Adjust Lua GC policy, make the fps more smooth.

aster2013 11 years ago
parent
commit
fa6d92d39a
2 changed files with 13 additions and 12 deletions
  1. 10 11
      Bin/Data/LuaScripts/24_Urho2DSprite.lua
  2. 3 1
      Source/Engine/LuaScript/LuaScript.cpp

+ 10 - 11
Bin/Data/LuaScripts/24_Urho2DSprite.lua

@@ -69,7 +69,7 @@ function CreateScene()
         staticSprite.sprite = sprite
         staticSprite.sprite = sprite
 
 
         -- Set move speed
         -- Set move speed
-        spriteNode.moveSpeed = { Random(-200.0, 200.0), Random(-200.0, 200.0) }
+        spriteNode.moveSpeed = Vector3(Random(-200.0, 200.0), Random(-200.0, 200.0), 0.0)
         -- Set rotate speed
         -- Set rotate speed
         spriteNode.rotateSpeed = Random(-90.0, 90.0)
         spriteNode.rotateSpeed = Random(-90.0, 90.0)
 
 
@@ -164,22 +164,21 @@ function HandleUpdate(eventType, eventData)
 
 
     for _, spriteNode in ipairs(spriteNodes) do
     for _, spriteNode in ipairs(spriteNodes) do
         
         
-        local x, y, z = spriteNode:GetPositionXYZ()
+        local position = spriteNode.position
         local moveSpeed = spriteNode.moveSpeed
         local moveSpeed = spriteNode.moveSpeed
-        local newPositionX = x + moveSpeed[1] * timeStep
-        local newPositionY = y + moveSpeed[2] * timeStep
+        local newPosition = position + moveSpeed * timeStep
 
 
-        if newPositionX < -halfWidth or newPositionX > halfWidth then
-            newPositionX = x
-            moveSpeed[1] = -moveSpeed[1]
+        if newPosition.x < -halfWidth or newPosition.x > halfWidth then
+            newPosition.x =  position.x
+            moveSpeed.x = -moveSpeed.x
         end
         end
 
 
-        if newPositionY < -halfHeight or newPositionY > halfHeight then
-            newPositionY = y
-            moveSpeed[2] = -moveSpeed[2]
+        if newPosition.y < -halfHeight or newPosition.y > halfHeight then
+            newPosition.y = position.y
+            moveSpeed.y = -moveSpeed.y
         end
         end
 
 
-        spriteNode:SetPositionXYZ(newPositionX, newPositionY, z)
+        spriteNode.position = newPosition
         spriteNode:Roll(spriteNode.rotateSpeed * timeStep)
         spriteNode:Roll(spriteNode.rotateSpeed * timeStep)
     end
     end
 end
 end

+ 3 - 1
Source/Engine/LuaScript/LuaScript.cpp

@@ -80,7 +80,6 @@ LuaScript::LuaScript(Context* context) :
 
 
     SetContext(luaState_, context_);
     SetContext(luaState_, context_);
 
 
-    lua_gc(luaState_, LUA_GCSETPAUSE, 125);
     lua_atpanic(luaState_, &LuaScript::AtPanic);
     lua_atpanic(luaState_, &LuaScript::AtPanic);
 
 
     luaL_openlibs(luaState_);
     luaL_openlibs(luaState_);
@@ -383,6 +382,9 @@ void LuaScript::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
         coroutineUpdate_->PushFloat(timeStep);
         coroutineUpdate_->PushFloat(timeStep);
         coroutineUpdate_->EndCall();
         coroutineUpdate_->EndCall();
     }
     }
+
+    // Collect garbage
+    lua_gc(luaState_, LUA_GCCOLLECT, 0);
 }
 }
 
 
 void LuaScript::HandleConsoleCommand(StringHash eventType, VariantMap& eventData)
 void LuaScript::HandleConsoleCommand(StringHash eventType, VariantMap& eventData)