forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcelx_misc.cpp
More file actions
366 lines (287 loc) · 8.7 KB
/
celx_misc.cpp
File metadata and controls
366 lines (287 loc) · 8.7 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// celx_misc.cpp
//
// Copyright (C) 2019, the Celestia Development Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <celutil/debug.h>
#include "celx_misc.h"
#include "celx_internal.h"
#include <celscript/legacy/cmdparser.h>
#include <celscript/legacy/execution.h>
#include <celestia/celestiacore.h>
#include <celttf/truetypefont.h>
using namespace std;
using namespace celestia::scripts;
LuaState *getLuaStateObject(lua_State*);
// Wrapper for a CEL-script, including the needed Execution Environment
class CelScriptWrapper : public ExecutionEnvironment
{
public:
CelScriptWrapper(CelestiaCore& appCore, istream& scriptfile):
core(appCore)
{
CommandParser parser(scriptfile, appCore.scriptMaps());
cmdSequence = parser.parse();
if (cmdSequence != nullptr)
{
script = new Execution(*cmdSequence, *this);
}
else
{
auto errors = parser.getErrors();
if (!errors.empty())
errorMessage = "Error while parsing CEL-script: " + errors[0];
else
errorMessage = "Error while parsing CEL-script.";
}
}
virtual ~CelScriptWrapper()
{
delete script;
delete cmdSequence;
}
string getErrorMessage() const
{
return errorMessage;
}
// tick the CEL-script. t is in seconds and doesn't have to start with zero
bool tick(double t)
{
// use first tick to set the time
if (tickTime == 0.0)
{
tickTime = t;
return false;
}
double dt = t - tickTime;
tickTime = t;
return script->tick(dt);
}
Simulation* getSimulation() const
{
return core.getSimulation();
}
Renderer* getRenderer() const
{
return core.getRenderer();
}
CelestiaCore* getCelestiaCore() const
{
return &core;
}
void showText(string s, int horig, int vorig, int hoff, int voff,
double duration)
{
core.showText(std::move(s), horig, vorig, hoff, voff, duration);
}
private:
Execution* script{ nullptr };
CelestiaCore& core;
CommandSequence* cmdSequence{ nullptr };
double tickTime { 0.0 };
string errorMessage;
};
// ==================== Celscript-object ====================
int celxClassId(CelScriptWrapper*)
{
return Celx_CelScript;
}
// create a CelScriptWrapper from a string:
int celscript_from_string(lua_State* l, string& script_text)
{
CelxLua celx(l);
istringstream scriptfile(script_text);
CelestiaCore* appCore = celx.appCore(AllErrors);
CelScriptWrapper* celscript = new CelScriptWrapper(*appCore, scriptfile);
if (celscript->getErrorMessage() != "")
{
string error = celscript->getErrorMessage();
delete celscript;
celx.doError(error.c_str());
}
else
{
celx.pushClass(celscript);
}
return 1;
}
static int celscript_tostring(lua_State* l)
{
lua_pushstring(l, "[Celscript]");
return 1;
}
static int celscript_tick(lua_State* l)
{
CelxLua celx(l);
auto script = *celx.getThis<CelScriptWrapper*>();
LuaState* stateObject = celx.getLuaStateObject();
double t = stateObject->getTime();
return celx.push(!(script->tick(t)));
}
static int celscript_gc(lua_State* l)
{
CelxLua celx(l);
auto script = *celx.getThis<CelScriptWrapper*>();
delete script;
return 0;
}
void CreateCelscriptMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_CelScript);
celx.registerMethod("__tostring", celscript_tostring);
celx.registerMethod("tick", celscript_tick);
celx.registerMethod("__gc", celscript_gc);
celx.pop(1); // remove metatable from stack
}
// ==================== Font Object ====================
static int font_bind(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for font:bind()");
auto font = *celx.getThis<std::shared_ptr<TextureFont>>();
font->bind();
return 0;
}
static int font_unbind(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for font:unbind()");
auto font = *celx.getThis<std::shared_ptr<TextureFont>>();
font->unbind();
return 0;
}
static int font_render(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for font:render");
const char* s = celx.safeGetString(2, AllErrors, "First argument to font:render must be a string");
auto font = *celx.getThis<std::shared_ptr<TextureFont>>();
#ifndef GL_ES
Eigen::Matrix4f p, m;
glGetFloatv(GL_PROJECTION_MATRIX, p.data());
glGetFloatv(GL_MODELVIEW_MATRIX, m.data());
font->setMVPMatrices(p, m);
#endif
return celx.push(font->render(s));
}
static int font_getwidth(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for font:getwidth");
const char* s = celx.safeGetString(2, AllErrors, "Argument to font:getwidth must be a string");
auto font = *celx.getThis<std::shared_ptr<TextureFont>>();
return celx.push(font->getWidth(s));
}
static int font_getheight(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for font:getheight()");
auto font = *celx.getThis<std::shared_ptr<TextureFont>>();
lua_pushnumber(l, font->getHeight());
return 1;
}
static int font_tostring(lua_State* l)
{
CelxLua celx(l);
return celx.push("[Font]");
}
void CreateFontMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Font);
celx.registerMethod("__tostring", font_tostring);
celx.registerMethod("bind", font_bind);
celx.registerMethod("render", font_render);
celx.registerMethod("unbind", font_unbind);
celx.registerMethod("getwidth", font_getwidth);
celx.registerMethod("getheight", font_getheight);
celx.pop(1); // remove metatable from stack
}
// ==================== Image =============================================
#if 0
static int image_new(lua_State* l, Image* i)
{
Image** ud = static_cast<Image**>(lua_newuserdata(l, sizeof(Image*)));
*ud = i;
Celx_SetClass(l, Celx_Image);
return 1;
}
#endif
int celxClassId(Image*)
{
return Celx_Image;
}
static int image_getheight(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for image:getheight()");
auto image = *celx.getThis<Image*>();
return celx.push(image->getHeight());
}
static int image_getwidth(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for image:getwidth()");
auto* image = *celx.getThis<Image*>();
return celx.push(image->getWidth());
}
static int image_tostring(lua_State* l)
{
CelxLua celx(l);
auto image = *celx.getThis<Image*>();
std::string s = fmt::sprintf("[Image:%dx%d]", image->getWidth(), image->getHeight());
return celx.push(s.c_str());
}
void CreateImageMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Image);
celx.registerMethod("__tostring", image_tostring);
celx.registerMethod("getheight", image_getheight);
celx.registerMethod("getwidth", image_getwidth);
celx.pop(1); // remove metatable from stack
}
// ==================== Texture ============================================
static int texture_bind(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for texture:bind()");
auto texture = *celx.getThis<Texture*>();
texture->bind();
return 0;
}
static int texture_getheight(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for texture:getheight()");
auto texture = *celx.getThis<Texture*>();
return celx.push(texture->getHeight());
}
static int texture_getwidth(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for texture:getwidth()");
auto texture = *celx.getThis<Texture*>();
return celx.push(texture->getWidth());
}
static int texture_tostring(lua_State* l)
{
CelxLua celx(l);
auto tex = *celx.getThis<Texture*>();
std::string s = fmt::sprintf("[Texture:%dx%d]", tex->getWidth(), tex->getHeight());
return celx.push(s.c_str());
}
void CreateTextureMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Texture);
celx.registerMethod("__tostring", texture_tostring);
celx.registerMethod("getheight", texture_getheight);
celx.registerMethod("getwidth", texture_getwidth);
celx.registerMethod("bind", texture_bind);
celx.pop(1); // remove metatable from stack
}