-
Notifications
You must be signed in to change notification settings - Fork 2
/
include.h
146 lines (119 loc) · 3.6 KB
/
include.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include "LuaEngine.h"
#include <atomic>
#define SDLError(L, prompt) luaL_error(L, #prompt ": %s", SDL_GetError())
#define TTFError(L, prompt) luaL_error(L, #prompt ": %s", TTF_GetError())
#define IMGError(L, prompt) luaL_error(L, #prompt ": %s", IMG_GetError())
#define MixError(L, prompt) luaL_error(L, #prompt ": %s", Mix_GetError())
#define lua_setfield_function(L, name, func) lua_pushcfunction(L, func);lua_setfield(L, -2, name)
template<typename PointerType>
void lua_newpointer(lua_State* L, PointerType* p)
{
auto ptr = (PointerType**)lua_newuserdata(L, sizeof(PointerType*));
*ptr = p;
}
template<typename PointerType>
PointerType* lua_checkpointer(lua_State* L, int idx, const char* name)
{
return *(PointerType**)luaL_checkudata(L, idx, name);
}
template<typename ResourceType>
class ShareableResource
{
public:
std::atomic<bool> isShared;
ResourceType* ptr;
void (*fnDeleter)(ResourceType*);
std::shared_ptr<ResourceType> sp;
ShareableResource(ResourceType* in, void (*delfn)(ResourceType*)) : isShared(false), ptr(in), fnDeleter(delfn)
{
}
ShareableResource(const std::shared_ptr<ResourceType>& in) : isShared(true), ptr(nullptr), fnDeleter(nullptr), sp(in)
{
}
~ShareableResource()
{
if (ptr)
{
fnDeleter(ptr);
ptr = nullptr;
}
}
void enable_share()
{
bool b = false;
if (isShared.compare_exchange_strong(b, true))
{
sp.reset(ptr, fnDeleter);
ptr = nullptr;
}
}
ResourceType* get()
{
if (!isShared) return ptr;
else return sp.get();
}
};
template<typename ResourceType>
void lua_newres(lua_State* L, const std::shared_ptr<ResourceType>& sp)
{
new (lua_newuserdata(L, sizeof(ShareableResource<ResourceType>))) ShareableResource<ResourceType>(sp);
}
template<typename ResourceType>
void lua_newres(lua_State* L, ResourceType* p, void(*delfn)(ResourceType*))
{
new (lua_newuserdata(L, sizeof(ShareableResource<ResourceType>))) ShareableResource<ResourceType>(p, delfn);
}
template<typename ResourceType>
ResourceType* lua_checkres(lua_State* L, int idx, const char* name)
{
auto pres = (ShareableResource<ResourceType>*)luaL_checkudata(L, idx, name);
return pres->get();
}
template<typename ResourceType>
ShareableResource<ResourceType>* lua_checkfullres(lua_State* L, int idx, const char* name)
{
return (ShareableResource<ResourceType>*)luaL_checkudata(L, idx, name);
}
template<typename T>
T* lua_newblock(lua_State* L)
{
return (T*)lua_newuserdata(L, sizeof(T));
}
template<typename T>
T* lua_checkblock(lua_State* L, int idx, const char* name)
{
return (T*)luaL_checkudata(L, idx, name);
}
template<typename T>
T* lua_testblock(lua_State* L, int idx, const char* name)
{
return (T*)luaL_testudata(L, idx, name);
}
// Shared Functions
// Surface
void put_surface(lua_State* L, SDL_Surface* surf);
void put_surface(lua_State* L, const std::shared_ptr<SDL_Surface>& surf);
void put_texture(lua_State* L, SDL_Texture* text); // Texture
const char* VirtualKeyToString(SDL_Keycode vkey); // Event
// Init Functions
void InitWindow(lua_State* L);
void InitRenderer(lua_State* L);
void InitSurface(lua_State* L);
void InitFont(lua_State* L);
void InitMusic(lua_State* L);
void InitEvent(lua_State* L);
void InitCommon(lua_State* L);
void InitTCPSocket(lua_State* L);
void InitUDPSocket(lua_State* L);
void InitNetwork(lua_State* L);
void InitThread(lua_State* L);
void InitChannel(lua_State* L);
void InitSocketSelector(lua_State* L);
void PlatInit();
#ifdef _WIN32
void put_winerror(lua_State* L, int errcode, const char* hint);
#else
void put_linuxerror(lua_State* L, int errcode, const char* hint);
#endif // ifdef _WIN32
int lua_errno(lua_State* L, const char* hint);