Centralizing getFileLocation() to make file loading more transparent.

This commit is contained in:
Steve 2016-03-04 14:29:50 +00:00
parent 2b3fecce88
commit 159d36017b
25 changed files with 238 additions and 245 deletions

View File

@ -48,7 +48,7 @@ void initBulletDefs(void)
memset(&bulletDef, 0, sizeof(Bullet) * BT_MAX);
text = readFile(getFileLocation("data/battle/bullets.json"));
text = readFile("data/battle/bullets.json");
root = cJSON_Parse(text);

View File

@ -41,7 +41,6 @@ extern void addMissileExplosion(Bullet *b);
extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore);
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern void playSound(int id);
extern char *getFileLocation(char *filename);
extern char *getTranslatedString(char *string);
extern Battle battle;

View File

@ -317,7 +317,7 @@ void loadCapitalShipDefs(void)
memset(&defHead, 0, sizeof(Entity));
defTail = &defHead;
filenames = getFileList(getFileLocation("data/capitalShips"), &count);
filenames = getFileList("data/capitalShips", &count);
for (i = 0 ; i < count ; i++)
{
@ -339,7 +339,7 @@ static void loadCapitalShipDef(char *filename)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(getFileLocation(filename));
text = readFile(filename);
e = malloc(sizeof(Entity));
memset(e, 0, sizeof(Entity));

View File

@ -33,7 +33,6 @@ extern void playBattleSound(int id, int x, int y);
extern float getAngle(int x1, int y1, int x2, int y2);
extern SDL_Texture *getTexture(char *filename);
extern char *readFile(char *filename);
extern char *getFileLocation(char *filename);
extern long flagsToLong(char *flags, int *add);
extern long lookup(char *name);
extern void doAI(void);

View File

@ -624,7 +624,7 @@ static void loadFighterDefList(char *dir)
char path[MAX_FILENAME_LENGTH];
int count, i;
filenames = getFileList(getFileLocation(dir), &count);
filenames = getFileList(dir, &count);
for (i = 0 ; i < count ; i++)
{
@ -647,7 +647,7 @@ static void loadFighterDef(char *filename)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(getFileLocation(filename));
text = readFile(filename);
e = malloc(sizeof(Entity));
memset(e, 0, sizeof(Entity));

View File

@ -43,7 +43,6 @@ extern long flagsToLong(char *flags, int *add);
extern void addShieldSplinterEffect(Entity *ent);
extern void completeMission(void);
extern void runScriptFunction(char *format, ...);
extern char *getFileLocation(char *filename);
extern void addDebris(int x, int y, int amount);
extern char **getFileList(char *dir, int *count);
extern char *getTranslatedString(char *string);

View File

@ -31,7 +31,7 @@ void loadItemDefs(void)
char *text;
Entity *e;
text = readFile(getFileLocation("data/battle/items.json"));
text = readFile("data/battle/items.json");
root = cJSON_Parse(text);

View File

@ -30,7 +30,6 @@ extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int
extern void playBattleSound(int id, int x, int y);
extern void addHudMessage(SDL_Color c, char *format, ...);
extern void updateObjective(char *name, int type);
extern char *getFileLocation(char *filename);
extern char *getTranslatedString(char *string);
extern Battle battle;

View File

@ -58,7 +58,7 @@ void initChallenges(void)
tail = &game.challengeMissionHead;
filenames = getFileList(getFileLocation("data/challenges"), &count);
filenames = getFileList("data/challenges", &count);
for (i = 0 ; i < count ; i++)
{

View File

@ -26,7 +26,6 @@ extern void selectWidget(const char *name, const char *group);
extern void retreatAllies(void);
extern void retreatEnemies(void);
extern char *getTranslatedString(char *string);
extern char *getFileLocation(char *filename);
extern char *getLookupName(char *prefix, long num);
extern char *timeToString(long millis, int showHours);
extern int getPercent(float current, float total);

View File

@ -44,7 +44,7 @@ Mission *loadMissionMeta(char *filename)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(getFileLocation(filename));
text = readFile(filename);
root = cJSON_Parse(text);
@ -126,7 +126,7 @@ void loadMission(char *filename)
stopMusic();
text = readFile(getFileLocation(filename));
text = readFile(filename);
root = cJSON_Parse(text);

View File

@ -43,7 +43,6 @@ extern void failIncompleteObjectives(void);
extern void completeConditions(void);
extern void retreatEnemies(void);
extern void initScript(cJSON *missionJSON);
extern char *getFileLocation(char *filename);
extern void updateCapitalShipComponentProperties(Entity *parent);
extern void countNumEnemies(void);
extern void initMissionInfo(void);

View File

@ -31,7 +31,7 @@ void initStarSystems(void)
tail = &game.starSystemHead;
text = readFile(getFileLocation("data/galaxy/starSystems.json"));
text = readFile("data/galaxy/starSystems.json");
root = cJSON_Parse(text);
for (node = cJSON_GetObjectItem(root, "starSystems")->child ; node != NULL ; node = node->next)
@ -92,7 +92,7 @@ static void loadMissions(StarSystem *starSystem)
sprintf(path, "data/missions/%s", name);
filenames = getFileList(getFileLocation(path), &count);
filenames = getFileList(path, &count);
for (i = 0 ; i < count ; i++)
{

View File

@ -25,7 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern char *readFile(char *filename);
extern long lookup(char *name);
extern int isMissionAvailable(Mission *mission, Mission *prev);
extern char *getFileLocation(char *filename);
extern char **getFileList(char *dir, int *count);
extern Mission *loadMissionMeta(char *filename);
extern int getJSONValue(cJSON *node, char *name, int defValue);

View File

@ -91,7 +91,7 @@ static void loadTrophyData(char *filename)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(getFileLocation(filename));
text = readFile(filename);
root = cJSON_Parse(text);
tail = &game.trophyHead;

View File

@ -23,7 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "time.h"
extern char *getFileLocation(char *filename);
extern long lookup(char *name);
extern char *readFile(char *filename);

View File

@ -213,7 +213,7 @@ static void loadConfig(void)
}
else
{
text = readFile(getFileLocation("data/app/config.json"));
text = readFile("data/app/config.json");
}
root = cJSON_Parse(text);

View File

@ -66,7 +66,6 @@ extern void expireTexts(int all);
extern void initInput(void);
extern void initModalDialog(void);
extern void createSaveFolder(void);
extern char *getFileLocation(char *filename);
extern void setLanguage(char *applicationName, char *languageCode);
extern char *getLookupName(char *prefix, long num);
extern long lookup(char *name);

View File

@ -29,11 +29,26 @@ int fileExists(char *filename)
return (stat(filename, &buffer) == 0);
}
char *getFileLocation(char *filename)
{
static char path[MAX_FILENAME_LENGTH];
memset(path, '\0', MAX_FILENAME_LENGTH);
if (fileExists(filename))
{
return filename;
}
sprintf(path, DATA_DIR"/%s", filename);
return path;
}
char *readFile(char *filename)
{
char *buffer = 0;
long length;
FILE *file = fopen(filename, "rb");
FILE *file = fopen(getFileLocation(filename), "rb");
if (file)
{
@ -77,21 +92,6 @@ char *getSaveFilePath(char *filename)
return path;
}
char *getFileLocation(char *filename)
{
static char path[MAX_FILENAME_LENGTH];
memset(path, '\0', MAX_FILENAME_LENGTH);
if (fileExists(filename))
{
return filename;
}
sprintf(path, DATA_DIR"/%s", filename);
return path;
}
char **getFileList(char *dir, int *count)
{
DIR *d;
@ -102,7 +102,7 @@ char **getFileList(char *dir, int *count)
i = 0;
filenames = NULL;
if ((d = opendir(dir)) != NULL)
if ((d = opendir(getFileLocation(dir))) != NULL)
{
while ((ent = readdir(d)) != NULL)
{

View File

@ -34,7 +34,7 @@ void initResources(void)
numBackgrounds = numPlanets = numMusicFiles = 0;
filenames = getFileList(getFileLocation("gfx/backgrounds"), &numBackgrounds);
filenames = getFileList("gfx/backgrounds", &numBackgrounds);
backgrounds = malloc(sizeof(char*) * numBackgrounds);
for (i = 0 ; i < numBackgrounds ; i++)
@ -49,7 +49,7 @@ void initResources(void)
free(filenames);
filenames = getFileList(getFileLocation("gfx/planets"), &numPlanets);
filenames = getFileList("gfx/planets", &numPlanets);
planets = malloc(sizeof(char*) * numPlanets);
for (i = 0 ; i < numPlanets ; i++)
@ -64,7 +64,7 @@ void initResources(void)
free(filenames);
filenames = getFileList(getFileLocation("music/battle/"), &numMusicFiles);
filenames = getFileList("music/battle/", &numMusicFiles);
musicFiles = malloc(sizeof(char*) * numMusicFiles);

View File

@ -21,5 +21,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h"
extern char **getFileList(char *dir, int *count);
extern char *getFileLocation(char *filename);
extern void blitScaled(SDL_Texture *texture, int x, int y, int w, int h);

View File

@ -89,34 +89,39 @@ void playBattleSound(int id, int x, int y)
}
}
static Mix_Chunk *loadSound(char *filename)
{
return Mix_LoadWAV(getFileLocation(filename));
}
static void loadSounds(void)
{
sounds[SND_ARMOUR_HIT] = Mix_LoadWAV(getFileLocation("sound/275151__bird-man__gun-shot.ogg"));
sounds[SND_SHIELD_HIT] = Mix_LoadWAV(getFileLocation("sound/49678__ejfortin__energy-short-sword-7.ogg"));
sounds[SND_PLASMA] = Mix_LoadWAV(getFileLocation("sound/268344__julien-matthey__jm-noiz-laser-01.ogg"));
sounds[SND_LASER] = Mix_LoadWAV(getFileLocation("sound/18382__inferno__hvylas.ogg"));
sounds[SND_MAG] = Mix_LoadWAV(getFileLocation("sound/146725__fins__laser.ogg"));
sounds[SND_SHIELD_BREAK] = Mix_LoadWAV(getFileLocation("sound/322603__clippysounds__glass-break.ogg"));
sounds[SND_PARTICLE] = Mix_LoadWAV(getFileLocation("sound/77087__supraliminal__laser-short.ogg"));
sounds[SND_MISSILE] = Mix_LoadWAV(getFileLocation("sound/65787__iwilldstroyu__laserrocket.ogg"));
sounds[SND_BOOST] = Mix_LoadWAV(getFileLocation("sound/18380__inferno__hvrl.ogg"));
sounds[SND_RADIO] = Mix_LoadWAV(getFileLocation("sound/321906__bruce965__walkie-talkie-roger-beep.ogg"));
sounds[SND_INCOMING] = Mix_LoadWAV(getFileLocation("sound/242856__plasterbrain__nuclear-alarm.ogg"));
sounds[SND_GET_ITEM] = Mix_LoadWAV(getFileLocation("sound/88275__s-dij__gbc-reload-06.ogg"));
sounds[SND_EXPLOSION_1] = Mix_LoadWAV(getFileLocation("sound/162265__qubodup__explosive.ogg"));
sounds[SND_EXPLOSION_2] = Mix_LoadWAV(getFileLocation("sound/207322__animationisaac__short-explosion.ogg"));
sounds[SND_EXPLOSION_3] = Mix_LoadWAV(getFileLocation("sound/254071__tb0y298__firework-explosion.ogg"));
sounds[SND_EXPLOSION_4] = Mix_LoadWAV(getFileLocation("sound/47252__nthompson__bad-explosion.ogg"));
sounds[SND_JUMP] = Mix_LoadWAV(getFileLocation("sound/276912__pauldihor__transform.ogg"));
sounds[SND_ECM] = Mix_LoadWAV(getFileLocation("sound/251431__onlytheghosts__fusion-gun-flash0-by-onlytheghosts.ogg"));
sounds[SND_MAG_HIT] = Mix_LoadWAV(getFileLocation("sound/172591__timbre__zapitydooda.ogg"));
sounds[SND_POWER_DOWN] = Mix_LoadWAV(getFileLocation("sound/39030__wildweasel__d1clsstf.ogg"));
sounds[SND_SELECT_WEAPON] = Mix_LoadWAV(getFileLocation("sound/329359__bassoonrckr__reed-guillotine.ogg"));
sounds[SND_ARMOUR_HIT] = loadSound("sound/275151__bird-man__gun-shot.ogg");
sounds[SND_SHIELD_HIT] = loadSound("sound/49678__ejfortin__energy-short-sword-7.ogg");
sounds[SND_PLASMA] = loadSound("sound/268344__julien-matthey__jm-noiz-laser-01.ogg");
sounds[SND_LASER] = loadSound("sound/18382__inferno__hvylas.ogg");
sounds[SND_MAG] = loadSound("sound/146725__fins__laser.ogg");
sounds[SND_SHIELD_BREAK] = loadSound("sound/322603__clippysounds__glass-break.ogg");
sounds[SND_PARTICLE] = loadSound("sound/77087__supraliminal__laser-short.ogg");
sounds[SND_MISSILE] = loadSound("sound/65787__iwilldstroyu__laserrocket.ogg");
sounds[SND_BOOST] = loadSound("sound/18380__inferno__hvrl.ogg");
sounds[SND_RADIO] = loadSound("sound/321906__bruce965__walkie-talkie-roger-beep.ogg");
sounds[SND_INCOMING] = loadSound("sound/242856__plasterbrain__nuclear-alarm.ogg");
sounds[SND_GET_ITEM] = loadSound("sound/88275__s-dij__gbc-reload-06.ogg");
sounds[SND_EXPLOSION_1] = loadSound("sound/162265__qubodup__explosive.ogg");
sounds[SND_EXPLOSION_2] = loadSound("sound/207322__animationisaac__short-explosion.ogg");
sounds[SND_EXPLOSION_3] = loadSound("sound/254071__tb0y298__firework-explosion.ogg");
sounds[SND_EXPLOSION_4] = loadSound("sound/47252__nthompson__bad-explosion.ogg");
sounds[SND_JUMP] = loadSound("sound/276912__pauldihor__transform.ogg");
sounds[SND_ECM] = loadSound("sound/251431__onlytheghosts__fusion-gun-flash0-by-onlytheghosts.ogg");
sounds[SND_MAG_HIT] = loadSound("sound/172591__timbre__zapitydooda.ogg");
sounds[SND_POWER_DOWN] = loadSound("sound/39030__wildweasel__d1clsstf.ogg");
sounds[SND_SELECT_WEAPON] = loadSound("sound/329359__bassoonrckr__reed-guillotine.ogg");
sounds[SND_GUI_CLICK] = Mix_LoadWAV(getFileLocation("sound/257786__xtrgamr__mouse-click.ogg"));
sounds[SND_GUI_SELECT] = Mix_LoadWAV(getFileLocation("sound/321104__nsstudios__blip2.ogg"));
sounds[SND_GUI_CLOSE] = Mix_LoadWAV(getFileLocation("sound/178064__jorickhoofd__slam-door-shut.ogg"));
sounds[SND_GUI_DENIED] = Mix_LoadWAV(getFileLocation("sound/249300__suntemple__access-denied.ogg"));
sounds[SND_GUI_CLICK] = loadSound("sound/257786__xtrgamr__mouse-click.ogg");
sounds[SND_GUI_SELECT] = loadSound("sound/321104__nsstudios__blip2.ogg");
sounds[SND_GUI_CLOSE] = loadSound("sound/178064__jorickhoofd__slam-door-shut.ogg");
sounds[SND_GUI_DENIED] = loadSound("sound/249300__suntemple__access-denied.ogg");
}
void destroySounds(void)

View File

@ -25,6 +25,5 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_BATTLE_SOUND_DISTANCE 1500
extern int getDistance(int x1, int y1, int x2, int y2);
extern char *getFileLocation(char *filename);
extern Entity *player;

View File

@ -255,7 +255,7 @@ static void loadWidgets()
char path[MAX_FILENAME_LENGTH];
int count, i;
filenames = getFileList(getFileLocation("data/widgets"), &count);
filenames = getFileList("data/widgets", &count);
for (i = 0 ; i < count ; i++)
{
@ -277,7 +277,7 @@ static void loadWidgetSet(char *filename)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
text = readFile(getFileLocation(filename));
text = readFile(filename);
root = cJSON_Parse(text);
for (node = root->child ; node != NULL ; node = node->next)

View File

@ -30,7 +30,6 @@ extern void blit(SDL_Texture *texture, int x, int y, int centered);
extern SDL_Texture *getTexture(char *filename);
extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2);
extern void playSound(int id);
extern char *getFileLocation(char *filename);
extern char **getFileList(char *dir, int *count);
extern char *getTranslatedString(char *string);