-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cpp
182 lines (153 loc) · 4.42 KB
/
Application.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
#define CHAISCRIPT_NO_THREADS 1
#include <cstdlib>
#include "Nano.h"
static Nano nn;
static chaiscript::ChaiScript chai;
static void printText(const std::string text, int x, int y)
{
nn.putText(text.c_str(), x, y);
}
static void clearScreen(int colour)
{
nn.clearScreen(colour);
}
static void drawRect(int x, int y, int w, int h, int colour)
{
nn.drawRect(colour, x, y, w, h);
}
static void loadWav(int index, std::string name)
{
nn.setSound(index, name);
}
static void playWav(int index)
{
nn.playSound(index);
}
static void loadBmp(int index, std::string name)
{
nn.setSprite(index, name);
}
static void drawBmp(int index, int x, int y)
{
nn.drawSprite(index, x, y);
}
static void loadMus(std::string name)
{
nn.setMusicTrack(name);
}
static void startMus()
{
nn.startMusic();
}
static void stopMus()
{
nn.stopMusic();
}
static void togglePaused()
{
nn.togglePauseMusic();
}
static void controlledDrawBmp(int srcIndex, int srcX, int srcY, int srcW, int srcH, int x, int y)
{
nn.drawSpriteControlled(srcIndex, srcX, srcY, srcW, srcH, x, y);
}
static float random()
{
return static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
}
static char* getText(const char* file)
{
PHYSFS_file* cb = PHYSFS_openRead(file);
PHYSFS_sint64 file_size = PHYSFS_fileLength(cb);
char* myBuf = new char[file_size];
PHYSFS_readBytes(cb, myBuf, file_size);
return myBuf;
}
int main(int argc, char** argv)
{
PHYSFS_init(argv[0]);
if (argc < 2)
PHYSFS_addToSearchPath("cartridge.n9o", 1);
else
PHYSFS_addToSearchPath(argv[1], 1);
chai.add_global_const(chaiscript::const_var(0), "BUTTON_UP");
chai.add_global_const(chaiscript::const_var(1), "BUTTON_DOWN");
chai.add_global_const(chaiscript::const_var(2), "BUTTON_LEFT");
chai.add_global_const(chaiscript::const_var(3), "BUTTON_RIGHT");
chai.add_global_const(chaiscript::const_var(4), "BUTTON_A"); //x, z, s, a | a, b, x, y
chai.add_global_const(chaiscript::const_var(5), "BUTTON_B");
chai.add_global_const(chaiscript::const_var(6), "BUTTON_C");
chai.add_global_const(chaiscript::const_var(7), "BUTTON_D");
chai.add(chaiscript::fun(&printText), "text");
chai.add(chaiscript::fun(&clearScreen), "clr");
chai.add(chaiscript::fun([]() { return clearScreen(0x000000); }), "clr");
chai.add(chaiscript::fun(&drawRect), "draw");
chai.add(chaiscript::fun(&random), "rand");
chai.add(chaiscript::fun(&loadWav), "loadSound");
chai.add(chaiscript::fun(&playWav), "playSound");
chai.add(chaiscript::fun(&loadBmp), "loadSprite");
chai.add(chaiscript::fun(&drawBmp), "drawSprite");
chai.add(chaiscript::fun(&controlledDrawBmp), "drawSprite");
chai.add(chaiscript::fun(&loadMus), "loadMusic");
chai.add(chaiscript::fun(&startMus), "startMusic");
chai.add(chaiscript::fun(&stopMus), "stopMusic");
chai.add(chaiscript::fun(&togglePaused), "pauseMusic");
std::function<void()> init = nullptr;
std::function<void()> loop = nullptr;
std::function<void(int)> keyPressed = nullptr;
std::function<void(int)> keyReleased = nullptr;
try
{
chai.eval(getText("game.chai"));
}
catch (const chaiscript::exception::eval_error &e) {
std::cout << "Base chai exception: " << e.pretty_print() << '\n';
nn.putText("Cartridge loading failed", 0, 48);
}
{
try
{
init = chai.eval<std::function<void()>>("init");
init();
}
catch (const chaiscript::exception::eval_error &e)
{
std::cout << "Cartridge loaded with no 'init' function. Continuing..." << std::endl << e.pretty_print() << std::endl;
}
try
{
keyPressed = chai.eval<std::function<void(int)>>("keyPressed");
}
catch (const chaiscript::exception::eval_error &e)
{
std::cout << "Cartridge loaded with no 'keyPressed' function. Continuing..." << std::endl << e.pretty_print() << std::endl;
keyReleased = NULL;
}
try
{
keyReleased = chai.eval<std::function<void(int)>>("keyReleased");
}
catch (const chaiscript::exception::eval_error &e)
{
std::cout << "Cartridge loaded with no 'keyReleased' function. Continuing..." << std::endl << e.pretty_print() << std::endl;
keyReleased = NULL;
}
try
{
loop = chai.eval<std::function<void()>>("loop");
}
catch (const chaiscript::exception::eval_error &e)
{
std::cout << "Cartridge loaded with no 'loop' function. Aborting." << std::endl << e.pretty_print() << std::endl;
return -1;
}
}
nn.loop(loop, keyPressed, keyReleased);
nn.~Nano();
return 0;
}
/*
catch (const chaiscript::exception::eval_error &e) {
std::cout << e.pretty_print() << '\n';
}
*/