-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdgmain.cpp
549 lines (495 loc) · 14.8 KB
/
cdgmain.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// cdgmain.cpp
// Isaac Brodsky
// 2012 AUGUST 6
#include <cstring>
#include "stdafx.h"
#include "cdg.h"
#include "cdgmain.h"
#include "cdgui.h"
#include "cdgrenderer.h"
#include <FL/Fl.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/fl_ask.H>
#include <sys/stat.h>
#include "gen.h"
#ifdef WIN32
// Only available in POSIX
#define strcasecmp _stricmp
#endif
//Defines
#define DEFAULT_WIDTH CDG_WIDTH
#define DEFAULT_HEIGHT CDG_HEIGHT
#define DEFAULT_BPP 24
const char *DEFAULT_CAPTION = "CD+G Deck";
const char *COPYRIGHT_MESSAGE = "CD+G Deck v1.0.4\n"
"Copyright 2012 Isaac Brodsky. All rights reserved.\n"
"This program is provided with no warranty.\n"
"\n"
"Uses SDL under the zlib license,\n\tCopyright (C) 1997-2009 Sam Lantinga\n"
"Uses SDL_ttf under the zlib license,\n\tCopyright (C) 1997-2009 Sam Lantinga\n"
"Uses SDL_mixer under the zlib license,\n\tCopyright (C) 1997-2012 Sam Lantinga\n"
"Uses libpng,\n\tlibpng zlib 1.5.13 - September 27, 2012\n"
"\tCopyright (c) 1998-2012 Glenn Randers-Pehrson\n"
"\tCopyright (c) 1996-1997 Andreas Dilger\n"
"\tCopyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n"
"Uses zlib,\n\tCopyright (C) 1995-2012 Jean-loup Gailly and Mark Adler\n"
"Uses FLTK under the LGPL license,\n\tCopyright 1998-2010 by Bill Spitzak and others.\n"
"Uses Bitstream Vera,\n"
"\tCopyright (c) 2003 by Bitstream, Inc. All Rights Reserved.\n"
"\tBitstream Vera is a trademark of Bitstream, Inc.\n"
"\n";
//Prototypes
//these functions shouldn't be used outside of cdgmain.cpp
void process_save_cdg(const CDG & cdg, bool maskBorder);
void process_input(MainRunState *run, const MainConfig *config, CDGUI *ui);
void process_render(SDL_Renderer *renderer, int &dirty, const CDG &cdg, MainRunState *run, const MainConfig *config, CDGUI *ui);
SDL_Window *graphics_init(MainConfig*);
void process_args(int argc, char* argv[], MainConfig*);
//Functions
#if (WIN32 && NDEBUG)
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
//insane hack workaround provided by Microsoft (through msvcrt, I believe)
//to retrieve properly parsed command line arguments.
//These variables are automagically populated at runtime.
//http://support.microsoft.com/kb/126571
char **argv = __argv;
int argc = __argc;
//TODO
//Probably would be a good idea to convert cerr << errs to use a function where
//I can have an #if and pop up a messagebox on (WIN32 && NDEBUG)
#else
int main( int argc, char* argv[] ) {
#endif
CDGUI *ui;
try
{
ui = new CDGUI();
}
catch (exception &e)
{
cout << e.what() << endl;
return 1000;
}
MainConfig config;
MainRunState run;
CDG *cdg = new CDG;
int dirty = 0;
SDL_Window* screen;
SDL_Renderer* renderer;
string caption = DEFAULT_CAPTION;
cout << DEFAULT_CAPTION << endl
<< "Copyright 2012 Isaac Brodsky" << endl
<< "Press F1 or use -help to see full copyright message." << endl << endl;
process_args(argc, argv, &config);
screen = graphics_init(&config);
if (screen == NULL) {
return 1;
}
renderer = SDL_CreateRenderer(screen, -1, 0);
// TODO: Replace SDL_mixer with something supporting seek
if (Mix_Init(MIX_INIT_FLAC | MIX_INIT_MP3 | MIX_INIT_OGG) == 0) {
// TODO: Fails on no OGG
cerr << "audio open " << Mix_GetError() << endl;
// config.audioEnabled = false;
} else {
atexit(Mix_Quit);
}
if (config.hasAutoFile) {
run.cdgFile = config.autoFile;
run.cdgLoaded = true;
if (config.hasAutoAudioFile) {
run.audioFile = config.autoAudioFile;
run.audioLoaded = true;
}
stat(run.cdgFile.c_str(), &run.cdgStat);
run.cdgLoc = 0;
if (config.autostart)
run.nextTransport = T_PLAY;
}/* else {
open_filechooser(&run);
run.nextTransport = T_PLAY;
}*/
cout << "\n";
uint32_t ticktime = SDL_GetTicks(), tickdeltatime, starttime, elapsedtime, loopbegintime, adjustmenttime = 0;
//Keep track of the current frame
uint32_t frame = 0, ticksnow, tickstotal = 0;
while (run.keeprunning) {
starttime = SDL_GetTicks();
process_input(&run, &config, ui);
if (run.cdgLoaded) {
switch (run.nextTransport) {
case T_PLAY:
if (run.transport == T_STOP) {
loopbegintime = SDL_GetTicks();
adjustmenttime = 0;
frame = 0;
tickstotal = 0;
stat(run.cdgFile.c_str(), &run.cdgStat);
run.cdgLoc = 0;
run.cdgIn.open(run.cdgFile, ios::binary);
delete cdg;
cdg = new CDG;
//Set the window caption
caption = DEFAULT_CAPTION;
caption.append(" - ");
caption.append(run.cdgFile);
SDL_SetWindowTitle(screen, caption.c_str());
dirty = 1;
if (config.audioEnabled && run.audioLoaded) {
if (run.audio != NULL) {
Mix_FreeMusic(run.audio);
}
run.audio = Mix_LoadMUS(run.audioFile.c_str());
if (!run.audio) {
cerr << "start load mix: " << Mix_GetError() << endl;
} else {
if (Mix_PlayMusic(run.audio, 0) == -1) {
cerr << "start mix: " << Mix_GetError() << endl;
}
}
}
} else {
if (config.audioEnabled && run.audioLoaded) {
Mix_ResumeMusic();
}
}
break;
case T_PAUSE:
if (config.audioEnabled && run.audioLoaded) {
Mix_PauseMusic();
}
break;
case T_STOP:
run.cdgIn.close();
run.cdgLoc = 0;
if (config.audioEnabled && Mix_PlayingMusic()) {
Mix_HaltMusic();
Mix_FreeMusic(run.audio);
run.audio = NULL;
}
break;
case T_NOCHANGE:
// Do nothing.
break;
}
if (run.nextTransport != T_NOCHANGE)
run.transport = run.nextTransport;
if (run.transport == T_PLAY && run.nextLoc != -1) {
//keep playing music through this (as opposed to pausing and resuming
//when ready... although that sounds completely reasonable too. :/
if (config.audioEnabled && run.audioLoaded) {
Mix_RewindMusic();
Mix_PauseMusic();
}
run.cdgLoc = CDG::percentToSecond(run.nextLoc, run.cdgStat.st_size);
cdg->seekTo(run.cdgIn, run.cdgLoc, config.seek);
dirty = 1;
if (config.audioEnabled && run.audioLoaded) {
Mix_SetMusicPosition(run.cdgLoc / BYTES_PER_SECOND);
Mix_ResumeMusic();
}
run.nextLoc = -1;
}
if (run.dumpImage)
{
process_save_cdg(*cdg, !config.showBorder);
run.dumpImage = false;
}
if (run.nextChannel != run.channel) {
cdg->setChannel(run.nextChannel);
run.channel = run.nextChannel;
}
}
run.nextTransport = T_NOCHANGE;
tickdeltatime = SDL_GetTicks() - ticktime;
//75 sectors per second
//96 (24 * 4) subchannel data per sector
//actually it's 98 bytes per subcode block
//But the first two bytes are replaced by the sync pattern (so
//P through W cannot be used then)
if (run.transport == T_PLAY) {
ticksnow = int(75.0 * 4.0 * ((double)tickdeltatime / 1000.0));
cdg->execCount(run.cdgIn, ticksnow, dirty);
run.cdgLoc += (24 * ticksnow);
tickstotal += ticksnow;
while (tickstotal < (((SDL_GetTicks() - loopbegintime - adjustmenttime) / 1000) * 300)) {
cdg->execCount(run.cdgIn, 1, dirty);
run.cdgLoc += 24;
tickstotal++;
}
} else if (run.transport == T_PAUSE) {
adjustmenttime += tickdeltatime;
}
ticktime = SDL_GetTicks();
process_render(renderer, dirty, *cdg, &run, &config, ui);
elapsedtime = SDL_GetTicks() - starttime;
if (elapsedtime < 1) //stupid
elapsedtime = 1;
frame++;
if(elapsedtime < 1000 / config.desiredFps) {
//Sleep the remaining frame time
SDL_Delay((1000 / config.desiredFps) - elapsedtime);
} else {
SDL_Delay(1);
}
if (run.transport == T_PLAY) {
if (run.cdgIn.eof()) {
run.nextTransport = T_STOP;
if (config.quitOnEof)
run.keeprunning = false;
}
}
}
run.cdgIn.close();
if (config.audioEnabled && run.audioLoaded) {
Mix_HaltMusic();
if (run.audio != NULL) {
Mix_FreeMusic(run.audio);
run.audio = NULL;
}
}
delete cdg;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(screen);
cout << "\nBye." << endl;
return 0;
}
void process_input(MainRunState *run, const MainConfig *config, CDGUI *ui) {
//If there's an event to handle
if(SDL_PollEvent(&(run->event))) {
switch (run->event.type) {
case SDL_QUIT:
cout << "Recieved TERMINATE signal.\n";
run->keeprunning = 0;
break;
case SDL_KEYDOWN:
if (config->useKeys) {
switch(run->event.key.keysym.sym) {
case SDLK_q:
case SDLK_ESCAPE: //quit
run->keeprunning = 0; break;
case SDLK_SPACE:
case SDLK_c:
run->nextTransport = (run->transport == T_PLAY) ? T_PAUSE : T_PLAY;
break;
case SDLK_r:
run->nextTransport = T_STOP;
/*in.close();
in.open(config.file, ios::binary);
cdg_init(&cdg, screen->format);*/
break;
case SDLK_F1:
fl_message("%s", COPYRIGHT_MESSAGE);
break;
case SDLK_s:
run->dumpImage = true;
break;
}
}
break;
case SDL_MOUSEBUTTONDOWN:
//If the left mouse button was pressed
switch (run->event.button.button) {
case SDL_BUTTON_LEFT:
if (config->useUI)
ui->processMouseDown(run, config);
break;
}
break;
case SDL_MOUSEBUTTONUP:
//If the left mouse button was pressed
switch (run->event.button.button) {
case SDL_BUTTON_LEFT:
if (config->useUI)
ui->processMouseUp(run, config);
break;
}
break;
}
}
}
void process_render(SDL_Renderer *renderer, int &dirty, const CDG &cdg, MainRunState *run, const MainConfig *config, CDGUI *ui) {
if (run->cdgLoaded) {
if (dirty) {
cdg_render(cdg, renderer, !config->showBorder, config->scaling);
dirty = 0;
}
} else {
render_test_pattern(renderer, config->w, config->h);
}
if (config->useUI)
ui->render(renderer, run, config);
SDL_RenderPresent(renderer);
}
SDL_Window *graphics_init(MainConfig *config) {
//Initialize all SDL subsystems
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
cerr << "FATAL> SDL failed to init\n";
cerr << SDL_GetError();
return NULL;
}
atexit(SDL_Quit);
//Set up the screen
int w = DEFAULT_WIDTH * config->scaling;
int h = (DEFAULT_HEIGHT * config->scaling) +
((config->useUI) ? UI_HEIGHT : 0);
if (config->woverride > 0)
w = config->woverride;
if (config->hoverride > 0)
h = config->hoverride;
SDL_Window *screen = SDL_CreateWindow(DEFAULT_CAPTION,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
w, h,
SDL_WINDOW_OPENGL | (config->fullscreen ? SDL_WINDOW_FULLSCREEN : 0));
config->h = h;
config->w = w;
//If there was an error in setting up the screen
if(screen == NULL) {
cerr << "FATAL> screen did not set up properly\n";
cerr << SDL_GetError();
return NULL;
}
return screen;
}
int open_filechooser(MainRunState *run, bool music) {
//from FLTK demo app
Fl_Native_File_Chooser native;
native.title("CD+G Deck - Pick a file");
native.type(Fl_Native_File_Chooser::BROWSE_FILE);
if (music)
native.filter("All Music Files\t*\n");
else
native.filter("CD+G Graphics (.cdg)\t*.cdg\nAll Files\t*\n");
//native.preset_file(G_filename->value());
// Show native chooser
switch (native.show()) {
case -1: cerr << "ERROR: " << native.errmsg() << endl; exit(1); break; // ERROR
case 1: /*exit(0);*/ break; // CANCEL
default: // PICKED FILE
if (native.filename()) {
if (music) {
run->audioFile = native.filename();
run->audioLoaded = true;
} else {
run->cdgFile = native.filename();
run->cdgLoaded = true;
run->audioLoaded = false;
stat(run->cdgFile.c_str(), &run->cdgStat);
run->cdgLoc = 0;
}
} else {
//null
}
return 1;
}
return 0;
}
void process_args(int argc, char* argv[], MainConfig *config) {
for (int i = 1; i < argc; i++) {
if (strcasecmp(argv[i], "-fps") == 0) {
i++;
if (i < argc) {
config->desiredFps = atoi(argv[i]);
}
} else if (strcasecmp(argv[i], "-help") == 0) {
cout << "usage: " << argv[0] << " [-help] [-quitonend] [-noaudio] [-noui] [-nokeys] [-autostart] [-seek (direct|enhanced)] [-scaling #] [-w #] [-h #] [-fullscreen|-windowed] [-audio file] [-] [file]" << endl;
cout << COPYRIGHT_MESSAGE;
exit(0);
} else if (strcasecmp(argv[i], "-quitonend") == 0) {
config->quitOnEof = true;
} else if (strcasecmp(argv[i], "-noui") == 0) {
config->useUI = false;
} else if (strcasecmp(argv[i], "-nokeys") == 0) {
config->useKeys = false;
} else if (strcasecmp(argv[i], "-noaudio") == 0) {
config->audioEnabled = false;
} else if (strcasecmp(argv[i], "-autostart") == 0) {
config->autostart = true;
} else if (strcasecmp(argv[i], "-seek") == 0) {
i++;
if (i < argc) {
if (strcasecmp(argv[i], "direct") == 0)
config->seek = SeekMode::SEEK_DIRECT;
else if (strcasecmp(argv[i], "enhanced") == 0)
config->seek = SeekMode::SEEK_ENHANCED;
else
cerr << "Bad value for -seek ... use 'direct' or 'enhanced'" << endl;
} else {
cerr << "No value for -seek ... use 'direct' or 'enhanced'" << endl;
}
} else if (strcasecmp(argv[i], "-scaling") == 0) {
i++;
if (i < argc) {
config->scaling = atoi(argv[i]);
} else {
cerr << "No value for -scaling" << endl;
}
} else if (strcasecmp(argv[i], "-fullscreen") == 0) {
config->fullscreen = true;
} else if (strcasecmp(argv[i], "-windowed") == 0) {
config->fullscreen = false;
} else if (strcasecmp(argv[i], "-w") == 0) {
i++;
if (i < argc) {
config->woverride = atoi(argv[i]);
} else {
cerr << "No value for -w" << endl;
}
} else if (strcasecmp(argv[i], "-h") == 0) {
i++;
if (i < argc) {
config->hoverride = atoi(argv[i]);
} else {
cerr << "No value for -h" << endl;
}
} else if (strcasecmp(argv[i], "-audio") == 0) {
i++;
if (i < argc) {
config->autoAudioFile = argv[i];
config->hasAutoAudioFile = true;
} else {
cerr << "No value for -audio" << endl;
}
} else if (strcasecmp(argv[i], "-showborder") == 0) {
config->showBorder = true;
} else if (strcasecmp(argv[i], "-gen") == 0) {
exit(genmain());
} else if (strcasecmp(argv[i], "-") == 0) {
string s = "";
for (int j = i + 1; j < argc; j++) {
if (j != i + 1)
s.append(" ");
s.append(argv[j]);
}
config->autoFile = s;
config->hasAutoFile = true;
return;
} else if (argv[i][0] == '-') {
cerr << "Unknown option \"" << argv[i] << "\"" << endl;
} else {
config->autoFile = argv[i];
config->hasAutoFile = true;
}
}
}
void process_save_cdg(const CDG & cdg, bool maskBorder)
{
Fl_Native_File_Chooser native;
native.title("CD+G Deck - Save screen capture");
native.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
native.filter("PNG Image (.png)\t*.png\n");
//native.preset_file(G_filename->value());
// Show native chooser
switch (native.show()) {
case -1: cerr << "ERROR: " << native.errmsg() << endl; return; // ERROR
case 1: return; // CANCEL
default: // PICKED FILE
if (native.filename()) {
if (!save_cdg_screen(cdg, maskBorder, native.filename()))
{
fl_message("Failed to save image.");
}
}
}
}