[Add] You can now save and partially load games (mission is data is lost).

This commit is contained in:
Allanis 2013-05-15 21:53:03 +01:00
parent 2e7a2b6e18
commit 46dfa4a8c3
3 changed files with 96 additions and 2 deletions

View File

@ -4,6 +4,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <errno.h> #include <errno.h>
#endif #endif
@ -49,3 +51,46 @@ int lfile_dirMakeExist(char* path) {
return 0; return 0;
} }
// List all the files in a dir (besides . and ..).
char** lfile_readDir(int* lfiles, char* path) {
char file[PATH_MAX];
char** files;
snprintf(file, PATH_MAX, "%s%s", lfile_basePath(), path);
#ifdef LINUX
DIR* d;
struct dirent *dir;
char* name;
int mfiles;
(*lfiles) = 0;
mfiles = 100;
files = malloc(sizeof(char*)*mfiles);
d = opendir(file);
if(d == NULL) {
return NULL;
}
while((dir = readdir(d)) != NULL) {
name = dir->d_name;
if((strcmp(name, ".")==0) || (strcmp(name, "..")==0))
continue;
if((*lfiles)+1 > mfiles) {
mfiles += 100;
files = realloc(files, sizeof(char*) * mfiles);
}
files[(*lfiles)] = strdup(name);
(*lfiles)++;
}
closedir(d);
#endif
return files;
}

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
char* lfile_basePath(void); char* lfile_basePath(void);
int lfile_dirMakeExist(char* path); int lfile_dirMakeExist(char* path);
char** lfile_readDir(int* lfiles, char* path);

View File

@ -22,6 +22,7 @@ extern void menu_main_close(void);
// Static. // Static.
static int save_data(xmlTextWriterPtr writer); static int save_data(xmlTextWriterPtr writer);
static void load_menu_close(char* str); static void load_menu_close(char* str);
static void load_menu_load(char* str);
static int load_game(char* file); static int load_game(char* file);
// Save all the game data. // Save all the game data.
@ -49,6 +50,7 @@ int save_all(void) {
xmlw_start(writer); xmlw_start(writer);
xmlw_startElem(writer, "lephisto_save"); xmlw_startElem(writer, "lephisto_save");
// Save the version or something..
xmlw_startElem(writer, "version"); xmlw_startElem(writer, "version");
xmlw_elem(writer, "lephisto", "%d.%d.%d", VMAJOR, VMINOR, VREV); xmlw_elem(writer, "lephisto", "%d.%d.%d", VMAJOR, VMINOR, VREV);
xmlw_elem(writer, "data", dataname); xmlw_elem(writer, "data", dataname);
@ -82,10 +84,41 @@ int save_all(void) {
// Open the load game menu. // Open the load game menu.
void load_game_menu(void) { void load_game_menu(void) {
unsigned int wid; unsigned int wid;
char** files;
int lfiles, i, len;
// Window.
wid = window_create("Load Game", -1, -1, LOAD_WIDTH, LOAD_HEIGHT); wid = window_create("Load Game", -1, -1, LOAD_WIDTH, LOAD_HEIGHT);
// Load the saves.
files = lfile_readDir(&lfiles, "saves");
for(i = 0; i < lfiles; i++) {
len = strlen(files[i]);
// No save extension.
if((len < 6) || strcmp(&files[i][len-3], ".ls")) {
free(files[i]);
memmove(&files[i], &files[i+1], sizeof(char*) * (lfiles-i-1));
lfiles--;
i--;
}
else // Remove the extension.
files[i][len-3] = '\0';
}
window_addList(wid, 20, -50,
LOAD_WIDTH-BUTTON_WIDTH-50, LOAD_HEIGHT-90,
"lstSaves", files, lfiles, 0, NULL);
// Buttons.
window_addButton(wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, window_addButton(wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnBack", "Back", load_menu_close); "btnBack", "Back", load_menu_close);
window_addButton(wid, -20, 30 + BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnLoad", "Load", load_menu_load);
// Default action.
window_setFptr(wid, load_menu_load);
} }
static void load_menu_close(char* str) { static void load_menu_close(char* str) {
@ -94,6 +127,21 @@ static void load_menu_close(char* str) {
window_destroy(window_get("Load Game")); window_destroy(window_get("Load Game"));
} }
static void load_menu_load(char* str) {
(void)str;
char* save, path[PATH_MAX];
int wid;
wid = window_get("Load Game");
save = toolkit_getList(wid, "lstSaves");
snprintf(path, PATH_MAX, "%ssaves/%s.ls", lfile_basePath(), save);
load_game(path);
load_menu_close(NULL);
menu_main_close();
}
// Load a new game. // Load a new game.
static int load_game(char* file) { static int load_game(char* file) {
xmlNodePtr node; xmlNodePtr node;