-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
420 lines (340 loc) · 9.94 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
particled
TMW particle effect viewer
A lot of code copied from various parts of TMW client.
*/
#include <iostream>
#include <vector>
#include <physfs.h>
#include <SDL.h>
#include <SDL_image.h>
#include <guichan.hpp>
#include <guichan/sdl/sdlimageloader.hpp>
#include <libxml/parser.h>
#include <getopt.h>
#include "main.h"
#include "log.h"
#include "map.h"
#include "particle.h"
#include "resources/image.h"
#include "resources/resourcemanager.h"
#include "resources/imageloader.h"
#include "resources/dye.h"
#include "graphics.h"
#include "configuration.h"
#include "widgets/effectviewer.h"
#include "widgets/playcontrolpanel.h"
struct Options
{
Options():
printHelpAndExit(false),
winWidth(200),
winHeight(200),
effectX(0),
effectY(0),
quitOnEffectEnd(false),
loop(false),
backgroundColour(0,0,0)
{};
bool printHelpAndExit;
int winWidth, winHeight;
int effectX, effectY;
std::string dataDir;
std::string effectFile;
bool quitOnEffectEnd;
bool loop;
gcn::Color backgroundColour;
};
// global variables:
Logger *logger;
Graphics* graphics;
gcn::Gui* gui;
gcn::Container* top;
gcn::ImageFont* font;
ImageLoader* imageLoader; // TMW's ImageLoader, not GCN's virtual class
EffectViewer* effectViewer;
PlayControlPanel* playControlPanel;
// in particlEd Configuration is empty (mock!)
Configuration config;
Options options;
void initEngine();
void exitEngine();
void parseOptions(int argc, char *argv[], Options &options);
void printHelp();
void startEffect();
int main(int argc, char* argv[])
{
logger = new Logger();
// parse options; set some defaults; control required ones
parseOptions(argc, argv, options);
if (options.printHelpAndExit) {
printHelp();
return(0);
}
if (options.effectX == 0)
options.effectX = options.winWidth / 2;
if (options.effectY == 0)
options.effectY = options.winHeight / 2;
if (options.effectFile.empty()) {
logger->error("Please, specify particle effect definition file.");
return(1);
}
PHYSFS_init(argv[0]);
logger->log("-> initialize engine");
initEngine();
effectViewer->setEffectPosition(options.effectX, options.effectY);
effectViewer->setBackgroundColour(options.backgroundColour);
startEffect();
logger->log("-> game loop");
SDL_Event event;
bool quit = false;
// event loop
while (true) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
} else if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
quit = true;
case SDLK_r:
// start new effect
startEffect();
break;
case SDLK_n:
// delete all particles and start new effect
effectViewer->cleanParticles();
startEffect();
break;
}
}
}
// std::cout << m->getNumSprites() << std::endl;
gui->logic();
gui->draw();
graphics->updateScreen();
// end if particle effect has ended
if (effectViewer->isEffectOver()) {
if (options.quitOnEffectEnd) {
quit = true;
} else if (options.loop) {
startEffect();
}
}
if (quit) {
break;
}
SDL_Delay(10);
}
logger->log("-> game loop ended");
logger->log("-> exit engine");
exitEngine();
logger->log("-> done.");
delete logger;
PHYSFS_deinit();
return(0);
}
/**
* Do all initialization stuff.
*/
void initEngine()
{
gcn::SDLImageLoader* sdlImageLoader;
// Initialize SDL
logger->log("[SDL] Initializing SDL...");
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
std::cerr << "Could not initialize SDL: " <<
SDL_GetError() << std::endl;
exit(1);
}
atexit(SDL_Quit);
SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
std::string title = "particlEd: ";
title += options.effectFile;
SDL_WM_SetCaption(title.c_str(), NULL);
logger->log("[SDL] OK.");
// Resource Manager
logger->log("[RM] Initializing Resource Manager");
ResourceManager *resman = ResourceManager::getInstance();
logger->log("[RM] adding directory to search path");
if (options.dataDir.empty()) {
logger->log("No data directory specified; setting \".\"");
resman->addToSearchPath(".", false);
} else {
resman->addToSearchPath(options.dataDir, false);
}
// directory with particlEd's own data:
resman->addToSearchPath("data", false);
logger->log("[RM] OK.");
logger->log("[GFX] Setting up video.");
int width = options.winWidth;
int height = options.winHeight+50;
int bpp = 0;
bool fullscreen = 0;
bool hwaccel = 0;
graphics = new Graphics();
// Try to set the desired video mode
if (!graphics->setVideoMode(width, height, bpp, fullscreen, hwaccel))
{
std::cerr << "Couldn't set "
<< width << "x" << height << "x" << bpp << " video mode: "
<< SDL_GetError() << std::endl;
exit(1);
}
logger->log("[GFX] initializing for drawing");
// Initialize for drawing
graphics->_beginDraw();
logger->log("[GFX] Ok.");
gui = new gcn::Gui();
gui->setGraphics(graphics);
// temporarily use gcn::SDLImageLoader - TMW's ImageLoader doesn't load
// the font for me
sdlImageLoader = new gcn::SDLImageLoader();
gcn::Image::setImageLoader(sdlImageLoader);
try {
std::string fontpath = resman->getPath("browserfont.png");
font = new gcn::ImageFont(fontpath.c_str(), " !\"#$%&'()=+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~|");
} catch (gcn::Exception e) {
logger->log("Error: Can't load font.");
exit(1);
}
//gcn::Widget::setGlobalFont(font);
delete sdlImageLoader;
// font loaded; go for TMW's ImageLoader
imageLoader = new ImageLoader();
gcn::Image::setImageLoader(imageLoader);
top = new gcn::Container;
top->setDimension(gcn::Rectangle(0,0,graphics->getWidth(),
graphics->getHeight()));
gui->setTop(top);
effectViewer = new EffectViewer();
top->add(effectViewer);
effectViewer->setDimension(gcn::Rectangle(0,0,
options.winWidth,
options.winHeight));
playControlPanel = new PlayControlPanel();
playControlPanel->setDimension(gcn::Rectangle(0,0,
options.winWidth,50));
playControlPanel->setPosition(0, options.winHeight);
top->add(playControlPanel);
}
/** Clear the engine */
void exitEngine()
{
delete effectViewer;
delete playControlPanel;
delete top;
delete graphics;
delete gui;
delete font;
delete imageLoader;
// Shutdown libxml
xmlCleanupParser();
ResourceManager::deleteInstance();
}
/** Starts effect defined in options */
void startEffect()
{
logger->log("Adding effect.");
effectViewer->setEffect(options.effectFile);
}
/** parse given colour string and put the colour to options.backgroundColour */
void setBackgroundColour(char *bgc_str)
{
// use dyeing engine to parse the colour:
std::string s(bgc_str);
Palette pal(s);
int col[3] = {255,255,255};
pal.getColor(255, col);
options.backgroundColour.r = col[0];
options.backgroundColour.g = col[1];
options.backgroundColour.b = col[2];
}
/** Parse command line options */
void parseOptions(int argc, char *argv[], Options &options)
{
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"win-width", required_argument, 0, 'W'},
{"win-height", required_argument, 0, 'H'},
{"effect-x", required_argument, 0, 'x'},
{"effect-y", required_argument, 0, 'y'},
{"quit-on-end", no_argument, 0, 'q'},
{"loop", no_argument, 0, 'l'},
{"datadir", required_argument, 0, 'd'},
{"bg-colour", required_argument, 0, 'b'},
{0, 0, 0, 0}
};
const char *optstring = "hW:H:x:y:qld:b:";
while (1) {
int result = getopt_long(argc, argv, optstring, long_options, NULL);
if (result == -1)
break;
switch (result) {
case 'h':
options.printHelpAndExit = true;
break;
case 'W':
options.winWidth = (int) atoi(optarg);
break;
case 'H':
options.winHeight = (int) atoi(optarg);
break;
case 'x':
options.effectX = (int) atoi(optarg);
break;
case 'y':
options.effectY = (int) atoi(optarg);
break;
case 'q':
options.quitOnEffectEnd = true;
if (options.loop) {
logger->log("Error: --loop shouldn't be used together with --quit-on-end");
}
break;
case 'l':
options.loop = true;
if (options.quitOnEffectEnd) {
logger->log("Error: --quit-on-end shouldn't be used together with --loop");
}
break;
case 'd':
options.dataDir = optarg;
break;
case 'b':
setBackgroundColour(optarg);
break;
default:
break;
}
}
// non-option argument: name of effect file
if (optind < argc) {
// std::cout << (argc - optind) << std::endl;
options.effectFile = argv[optind];
}
}
void printHelp()
{
std::cout << "-- particlEd --" << std::endl;
std::cout << "---------------" << std::endl;
std::cout << "TMW particle effect viewer" << std::endl;
std::cout << "based on TMW code written by TMW development team" << std::endl;
std::cout << std::endl;
std::cout << "OPTIONS:" << std::endl;
std::cout << "-h -W WIDTH -H HEIGHT -x X -y Y -q -d DIR -b COLOUR EFFECT" << std::endl;
std::cout << std::endl;
std::cout << "-h --help print this help and exit." << std::endl;
std::cout << "-W --win-width set window width (default 200)" << std::endl;
std::cout << "-h --win-height (default 200)" << std::endl;
std::cout << "-x --effect-x set x of the effect (default 100)" << std::endl;
std::cout << "-Y --effect-y (default 100)" << std::endl;
std::cout << "-q --quit-on-end Close program as soon as effect ends" << std::endl;
std::cout << "-l --loop restart effect whenever it ends" << std::endl;
std::cout << "-d --datadir set TMW data directory for PhysFS (default \".\")" << std::endl;
std::cout << "-b --bg-colour hexadecimal RGB colour (e.g. \"#ff0000\" - red)" << std::endl;
std::cout << std::endl;
std::cout << "EFFECT effect file location (relative to given DIR)" << std::endl;
}