-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
612 lines (481 loc) · 15.1 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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#ifdef WII
#include <fat.h>
#endif
#ifdef PSP
#include <pspkernel.h>
#include <psppower.h>
#endif
#ifdef ANDROID
#include <jni.h>
#include <android/log.h>
#include <SDL_screenkeyboard.h>
#endif
#ifdef OSX
#include <mach-o/dyld.h>
#endif
#ifdef DREAMCAST
#include <kos.h>
#endif
#include <iostream>
#include <SDL/SDL.h> //Include da SDL
#include <SDL/SDL_image.h> //Include da biblioteca SDL_Image
#include <SDL/SDL_mixer.h> // Include da biblioteca SDL_Mixer
#include <SDL/SDL_ttf.h> // Include da biblioteca SDL_ttf
#include <SDL/SDL_endian.h>
#include <SDL/SDL_mixer.h>
// ************** CODE AND DEBUG flags ************** //
//#define DEBUG_SHOW_FPS 1
//#define PS2LOADFROMFIXEDPOINT 1
//#define DISABLESOUND 1
#define PS2LINK 1
#define DEBUG_OUTPUT 1 // will output all DEBUG_COUT messages, comments this out to disable all console output messages
// GLOBAL/EXTERN VARIABLES
std::string FILEPATH;
std::string SAVEPATH;
SDL_Event event;
bool have_save = false;
#include "defines.h"
#include "graphicslib.h"
#include "graphic/draw.h"
#include "inputlib.h"
#include "timerlib.h"
#include "soundlib.h"
#include "game.h"
#include "scenes/ending.h"
#if defined(LINUX) || defined(OSX)
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/param.h>
#elif defined(PLAYSTATION2) || defined(DINGUX) || defined(PSP) || defined(ANDROID) || defined(OPEN_PANDORA) || defined(WII)
#include <unistd.h>
#define MAXPATHLEN 256
#elif defined(WIN32)
#include <direct.h>
#undef main // to build on win32
#endif
#if defined(DINGUX)
std::string EXEC_NAME("rockbot.dge");
#elif defined(WIN32)
std::string EXEC_NAME("rockbot.exe");
#elif defined(PLAYSTATION2)
std::string EXEC_NAME("rockbot.elf");
#elif defined(PSP)
std::string EXEC_NAME("EBOOT.PBP");
#elif defined(ANDROID)
std::string EXEC_NAME("");
#else
std::string EXEC_NAME("rockbot");
#endif
// INTERNAL GLOBALS
timerLib timer;
inputLib input;
soundLib soundManager;
graphicsLib graphLib;
draw draw_lib;
game gameControl;
CURRENT_FILE_FORMAT::st_save game_save;
struct CURRENT_FILE_FORMAT::st_game_config game_config;
bool GAME_FLAGS[FLAG_COUNT];
int default_keys_codes[BTN_COUNT]; // number indicator for the keyboard-keys
int default_button_codes[BTN_COUNT]; // number indicator for the keyboard-keys
#include "file/file_io.h"
CURRENT_FILE_FORMAT::file_io fio;
CURRENT_FILE_FORMAT::file_game game_data;
CURRENT_FILE_FORMAT::file_stage stage_data;
#include "defines.h"
FREEZE_EFFECT_TYPES freeze_weapon_effect = FREEZE_EFFECT_NONE;
int freeze_weapon_id = -1;
struct format_v_2_0_1::st_checkpoint checkpoint;
#ifdef PLAYSTATION2
#include "ports/ps2/modules.h"
void PS2_copy_to_memorycard(std::string file_in, std::string file_out)
{
FILE *fp_read = fopen(file_in.c_str(), "rb");
if (!fp_read) {
std::cout << "ERROR: Could not open read file '" << file_in << "'" << std::endl;
graphLib.show_debug_msg("ERROR #1");
graphLib.show_debug_msg(file_in);
return;
} else {
std::cout << "WARNING: Opened file '" << file_in << "'" << std::endl;
}
file_out = SAVEPATH + "/" + file_out;
FILE *fp_write = fopen(file_out.c_str(), "wb");
if (!fp_write) {
std::cout << "ERROR: Could not open write file '" << file_out << "'" << std::endl;
graphLib.show_debug_msg("ERROR #2");
graphLib.show_debug_msg(file_out);
return;
} else {
std::cout << "WARNING: Opened file '" << file_out << "'" << std::endl;
}
int a;
while(1)
{
a = fgetc(fp_read);
if (!feof(fp_read)) {
fputc(a, fp_write);
} else {
break;
}
}
fclose(fp_read);
fclose(fp_write);
}
void PS2_create_save_icons()
{
// copy save icon files, if they do not exist
std::string filename = SAVEPATH + "/icon.sys";
FILE *fp = fopen(filename.c_str(), "rb");
if (fp == NULL) {
std::string filename = FILEPATH + "data/images/icon.sys";
PS2_copy_to_memorycard(filename, "icon.sys");
filename = FILEPATH + "data/images/rockbot_ps2_icon.icn";
PS2_copy_to_memorycard(filename, "rockbot_ps2_icon.icn");
} else {
fclose(fp);
}
}
#endif
#ifdef PSP
PSP_MODULE_INFO("Rockbot", PSP_MODULE_USER, 1, 0);
//PSP_HEAP_SIZE_KB(-1024);
PSP_HEAP_SIZE_MAX();
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
#endif
void get_filepath()
{
#ifdef WIN32
char* buffer;
if( (buffer = _getcwd( NULL, 0 )) != NULL ) {
FILEPATH = std::string(buffer);
FILEPATH += "/";
}
delete[] buffer;
#elif defined OSX
char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0) {
FILEPATH = std::string(path);
FILEPATH = FILEPATH.substr(0, FILEPATH.length()-7);
}
#else
char *buffer = new char[MAXPATHLEN];
getcwd(buffer, MAXPATHLEN);
if(buffer != NULL){
FILEPATH = std::string(buffer);
}
FILEPATH += "/";
delete[] buffer;
#endif
/*
#ifdef ANDROID
FILEPATH = "/sdcard/Android/data/net.upperland.rockbot/files/";
#elif WII
FILEPATH = "sd:/apps/Rockbot/";
printf("MAIN #D\n");
#elif defined(PLAYSTATION2) && defined(PS2LOADFROMFIXEDPOINT) // DEBUG
FILEPATH = "mass:/PS2/Rockbot/";
#endif
*/
#if defined(PLAYSTATION2) && defined(PS2LOADFROMFIXEDPOINT) // DEBUG
FILEPATH = "mass:/PS2/Rockbot/";
#elif WII
FILEPATH = "sd:/apps/Rockbot/";
printf("MAIN #D\n");
#endif
std::cout << "get_filepath - FILEPATH:" << FILEPATH << std::endl;
#ifdef DREAMCAST
FILEPATH = "/cd/";
#endif
}
/*
void execute_memory_test() {
#ifdef PSP
psp_ram _ram_counter;
#endif
int stage_number = 1;
int map_id = 0;
int npc_id = 0;
int object_id = 0;
fio.read_stage(stage_data, stage_number);
#ifdef PSP
std::cout << "MEMTEST[NPC]::BEFORE='" << _ram_counter.ramAvailable() << "'" << std::endl;
#endif
classnpc* new_npc = new classnpc(stage_number, map_id, stage_data.maps[map_id].map_npcs[npc_id].id_npc, npc_id);
new_npc->clean_character_graphics_list();
delete new_npc;
#ifdef PSP
std::cout << "MEMTEST[NPC]::AFTER='" << _ram_counter.ramAvailable() << "'" << std::endl;
#endif
#ifdef PSP
std::cout << "MEMTEST[OBJECT]::BEFORE='" << _ram_counter.ramAvailable() << "'" << std::endl;
#endif
object* new_obj = new object(object_id, NULL, st_position(0, 0));
new_obj->remove_graphic();
delete new_obj;
#ifdef PSP
std::cout << "MEMTEST[OBJECT]::AFTER='" << _ram_counter.ramAvailable() << "'" << std::endl;
#endif
#ifdef PSP
std::cout << "MEMTEST[MAP]::BEFORE='" << _ram_counter.ramAvailable() << "'" << std::endl;
#endif
classMap* new_map = new classMap();
new_map->setMapNumber(map_id);
new_map->setStageNumber(stage_number);
new_map->loadMap();
delete new_map;
#ifdef PSP
std::cout << "MEMTEST[MAP]::AFTER='" << _ram_counter.ramAvailable() << "'" << std::endl;
#endif
std::fflush(stdout);
input.waitTime(200);
std::fflush(stdout);
std::cout << "MEMTEST[END]" << std::endl;
input.waitTime(200);
}
*/
#ifdef WII
extern "C" int main(int argc, char *argv[])
#else
#ifdef DREAMCAST
KOS_INIT_FLAGS(INIT_DEFAULT);
#endif
int main(int argc, char *argv[])
#endif
{
#ifdef PSP
SetupCallbacks();
//scePowerSetClockFrequency(333, 333, 166);
#endif
for (int i=0; i<FLAG_COUNT; i++) {
GAME_FLAGS[i] = false;
}
UNUSED(argc);
string argvString = "";
#ifndef WII
argvString = string(argv[0]);
#else
if (!fatInitDefault()) {
printf("fatInitDefault ERROR #1");
std::fflush(stdout);
timer.delay(500);
exit(-1);
}
#endif
get_filepath();
// fallback in case getcwd returns null
if (FILEPATH.size() == 0) {
std::cout << "Could not read path, fallback to using argv" << std::endl;
FILEPATH = argvString.substr(0, argvString.size()-EXEC_NAME.size());
}
std::cout << "main - argvString: '" << argvString << "', FILEPATH: '" << FILEPATH << "'" << std::endl; std::fflush(stdout);
#ifdef PLAYSTATION2
std::cout << "PS2.DEBUG #1" << std::endl; std::fflush(stdout);
#ifndef PS2LINK
SifIopReset(NULL, 0); // clean previous loading of irx by apps like ulaunchElf. Comment this line to get cout on ps2link
#endif
printf("DEBUG.PS2 #1.1\n");
/* SP193: Being creative (Do something while waiting for the slow IOP to be reset). =D */
int main_id = GetThreadId();
ChangeThreadPriority(main_id, 72);
std::cout << "PS2.DEBUG #1.1" << std::endl; std::fflush(stdout);
printf("DEBUG.PS2 #1.2\n");
#ifndef PS2LINK
while(SifIopSync()) {
std::cout << "PS2.SifIopSync()" << std::endl;
}
#endif
/* Initialize and connect to all SIF services on the IOP. */
SifInitRpc(0);
SifInitIopHeap();
SifLoadFileInit();
fioInit();
printf("DEBUG.PS2 #1.3\n");
/* Apply the SBV LMB patch to allow modules to be loaded from a buffer in EE RAM. */
sbv_patch_enable_lmb();
// --- DEBUG --- //
//FILEPATH = "cdfs:/";
// --- DEBUG --- //
std::cout << "PS2.DEBUG #2" << std::endl; std::fflush(stdout);
if (FILEPATH.find("mass:") != std::string::npos) {
printf("DEBUG.PS2 #1.4\n");
std::cout << "PS2.DEBUG Load USB" << std::endl; std::fflush(stdout);
PS2_load_USB();
}
if (FILEPATH.find("cdfs") != std::string::npos || FILEPATH.find("cdrom") != std::string::npos) {
printf("DEBUG.PS2 #1.5\n");
std::cout << "PS2.DEBUG Load CDROM" << std::endl; std::fflush(stdout);
FILEPATH = "cdfs:";
PS2_load_CDROM();
}
printf("DEBUG.PS2 #2\n");
std::cout << "PS2.DEBUG #3" << std::endl; std::fflush(stdout);
#endif
// check command-line paramethers
if (argc > 1) {
for (int i=1; i<argc; i++) {
std::string temp_argv(argv[i]);
if (temp_argv == "--fullscreen") {
} else if (temp_argv == "--quickload") {
GAME_FLAGS[FLAG_QUICKLOAD] = true;
} else if (temp_argv == "--invencible") { // player have infinite HP
GAME_FLAGS[FLAG_INVENCIBLE] = true;
} else if (temp_argv == "--allweapons") { // player have all weapons available even if
GAME_FLAGS[FLAG_ALLWEAPONS] = true;
} else if (temp_argv == "--infinitejump") { // player can jump again and again
GAME_FLAGS[FLAG_INFINITE_JUMP] = true;
} else if (temp_argv == "--rockbot") {
GAME_FLAGS[FLAG_PLAYER_ROCKBOT] = true;
} else if (temp_argv == "--betabot") {
GAME_FLAGS[FLAG_PLAYER_BETABOT] = true;
} else if (temp_argv == "--candybot") {
GAME_FLAGS[FLAG_PLAYER_CANDYBOT] = true;
} else if (temp_argv == "--kittybot") {
GAME_FLAGS[FLAG_PLAYER_KITTYBOT] = true;
}
}
}
/// DEBUG ///
//GAME_FLAGS[FLAG_QUICKLOAD] = true;
// PS2 version have to load config AFTER SDL_Init due to SDK issues
#ifdef LINUX
SAVEPATH = std::string(getenv("HOME")) + "/.rockbot/";
mkdir(SAVEPATH.c_str(), 0777);
//std::cout << "SAVEPATH: " << SAVEPATH << ", mkdir-res: " << res << ", errno: " << errno << std::endl;
#elif WIN32
SAVEPATH = std::string(getenv("APPDATA")) + "/rockbot";
std::cout << "SAVEPATH: " << SAVEPATH << std::endl;
_mkdir(SAVEPATH.c_str());
#elif DREAMCAST
SAVEPATH = "/vmu/a1";
#else
SAVEPATH = FILEPATH;
#endif
std::cout << "SAVEPATH: " << SAVEPATH << std::endl;
fio.check_conversion();
fio.read_game(game_data);
//GAME_FLAGS[FLAG_INFINITE_HP] = true; // DEBUG
gameControl.get_drop_item_ids();
soundManager.init_audio_system();
#ifndef PLAYSTATION2
fio.load_config(game_config);
#endif
// INIT GRAPHICS
if (graphLib.initGraphics() != true) {
printf("ERROR intializing graphic\n");
return -1;
}
// define SAVEPATH
#ifdef PLAYSTATION2
PS2_load_MC();
SAVEPATH = "mc0:Rockbot/";
if (fioMkdir(SAVEPATH.c_str()) < 0) {
std::cout << "main - warning: could not create '" << SAVEPATH << "' folder" << std::endl; std::fflush(stdout);
/// @TODO - check if directory exists
} else {
std::cout << "Folder '" << SAVEPATH << "' created" << std::endl; std::fflush(stdout);
}
#endif
have_save = fio.save_exists();
#ifndef DEBUG_OUTPUT // redirect output to null
std::string cout_file = "/dev/null";
std::ofstream out(cout_file.c_str());
std::cout.rdbuf(out.rdbuf());
#else
// --- REDIRECT STDOUT TO A FILE --- //
#if defined(PSP) || defined(WII) || defined(ANDROID) || defined(DINGUX) || defined(PLAYSTATION2)
//std::string cout_file = SAVEPATH + "/stdout.txt";
std::string cout_file = FILEPATH + "/stdout.txt";
std::streambuf *coutbuf = std::cout.rdbuf();
std::ofstream out(cout_file.c_str());
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
#endif
#endif
graphLib.preload();
#ifdef PLAYSTATION2
fio.load_config(game_config);
PS2_create_save_icons();
#endif
draw_lib.preload();
gameControl.currentStage = INTRO_STAGE;
// INIT GAME
if (GAME_FLAGS[FLAG_QUICKLOAD] == false) {
if (gameControl.showIntro() == false) {
std::cout << "ERROR SHOWING INTRO" << std::endl;
return 0;
}
} else {
gameControl.quick_load_game();
//ending end_obj;
//end_obj.start();
//return 1;
}
input.clean();
input.p1_input[BTN_START] = 0;
input.waitTime(200);
input.clean();
bool run_game = true;
while (run_game) {
#if !defined(DINGUX)
timer.start_ticker();
#endif
#ifdef PLAYSTATION2
RotateThreadReadyQueue(_MIXER_THREAD_PRIORITY);
#endif
gameControl.showGame();
#ifdef DEBUG_SHOW_FPS
gameControl.fps_count();
#endif
draw_lib.update_screen();
if (input.p1_input[BTN_QUIT] == 1) {
std::fflush(stdout);
gameControl.leave_game();
}
unsigned int now_ticks = timer.get_ticks();
if (now_ticks < (1000 / FRAMES_PER_SECOND)) {
timer.delay((1000 / FRAMES_PER_SECOND) - now_ticks);
}
}
/// @TODO: sdl quit sub-systems
#ifdef PSP
sceKernelExitGame();
return 0;
#else
SDL_Quit();
#endif
return 1;
}
#ifdef ANDROID
extern "C" {
JNIEXPORT void JNICALL Java_net_upperland_rockbot_DemoRenderer_nativeInit(JNIEnv * env, jobject obj);
};
JNIEXPORT void JNICALL Java_net_upperland_rockbot_DemoRenderer_nativeInit(JNIEnv * env, jobject obj)
{
char * argv[1];
argv[0] = "./";
main(1, argv);
}
#endif