51 lines
1.8 KiB
C
51 lines
1.8 KiB
C
/**
|
|
Timer library for OCT
|
|
Lua functions will be able to register a function to be called after a certain amount of time. Timer values are updated on every call to the rendering loop, and when timer hits 0 the corresponding function is called
|
|
Optionally, we can also specify a string argument to be passed to the function when called
|
|
**/
|
|
#ifndef OCT_TIMER_H
|
|
#define OCT_TIMER_H
|
|
|
|
#include <inttypes.h>
|
|
#include <lua5.3/lualib.h>
|
|
#include <lua5.3/lauxlib.h>
|
|
|
|
#define OCT_TIMER_MAXIMUM_FUNCTION_NAME_LENGTH 128
|
|
#define OCT_TIMER_MAXIMUM_ARGUMENT_LENGTH 128
|
|
#define OCT_TIMER_MAXIMUM_ID_LENGTH 128
|
|
#define OCT_TIMER_INITIAL_NUM_TIMERS 10
|
|
#define OCT_TIMER_LIST_REALLOC_LENGTH 10
|
|
|
|
struct oct_timer_elem {
|
|
char id[OCT_TIMER_MAXIMUM_ID_LENGTH];
|
|
int64_t millis;
|
|
char lua_function[OCT_TIMER_MAXIMUM_FUNCTION_NAME_LENGTH];
|
|
char lua_argument[OCT_TIMER_MAXIMUM_ARGUMENT_LENGTH];
|
|
struct oct_timer_elem* next;
|
|
struct oct_timer_elem* prev;
|
|
};
|
|
|
|
// Timer list is a doubly-linked list with O(1) inserts
|
|
// I can't imagine we're cancelling timers that often so this is fine
|
|
// Instead I think we will be inserting most of the time
|
|
// I could optimize this by inserting new timers in order of time to execution, but I don't see a reason to optimize yet
|
|
// O(1) inserts, O(n) accesses
|
|
struct oct_timer_list_ {
|
|
uint32_t size;
|
|
struct oct_timer_elem* first;
|
|
struct oct_timer_elem* last;
|
|
};
|
|
|
|
extern struct oct_timer_list_ oct_timer_list;
|
|
|
|
int oct_timer_list_initialize();
|
|
int oct_timer_list_deinitialize();
|
|
int oct_timer_initialize_lua(lua_State *L);
|
|
int oct_timer_register(lua_State *L);
|
|
int oct_timer_unregister_lua(lua_State *L);
|
|
int oct_timer_unregister(char* id, int do_free); // free = 0 => depend on user to free the unregistered elem, free = 1 => free the unregistered elem
|
|
struct oct_timer_elem* oct_timer_find(char* id);
|
|
int oct_timer_tick(lua_State *L);
|
|
|
|
#endif
|