galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
LuaTime.cpp
Go to the documentation of this file.
1
7
8#include <entt/locator/locator.hpp>
9#include <SDL3/SDL_timer.h>
10#include <sol/sol.hpp>
11
13#include "galaxy/time/Time.hpp"
14#include "galaxy/time/Timer.hpp"
15
16#include "../Lua.hpp"
17
18namespace galaxy
19{
20 void Lua::inject_time() noexcept
21 {
22 auto& lua = entt::locator<sol::state>::value();
23
24 lua.set_function("time_now", &time::now);
25 lua.set_function("time_since_epoch", &time::time_since_epoch);
26 lua.set_function("time_set_dt", sol::resolve<void(const double)>(&time::dt));
27 lua.set_function("time_get_dt", sol::resolve<double(void)>(&time::dt));
28 lua.set_function("time_delay", &time::delay);
29 lua.set_function("time_ms_to_ns", &time::ms_to_ns);
30 lua.set_function("time_ns_to_ms", &time::ns_to_ms);
31 lua.set_function("time_ns_to_s", &time::ns_to_s);
32 lua.set_function("time_ns_to_us", &time::ns_to_us);
33 lua.set_function("time_s_to_ns", &time::s_to_ns);
34 lua.set_function("time_us_to_ns", &time::us_to_ns);
35
36 lua.set("GALAXY_MS_PER_SECOND", SDL_MS_PER_SECOND);
37 lua.set("GALAXY_NS_PER_MS", SDL_NS_PER_MS);
38 lua.set("GALAXY_NS_PER_SECOND", SDL_NS_PER_SECOND);
39 lua.set("GALAXY_NS_PER_US", SDL_NS_PER_US);
40 lua.set("GALAXY_US_PER_SECOND", SDL_US_PER_SECOND);
41
42 auto stopwatch_type = lua.new_usertype<Stopwatch>("Stopwatch", sol::constructors<Stopwatch()>());
43 // stopwatch_type["set"] = &Stopwatch::set;
44 stopwatch_type["repeat"] = &Stopwatch::repeat;
45 stopwatch_type["start"] = &Stopwatch::start;
46 stopwatch_type["stop"] = &Stopwatch::stop;
47 stopwatch_type["pause"] = &Stopwatch::pause;
48 stopwatch_type["unpause"] = &Stopwatch::unpause;
49 stopwatch_type["update"] = &Stopwatch::update;
50 stopwatch_type["stopped"] = &Stopwatch::stopped;
51 stopwatch_type["paused"] = &Stopwatch::paused;
52 stopwatch_type["get_time_passed"] = &Stopwatch::get_time_passed;
53
54 auto timer_type = lua.new_usertype<Timer>("Timer", sol::constructors<Timer()>());
55 // timer_type["set"] = &Timer::set;
56 timer_type["start"] = &Timer::start;
57 timer_type["stop"] = &Timer::stop;
58 timer_type["pause"] = &Timer::pause;
59 timer_type["stopped"] = &Timer::stopped;
60 timer_type["paused"] = &Timer::paused;
61 }
62} // namespace galaxy
static void inject_time() noexcept
Register galaxy time support functions into Lua.
Definition LuaTime.cpp:20
Synchronous stopwatch.
Definition Stopwatch.hpp:19
void stop()
Stop Stopwatch.
Definition Stopwatch.cpp:50
void pause()
Pause the Stopwatch.
Definition Stopwatch.cpp:59
void unpause()
Resume the Stopwatch.
Definition Stopwatch.cpp:70
void start()
Start Stopwatch.
Definition Stopwatch.cpp:41
void repeat(const bool repeat)
Make function repeat itself instead of running once.
Definition Stopwatch.cpp:36
bool paused() const
Is the Stopwatch paused?
void update()
Call this if you want to trigger the callback after delay has passed.
Definition Stopwatch.cpp:81
std::uint64_t get_time_passed() const noexcept
Time passed in milliseconds.
bool stopped() const
Is the Stopwatch finished?
Definition Stopwatch.cpp:97
Asynchronous timer class.
Definition Timer.hpp:19
void stop() noexcept
Stop timer.
Definition Timer.cpp:67
void pause(const bool pause) noexcept
Pause the timer.
Definition Timer.cpp:79
void start(const bool repeat=false)
Start timer.
Definition Timer.cpp:40
bool paused() const noexcept
Is the timer paused?
Definition Timer.cpp:89
bool stopped() const noexcept
Is the timer finished?
Definition Timer.cpp:84
std::uint64_t ms_to_ns(const std::uint64_t ms) noexcept
Converts milliseconds to nanoseconds.
Definition Time.cpp:44
void delay(const int milli) noexcept
Delay the current thread for x time.
Definition Time.cpp:39
std::uint64_t s_to_ns(const std::uint64_t s) noexcept
Converts seconds to nanoseconds.
Definition Time.cpp:64
std::uint64_t us_to_ns(const std::uint64_t us) noexcept
Converts microseconds to nanoseconds.
Definition Time.cpp:69
std::uint64_t ns_to_ms(const std::uint64_t ns) noexcept
Converts nanoseconds to milliseconds.
Definition Time.cpp:49
std::uint64_t ns_to_us(const std::uint64_t ns) noexcept
Converts nanoseconds to microseconds.
Definition Time.cpp:59
void dt(const double dt) noexcept
Set galaxy delta time.
Definition Time.cpp:29
auto now() noexcept -> std::chrono::local_time< std::chrono::system_clock::duration >
Current local time.
Definition Time.cpp:18
std::uint64_t ns_to_s(const std::uint64_t ns) noexcept
Converts nanoseconds to seconds.
Definition Time.cpp:54
std::uint64_t time_since_epoch() noexcept
Time since epoch.
Definition Time.cpp:23
Application.hpp galaxy.