-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
293 lines (239 loc) · 8.03 KB
/
main.cpp
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
#define SOL_ALL_SAFETIES_ON 1
#include <stdio.h>
#include <string>
#include <vector>
#include <allegro5/allegro_font.h>
#include "include/sol.hpp"
#include "engine/App.h"
#include "engine/Game.h"
#include "engine/Graphics.h"
#include "engine/Color.h"
#include "engine/Font.h"
#include "engine/Transform.h"
#include "engine/Keyboard.h"
#include "engine/Rect.h"
#include "engine/Image.h"
#include "engine/Viewport.h"
#include "engine/Camera.h"
#include <fstream>
#include <assert.h>
#include <iostream>
inline bool exists_test (const std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}
int main(int argc, char* args[])
{
float currentTime = 0;
float deltaTime = 0;
float lastUpdate = 0;
int width = 640;
int height = 480;
std::string title = "GXE JellyFish";
sol::state lua;
lua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::string, sol::lib::io, sol::lib::math, sol::lib::table, sol::lib::debug, sol::lib::package, sol::lib::os);
std::string x = lua["package"]["path"];
std::string xx = lua["package"]["cpath"];
lua["package"]["path"] = x + ";./?.lua";
lua["package"]["cpath"] = xx + ";./?.dll;./?.dylib";
lua.new_usertype<Graphics>("GXE_Graphics",
//TODO: Make sprite and geometric drawing (should be easy *foreshadowing maybe*)
"ClearScreen", &Graphics::clearScreen,
"DrawCircle", &Graphics::drawCircle,
"DrawRect", &Graphics::drawRect,
"DrawLine", &Graphics::drawLine,
//Holy ducking sit i could of just passed a refrence to the font object this whole time!?!?
"DrawText", &Graphics::drawText,
"DrawFancyText", &Graphics::drawFancyText,
"DrawImage", &Graphics::drawImage,
"DrawRotatedImage", &Graphics::drawRotatedImage,
"PauseDraw", &Graphics::pauseDraw,
"SetView", &Graphics::setViewport,
"RestoreView", &Graphics::restoreViewport,
"DrawView", &Graphics::drawView
);
lua.new_usertype<Transform>("GXE_Transform",
sol::constructors<Transform()>(),
"Push", &Transform::pushTrans,
"Pop", &Transform::popTrans,
"Translate", &Transform::translate,
"Scale", &Transform::scale
);
lua.new_usertype<Viewport>("GXE_View",
sol::constructors<Viewport(Rect)>()
);
lua.new_usertype<Camera>("GXE_Camera",
sol::constructors<Camera(float, float)>(),
"pos",&Camera::space,
"zoom",&Camera::zoom,
"Mount",&Camera::mount,
"Unmount",&Camera::unmount,
"CenterTarget",&Camera::centerOnTarget
);
lua.new_usertype<Color>("GXE_Color",
sol::constructors<Color(float,float,float,float)>(),
"r", &Color::r,
"g", &Color::g,
"b", &Color::b,
"a", &Color::a,
"EyeStrainBlue", sol::var(Color::EyeStrainBlue),
"MonogameBlue", sol::var(Color::MonogameBlue),
"BloodShotRed", sol::var(Color::BloodShotRed),
"ReallyGreen", sol::var(Color::ReallyGreen)
);
lua.new_usertype<Font>("GXE_Font",
sol::constructors<Font(),Font(std::string,int)>()
);
lua.new_usertype<Image>("GXE_Image",
sol::constructors<Image(std::string)>(),
"crop", &Image::crop,
"source", &Image::source
);
lua.new_usertype<Rect>("GXE_Rect",
sol::constructors<Rect(float, float, float, float),Rect(float, float)>(),
"x", &Rect::x,
"y", &Rect::y,
"w", &Rect::w,
"h", &Rect::h,
"CheckAABB", &Rect::checkCollision,
"MoveAndDetect", &Rect::obstacleCollision/*,
"MoveAndSeperate", &Rect::obstacleSolidCollision*/
);
lua.new_usertype<Keyboard>("GXE_Input",
"KeyDown", &Keyboard::isKeyDown,
"KeyJustPress", &Keyboard::isKeyJustPress,
"mouse", sol::var(Keyboard::mouse),
//Key constants
"KEY_UP", sol::var(Keyboard::KEY_UP),
"KEY_DOWN", sol::var(Keyboard::KEY_DOWN),
"KEY_LEFT", sol::var(Keyboard::KEY_LEFT),
"KEY_RIGHT", sol::var(Keyboard::KEY_RIGHT),
"KEY_W", sol::var(Keyboard::KEY_W),
"KEY_A", sol::var(Keyboard::KEY_A),
"KEY_S", sol::var(Keyboard::KEY_S),
"KEY_D", sol::var(Keyboard::KEY_D)
);
if (!(args[1] == NULL))
{
std::string fn = args[1];
if(fn.substr(fn.find_last_of(".") + 1) == "lua")
{
if (exists_test(fn))
{
lua.script_file(fn);
}else
{
printf("\nfile does not exist");
return 0;
}
}else
{
printf("\nNot a lua file (should have .lua extention)");
return 0;
}
}else
{
std::string fn = "main.lua";
if (exists_test(fn))
{
lua.script_file(fn);
}else
{
printf("\nfile does not exist");
return 0;
}
}
//lua.script_file("main.lua");
sol::optional<float> widthDef = lua["config"]["width"];
sol::optional<float> heightDef = lua["config"]["height"];
sol::optional<std::string> titleDef = lua["config"]["title"];
if(widthDef == sol::nullopt)
{
printf("\nNo width specified... using default");
}else
{
width = lua["config"]["width"];
}
if(heightDef == sol::nullopt)
{
printf("\nNo height specified... using default");
}else
{
height = lua["config"]["height"];
}
if(titleDef == sol::nullopt)
{
printf("\nNo title specified... using default");
}else
{
title = lua["config"]["title"];
}
App app(title.c_str(), width, height);
sol::optional<sol::function> initDef = lua["init"];
if(initDef == sol::nullopt)
{
printf("\nNo init function... closing");
return 0;
}
//Check Update functions
sol::optional<sol::function> updateDef = lua["update"];
if(updateDef == sol::nullopt)
{
printf("\nNo update function... closing");
return 0;
}
sol::optional<sol::function> drawDef = lua["draw"];
if(drawDef == sol::nullopt)
{
printf("\nNo draw function... closing");
return 0;
}
al_store_state(&Graphics::previous_state, ALLEGRO_STATE_TARGET_BITMAP);
ALLEGRO_EVENT event;
al_start_timer(app.timer);
bool done = false;
bool redraw = true;
lua["init"]();
while(1) {
al_wait_for_event(app.queue, &event);
switch(event.type) {
case ALLEGRO_EVENT_TIMER:
redraw = true;
break;
case ALLEGRO_EVENT_KEY_DOWN:
Keyboard::keys[event.keyboard.keycode] = true;
break;
case ALLEGRO_EVENT_KEY_UP:
Keyboard::keys[event.keyboard.keycode] = false;
break;
case ALLEGRO_EVENT_MOUSE_AXES:
Keyboard::mouse.x= event.mouse.x;
Keyboard::mouse.y= event.mouse.y;
lua["GXE_Input"]["mouse"]["x"] = event.mouse.x;
lua["GXE_Input"]["mouse"]["y"] = event.mouse.y;
break;
case ALLEGRO_EVENT_DISPLAY_CLOSE:
done = true;
break;
}
if(done){
break;
}
if(redraw && al_is_event_queue_empty(app.queue))
{
lua["update"](deltaTime);
currentTime = al_get_time();
deltaTime = currentTime - lastUpdate;
lastUpdate = currentTime;
lua["draw"]();
al_flip_display();
redraw = false;
}
}
sol::optional<sol::function> exitDef = lua["exit"];
if(!(exitDef == sol::nullopt))
{
lua["exit"]();
}
return 0;
}