diff --git a/src/input.c b/src/input.c index 8cd19e1..df81998 100644 --- a/src/input.c +++ b/src/input.c @@ -246,8 +246,8 @@ static void input_key(int keynum, double value, int abs) { else if(KEY("pause") && NOHYP()) { if(value == KEY_PRESS) { if(!toolkit) { - if(paused) unpause(); - } else pause(); + if(paused) unpause_game(); + } else pause_game(); } } // Opens a menu. @@ -336,8 +336,8 @@ void input_handle(SDL_Event* event) { if(event->type == SDL_ACTIVEEVENT) { if(event->active.state != SDL_APPMOUSEFOCUS) { // We don't need mouse focus. - if((event->active.gain == 0) && !paused) pause(); - else if((event->active.gain == 1) && pause) unpause(); + if((event->active.gain == 0) && !paused) pause_game(); + else if((event->active.gain == 1) && paused) unpause_game(); return; } } diff --git a/src/lephisto.c b/src/lephisto.c index 5afc26b..ff3947d 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -1,6 +1,13 @@ #include <SDL.h> #include <string.h> +#ifdef LINUX +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <errno.h> +#endif + #include "lephisto.h" #include "conf.h" #include "log.h" @@ -74,6 +81,8 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, #else int main(int argc, char** argv) { #endif + + char* home, dir[PATH_MAX]; // Print the version. snprintf(version, VERSION_LEN, "%d.%d.%d", VMAJOR, VMINOR, VREV); @@ -85,6 +94,21 @@ int main(int argc, char** argv) { // Input must be initialized for config to work. input_init(); + // Create the home directory if needed. +#ifdef LINUX + struct stat buf; + + home = getenv("HOME"); + snprintf(dir, PATH_MAX, "%s/.lephisto", home); + stat(dir, &buf); + if(!S_ISDIR(buf.st_mode)) { + if(mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO) < 0) + WARN("Unable to create lephisto directory '%s'", dir); + else + DEBUG("Created lephisto directory '%s'", dir); + } +#endif + // Set the configuration. conf_setDefaults(); // Default config values. conf_loadConfig(CONF_FILE); // Have Lua parse config. diff --git a/src/menu.c b/src/menu.c index 152a336..58fe034 100644 --- a/src/menu.c +++ b/src/menu.c @@ -4,7 +4,6 @@ #include "toolkit.h" #include "log.h" #include "lephisto.h" -#include "pause.h" #include "pilot.h" #include "space.h" #include "player.h" @@ -104,7 +103,7 @@ static void menu_main_load(char* str) { (void)str; menu_main_close(); - load_game("text.xml"); + load_game("test.xml"); } static void menu_main_new(char* str) { @@ -121,7 +120,6 @@ void menu_small(void) { menu_isOpen(MENU_SMALL) || menu_isOpen(MENU_DEATH)) return; // It's already open.. - pause(); unsigned int wid; @@ -145,7 +143,6 @@ static void menu_small_close(char* str) { if(strcmp(str, "btnResume")==0) window_destroy(window_get("Menu")); - unpause(); menu_Close(MENU_SMALL); } @@ -164,7 +161,6 @@ static void exit_game(void) { // Info menu. void menu_info(void) { if(menu_isOpen(MENU_INFO)) return; - pause(); char str[128]; char* lt; @@ -222,7 +218,6 @@ static void menu_info_close(char* str) { window_destroy(window_get("Info")); menu_Close(MENU_INFO); - unpause(); } static void info_outfits_menu(char* str) { diff --git a/src/pause.c b/src/pause.c index 8928efe..0a9d518 100644 --- a/src/pause.c +++ b/src/pause.c @@ -21,7 +21,7 @@ static void pilots_unpause(void); static void pilots_delay(unsigned int delay); // Pause the game. -void pause(void) { +void pause_game(void) { if(paused) return; // Well well.. We are paused already. pilots_pause(); @@ -32,7 +32,7 @@ void pause(void) { paused = 1; // We should unpause it. } -void unpause(void) { +void unpause_game(void) { if(!paused) return; // We are unpaused already. pilots_unpause(); diff --git a/src/pause.h b/src/pause.h index 10b9a7f..b39d9f0 100644 --- a/src/pause.h +++ b/src/pause.h @@ -2,8 +2,8 @@ extern int paused; -void pause(void); -void unpause(void); +void pause_game(void); +void unpause_game(void); void pause_delay(unsigned int delay); diff --git a/src/pilot.c b/src/pilot.c index f1a14f9..80ecb58 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -872,7 +872,7 @@ Pilot* pilot_createEmpty(Ship* ship, char* name, int faction, AI_Profile* ai, const int flags) { Pilot* dyn; - dyn = MALLOC_L(Pilot*); + dyn = MALLOC_L(Pilot); pilot_init(dyn, ship, name, faction, ai, 0., NULL, NULL, flags | PILOT_EMPTY); return dyn; } diff --git a/src/player.c b/src/player.c index 3e661cf..296283a 100644 --- a/src/player.c +++ b/src/player.c @@ -320,14 +320,14 @@ void player_cleanup(void) { int i; // Cleanup name. - if(player_name) free(player_name); + if(player_name != NULL) free(player_name); // Cleanup messages. for(i = 0; i < msg_max; i++) memset(msg_stack[i].str, '\0', MSG_SIZE_MAX); // Clean up the stack. - if(player_stack) { + if(player_stack != NULL) { for(i = 0; i < player_nstack; i++) { pilot_free(player_stack[i]); free(player_lstack[i]); @@ -339,7 +339,7 @@ void player_cleanup(void) { // Nothing left. player_nstack = 0; } - if(missions_done) { + if(missions_done != NULL) { free(missions_done); missions_done = NULL; missions_ndone = 0; @@ -1595,7 +1595,7 @@ static int player_parseShip(xmlNodePtr parent, int is_player) { xmlr_attr(parent, "model", model); // Player is currently on this ship. - if(is_player) { + if(is_player != 0) { pilot_create(ship_get(model), name, faction_get("Player"), NULL, 0., NULL, NULL, PILOT_PLAYER | PILOT_NO_OUTFITS); ship = player; @@ -1609,7 +1609,7 @@ static int player_parseShip(xmlNodePtr parent, int is_player) { node = parent->xmlChildrenNode; do { - if(!is_player) xmlr_str(node, "location", loc); + if(!is_player == 0) xmlr_str(node, "location", loc); if(xml_isNode(node, "outfits")) { cur = node->xmlChildrenNode; @@ -1645,9 +1645,9 @@ static int player_parseShip(xmlNodePtr parent, int is_player) { } while(xml_nextNode(node)); // Add it to the stack if it's not what the player is in. - if(!is_player) { + if(is_player == 0) { player_stack = realloc(player_stack, sizeof(Pilot*)*(player_nstack+1)); - player_stack[player_nstack] = pilot_copy(player); + player_stack[player_nstack] = ship; player_lstack = realloc(player_lstack, sizeof(char*)*(player_nstack+1)); player_lstack[player_nstack] = strdup(loc); player_nstack++; diff --git a/src/save.c b/src/save.c index fb75cbc..bb6219a 100644 --- a/src/save.c +++ b/src/save.c @@ -1,6 +1,15 @@ +#include <stdlib.h> +#ifdef LINUX +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <errno.h> +#endif + #include "lephisto.h" #include "log.h" #include "xml.h" +#include "player.h" #include "save.h" // Externs. @@ -22,7 +31,7 @@ static int save_data(xmlTextWriterPtr writer) { // Save the current game. int save_all(void) { - char* file; + char file[PATH_MAX], *home; xmlDocPtr doc; xmlTextWriterPtr writer; @@ -45,7 +54,21 @@ int save_all(void) { xmlw_endElem(writer); // lephisto_save. xmlw_done(writer); - file = "test.xml"; +#ifdef LINUX + struct stat buf; + home = getenv("HOME"); + snprintf(file, PATH_MAX, "%s/.lephisto/saves", home); + stat(file, &buf); + if(!S_ISDIR(buf.st_mode)) + if(mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { + WARN("Unable to create '%s' for saving: %s", file, strerror(errno)); + WARN("Aborting save..."); + xmlFreeTextWriter(writer); + xmlFreeDoc(doc); + return -1; + } + snprintf(file, PATH_MAX, "%s/.lephisto/saves/%s.ls", home, player_name); +#endif xmlFreeTextWriter(writer); xmlSaveFileEnc(file, doc, "UTF-8"); diff --git a/src/toolkit.c b/src/toolkit.c index c5d8f0f..57b8d01 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -495,6 +495,7 @@ unsigned int window_create(char* name, const int x, const int y, // Toolkit is enabled. SDL_ShowCursor(SDL_ENABLE); toolkit = 1; // Enable it. + pause_game(); } return wid; @@ -558,7 +559,7 @@ void window_destroy(const unsigned int wid) { // No windows left. SDL_ShowCursor(SDL_DISABLE); toolkit = 0; // Disable the toolkit. - if(paused) unpause(); + if(paused) unpause_game(); } }