Lua delta time milliseconds style
I am in the middle of implementing a tweener in lua. However this tweener needs millisecond accuracy and lua doesn't have the mojo cross platform to do this. So I braved a little c++, which is way out of my comfort zone.
#include <boost/chrono/chrono.hpp>
#include <boost/chrono/chrono_io.hpp>
#include <boost/chrono/process_cpu_clocks.hpp>
#include <boost/chrono/thread_clock.hpp>
#include <boost/chrono/ceil.hpp>
#include <boost/chrono/floor.hpp>
#include <boost/chrono/round.hpp>
using namespace boost::chrono;
steady_clock::time_point current_time;
steady_clock::time_point last_time;
void update_time() {
last_time = current_time;
current_time = steady_clock::now();
}
long delta_time() {
return static_cast<long>((duration_cast<milliseconds>(current_time - last_time)).count());
}
voila a timer
I was venturing down the road of doing a lua binding but trying to compile against leadwerks lua scared me so I made an empty gmf and send it a message on my main loop and attached a listener. The message gets sent before any of the stuffs happens.
Main Loop
update_time(); SendEntityMessage(timer, to_string(delta_time()).c_str());
Lua Listener
require "scripts/class" local class=CreateClass(...) function class:CreateObject(model) local object=self.super:CreateObject(model) function object:ReceiveMessage(message) delta_time = tonumber(message) end function object:Free() self.super:Free() end end
Good enough! :shipit:
P.S.
Tweener+ Promise= Hawt Animations
And if my c++ is offensive I wouldn't mind hearing; unless it has to do with the worthless adherence to Hungarian notation.
Peace,
Chris
-
1
1 Comment
Recommended Comments