-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeometry.cpp
More file actions
177 lines (176 loc) · 7.03 KB
/
geometry.cpp
File metadata and controls
177 lines (176 loc) · 7.03 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//=============================================================================
// ■ Geometry
//-----------------------------------------------------------------------------
// A Lua module for dealing with points, rectangles and margins.
// These are duck-typed pseudo classes, which need only a table with named
// fields present:
// - Point: x, y
// - Rect: x, y, w (width) and h (height)
// - Margin: t (top), r (right), b (bottom) and l (left)
// Thus a rectangle is a point.
//=============================================================================
namespace Geometry {
//-------------------------------------------------------------------------
// ● Definitions
// Point and Rect are defined by SDL.
//-------------------------------------------------------------------------
struct Margin {
int t, r, b, l;
};
//-------------------------------------------------------------------------
// ● to_rect
// Unspecified values will be set to zero.
//-------------------------------------------------------------------------
void to_rect(lua_State* L, int index, SDL_Rect* rect) {
lua_getfield(L, index, "x"); rect->x = lua_tointeger(L, -1);
lua_getfield(L, index, "y"); rect->y = lua_tointeger(L, -1);
lua_getfield(L, index, "w"); rect->w = lua_tointeger(L, -1);
lua_getfield(L, index, "h"); rect->h = lua_tointeger(L, -1);
lua_pop(L, 4);
}
//-------------------------------------------------------------------------
// ● check_rect
//-------------------------------------------------------------------------
void check_rect(lua_State* L, int index, SDL_Rect* rect) {
lua_getfield(L, index, "x"); rect->x = luaL_checkinteger(L, -1);
lua_getfield(L, index, "y"); rect->y = luaL_checkinteger(L, -1);
lua_getfield(L, index, "w"); rect->w = luaL_checkinteger(L, -1);
lua_getfield(L, index, "h"); rect->h = luaL_checkinteger(L, -1);
lua_pop(L, 4);
}
//-------------------------------------------------------------------------
// ● create_rect
//-------------------------------------------------------------------------
void create_rect(lua_State* L, const SDL_Rect* rect) {
lua_createtable(L, 0, 4);
lua_pushnumber(L, rect->x); lua_setfield(L, -2, "x");
lua_pushnumber(L, rect->y); lua_setfield(L, -2, "y");
lua_pushnumber(L, rect->w); lua_setfield(L, -2, "w");
lua_pushnumber(L, rect->h); lua_setfield(L, -2, "h");
}
//-------------------------------------------------------------------------
// ● to_point
//-------------------------------------------------------------------------
void to_point(lua_State* L, int index, SDL_Point* point) {
lua_getfield(L, index, "x"); point->x = lua_tointeger(L, -1);
lua_getfield(L, index, "y"); point->y = lua_tointeger(L, -1);
lua_pop(L, 2);
}
//-------------------------------------------------------------------------
// ● check_point
//-------------------------------------------------------------------------
void check_point(lua_State* L, int index, SDL_Point* point) {
lua_getfield(L, index, "x"); point->x = luaL_checkinteger(L, -1);
lua_getfield(L, index, "y"); point->y = luaL_checkinteger(L, -1);
lua_pop(L, 2);
}
//-------------------------------------------------------------------------
// ● create_point
//-------------------------------------------------------------------------
void create_point(lua_State* L, const SDL_Point* point) {
lua_createtable(L, 0, 2);
lua_pushnumber(L, point->x); lua_setfield(L, -2, "x");
lua_pushnumber(L, point->y); lua_setfield(L, -2, "y");
}
//-------------------------------------------------------------------------
// ● check_margin
//-------------------------------------------------------------------------
void check_margin(lua_State* L, int index, Margin* margin) {
lua_getfield(L, index, "t"); margin->t = luaL_checkinteger(L, -1);
lua_getfield(L, index, "r"); margin->r = luaL_checkinteger(L, -1);
lua_getfield(L, index, "b"); margin->b = luaL_checkinteger(L, -1);
lua_getfield(L, index, "l"); margin->l = luaL_checkinteger(L, -1);
lua_pop(L, 4);
}
//-------------------------------------------------------------------------
// ● rect_empty(rect)
//-------------------------------------------------------------------------
int lua_rect_empty(lua_State* L) {
SDL_Rect rect;
check_rect(L, 1, &rect);
lua_pushboolean(L, SDL_RectEmpty(&rect));
return 1;
}
//-------------------------------------------------------------------------
// ● point_in_rect(point, rect)
//-------------------------------------------------------------------------
int lua_point_in_rect(lua_State* L) {
SDL_Point point;
SDL_Rect rect;
check_point(L, 1, &point);
check_rect(L, 2, &rect);
lua_pushboolean(L, SDL_PointInRect(&point, &rect));
return 1;
}
//-------------------------------------------------------------------------
// ● rect_has_intersection(rect1, rect2)
//-------------------------------------------------------------------------
int lua_rect_has_intersection(lua_State* L) {
SDL_Rect rect1, rect2;
check_rect(L, 1, &rect1);
check_rect(L, 2, &rect2);
lua_pushboolean(L, SDL_HasIntersection(&rect1, &rect1));
return 1;
}
//-------------------------------------------------------------------------
// ● intersect_rect(rect1, rect2)
// nil will be returned if there's no intersection.
//-------------------------------------------------------------------------
int lua_intersect_rect(lua_State* L) {
SDL_Rect rect1, rect2, rect3;
check_rect(L, 1, &rect1);
check_rect(L, 2, &rect2);
if (SDL_IntersectRect(&rect1, &rect2, &rect3)) {
create_rect(L, &rect3);
} else {
lua_pushnil(L);
}
return 1;
}
//-------------------------------------------------------------------------
// ● expand_rect(rect, margin) → new rect
//-------------------------------------------------------------------------
int lua_expand_rect(lua_State* L) {
SDL_Rect rect;
Margin margin;
check_rect(L, 1, &rect);
check_margin(L, 2, &margin);
rect.x -= margin.l;
rect.y -= margin.t;
rect.w += margin.l + margin.r;
rect.h += margin.t + margin.b;
create_rect(L, &rect);
return 1;
}
//-------------------------------------------------------------------------
// ● shrink_rect(rect, margin) → new rect
//-------------------------------------------------------------------------
int lua_shrink_rect(lua_State* L) {
SDL_Rect rect;
Margin margin;
check_rect(L, 1, &rect);
check_margin(L, 2, &margin);
rect.x += margin.l;
rect.y += margin.t;
rect.w -= margin.l + margin.r;
rect.h -= margin.t + margin.b;
create_rect(L, &rect);
return 1;
}
//-------------------------------------------------------------------------
// ● init
//-------------------------------------------------------------------------
void init() {
const luaL_reg reg[] = {
{"rect_empty", lua_rect_empty},
{"point_in_rect", lua_point_in_rect},
{"rect_has_intersection", lua_rect_has_intersection},
{"intersect_rect", lua_intersect_rect},
{"expand_rect", lua_expand_rect},
{"shrink_rect", lua_shrink_rect},
{NULL, NULL}
};
luaL_register(L, "Geometry", reg);
lua_pop(L, 1);
}
}