int main(int argc, char *argv[]) { int r; char *data_file = NULL; char *save_file = NULL; int screen_width = DEFAULT_SCREEN_WIDTH; int screen_height = DEFAULT_SCREEN_HEIGHT; int fullscreen = 0; int game_map = 1; int map_generator = 0; int preserve_map_bugs = 0; int log_level = DEFAULT_LOG_LEVEL; int opt; while (1) { opt = getopt(argc, argv, "d:fg:hl:m:pr:t:"); if (opt < 0) break; switch (opt) { case 'd': { int d = atoi(optarg); if (d >= 0 && d < LOG_LEVEL_MAX) { log_level = d; } } break; case 'f': fullscreen = 1; break; case 'g': data_file = malloc(strlen(optarg)+1); if (data_file == NULL) exit(EXIT_FAILURE); strcpy(data_file, optarg); break; case 'h': fprintf(stdout, HELP, argv[0]); exit(EXIT_SUCCESS); break; case 'l': save_file = malloc(strlen(optarg)+1); if (save_file == NULL) exit(EXIT_FAILURE); strcpy(save_file, optarg); break; case 'm': game_map = atoi(optarg); break; case 'p': preserve_map_bugs = 1; break; case 'r': { char *hstr = strchr(optarg, 'x'); if (hstr == NULL) { fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); } screen_width = atoi(optarg); screen_height = atoi(hstr+1); } break; case 't': map_generator = atoi(optarg); break; default: fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); break; } } /* Set up logging */ log_set_file(stdout); log_set_level(log_level); LOGI("main", "freeserf %s", FREESERF_VERSION); r = load_data_file(data_file); if (r < 0) { LOGE("main", "Could not load game data."); exit(EXIT_FAILURE); } free(data_file); gfx_data_fixup(); LOGI("main", "SDL init..."); r = sdl_init(); if (r < 0) exit(EXIT_FAILURE); /* TODO move to right place */ midi_play_track(MIDI_TRACK_0); audio_set_volume(75); /*gfx_set_palette(DATA_PALETTE_INTRO);*/ gfx_set_palette(DATA_PALETTE_GAME); LOGI("main", "SDL resolution %ix%i...", screen_width, screen_height); r = sdl_set_resolution(screen_width, screen_height, fullscreen); if (r < 0) exit(EXIT_FAILURE); game.mission_level = game_map - 1; /* set game map */ game.map_generator = map_generator; game.map_preserve_bugs = preserve_map_bugs; /* Init globals */ allocate_global_memory(); player_interface_init(); /* Initialize global lookup tables */ init_spiral_pattern(); game_init(); /* Either load a save game if specified or start a new game. */ if (save_file != NULL) { int r = game_load_save_game(save_file); if (r < 0) exit(EXIT_FAILURE); free(save_file); } else { game_load_random_map(); } /* Move viewport to initial position */ viewport_move_to_map_pos(&interface.viewport, interface.map_cursor_pos); /* Start game loop */ game_loop(); LOGI("main", "Cleaning up..."); /* Clean up */ audio_cleanup(); sdl_deinit(); gfx_unload(); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { int r; char *data_file = NULL; char *save_file = NULL; int map_generator = 0; log_set_file(stdout); init_config_data(); //- read config data int screen_width = get_config_int(CONFIG_SCREEN_WIDTH, DEFAULT_SCREEN_WIDTH); int screen_height = get_config_int(CONFIG_SCREEN_HEIGHT, DEFAULT_SCREEN_HEIGHT); int fullscreen = get_config_bool(CONFIG_FULLSCREEN, 0); int log_level = get_config_int(CONFIG_LOGGLEVEL, DEFAULT_LOG_LEVEL); enum_lng_t language = str_to_lagEnum((char *)get_config_string(CONFIG_LANGUAGE, "EN")); int play_midi = get_config_bool(CONFIG_MUSIC, 1); int play_SFX = get_config_bool(CONFIG_SFX, 1); int volume = get_config_int(CONFIG_VOLUME, 75); //- read command line parameters int opt; while (1) { opt = getopt(argc, argv, "d:fg:hl:r:t:s:"); if (opt < 0) break; switch (opt) { case 'd': { int d = atoi(optarg); if (d >= 0 && d < LOG_LEVEL_MAX) { log_level = d; } } break; case 'f': fullscreen = 1; break; case 'g': data_file = (char *)malloc(strlen(optarg)+1); if (data_file == NULL) exit(EXIT_FAILURE); strcpy(data_file, optarg); break; case 'h': fprintf(stdout, HELP, argv[0]); exit(EXIT_SUCCESS); break; case 'l': save_file = (char *)malloc(strlen(optarg)+1); if (save_file == NULL) exit(EXIT_FAILURE); strcpy(save_file, optarg); break; case 'r': { char *hstr = strchr(optarg, 'x'); if (hstr == NULL) { fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); } screen_width = atoi(optarg); screen_height = atoi(hstr+1); } break; case 't': map_generator = atoi(optarg); break; case 's': { char * tmp_language_str = (char *) malloc(strlen(optarg) + 1); if (tmp_language_str != NULL) { strcpy(tmp_language_str, optarg); language = str_to_lagEnum(tmp_language_str); } } break; default: fprintf(stderr, USAGE, argv[0]); exit(EXIT_FAILURE); break; } } /* Set up logging */ log_set_level((log_level_t)log_level); LOGI("main", "freeserf %s", FREESERF_VERSION); /* load language */ init_language_data(language); r = load_data_file(data_file); if (r < 0) { LOGE("main", "Could not load game data."); exit(EXIT_FAILURE); } free(data_file); gfx_data_fixup(); LOGI("main", "SDL init..."); r = sdl_init(); if (r < 0) exit(EXIT_FAILURE); /* TODO move to right place */ midi_play_track(MIDI_TRACK_0); audio_set_volume(volume); midi_enable(play_midi); sfx_enable(play_SFX); /*gfx_set_palette(DATA_PALETTE_INTRO);*/ gfx_set_palette(DATA_PALETTE_GAME); LOGI("main", "SDL resolution %ix%i...", screen_width, screen_height); r = sdl_set_resolution(screen_width, screen_height, fullscreen); if (r < 0) exit(EXIT_FAILURE); game.map_generator = map_generator; /* Initialize global lookup tables */ init_spiral_pattern(); game_init(); /* Initialize Missions*/ init_mission("missions.xml"); /* Initialize interface */ interface_init(&interface); gui_object_set_size((gui_object_t *)&interface, screen_width, screen_height); gui_object_set_displayed((gui_object_t *)&interface, 1); /* Either load a save game if specified or start a new game. */ if (save_file != NULL) { int r = game_load_save_game(save_file); if (r < 0) exit(EXIT_FAILURE); free(save_file); interface_set_player(&interface, 0); } else { int r = game_load_random_map(3, &interface.random); if (r < 0) exit(EXIT_FAILURE); /* Add default player */ r = game_add_player(12, 64, 40, 40, 40); if (r < 0) exit(EXIT_FAILURE); interface_set_player(&interface, r); } viewport_map_reinit(); if (save_file != NULL) { interface_close_game_init(&interface); } /* Start game loop */ game_loop(); LOGI("main", "Cleaning up..."); /* Clean up */ map_deinit(); viewport_map_deinit(); audio_cleanup(); sdl_deinit(); gfx_unload(); language_cleanup(); mission_cleanup(); config_cleanup(); return EXIT_SUCCESS; }