Path: blob/main/games/alex4/files/patch-main.c
16461 views
--- main.c.orig 2019-02-04 06:12:31 UTC1+++ main.c2@@ -38,6 +38,7 @@3#include "main.h"4#include "edit.h"5#include "shooter.h"6+#include "unix.h"78#include "../data/data.h"910@@ -66,7 +67,6 @@ PALETTE org_pal;11Tscroller hscroll;12Thisc *hisc_table;13Thisc *hisc_table_space;14-char working_directory[1024];1516// the map17Tmap *map = NULL;18@@ -126,6 +126,7 @@ int menu_choice = 1;19int playing_original_game = 1;20int init_ok = 0;2122+static FILE* log_fp = NULL;232425// // // // // // // // // // // // // // // // // // // // //26@@ -154,20 +155,18 @@ char *get_init_string() {27// loggs the text to the text file28void log2file(char *format, ...) {29va_list ptr; /* get an arg pointer */30- FILE *fp;3132- fp = fopen("log.txt", "at");33- if (fp) {34+ if (log_fp) {35/* initialize ptr to point to the first argument after the format string */36va_start(ptr, format);3738/* Write to logfile. */39- vfprintf(fp, format, ptr); // Write passed text.40- fprintf(fp, "\n"); // New line..41+ vfprintf(log_fp, format, ptr); // Write passed text.42+ fprintf(log_fp, "\n"); // New line..4344va_end(ptr);4546- fclose(fp);47+ fflush(log_fp);48}4950}51@@ -618,6 +617,10 @@ int init_game(const char *map_file) {52BITMAP *bmp;53int i;54int w, h;55+#ifdef __unix__56+ char filename[512];57+ char *homedir = get_homedir();58+#endif5960log2file("\nInit routines:");6162@@ -625,7 +628,13 @@ int init_game(const char *map_file) {63log2file(" initializing allegro");64text_mode(-1);65garble_string(init_string, 53);66+#ifdef __unix__67+ snprintf(filename, sizeof(filename), "%s/.alex4/alex4.ini",68+ homedir? homedir:".");69+ override_config_file(filename);70+#else71set_config_file("alex4.ini");72+#endif73set_window_close_button(FALSE);7475// install timers76@@ -695,6 +704,7 @@ int init_game(const char *map_file) {77textout_centre(swap_screen, font, "loading...", 320, 200, 1);78blit_to_screen(swap_screen);7980+#ifndef __unix__81// set switch modes and callbacks82if (set_display_switch_mode(SWITCH_PAUSE) < 0)83log2file(" * display switch mode failed");84@@ -702,6 +712,7 @@ int init_game(const char *map_file) {85log2file(" * display switch in failed");86if (set_display_switch_callback(SWITCH_OUT, display_switch_out) < 0)87log2file(" * display switch out failed");88+#endif899091// set win title (no! really???)92@@ -718,7 +729,7 @@ int init_game(const char *map_file) {93// load data94log2file(" loading data");95packfile_password(init_string);96- data = load_datafile("data/data.dat");97+ data = load_datafile(DATADIR "data.dat");98packfile_password(NULL);99if (data == NULL) {100log2file(" *** failed");101@@ -728,7 +739,13 @@ int init_game(const char *map_file) {102103// load options104log2file(" loading options");105+#ifdef __unix__106+ snprintf(filename, sizeof(filename), "%s/.alex4/alex4.sav",107+ homedir? homedir:".");108+ pf = pack_fopen(filename, "rp");109+#else110pf = pack_fopen("alex4.sav", "rp");111+#endif112if (pf) {113load_options(&options, pf);114pack_fclose(pf);115@@ -740,7 +757,13 @@ int init_game(const char *map_file) {116117// loading highscores118log2file(" loading hiscores");119+#ifdef __unix__120+ snprintf(filename, sizeof(filename), "%s/.alex4/alex4.hi",121+ homedir? homedir:".");122+ pf = pack_fopen(filename, "rp");123+#else124pf = pack_fopen("alex4.hi", "rp");125+#endif126if (pf) {127load_hisc_table(hisc_table, pf);128load_hisc_table(hisc_table_space, pf);129@@ -776,7 +799,7 @@ int init_game(const char *map_file) {130log2file(" loading original maps");131packfile_password(init_string);132num_levels = -1; // skip end object when counting133- maps = load_datafile_callback("data/maps.dat", count_maps_callback);134+ maps = load_datafile_callback(DATADIR "maps.dat", count_maps_callback);135packfile_password(NULL);136if (maps == NULL) {137log2file(" *** failed");138@@ -835,11 +858,12 @@ int init_game(const char *map_file) {139// install sound140log2file(" installing sound");141set_volume_per_voice(0);142- switch(get_config_int("sound", "sound_device", 0)) {143+ switch(get_config_int("sound", "sound_device", 1)) {144case 1:145i = DIGI_AUTODETECT;146log2file(" DIGI_AUTODETECT selected (%d)", i);147break;148+#ifdef ALLEGRO_WINDOWS149case 2:150i = DIGI_DIRECTX(0);151log2file(" DIGI_DIRECTX(0) selected (%d)", i);152@@ -848,6 +872,20 @@ int init_game(const char *map_file) {153i = DIGI_DIRECTAMX(0);154log2file(" DIGI_DIRECTAMX(0) selected (%d)", i);155break;156+#elif defined ALLEGRO_UNIX157+#ifdef DIGI_OSS158+ case 2:159+ i = DIGI_OSS;160+ log2file(" DIGI_OSS selected (%d)", i);161+ break;162+#endif163+#ifdef DIGI_ALSA164+ case 3:165+ i = DIGI_ALSA;166+ log2file(" DIGI_ALSA selected (%d)", i);167+ break;168+#endif169+#endif170default:171i = -770405; // dummy number172got_sound = 0;173@@ -870,9 +908,9 @@ int init_game(const char *map_file) {174if (get_config_int("sound", "use_sound_datafile", 1)) {175log2file(" loading sound datafile");176packfile_password(init_string);177- sfx_data = load_datafile("data/sfx_44.dat");178+ sfx_data = load_datafile(DATADIR "sfx_44.dat");179if (sfx_data == NULL) {180- sfx_data = load_datafile("data/sfx_22.dat");181+ sfx_data = load_datafile(DATADIR "sfx_22.dat");182log2file(" sfx_44.dat not found");183s = 0;184}185@@ -971,6 +1009,10 @@ int init_game(const char *map_file) {186void uninit_game() {187int i;188PACKFILE *pf;189+#ifdef __unix__190+ char filename[512];191+ char *homedir = get_homedir();192+#endif193194log2file("\nExit routines:");195196@@ -989,14 +1031,26 @@ void uninit_game() {197// only save if everything was inited ok!198if (init_ok) {199log2file(" saving options");200+#ifdef __unix__201+ snprintf(filename, sizeof(filename), "%s/.alex4/alex4.sav",202+ homedir? homedir:".");203+ pf = pack_fopen(filename, "wp");204+#else205pf = pack_fopen("alex4.sav", "wp");206+#endif207if (pf) {208save_options(&options, pf);209pack_fclose(pf);210}211212log2file(" saving highscores");213+#ifdef __unix__214+ snprintf(filename, sizeof(filename), "%s/.alex4/alex4.hi",215+ homedir? homedir:".");216+ pf = pack_fopen(filename, "wp");217+#else218pf = pack_fopen("alex4.hi", "wp");219+#endif220if (pf) {221save_hisc_table(hisc_table, pf);222save_hisc_table(hisc_table_space, pf);223@@ -1158,9 +1212,9 @@ void draw_custom_ending(BITMAP *bmp) {224225blit(data[INTRO_BG].dat, bmp, 0, 0, 0, 0, 160, 120);226227- r = 70 + fixtoi(20 * fcos(itofix(game_count >> 1)) + 20 * fsin(itofix((int)(game_count / 2.7))) );228+ r = 70 + fixtoi(20 * fixcos(itofix(game_count >> 1)) + 20 * fixsin(itofix((int)(game_count / 2.7))) );229for(i = 0; i < 256; i += 32)230- draw_sprite(bmp, head, 80 - head->w/2 + fixtoi(r * fcos(itofix(game_count + i))), 60 - head->h/2 + fixtoi(r * fsin(itofix(game_count + i))));231+ draw_sprite(bmp, head, 80 - head->w/2 + fixtoi(r * fixcos(itofix(game_count + i))), 60 - head->h/2 + fixtoi(r * fixsin(itofix(game_count + i))));232233draw_sprite_h_flip(bmp, data[ALEX].dat, 60, 40);234draw_sprite(bmp, data[LOLA].dat, 84, 40);235@@ -1289,7 +1343,7 @@ void show_cutscene(int level) {236// poll music machine237if (got_sound) al_poll_duh(dp);238239- if (mode == 1 && (keypressed() || is_fire(&ctrl) || is_jump(&ctrl) ) || my_counter > 200) {240+ if (((mode == 1) && (keypressed() || is_fire(&ctrl) || is_jump(&ctrl))) || (my_counter > 200)) {241mode = 2;242}243244@@ -1343,7 +1397,7 @@ void show_scores(int space, Thisc *table) {245if (space) {246// get space bg247packfile_password(init_string);248- df = load_datafile_object("data/a45.dat", "BG1");249+ df = load_datafile_object(DATADIR "a45.dat", "BG1");250packfile_password(NULL);251if (df != NULL) {252bg = df->dat;253@@ -2149,7 +2203,7 @@ void check_alex_with_enemies() {254255// calculates camera pos for map m considering player p256void calculate_camera_pos(Tplayer *p, Tmap *m) {257- static camera_type = 1;258+ static int camera_type = 1;259260if (p->actor->status == AC_BALL) {261camera_type = 2;262@@ -2485,7 +2539,7 @@ void draw_title(BITMAP *bmp, int tick) {263textout(bmp, data[THE_FONT].dat, "QUIT", x+1, y+1, 1);264textout(bmp, data[THE_FONT].dat, "QUIT", x, y, 4);265266- draw_sprite(bmp, data[POINTER].dat, x - 25 + fixtoi(3 * fcos(itofix(tick << 2))), 44 + menu_choice * step);267+ draw_sprite(bmp, data[POINTER].dat, x - 25 + fixtoi(3 * fixcos(itofix(tick << 2))), 44 + menu_choice * step);268}269270271@@ -2841,6 +2895,10 @@ int do_main_menu() {272}273else {274PACKFILE *pf;275+#ifdef __unix__276+ char filename[512];277+ char *homedir = get_homedir();278+#endif279log2file(" level complete");280if (got_sound) stop_music();281if (level < MAX_LEVELS && playing_original_game) {282@@ -2875,7 +2933,14 @@ int do_main_menu() {283284// save options285log2file(" saving options");286+#ifdef __unix__287+ snprintf(filename, sizeof(filename),288+ "%s/.alex4/alex4.sav",289+ homedir? homedir:".");290+ pf = pack_fopen(filename, "wp");291+#else292pf = pack_fopen("alex4.sav", "wp");293+#endif294if (pf) {295save_options(&options, pf);296pack_fclose(pf);297@@ -2969,24 +3034,36 @@ int do_main_menu() {298299// main300int main(int argc, char **argv) {301- FILE *fp;302int i;303char full_path[1024];304+#ifndef __unix__305+ char working_directory[1024];306+#else307+ char *homedir = get_homedir();308+#endif309310// init allegro311allegro_init();312313+#ifdef __unix__314+ // start logfile315+ snprintf(full_path, sizeof(full_path), "%s/.alex4",316+ homedir? homedir:".");317+ check_and_create_dir(full_path);318+ snprintf(full_path, sizeof(full_path), "%s/.alex4/log.txt",319+ homedir? homedir:".");320+ log_fp = fopen(full_path, "wt");321+#else322// get working directory323get_executable_name(full_path, 1024);324replace_filename(working_directory, full_path, "", 1024);325chdir(working_directory);326327-328// start logfile329- fp = fopen("log.txt", "wt");330- if (fp) {331- fprintf(fp, "Alex 4 (%s) - log file\n-------------------\n", GAME_VERSION_STR);332- fclose(fp);333+ log_fp = fopen("log.txt", "wt");334+#endif335+ if (log_fp) {336+ fprintf(log_fp, "Alex 4 (%s) - log file\n-------------------\n", GAME_VERSION_STR);337}338339// log program arguments340@@ -2994,7 +3071,9 @@ int main(int argc, char **argv) {341for(i = 0; i < argc; i ++) {342log2file(" %s", argv[i]);343}344+#ifndef __unix__345log2file("Working directory is:\n %s", working_directory);346+#endif347348// test wether to play real game349// or custom levels350@@ -3022,6 +3101,8 @@ int main(int argc, char **argv) {351uninit_game();352allegro_exit();353log2file("\nDone...\n");354+ if (log_fp)355+ fclose(log_fp);356357return 0;358} END_OF_MAIN();359360361