From 159d36017b75fd8a1953dd4421d7059ad29716e9 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 4 Mar 2016 14:29:50 +0000 Subject: [PATCH] Centralizing getFileLocation() to make file loading more transparent. --- src/battle/bullets.c | 2 +- src/battle/bullets.h | 1 - src/battle/capitalShips.c | 4 +- src/battle/capitalShips.h | 1 - src/battle/fighters.c | 4 +- src/battle/fighters.h | 1 - src/battle/items.c | 46 ++++++++--------- src/battle/items.h | 1 - src/challenges/challenges.c | 94 ++++++++++++++++----------------- src/challenges/challenges.h | 1 - src/galaxy/mission.c | 4 +- src/galaxy/mission.h | 1 - src/galaxy/starSystems.c | 4 +- src/galaxy/starSystems.h | 1 - src/game/trophies.c | 2 +- src/game/trophies.h | 1 - src/system/init.c | 22 ++++---- src/system/init.h | 1 - src/system/io.c | 72 +++++++++++++------------- src/system/resources.c | 40 +++++++-------- src/system/resources.h | 1 - src/system/sound.c | 77 ++++++++++++++------------- src/system/sound.h | 1 - src/system/widgets.c | 100 ++++++++++++++++++------------------ src/system/widgets.h | 1 - 25 files changed, 238 insertions(+), 245 deletions(-) diff --git a/src/battle/bullets.c b/src/battle/bullets.c index 3446f31..0264cba 100644 --- a/src/battle/bullets.c +++ b/src/battle/bullets.c @@ -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); diff --git a/src/battle/bullets.h b/src/battle/bullets.h index e2bcfcb..a5934c3 100644 --- a/src/battle/bullets.h +++ b/src/battle/bullets.h @@ -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; diff --git a/src/battle/capitalShips.c b/src/battle/capitalShips.c index 4e9106f..0e7ab4e 100644 --- a/src/battle/capitalShips.c +++ b/src/battle/capitalShips.c @@ -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)); diff --git a/src/battle/capitalShips.h b/src/battle/capitalShips.h index 621a5b3..f24ee88 100644 --- a/src/battle/capitalShips.h +++ b/src/battle/capitalShips.h @@ -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); diff --git a/src/battle/fighters.c b/src/battle/fighters.c index 24053e2..9d4d853 100644 --- a/src/battle/fighters.c +++ b/src/battle/fighters.c @@ -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)); diff --git a/src/battle/fighters.h b/src/battle/fighters.h index c26e414..c6c420b 100644 --- a/src/battle/fighters.h +++ b/src/battle/fighters.h @@ -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); diff --git a/src/battle/items.c b/src/battle/items.c index 9911da0..404134c 100644 --- a/src/battle/items.c +++ b/src/battle/items.c @@ -30,32 +30,32 @@ void loadItemDefs(void) cJSON *root, *node; char *text; Entity *e; - - text = readFile(getFileLocation("data/battle/items.json")); - + + text = readFile("data/battle/items.json"); + root = cJSON_Parse(text); - + memset(&defHead, 0, sizeof(Entity)); defTail = &defHead; - + for (node = root->child ; node != NULL ; node = node->next) { e = malloc(sizeof(Entity)); memset(e, 0, sizeof(Entity)); - + e->type = ET_ITEM; e->active = 1; STRNCPY(e->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); STRNCPY(e->defName, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); e->texture = getTexture(cJSON_GetObjectItem(node, "texture")->valuestring); - + e->health = e->maxHealth = FPS; - + SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h); - + defTail->next = e; } - + cJSON_Delete(root); free(text); } @@ -63,26 +63,26 @@ void loadItemDefs(void) Entity *spawnItem(char *name) { Entity *item, *def; - + item = spawnEntity(); - + def = getItemDef(name); - + memcpy(item, def, sizeof(Entity)); - + item->dx = rand() % 100 - rand() % 100; item->dx *= 0.01; item->dy = rand() % 100 - rand() % 100; item->dy *= 0.01; item->action = action; - + return item; } static Entity *getItemDef(char *name) { Entity *e; - + for (e = defHead.next ; e != NULL ; e = e->next) { if (strcmp(e->name, name) == 0) @@ -90,7 +90,7 @@ static Entity *getItemDef(char *name) return e; } } - + printf("Error: no such item '%s'\n", name); exit(1); } @@ -99,24 +99,24 @@ static void action(void) { Entity *e, **candidates; int i; - + candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self); - + for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) { if ((e->flags & EF_COLLECTS_ITEMS) && collision(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h)) { self->health = 0; playBattleSound(SND_GET_ITEM, self->x, self->y); - + updateObjective(self->name, TT_ITEM); - + if (e == player) { addHudMessage(colors.white, _("Picked up %s"), self->name); battle.stats[STAT_ITEMS_COLLECTED]++; } - + self->action = NULL; } } @@ -125,7 +125,7 @@ static void action(void) void destroyItemDefs(void) { Entity *e; - + while (defHead.next) { e = defHead.next; diff --git a/src/battle/items.h b/src/battle/items.h index 9554d14..bc1c79e 100644 --- a/src/battle/items.h +++ b/src/battle/items.h @@ -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; diff --git a/src/challenges/challenges.c b/src/challenges/challenges.c index 57fd402..5a734ed 100644 --- a/src/challenges/challenges.c +++ b/src/challenges/challenges.c @@ -43,7 +43,7 @@ void initChallenges(void) char **filenames; char path[MAX_FILENAME_LENGTH]; int count, i; - + challengeDescription[CHALLENGE_ARMOUR] = _("Retain at least %d%% armour"); challengeDescription[CHALLENGE_TIME] = _("Complete challenge in %d seconds or less"); challengeDescription[CHALLENGE_SHOT_ACCURACY] = _("Attain a %d%% shot hit accuracy"); @@ -55,22 +55,22 @@ void initChallenges(void) challengeDescription[CHALLENGE_PLAYER_KILLS] = _("Take down %d enemy targets"); challengeDescription[CHALLENGE_DISABLE] = _("Disable %d or more enemy fighters"); challengeDescription[CHALLENGE_TIME_MINS] = _("Complete challenge in %d minutes or less"); - + tail = &game.challengeMissionHead; - - filenames = getFileList(getFileLocation("data/challenges"), &count); - + + filenames = getFileList("data/challenges", &count); + for (i = 0 ; i < count ; i++) { sprintf(path, "data/challenges/%s", filenames[i]); - + mission = loadMissionMeta(path); tail->next = mission; tail = mission; - + free(filenames[i]); } - + free(filenames); } @@ -81,7 +81,7 @@ void doChallenges(void) if (game.currentMission->challengeData.timeLimit > 0 && battle.stats[STAT_TIME] >= game.currentMission->challengeData.timeLimit) { updateChallenges(); - + if (hasFailedAllChallenges()) { failChallenge(); @@ -91,12 +91,12 @@ void doChallenges(void) completeChallenge(); } } - + /* disabled enemies count as killed during challenges - not player exclusive, but no need to worry about AI contributions here */ if (game.currentMission->challengeData.killLimit > 0 && (battle.stats[STAT_ENEMIES_KILLED_PLAYER] + battle.stats[STAT_ENEMIES_DISABLED]) >= game.currentMission->challengeData.killLimit) { updateChallenges(); - + completeChallenge(); } } @@ -106,17 +106,17 @@ static int hasFailedAllChallenges(void) { int i; Challenge *c; - + for (i = 0 ; i < MAX_CHALLENGES ; i++) { c = game.currentMission->challengeData.challenges[i]; - + if (c && c->passed) { return 0; } } - + return 1; } @@ -124,13 +124,13 @@ static void updateChallenges(void) { int i; Challenge *c; - + updateAccuracyStats(battle.stats); - + for (i = 0 ; i < MAX_CHALLENGES ; i++) { c = game.currentMission->challengeData.challenges[i]; - + if (c && !c->passed) { switch (c->type) @@ -139,34 +139,34 @@ static void updateChallenges(void) case CHALLENGE_TIME_MINS: updateTimeChallenge(c); break; - + case CHALLENGE_SHOT_ACCURACY: case CHALLENGE_ROCKET_ACCURACY: case CHALLENGE_MISSILE_ACCURACY: updateAccuracyChallenge(c); break; - + case CHALLENGE_ARMOUR: updateArmourChallenge(c); break; - + case CHALLENGE_NO_LOSSES: case CHALLENGE_1_LOSS: case CHALLENGE_LOSSES: updateLossesChallenge(c); break; - + case CHALLENGE_PLAYER_KILLS: updatePlayerKillsChallenge(c); break; - + case CHALLENGE_DISABLE: updateDisabledChallenge(c); break; } } } - + if (dev.debug) { printStats(); @@ -176,7 +176,7 @@ static void updateChallenges(void) static void printStats(void) { int i; - + for (i = 0 ; i < STAT_MAX ; i++) { if (battle.stats[i]) @@ -203,7 +203,7 @@ static void updateTimeChallenge(Challenge *c) c->passed = 1; } break; - + case CHALLENGE_TIME_MINS: if ((battle.stats[STAT_TIME] / FPS) / 60 < c->value) { @@ -216,26 +216,26 @@ static void updateTimeChallenge(Challenge *c) static void updateAccuracyChallenge(Challenge *c) { float percent; - + switch (c->type) { case CHALLENGE_SHOT_ACCURACY: percent = battle.stats[STAT_SHOT_ACCURACY]; break; - + case CHALLENGE_ROCKET_ACCURACY: percent = battle.stats[STAT_ROCKET_ACCURACY]; break; - + case CHALLENGE_MISSILE_ACCURACY: percent = battle.stats[STAT_MISSILE_ACCURACY]; break; - + default: percent = 0; break; } - + if (percent >= c->value) { c->passed = 1; @@ -245,11 +245,11 @@ static void updateAccuracyChallenge(Challenge *c) static void updateArmourChallenge(Challenge *c) { float percent; - + percent = player->health; percent /= player->maxHealth; percent *= 100; - + if (percent >= c->value) { c->passed = 1; @@ -289,17 +289,17 @@ Challenge *getChallenge(Mission *mission, int type, int value) { int i; Challenge *c; - + for (i = 0 ; i < MAX_CHALLENGES ; i++) { c = mission->challengeData.challenges[i]; - + if (c->type == type && c->value == value) { return c; } } - + return NULL; } @@ -312,7 +312,7 @@ static char *getFormattedChallengeDescription(const char *format, ...) va_start(args, format); vsprintf(descriptionBuffer, format, args); va_end(args); - + return descriptionBuffer; } @@ -321,19 +321,19 @@ void updateChallengeMissions(void) int i; Mission *m; Challenge *c; - + for (m = game.challengeMissionHead.next ; m != NULL ; m = m->next) { m->totalChallenges = m->completedChallenges = 0; - + for (i = 0 ; i < MAX_CHALLENGES ; i++) { c = m->challengeData.challenges[i]; - + if (c) { m->totalChallenges++; - + if (c->passed) { m->completedChallenges++; @@ -350,13 +350,13 @@ static void completeChallenge(void) battle.status = MS_COMPLETE; battle.missionFinishedTimer = FPS; selectWidget("continue", "battleWon"); - + game.stats[STAT_CHALLENGES_COMPLETED]++; - + retreatAllies(); - + retreatEnemies(); - + player->flags |= EF_IMMORTAL; } } @@ -368,11 +368,11 @@ static void failChallenge(void) battle.status = MS_FAILED; battle.missionFinishedTimer = FPS; selectWidget("retry", "battleLost"); - + retreatAllies(); - + retreatEnemies(); - + player->flags |= EF_IMMORTAL; } } diff --git a/src/challenges/challenges.h b/src/challenges/challenges.h index 1299ddb..81f7cfe 100644 --- a/src/challenges/challenges.h +++ b/src/challenges/challenges.h @@ -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); diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index 2aa111b..1338273 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -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); diff --git a/src/galaxy/mission.h b/src/galaxy/mission.h index be7b5e1..0065998 100644 --- a/src/galaxy/mission.h +++ b/src/galaxy/mission.h @@ -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); diff --git a/src/galaxy/starSystems.c b/src/galaxy/starSystems.c index 341d7f5..8bfc4a4 100644 --- a/src/galaxy/starSystems.c +++ b/src/galaxy/starSystems.c @@ -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++) { diff --git a/src/galaxy/starSystems.h b/src/galaxy/starSystems.h index 7c4d9f3..f8332a9 100644 --- a/src/galaxy/starSystems.h +++ b/src/galaxy/starSystems.h @@ -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); diff --git a/src/game/trophies.c b/src/game/trophies.c index 8a89b7a..115d967 100644 --- a/src/game/trophies.c +++ b/src/game/trophies.c @@ -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; diff --git a/src/game/trophies.h b/src/game/trophies.h index 6492006..d696922 100644 --- a/src/game/trophies.h +++ b/src/game/trophies.h @@ -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); diff --git a/src/system/init.c b/src/system/init.c index 364a77a..1914da1 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -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); @@ -231,19 +231,19 @@ static void loadConfig(void) while (node) { i = lookup(node->string); - + app.keyControls[i] = node->valueint; - + node = node->next; } - + node = cJSON_GetObjectItem(controlsJSON, "mouse")->child; while (node) { i = lookup(node->string); - + app.mouseControls[i] = node->valueint; - + node = node->next; } } @@ -271,23 +271,23 @@ void saveConfig(void) cJSON_AddNumberToObject(root, "fullscreen", app.fullscreen); cJSON_AddNumberToObject(root, "musicVolume", app.musicVolume); cJSON_AddNumberToObject(root, "soundVolume", app.soundVolume); - + keysJSON = cJSON_CreateObject(); for (i = 0 ; i < CONTROL_MAX ; i++) { cJSON_AddNumberToObject(keysJSON, getLookupName("CONTROL_", i), app.keyControls[i]); } - + mouseJSON = cJSON_CreateObject(); for (i = 0 ; i < CONTROL_MAX ; i++) { cJSON_AddNumberToObject(mouseJSON, getLookupName("CONTROL_", i), app.mouseControls[i]); } - + controlsJSON = cJSON_CreateObject(); cJSON_AddItemToObject(controlsJSON, "keys", keysJSON); cJSON_AddItemToObject(controlsJSON, "mouse", mouseJSON); - + cJSON_AddItemToObject(root, "controls", controlsJSON); out = cJSON_Print(root); @@ -335,7 +335,7 @@ void cleanup(void) destroyGalacticMap(); destroyWidgets(); - + destroyResources(); TTF_Quit(); diff --git a/src/system/init.h b/src/system/init.h index 56fb8d3..6d9faea 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -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); diff --git a/src/system/io.c b/src/system/io.c index 97847fd..f0a4857 100644 --- a/src/system/io.c +++ b/src/system/io.c @@ -25,15 +25,30 @@ static int stringComparator(const void *a, const void *b); int fileExists(char *filename) { struct stat buffer; - + 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) { @@ -54,14 +69,14 @@ char *readFile(char *filename) int writeFile(char *filename, char *data) { FILE *file = fopen(filename, "wb"); - + if (file) { fprintf(file, "%s\n", data); fclose(file); return 1; } - + return 0; } @@ -69,26 +84,11 @@ char *getSaveFilePath(char *filename) { static char path[MAX_FILENAME_LENGTH]; memset(path, '\0', MAX_FILENAME_LENGTH); - + sprintf(path, "%s/%s", app.saveDir, filename); - + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "getSaveFilePath = '%s'", path); - - 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; } @@ -98,11 +98,11 @@ char **getFileList(char *dir, int *count) int i; struct dirent *ent; char **filenames; - + i = 0; filenames = NULL; - - if ((d = opendir(dir)) != NULL) + + if ((d = opendir(getFileLocation(dir))) != NULL) { while ((ent = readdir(d)) != NULL) { @@ -111,45 +111,45 @@ char **getFileList(char *dir, int *count) i++; } } - + if (i > 0) { filenames = malloc(sizeof(char*) * i); memset(filenames, 0, sizeof(char*) * i); - + rewinddir(d); - + i = 0; - + while ((ent = readdir(d)) != NULL) { if (ent->d_name[0] != '.') { filenames[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH); - + STRNCPY(filenames[i], ent->d_name, MAX_FILENAME_LENGTH); - + i++; } } } - + closedir(d); } - + *count = i; - + if (filenames) { qsort(filenames, i, sizeof(char*), stringComparator); } - + return filenames; } static int stringComparator(const void *a, const void *b) -{ +{ char **s1 = (char **)a; char **s2 = (char **)b; return strcmp(*s1, *s2); -} +} diff --git a/src/system/resources.c b/src/system/resources.c index 055cf87..fb5348f 100644 --- a/src/system/resources.c +++ b/src/system/resources.c @@ -31,53 +31,53 @@ void initResources(void) { char **filenames; int i; - + 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++) { backgrounds[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH); sprintf(backgrounds[i], "gfx/backgrounds/%s", filenames[i]); - + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "'%s' added to AUTO resources list", filenames[i]); - + free(filenames[i]); } - + free(filenames); - - filenames = getFileList(getFileLocation("gfx/planets"), &numPlanets); + + filenames = getFileList("gfx/planets", &numPlanets); planets = malloc(sizeof(char*) * numPlanets); - + for (i = 0 ; i < numPlanets ; i++) { planets[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH); sprintf(planets[i], "gfx/planets/%s", filenames[i]); - + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "'%s' added to AUTO resources list", filenames[i]); - + free(filenames[i]); } - + free(filenames); - - filenames = getFileList(getFileLocation("music/battle/"), &numMusicFiles); - + + filenames = getFileList("music/battle/", &numMusicFiles); + musicFiles = malloc(sizeof(char*) * numMusicFiles); - + for (i = 0 ; i < numMusicFiles ; i++) { musicFiles[i] = malloc(sizeof(char) * MAX_FILENAME_LENGTH); sprintf(musicFiles[i], "music/battle/%s", filenames[i]); - + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "'%s' added to AUTO resources list", filenames[i]); - + free(filenames[i]); } - + free(filenames); } diff --git a/src/system/resources.h b/src/system/resources.h index d5316cc..f0a02f3 100644 --- a/src/system/resources.h +++ b/src/system/resources.h @@ -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); diff --git a/src/system/sound.c b/src/system/sound.c index b649523..3b122b2 100644 --- a/src/system/sound.c +++ b/src/system/sound.c @@ -31,9 +31,9 @@ static int lastPlayerY; void initSounds(void) { memset(sounds, 0, sizeof(Mix_Chunk*) * SND_MAX); - + music = NULL; - + loadSounds(); } @@ -45,9 +45,9 @@ void playMusic(char *filename) Mix_FreeMusic(music); music = NULL; } - + music = Mix_LoadMUS(getFileLocation(filename)); - + Mix_PlayMusic(music, -1); } @@ -66,15 +66,15 @@ void playBattleSound(int id, int x, int y) float distance; int channel; float vol; - + if (player != NULL) { lastPlayerX = player->x; lastPlayerY = player->y; } - + distance = getDistance(lastPlayerX, lastPlayerY, x, y); - + if (distance <= MAX_BATTLE_SOUND_DISTANCE) { channel = Mix_PlayChannel(-1, sounds[id], 0); @@ -83,46 +83,51 @@ void playBattleSound(int id, int x, int y) vol = 255; vol /= MAX_BATTLE_SOUND_DISTANCE; vol *= distance; - + Mix_SetDistance(channel, vol); } } } +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_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_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] = 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) { int i; - + for (i = 0 ; i < SND_MAX ; i++) { if (sounds[i]) @@ -130,7 +135,7 @@ void destroySounds(void) Mix_FreeChunk(sounds[i]); } } - + if (music != NULL) { Mix_FreeMusic(music); diff --git a/src/system/sound.h b/src/system/sound.h index f6b911f..fb82d09 100644 --- a/src/system/sound.h +++ b/src/system/sound.h @@ -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; diff --git a/src/system/widgets.c b/src/system/widgets.c index cf86474..62e9360 100644 --- a/src/system/widgets.c +++ b/src/system/widgets.c @@ -38,16 +38,16 @@ static int drawingWidgets; void initWidgets(void) { memset(&head, 0, sizeof(Widget)); - + tail = &head; - + selectedWidget = NULL; - + optionsLeft = getTexture("gfx/widgets/optionsLeft.png"); optionsRight = getTexture("gfx/widgets/optionsRight.png"); - + loadWidgets(); - + drawingWidgets = 0; } @@ -56,10 +56,10 @@ void doWidgets(void) if (drawingWidgets) { handleMouse(); - + handleKeyboard(); } - + drawingWidgets = 0; } @@ -89,10 +89,10 @@ void drawWidgets(const char *group) { int mouseOver; Widget *w; - + drawingWidgets = 1; mouseOver = 0; - + for (w = head.next; w != NULL ; w = w->next) { if ((app.modalDialog.type == MD_NONE || (app.modalDialog.type != MD_NONE && w->isModal)) && w->visible && strcmp(w->group, group) == 0) @@ -100,18 +100,18 @@ void drawWidgets(const char *group) if (!mouseOver) { mouseOver = (w->type != WT_SELECT && w->enabled && collision(w->rect.x, w->rect.y, w->rect.w, w->rect.h, app.mouse.x, app.mouse.y, 1, 1)); - + if (mouseOver && selectedWidget != w) { if (w->type == WT_BUTTON) { playSound(SND_GUI_CLICK); } - + selectedWidget = w; } } - + if (w->texture) { blit(w->texture , w->rect.x, w->rect.y, 0); @@ -120,7 +120,7 @@ void drawWidgets(const char *group) { SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); SDL_RenderFillRect(app.renderer, &w->rect); - + if (w == selectedWidget) { SDL_SetRenderDrawColor(app.renderer, 64, 128, 200, SDL_ALPHA_OPAQUE); @@ -134,14 +134,14 @@ void drawWidgets(const char *group) SDL_RenderDrawRect(app.renderer, &w->rect); } } - + switch (w->type) { case WT_BUTTON: SDL_RenderDrawRect(app.renderer, &w->rect); drawText(w->rect.x + (w->rect.w / 2), w->rect.y + 2, 20, TA_CENTER, colors.white, w->text); break; - + case WT_SELECT: drawText(w->rect.x + 10, w->rect.y + 2, 20, TA_LEFT, colors.white, w->text); drawText(w->rect.x + w->rect.w - 10, w->rect.y + 2, 20, TA_RIGHT, colors.white, w->options[w->currentOption]); @@ -162,13 +162,13 @@ void drawWidgets(const char *group) static void changeSelectedValue(Widget *w, int dir) { int oldOption = w->currentOption; - + w->currentOption += dir; - + w->currentOption = MIN(MAX(0, w->currentOption), w->numOptions - 1); - + w->onChange(w->options[w->currentOption]); - + if (oldOption != w->currentOption) { playSound(SND_GUI_CLICK); @@ -179,7 +179,7 @@ void setWidgetOption(const char *name, const char *group, const char *value) { int i; Widget *w = getWidget(name, group); - + if (w) { for (i = 0 ; i < w->numOptions ; i++) @@ -196,7 +196,7 @@ void setWidgetOption(const char *name, const char *group, const char *value) static void handleMouse(void) { Widget *old; - + if (selectedWidget && collision(selectedWidget->rect.x, selectedWidget->rect.y, selectedWidget->rect.w, selectedWidget->rect.h, app.mouse.x, app.mouse.y, 1, 1)) { if (app.mouse.button[SDL_BUTTON_LEFT]) @@ -217,7 +217,7 @@ static void handleMouse(void) app.mouse.button[SDL_BUTTON_LEFT] = 0; } break; - + case WT_SELECT_BUTTON: changeSelectedValue(selectedWidget->parent, selectedWidget->value); app.mouse.button[SDL_BUTTON_LEFT] = 0; @@ -230,7 +230,7 @@ static void handleMouse(void) static void handleKeyboard(void) { Widget *old; - + if (app.keyboard[SDL_SCANCODE_SPACE] ||app.keyboard[SDL_SCANCODE_RETURN]) { if (selectedWidget != NULL && selectedWidget->type == WT_BUTTON) @@ -238,12 +238,12 @@ static void handleKeyboard(void) playSound(SND_GUI_SELECT); old = selectedWidget; selectedWidget->action(); - + if (old == selectedWidget) { selectedWidget = NULL; } - + app.keyboard[SDL_SCANCODE_SPACE] = app.keyboard[SDL_SCANCODE_RETURN] = 0; } } @@ -254,18 +254,18 @@ static void loadWidgets() char **filenames; 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++) { sprintf(path, "data/widgets/%s", filenames[i]); - + loadWidgetSet(path); - + free(filenames[i]); } - + free(filenames); } @@ -274,17 +274,17 @@ static void loadWidgetSet(char *filename) cJSON *root, *node; char *text; Widget *w; - + 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) { w = malloc(sizeof(Widget)); memset(w, 0, sizeof(Widget)); - + w->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); STRNCPY(w->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); STRNCPY(w->group, cJSON_GetObjectItem(node, "group")->valuestring, MAX_NAME_LENGTH); @@ -292,12 +292,12 @@ static void loadWidgetSet(char *filename) w->rect.y = cJSON_GetObjectItem(node, "y")->valueint; w->enabled = 1; w->visible = 1; - + if (w->rect.x == -1) { w->rect.x = SCREEN_WIDTH / 2; } - + switch (w->type) { case WT_BUTTON: @@ -307,12 +307,12 @@ static void loadWidgetSet(char *filename) w->rect.x -= w->rect.w / 2; w->rect.y -= (w->rect.h / 2) + 8; break; - + case WT_IMG_BUTTON: w->texture = getTexture(cJSON_GetObjectItem(node, "texture")->valuestring); SDL_QueryTexture(w->texture, NULL, NULL, &w->rect.w, &w->rect.h); break; - + case WT_SELECT: STRNCPY(w->text, cJSON_GetObjectItem(node, "text")->valuestring, MAX_NAME_LENGTH); w->rect.w = cJSON_GetObjectItem(node, "w")->valueint; @@ -323,7 +323,7 @@ static void loadWidgetSet(char *filename) createOptions(w, cJSON_GetObjectItem(node, "options")->valuestring); break; } - + tail->next = w; tail = w; } @@ -336,9 +336,9 @@ static void createOptions(Widget *w, char *options) { int i; char *option; - + w->numOptions = 1; - + for (i = 0 ; i < strlen(options) ; i++) { if (options[i] == ';') @@ -346,18 +346,18 @@ static void createOptions(Widget *w, char *options) w->numOptions++; } } - + w->options = malloc(w->numOptions * sizeof(char*)); - + i = 0; option = strtok(options, ";"); while (option) { w->options[i] = malloc(strlen(option) + 1); strcpy(w->options[i], option); - + option = strtok(NULL, ";"); - + i++; } } @@ -366,16 +366,16 @@ static void createSelectButtons(Widget *w) { int i; Widget *btn; - + for (i = 0 ; i < 2 ; i++) { btn = malloc(sizeof(Widget)); memcpy(btn, w, sizeof(Widget)); strcpy(btn->name, ""); - + btn->type = WT_SELECT_BUTTON; btn->parent = w; - + if (i == 0) { btn->value = -1; @@ -390,7 +390,7 @@ static void createSelectButtons(Widget *w) btn->rect.y += 4; btn->texture = optionsRight; } - + tail->next = btn; tail = btn; } @@ -408,7 +408,7 @@ void destroyWidgets(void) { free(w->options[i]); } - + next = w->next; free(w); w = next; diff --git a/src/system/widgets.h b/src/system/widgets.h index 1d91e10..dda2fe8 100644 --- a/src/system/widgets.h +++ b/src/system/widgets.h @@ -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);