Jump to content
  • entry
    1
  • comment
    1
  • views
    5,817

Lua delta time milliseconds style


ChrisMAN

5,661 views

 Share

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

  • Upvote 1
 Share

1 Comment


Recommended Comments

Yesterday I needed again an accurate nanosecond timer in C++, so I found my old post in the Forums which had this code. It works on all platforms and doesn't need Boost:

 

#include <sys/time.h>

#include <stdio.h>

 

long timeGetTime();

long system_starttime=timeGetTime();

long timeGetTime()

{

struct timeval start;

long mtime;

gettimeofday(&start, NULL);

mtime = (1000*start.tv_sec + start.tv_usec/1000.0) + 0.5 - system_starttime;

return mtime;

}

  • Upvote 1
Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...