1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#include "common/config.h"
22
23// LOVE
24#include "wrap_Timer.h"
25
26namespace love
27{
28namespace timer
29{
30
31#define instance() (Module::getInstance<Timer>(Module::M_TIMER))
32
33int w_step(lua_State *L)
34{
35 lua_pushnumber(L, instance()->step());
36 return 1;
37}
38
39int w_getDelta(lua_State *L)
40{
41 lua_pushnumber(L, instance()->getDelta());
42 return 1;
43}
44
45int w_getFPS(lua_State *L)
46{
47 lua_pushinteger(L, instance()->getFPS());
48 return 1;
49}
50
51int w_getAverageDelta(lua_State *L)
52{
53 lua_pushnumber(L, instance()->getAverageDelta());
54 return 1;
55}
56
57int w_sleep(lua_State *L)
58{
59 instance()->sleep(luaL_checknumber(L, 1));
60 return 0;
61}
62
63int w_getTime(lua_State *L)
64{
65 lua_pushnumber(L, instance()->getTime());
66 return 1;
67}
68
69// List of functions to wrap.
70static const luaL_Reg functions[] =
71{
72 { "step", w_step },
73 { "getDelta", w_getDelta },
74 { "getFPS", w_getFPS },
75 { "getAverageDelta", w_getAverageDelta },
76 { "sleep", w_sleep },
77 { "getTime", w_getTime },
78 { 0, 0 }
79};
80
81
82extern "C" int luaopen_love_timer(lua_State *L)
83{
84 Timer *instance = instance();
85 if (instance == nullptr)
86 {
87 luax_catchexcept(L, [&](){ instance = new love::timer::Timer(); });
88 }
89 else
90 instance->retain();
91
92 WrappedModule w;
93 w.module = instance;
94 w.name = "timer";
95 w.type = &Module::type;
96 w.functions = functions;
97 w.types = 0;
98
99 return luax_register_module(L, w);
100}
101
102} // timer
103} // love
104