/** * @brief Player death menu, appears when player got creamed. */ void menu_death (void) { unsigned int wid; char path[PATH_MAX]; wid = window_create( "Death", -1, -1, DEATH_WIDTH, DEATH_HEIGHT ); window_onClose( wid, menu_death_close ); /* Allow the player to continue if the savegame exists, if not, propose to restart */ nsnprintf(path, PATH_MAX, "%ssaves/%s.ns", nfile_dataPath(), player.name); if (!player_isTut() && nfile_fileExists(path)) window_addButtonKey( wid, 20, 20 + BUTTON_HEIGHT*2 + 20*2, BUTTON_WIDTH, BUTTON_HEIGHT, "btnContinue", _("Continue"), menu_death_continue, SDLK_c ); else window_addButtonKey( wid, 20, 20 + BUTTON_HEIGHT*2 + 20*2, BUTTON_WIDTH, BUTTON_HEIGHT, "btnRestart", _("Restart"), menu_death_restart, SDLK_r ); window_addButtonKey( wid, 20, 20 + (BUTTON_HEIGHT+20), BUTTON_WIDTH, BUTTON_HEIGHT, "btnMain", _("Main Menu"), menu_death_main, SDLK_m ); window_addButtonKey( wid, 20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, "btnExit", _("Exit Game"), menu_exit, SDLK_x ); menu_Open(MENU_DEATH); /* Makes it all look cooler since everything still goes on. */ unpause_game(); }
/** * @brief Checks to see if there's a savegame available. * * @return 1 if a savegame is available, 0 otherwise. */ int save_hasSave (void) { char **files; int nfiles, i, len; int has_save; /* Look for saved games. */ files = nfile_readDir( &nfiles, "%ssaves", nfile_dataPath() ); has_save = 0; for (i=0; i<nfiles; i++) { len = strlen(files[i]); /* no save extension */ if ((len >= 5) && (strcmp(&files[i][len-3],".ns")==0)) { has_save = 1; break; } } /* Clean up. */ for (i=0; i<nfiles; i++) free(files[i]); free(files); return has_save; }
/** * @brief Saves the current game. * * @return 0 on success. */ int save_all (void) { char file[PATH_MAX]; xmlDocPtr doc; xmlTextWriterPtr writer; /* Do not save during tutorial. Or if saving is off. */ if (player_isTut() || player_isFlag(PLAYER_NOSAVE)) return 0; /* Create the writer. */ writer = xmlNewTextWriterDoc(&doc, conf.save_compress); if (writer == NULL) { ERR("testXmlwriterDoc: Error creating the xml writer"); return -1; } /* Set the writer parameters. */ xmlw_setParams( writer ); /* Start element. */ xmlw_start(writer); xmlw_startElem(writer,"naev_save"); /* Save the version and such. */ xmlw_startElem(writer,"version"); xmlw_elem( writer, "naev", "%d.%d.%d", VMAJOR, VMINOR, VREV ); xmlw_elem( writer, "data", "%s", ndata_name() ); xmlw_endElem(writer); /* "version" */ /* Save the data. */ if (save_data(writer) < 0) { ERR("Trying to save game data"); goto err_writer; } /* Finish element. */ xmlw_endElem(writer); /* "naev_save" */ xmlw_done(writer); /* Write to file. */ if ((nfile_dirMakeExist("%s", nfile_dataPath()) < 0) || (nfile_dirMakeExist("%ssaves", nfile_dataPath()) < 0)) { WARN("Failed to create save directory '%ssaves'.", nfile_dataPath()); goto err_writer; } nsnprintf(file, PATH_MAX, "%ssaves/%s.ns", nfile_dataPath(), player.name); /* Back up old savegame. */ if (!save_loaded) { if (nfile_backupIfExists(file) < 0) { WARN("Aborting save..."); goto err_writer; } } save_loaded = 0; /* Critical section, if crashes here player's game gets corrupted. * Luckily we have a copy just in case... */ xmlFreeTextWriter(writer); if (xmlSaveFileEnc(file, doc, "UTF-8") < 0) { WARN("Failed to write savegame! You'll most likely have to restore it by copying your backup savegame over your current savegame."); goto err; } xmlFreeDoc(doc); return 0; err_writer: xmlFreeTextWriter(writer); err: xmlFreeDoc(doc); return -1; }
/** * @brief Reload the current savegame. */ void save_reload (void) { char path[PATH_MAX]; nsnprintf(path, PATH_MAX, "%ssaves/%s.ns", nfile_dataPath(), player.name); load_game( path, 0 ); }
/** * @brief The entry point of Naev. * * @param[in] argc Number of arguments. * @param[in] argv Array of argc arguments. * @return EXIT_SUCCESS on success. */ int main( int argc, char** argv ) { char buf[PATH_MAX]; /* Save the binary path. */ binary_path = strdup(argv[0]); /* Print the version */ LOG( " "APPNAME" v%s", naev_version(0) ); #ifdef GIT_COMMIT DEBUG( " git HEAD at " GIT_COMMIT ); #endif /* GIT_COMMIT */ /* Initializes SDL for possible warnings. */ SDL_Init(0); /* Initialize the threadpool */ threadpool_init(); /* Set up debug signal handlers. */ debug_sigInit(); /* Must be initialized before input_init is called. */ if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { WARN("Unable to initialize SDL Video: %s", SDL_GetError()); return -1; } /* Get desktop dimensions. */ #if SDL_VERSION_ATLEAST(1,2,10) const SDL_VideoInfo *vidinfo = SDL_GetVideoInfo(); gl_screen.desktop_w = vidinfo->current_w; gl_screen.desktop_h = vidinfo->current_h; #else /* #elif SDL_VERSION_ATLEAST(1,2,10) */ gl_screen.desktop_w = 0; gl_screen.desktop_h = 0; #endif /* #elif SDL_VERSION_ATLEAST(1,2,10) */ /* We'll be parsing XML. */ LIBXML_TEST_VERSION xmlInitParser(); /* Input must be initialized for config to work. */ input_init(); conf_setDefaults(); /* set the default config values */ /* * Attempts to load the data path from datapath.lua * At this early point in the load process, the binary path * is the only place likely to be checked. */ conf_loadConfigPath(); /* Parse the user data path override first. */ conf_parseCLIPath( argc, argv ); /* Create the home directory if needed. */ if (nfile_dirMakeExist("%s", nfile_configPath())) WARN("Unable to create config directory '%s'", nfile_configPath()); /* Set the configuration. */ nsnprintf(buf, PATH_MAX, "%s"CONF_FILE, nfile_configPath()); #if HAS_UNIX /* TODO get rid of this cruft ASAP. */ int oldconfig = 0; if (!nfile_fileExists( buf )) { char *home, buf2[PATH_MAX]; home = SDL_getenv( "HOME" ); if (home != NULL) { nsnprintf( buf2, PATH_MAX, "%s/.naev/"CONF_FILE, home ); if (nfile_fileExists( buf2 )) oldconfig = 1; } } #endif /* HAS_UNIX */ conf_loadConfig(buf); /* Lua to parse the configuration file */ conf_parseCLI( argc, argv ); /* parse CLI arguments */ /* Enable FPU exceptions. */ #if defined(HAVE_FEENABLEEXCEPT) && defined(DEBUGGING) if (conf.fpu_except) feenableexcept( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW ); #endif /* defined(HAVE_FEENABLEEXCEPT) && defined(DEBUGGING) */ /* Open data. */ if (ndata_open() != 0) ERR("Failed to open ndata."); /* Load the start info. */ if (start_load()) ERR("Failed to load module start data."); /* Load the data basics. */ LOG(" %s", ndata_name()); DEBUG(); /* Display the SDL Version. */ print_SDLversion(); DEBUG(); /* random numbers */ rng_init(); /* * OpenGL */ if (gl_init()) { /* initializes video output */ ERR("Initializing video output failed, exiting..."); SDL_Quit(); exit(EXIT_FAILURE); } window_caption(); gl_fontInit( NULL, NULL, conf.font_size_def ); /* initializes default font to size */ gl_fontInit( &gl_smallFont, NULL, conf.font_size_small ); /* small font */ /* Display the load screen. */ loadscreen_load(); loadscreen_render( 0., "Initializing subsystems..." ); time_ms = SDL_GetTicks(); /* * Input */ if ((conf.joystick_ind >= 0) || (conf.joystick_nam != NULL)) { if (joystick_init()) WARN("Error initializing joystick input"); if (conf.joystick_nam != NULL) { /* use the joystick name to find a joystick */ if (joystick_use(joystick_get(conf.joystick_nam))) { WARN("Failure to open any joystick, falling back to default keybinds"); input_setDefault(); } free(conf.joystick_nam); } else if (conf.joystick_ind >= 0) /* use a joystick id instead */ if (joystick_use(conf.joystick_ind)) { WARN("Failure to open any joystick, falling back to default keybinds"); input_setDefault(); } } /* * OpenAL - Sound */ if (conf.nosound) { LOG("Sound is disabled!"); sound_disabled = 1; music_disabled = 1; } if (sound_init()) WARN("Problem setting up sound!"); music_choose("load"); /* FPS stuff. */ fps_setPos( 15., (double)(gl_screen.h-15-gl_defFont.h) ); /* Misc graphics init */ if (nebu_init() != 0) { /* Initializes the nebula */ /* An error has happened */ ERR("Unable to initialize the Nebula subsystem!"); /* Weirdness will occur... */ } gui_init(); /* initializes the GUI graphics */ toolkit_init(); /* initializes the toolkit */ map_init(); /* initializes the map. */ cond_init(); /* Initialize conditional subsystem. */ cli_init(); /* Initialize console. */ /* Data loading */ load_all(); /* Generate the CSV. */ if (conf.devcsv) dev_csv(); /* Unload load screen. */ loadscreen_unload(); /* Start menu. */ menu_main(); /* Force a minimum delay with loading screen */ if ((SDL_GetTicks() - time_ms) < NAEV_INIT_DELAY) SDL_Delay( NAEV_INIT_DELAY - (SDL_GetTicks() - time_ms) ); fps_init(); /* initializes the time_ms */ #if HAS_UNIX /* Tell the player to migrate their configuration files out of ~/.naev */ /* TODO get rid of this cruft ASAP. */ if ((oldconfig) && (!conf.datapath)) { char path[PATH_MAX], *script, *home; uint32_t scriptsize; int ret; nsnprintf( path, PATH_MAX, "%s/naev-confupdate.sh", ndata_getDirname() ); home = SDL_getenv("HOME"); ret = dialogue_YesNo( "Warning", "Your configuration files are in a deprecated location and must be migrated:\n" " \er%s/.naev/\e0\n\n" "The update script can likely be found in your Naev data directory:\n" " \er%s\e0\n\n" "Would you like to run it automatically?", home, path ); /* Try to run the script. */ if (ret) { ret = -1; /* Running from ndata. */ if (ndata_getPath() != NULL) { script = ndata_read( "naev-confupdate.sh", &scriptsize ); if (script != NULL) ret = system(script); } /* Running from laid-out files or ndata_read failed. */ if ((nfile_fileExists(path)) && (ret == -1)) { script = nfile_readFile( (int*)&scriptsize, path ); if (script != NULL) ret = system(script); } /* We couldn't find the script. */ if (ret == -1) { dialogue_alert( "The update script was not found at:\n\er%s\e0\n\n" "Please locate and run it manually.", path ); } /* Restart, as the script succeeded. */ else if (!ret) { dialogue_msg( "Update Completed", "Configuration files were successfully migrated. Naev will now restart." ); execv(argv[0], argv); } else { /* I sincerely hope this else is never hit. */ dialogue_alert( "The update script encountered an error. Please exit Naev and move your config and save files manually:\n\n" "\er%s/%s\e0 =>\n \eD%s\e0\n\n" "\er%s/%s\e0 =>\n \eD%s\e0\n\n" "\er%s/%s\e0 =>\n \eD%snebula/\e0\n\n", home, ".naev/conf.lua", nfile_configPath(), home, ".naev/{saves,screenshots}/", nfile_dataPath(), home, ".naev/gen/*.png", nfile_cachePath() ); } } else {
/** * @brief Loads or refreshes saved games. */ int load_refresh (void) { char **files, buf[PATH_MAX], *tmp; int nfiles, i, len; int ok; nsave_t *ns; if (load_saves != NULL) load_free(); load_saves = array_create( nsave_t ); /* load the saves */ files = nfile_readDir( &nfiles, "%ssaves", nfile_dataPath() ); for (i=0; i<nfiles; i++) { len = strlen(files[i]); /* no save or backup save extension */ if (((len < 5) || strcmp(&files[i][len-3],".ns")) && ((len < 12) || strcmp(&files[i][len-10],".ns.backup"))) { free(files[i]); memmove( &files[i], &files[i+1], sizeof(char*) * (nfiles-i-1) ); nfiles--; i--; } } /* Make sure files are none. */ if (files == NULL) return 0; /* Make sure backups are after saves. */ for (i=0; i<nfiles-1; i++) { len = strlen( files[i] ); /* Only interested in swapping backup with file after it if it's not backup. */ if ((len < 12) || strcmp( &files[i][len-10],".ns.backup" )) continue; /* Don't match. */ if (strncmp( files[i], files[i+1], (len-10) )) continue; /* Swap around. */ tmp = files[i]; files[i] = files[i+1]; files[i+1] = tmp; } /* Allocate and parse. */ ok = 0; ns = NULL; for (i=0; i<nfiles; i++) { if (!ok) ns = &array_grow( &load_saves ); nsnprintf( buf, sizeof(buf), "%ssaves/%s", nfile_dataPath(), files[i] ); ok = load_load( ns, buf ); } /* If the save was invalid, array is 1 member too large. */ if (ok) array_resize( &load_saves, array_size(load_saves)-1 ); /* Clean up memory. */ for (i=0; i<nfiles; i++) free(files[i]); free(files); return 0; }