Merge branch 'paths' into develop

This commit is contained in:
Steve 2018-04-22 09:11:25 +01:00
commit c2b2b86710
19 changed files with 85 additions and 33 deletions

View File

@ -56,7 +56,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_DESCRIPTION_LENGTH 512 #define MAX_DESCRIPTION_LENGTH 512
#define MAX_LINE_LENGTH 1024 #define MAX_LINE_LENGTH 1024
#define MAX_FILENAME_LENGTH 1024 #define MAX_FILENAME_LENGTH 1024
#define MAX_PATH_LENGTH 4096
#define NUM_TEXTURE_BUCKETS 32 #define NUM_TEXTURE_BUCKETS 32
#define NUM_ATLAS_BUCKETS 64 #define NUM_ATLAS_BUCKETS 64

View File

@ -275,7 +275,7 @@ static void loadMetaInfo(void)
void loadGame(int slot) void loadGame(int slot)
{ {
cJSON *root, *node, *statsJSON; cJSON *root, *node, *statsJSON;
char *text, filename[MAX_PATH_LENGTH], *statName; char *text, *filename, *statName;
int i; int i;
Tuple *t; Tuple *t;
Trophy *trophy; Trophy *trophy;
@ -286,7 +286,7 @@ void loadGame(int slot)
game.saveSlot = slot; game.saveSlot = slot;
sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot); filename = buildFormattedString("%s/%d/game.json", app.saveDir, game.saveSlot);
if (fileExists(filename)) if (fileExists(filename))
{ {
@ -341,17 +341,19 @@ void loadGame(int slot)
free(text); free(text);
} }
free(filename);
} }
void saveGame(void) void saveGame(void)
{ {
cJSON *root, *statsJSON, *keysJSON, *keyJSON, *missionsJSON, *missionJSON, *trophiesJSON, *trophyJSON; cJSON *root, *statsJSON, *keysJSON, *keyJSON, *missionsJSON, *missionJSON, *trophiesJSON, *trophyJSON;
char filename[MAX_PATH_LENGTH], *out; char *filename, *out;
Tuple *t; Tuple *t;
Trophy *trophy; Trophy *trophy;
int i; int i;
sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot); filename = buildFormattedString("%s/%d/game.json", app.saveDir, game.saveSlot);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving game to '%s' ...", filename); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving game to '%s' ...", filename);
@ -409,6 +411,8 @@ void saveGame(void)
cJSON_Delete(root); cJSON_Delete(root);
free(out); free(out);
free(filename);
} }
/* /*
@ -418,10 +422,10 @@ void saveGame(void)
void restoreGameState(void) void restoreGameState(void)
{ {
cJSON *root, *node, *statsJSON; cJSON *root, *node, *statsJSON;
char *text, filename[MAX_PATH_LENGTH]; char *text, *filename;
int i; int i;
sprintf(filename, "%s/%d/game.json", app.saveDir, game.saveSlot); filename = buildFormattedString("%s/%d/game.json", app.saveDir, game.saveSlot);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Restoring game from '%s' ...", filename); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Restoring game from '%s' ...", filename);
@ -453,6 +457,8 @@ void restoreGameState(void)
cJSON_Delete(root); cJSON_Delete(root);
free(text); free(text);
free(filename);
} }
char *getSaveWidgetLabel(char *filename) char *getSaveWidgetLabel(char *filename)
@ -498,9 +504,9 @@ char *getSaveWidgetLabel(char *filename)
void deleteSaveSlot(int slot) void deleteSaveSlot(int slot)
{ {
int i, numFiles; int i, numFiles;
char path[MAX_PATH_LENGTH], **filenames; char *path, **filenames;
sprintf(path, "%s/%d", app.saveDir, slot); path = buildFormattedString("%s/%d", app.saveDir, slot);
filenames = getFileList(path, &numFiles); filenames = getFileList(path, &numFiles);
@ -518,6 +524,8 @@ void deleteSaveSlot(int slot)
} }
free(filenames); free(filenames);
free(path);
} }
static int sortItems(const void *a, const void *b) static int sortItems(const void *a, const void *b)

View File

@ -33,6 +33,7 @@ extern int lookup(char *name);
extern char *readFile(const char *filename); extern char *readFile(const char *filename);
extern char *timeToString(int seconds, int showHours); extern char *timeToString(int seconds, int showHours);
extern int writeFile(const char *filename, const char *data); extern int writeFile(const char *filename, const char *data);
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;
extern Entity *self; extern Entity *self;

View File

@ -164,7 +164,7 @@ static void draw(void)
static int getRecentSave(void) static int getRecentSave(void)
{ {
char filename[MAX_PATH_LENGTH]; char *filename;
int i, slot, curModTime, modTime; int i, slot, curModTime, modTime;
slot = -1; slot = -1;
@ -172,7 +172,7 @@ static int getRecentSave(void)
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{ {
sprintf(filename, "%s/%d/game.json", app.saveDir, i); filename = buildFormattedString("%s/%d/game.json", app.saveDir, i);
if (fileExists(filename)) if (fileExists(filename))
{ {
@ -184,6 +184,8 @@ static int getRecentSave(void)
slot = i; slot = i;
} }
} }
free(filename);
} }
return slot; return slot;
@ -192,7 +194,7 @@ static int getRecentSave(void)
static void populateSaveSlotWidgets(void) static void populateSaveSlotWidgets(void)
{ {
int i; int i;
char name[MAX_NAME_LENGTH], filename[MAX_PATH_LENGTH]; char name[MAX_NAME_LENGTH], *filename;
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{ {
@ -200,7 +202,7 @@ static void populateSaveSlotWidgets(void)
save[i] = getWidget(name, "saveSlot"); save[i] = getWidget(name, "saveSlot");
sprintf(filename, "%s/%d/game.json", app.saveDir, i); filename = buildFormattedString("%s/%d/game.json", app.saveDir, i);
if (fileExists(filename)) if (fileExists(filename))
{ {
@ -216,6 +218,8 @@ static void populateSaveSlotWidgets(void)
save[i]->value[1] = i; save[i]->value[1] = i;
save[i]->action = &doSaveSlot; save[i]->action = &doSaveSlot;
free(filename);
} }
} }

View File

@ -54,6 +54,7 @@ extern void setSelectedWidget(char *name, char *group);
extern void showWidgetGroup(char *group); extern void showWidgetGroup(char *group);
extern void startSectionTransition(void); extern void startSectionTransition(void);
extern void stopMusic(void); extern void stopMusic(void);
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;
extern Colors colors; extern Colors colors;

View File

@ -24,8 +24,7 @@ static void mkpath(const char *path);
void createSaveFolder(void) void createSaveFolder(void)
{ {
char *userHome; char *userHome, *dir;
char dir[MAX_PATH_LENGTH];
int i; int i;
userHome = getenv("HOME"); userHome = getenv("HOME");
@ -40,12 +39,14 @@ void createSaveFolder(void)
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{ {
sprintf(dir, "%s/.local/share/blobwarsAttrition/%d", userHome, i); dir = buildFormattedString("%s/.local/share/blobwarsAttrition/%d", userHome, i);
mkpath(dir); mkpath(dir);
free(dir);
} }
sprintf(app.saveDir, "%s/.local/share/blobwarsAttrition", userHome); app.saveDir = buildFormattedString("%s/.local/share/blobwarsAttrition", userHome);
} }
static void mkpath(const char *path) static void mkpath(const char *path)

View File

@ -25,5 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../../common.h" #include "../../common.h"
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;
extern Dev dev; extern Dev dev;

View File

@ -24,8 +24,7 @@ static void mkpath(const char *path);
void createSaveFolder(void) void createSaveFolder(void)
{ {
char *userHome; char *userHome, *dir;
char dir[MAX_PATH_LENGTH];
int i; int i;
userHome = getenv("USERPROFILE"); userHome = getenv("USERPROFILE");
@ -40,12 +39,14 @@ void createSaveFolder(void)
for (i = 0 ; i < MAX_SAVE_SLOTS ; i++) for (i = 0 ; i < MAX_SAVE_SLOTS ; i++)
{ {
sprintf(dir, "%s\\blobwarsAttrition\\%d", userHome, i); dir = buildFormattedString("%s\\blobwarsAttrition\\%d", userHome, i);
mkpath(dir); mkpath(dir);
free(dir);
} }
sprintf(app.saveDir, "%s\\blobwarsAttrition", userHome); app.saveDir = buildFormattedString("%s\\blobwarsAttrition", userHome);
} }
void mkpath(const char *path) void mkpath(const char *path)

View File

@ -24,5 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../../common.h" #include "../../common.h"
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;
extern Dev dev; extern Dev dev;

View File

@ -337,7 +337,7 @@ typedef struct {
} Config; } Config;
typedef struct { typedef struct {
char saveDir[MAX_FILENAME_LENGTH]; char *saveDir;
float scaleX; float scaleX;
float scaleY; float scaleY;
Mouse mouse; Mouse mouse;

View File

@ -261,20 +261,22 @@ static void initColor(SDL_Color *c, int r, int g, int b)
void saveScreenshot(char *name) void saveScreenshot(char *name)
{ {
char filename[MAX_PATH_LENGTH]; char *filename;
SDL_Surface *screenshot; SDL_Surface *screenshot;
if (name != NULL) if (name != NULL)
{ {
sprintf(filename, "%s/%d/%s.png", app.saveDir, game.saveSlot, name); filename = buildFormattedString("%s/%d/%s.png", app.saveDir, game.saveSlot, name);
} }
else else
{ {
sprintf(filename, "%s/%d.png", dev.screenshotFolder, SDL_GetTicks()); filename = buildFormattedString("%s/%d.png", dev.screenshotFolder, SDL_GetTicks());
} }
screenshot = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); screenshot = SDL_CreateRGBSurface(0, SCREEN_WIDTH, SCREEN_HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
SDL_RenderReadPixels(app.renderer, NULL, SDL_PIXELFORMAT_ARGB8888, screenshot->pixels, screenshot->pitch); SDL_RenderReadPixels(app.renderer, NULL, SDL_PIXELFORMAT_ARGB8888, screenshot->pixels, screenshot->pitch);
SDL_SavePNG(screenshot, filename); SDL_SavePNG(screenshot, filename);
SDL_FreeSurface(screenshot); SDL_FreeSurface(screenshot);
free(filename);
} }

View File

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "savepng.h" #include "savepng.h"
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;
extern Colors colors; extern Colors colors;

View File

@ -228,11 +228,11 @@ static void loadConfig(void)
{ {
int i; int i;
cJSON *root, *controlsJSON, *node; cJSON *root, *controlsJSON, *node;
char *text, filename[MAX_PATH_LENGTH]; char *text, *filename;
initDefaultConfig(); initDefaultConfig();
sprintf(filename, "%s/%s", app.saveDir, CONFIG_FILENAME); filename = buildFormattedString("%s/%s", app.saveDir, CONFIG_FILENAME);
if (fileExists(filename)) if (fileExists(filename))
{ {
@ -278,15 +278,17 @@ static void loadConfig(void)
cJSON_Delete(root); cJSON_Delete(root);
free(text); free(text);
} }
free(filename);
} }
void saveConfig(void) void saveConfig(void)
{ {
int i; int i;
char *out, filename[MAX_PATH_LENGTH]; char *out, *filename;
cJSON *root, *controlsJSON, *keysJSON, *joypadJSON; cJSON *root, *controlsJSON, *keysJSON, *joypadJSON;
sprintf(filename, "%s/%s", app.saveDir, CONFIG_FILENAME); filename = buildFormattedString("%s/%s", app.saveDir, CONFIG_FILENAME);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving config ..."); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving config ...");
@ -330,6 +332,8 @@ void saveConfig(void)
cJSON_Delete(root); cJSON_Delete(root);
free(out); free(out);
free(filename);
} }
void cleanup(void) void cleanup(void)

View File

@ -54,5 +54,6 @@ extern void presentScene(void);
extern char *readFile(const char *filename); extern char *readFile(const char *filename);
extern void setLanguage(char *applicationName, char *languageCode); extern void setLanguage(char *applicationName, char *languageCode);
extern int writeFile(const char *filename, const char *data); extern int writeFile(const char *filename, const char *data);
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;

View File

@ -81,6 +81,25 @@ void *resize(void *array, int oldSize, int newSize)
return newArray; return newArray;
} }
char *buildFormattedString(const char *format, ...)
{
int n;
char *s;
va_list args;
va_start(args, format);
n = vsnprintf(NULL, 0, format, args) + 1;
va_end(args);
s = malloc(sizeof(char) * n);
va_start(args, format);
vsprintf(s, format, args);
va_end(args);
return s;
}
/* /*
* public domain strtok_r() by Charlie Gordon * public domain strtok_r() by Charlie Gordon
* *

View File

@ -29,7 +29,7 @@ static void loadObjectives(cJSON *root);
void loadWorld(char *id) void loadWorld(char *id)
{ {
cJSON *root; cJSON *root;
char *text, filename[MAX_PATH_LENGTH]; char *text, *filename;
memset(&world, 0, sizeof(World)); memset(&world, 0, sizeof(World));
@ -38,7 +38,7 @@ void loadWorld(char *id)
world.objectiveTail = &world.objectiveHead; world.objectiveTail = &world.objectiveHead;
world.particleTail = &world.particleHead; world.particleTail = &world.particleHead;
sprintf(filename, "%s/%d/%s.json", app.saveDir, game.saveSlot, id); filename = buildFormattedString("%s/%d/%s.json", app.saveDir, game.saveSlot, id);
if (!game.isComplete && fileExists(filename)) if (!game.isComplete && fileExists(filename))
{ {
@ -79,6 +79,8 @@ void loadWorld(char *id)
cJSON_Delete(root); cJSON_Delete(root);
free(text); free(text);
free(filename);
} }
static void loadEnemyTypes(char *enemyTypes) static void loadEnemyTypes(char *enemyTypes)

View File

@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern Entity *createEntity(char *typeStr); extern Entity *createEntity(char *typeStr);
extern int fileExists(const char *filename); extern int fileExists(const char *filename);
extern char *readFile(const char *filename); extern char *readFile(const char *filename);
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;
extern Dev dev; extern Dev dev;

View File

@ -30,9 +30,9 @@ static int canPersistEntity(void);
void saveWorld(void) void saveWorld(void)
{ {
cJSON *root; cJSON *root;
char filename[MAX_PATH_LENGTH], *out; char *filename, *out;
sprintf(filename, "%s/%d/%s.json", app.saveDir, game.saveSlot, world.id); filename = buildFormattedString("%s/%d/%s.json", app.saveDir, game.saveSlot, world.id);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving world to '%s' ...", filename); SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Saving world to '%s' ...", filename);
@ -66,6 +66,8 @@ void saveWorld(void)
cJSON_Delete(root); cJSON_Delete(root);
free(out); free(out);
free(filename);
} }
static void saveEnemyTypes(cJSON *root) static void saveEnemyTypes(cJSON *root)

View File

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../json/cJSON.h" #include "../json/cJSON.h"
extern int writeFile(const char *filename, const char *data); extern int writeFile(const char *filename, const char *data);
extern char *buildFormattedString(const char *format, ...);
extern App app; extern App app;
extern Entity *self; extern Entity *self;