diff --git a/common.mk b/common.mk index ce504d8..e875f44 100644 --- a/common.mk +++ b/common.mk @@ -9,7 +9,7 @@ vpath %.h $(SEARCHPATH) DEPS += defs.h structs.h OBJS += ai.o -OBJS += battle.o bullets.o +OBJS += background.o battle.o bullets.o OBJS += capitalShips.o challengeHome.o challenges.o cJSON.o OBJS += debris.o dev.o draw.o OBJS += effects.o entities.o extractionPoint.o diff --git a/data/challenges/01.json b/data/challenges/01.json index b536302..8840f03 100644 --- a/data/challenges/01.json +++ b/data/challenges/01.json @@ -1,8 +1,8 @@ { "name" : "Destroy all Darts", "description" : "Destroy all Darts", - "background" : "gfx/backgrounds/background03.jpg", - "planet" : "gfx/planets/spirit.png", + "background" : "AUTO", + "planet" : "AUTO", "music" : "", "player" : { "type" : "Nymph", @@ -14,7 +14,7 @@ }, "challenge" : { "timeLimit" : 30, - "killLimit" : 3, + "killLimit" : 5, "challenges" : [ { "type" : "CHALLENGE_TIME", @@ -29,7 +29,7 @@ "side" : "SIDE_PIRATE", "x" : 25, "y" : 22, - "number" : 3, + "number" : 5, "scatter" : 1000 } ] diff --git a/data/challenges/02.json b/data/challenges/02.json index c80aea1..2e1f646 100644 --- a/data/challenges/02.json +++ b/data/challenges/02.json @@ -1,8 +1,8 @@ { "name" : "Destroy all Darts", "description" : "Destroy all Darts", - "background" : "gfx/backgrounds/background03.jpg", - "planet" : "gfx/planets/spirit.png", + "background" : "AUTO", + "planet" : "AUTO", "music" : "", "player" : { "type" : "Nymph", @@ -33,7 +33,7 @@ "side" : "SIDE_PIRATE", "x" : 25, "y" : 22, - "number" : 3, + "number" : 5, "scatter" : 1000 } ] diff --git a/data/challenges/03.json b/data/challenges/03.json index 473a2bd..4ef6daa 100644 --- a/data/challenges/03.json +++ b/data/challenges/03.json @@ -1,8 +1,8 @@ { "name" : "Destroy all Darts", "description" : "Destroy all Darts", - "background" : "gfx/backgrounds/background03.jpg", - "planet" : "gfx/planets/spirit.png", + "background" : "AUTO", + "planet" : "AUTO", "music" : "", "player" : { "type" : "Nymph", diff --git a/src/draw/background.c b/src/draw/background.c new file mode 100644 index 0000000..4b139ce --- /dev/null +++ b/src/draw/background.c @@ -0,0 +1,130 @@ +/* +Copyright (C) 2015-2016 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#include "background.h" + +static PointF backgroundPoint[4]; +char **backgrounds; +char **planets; +int numBackgrounds; +int numPlanets; + +void initBackground(void) +{ + char **filenames; + int i; + + numBackgrounds = numPlanets = 0; + + filenames = getFileList(getFileLocation("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]); + + free(filenames[i]); + } + + free(filenames); + + 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]); + + free(filenames[i]); + } + + free(filenames); + + backgroundPoint[0].x = -SCREEN_WIDTH / 2; + backgroundPoint[0].y = -SCREEN_HEIGHT / 2; + + backgroundPoint[1].x = SCREEN_WIDTH / 2; + backgroundPoint[1].y = -SCREEN_HEIGHT / 2; + + backgroundPoint[2].x = -SCREEN_WIDTH / 2; + backgroundPoint[2].y = SCREEN_HEIGHT / 2; + + backgroundPoint[3].x = SCREEN_WIDTH / 2; + backgroundPoint[3].y = SCREEN_HEIGHT / 2; +} + +void scrollBackground(float x, float y) +{ + int i; + + for (i = 0 ; i < 4 ; i++) + { + backgroundPoint[i].x += x; + backgroundPoint[i].y += y; + + if (backgroundPoint[i].x < 0) + { + backgroundPoint[i].x += (SCREEN_WIDTH * 2); + } + + if (backgroundPoint[i].x >= SCREEN_WIDTH) + { + backgroundPoint[i].x -= (SCREEN_WIDTH * 2); + } + + if (backgroundPoint[i].y < 0) + { + backgroundPoint[i].y += (SCREEN_HEIGHT * 2); + } + + if (backgroundPoint[i].y >= SCREEN_HEIGHT) + { + backgroundPoint[i].y -= (SCREEN_HEIGHT * 2); + } + } +} + +void drawBackground(SDL_Texture *texture) +{ + int i; + + for (i = 0 ; i < 4 ; i++) + { + blitScaled(texture, backgroundPoint[i].x, backgroundPoint[i].y, SCREEN_WIDTH, SCREEN_HEIGHT); + } +} + +char *getBackgroundTexture(int i) +{ + return backgrounds[i % numBackgrounds]; +} + +char *getPlanetTexture(int i) +{ + return planets[i % numPlanets]; +} + +void destroyBackground(void) +{ + free(backgrounds); + free(planets); +} diff --git a/src/draw/background.h b/src/draw/background.h new file mode 100644 index 0000000..d5316cc --- /dev/null +++ b/src/draw/background.h @@ -0,0 +1,25 @@ +/* +Copyright (C) 2015-2016 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +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/draw/draw.c b/src/draw/draw.c index 3daef12..5d0bd3e 100644 --- a/src/draw/draw.c +++ b/src/draw/draw.c @@ -20,23 +20,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "draw.h" -static PointF backgroundPoint[4]; - -void initBackground(void) -{ - backgroundPoint[0].x = -SCREEN_WIDTH / 2; - backgroundPoint[0].y = -SCREEN_HEIGHT / 2; - - backgroundPoint[1].x = SCREEN_WIDTH / 2; - backgroundPoint[1].y = -SCREEN_HEIGHT / 2; - - backgroundPoint[2].x = -SCREEN_WIDTH / 2; - backgroundPoint[2].y = SCREEN_HEIGHT / 2; - - backgroundPoint[3].x = SCREEN_WIDTH / 2; - backgroundPoint[3].y = SCREEN_HEIGHT / 2; -} - void prepareScene(void) { SDL_SetRenderTarget(app.renderer, app.backBuffer); @@ -161,47 +144,6 @@ void drawFilledCircle(int cx, int cy, int radius, int r, int g, int b, int a) SDL_SetRenderDrawBlendMode(app.renderer, SDL_BLENDMODE_NONE); } -void scrollBackground(float x, float y) -{ - int i; - - for (i = 0 ; i < 4 ; i++) - { - backgroundPoint[i].x += x; - backgroundPoint[i].y += y; - - if (backgroundPoint[i].x < 0) - { - backgroundPoint[i].x += (SCREEN_WIDTH * 2); - } - - if (backgroundPoint[i].x >= SCREEN_WIDTH) - { - backgroundPoint[i].x -= (SCREEN_WIDTH * 2); - } - - if (backgroundPoint[i].y < 0) - { - backgroundPoint[i].y += (SCREEN_HEIGHT * 2); - } - - if (backgroundPoint[i].y >= SCREEN_HEIGHT) - { - backgroundPoint[i].y -= (SCREEN_HEIGHT * 2); - } - } -} - -void drawBackground(SDL_Texture *texture) -{ - int i; - - for (i = 0 ; i < 4 ; i++) - { - blitScaled(texture, backgroundPoint[i].x, backgroundPoint[i].y, SCREEN_WIDTH, SCREEN_HEIGHT); - } -} - void saveScreenshot(void) { static int i = 0; diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index 3b5b57b..48eecd2 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -31,6 +31,8 @@ static unsigned long hashcode(const char *str); static char **toTypeArray(char *types, int *numTypes); static void loadEpicData(cJSON *node); static void loadChallengeData(cJSON *node); +static char *getAutoBackground(char *filename); +static char *getAutoPlanet(char *filename); Mission *loadMissionMeta(char *filename) { @@ -108,7 +110,7 @@ Mission *loadMissionMeta(char *filename) void loadMission(char *filename) { cJSON *root; - char *text, music[MAX_DESCRIPTION_LENGTH]; + char *text, music[MAX_DESCRIPTION_LENGTH], *background, *planet; float planetScale; startSectionTransition(); @@ -117,20 +119,9 @@ void loadMission(char *filename) text = readFile(getFileLocation(filename)); - srand(hashcode(filename)); - root = cJSON_Parse(text); - battle.background = getTexture(cJSON_GetObjectItem(root, "background")->valuestring); - - planetScale = 25 + (rand() % 100); - planetScale *= 0.01; - battle.planetTexture = getTexture(cJSON_GetObjectItem(root, "planet")->valuestring); - battle.planet.x = (SCREEN_WIDTH / 2) - (rand() % SCREEN_WIDTH) + (rand() % SCREEN_WIDTH); - battle.planet.y = (SCREEN_HEIGHT / 2) - (rand() % SCREEN_HEIGHT) + (rand() % SCREEN_HEIGHT); - SDL_QueryTexture(battle.planetTexture, NULL, NULL, &battle.planetWidth, &battle.planetHeight); - battle.planetWidth *= planetScale; - battle.planetHeight *= planetScale; + srand(hashcode(filename)); loadObjectives(cJSON_GetObjectItem(root, "objectives")); @@ -170,10 +161,33 @@ void loadMission(char *filename) initScript(cJSON_GetObjectItem(root, "script")); - free(text); + /* planet and background loading must come last, so AUTO works properly */ + + background = cJSON_GetObjectItem(root, "background")->valuestring; + if (strcmp(background, "AUTO") == 0) + { + background = getAutoBackground(filename); + } + battle.background = getTexture(background); + + planet = cJSON_GetObjectItem(root, "planet")->valuestring; + if (strcmp(planet, "AUTO") == 0) + { + planet = getAutoPlanet(filename); + } + planetScale = 75 + (rand() % 125); + planetScale *= 0.01; + battle.planetTexture = getTexture(planet); + battle.planet.x = (SCREEN_WIDTH / 2) - (rand() % SCREEN_WIDTH) + (rand() % SCREEN_WIDTH); + battle.planet.y = (SCREEN_HEIGHT / 2) - (rand() % SCREEN_HEIGHT) + (rand() % SCREEN_HEIGHT); + SDL_QueryTexture(battle.planetTexture, NULL, NULL, &battle.planetWidth, &battle.planetHeight); + battle.planetWidth *= planetScale; + battle.planetHeight *= planetScale; srand(time(NULL)); + free(text); + endSectionTransition(); /* only increment num missions / challenges started if there are some (Free Flight excluded, for example) */ @@ -201,6 +215,38 @@ void loadMission(char *filename) playMusic(music); } +static char *getAutoBackground(char *filename) +{ + int hash; + + if (!battle.challengeData.isChallenge) + { + hash = hashcode(game.selectedStarSystem); + } + else + { + hash = hashcode(filename); + } + + return getBackgroundTexture(hash); +} + +static char *getAutoPlanet(char *filename) +{ + int hash; + + if (!battle.challengeData.isChallenge) + { + hash = hashcode(game.selectedStarSystem); + } + else + { + hash = hashcode(filename); + } + + return getPlanetTexture(hash); +} + void completeMission(void) { if (battle.status == MS_IN_PROGRESS) diff --git a/src/galaxy/mission.h b/src/galaxy/mission.h index 742893e..faccf16 100644 --- a/src/galaxy/mission.h +++ b/src/galaxy/mission.h @@ -51,6 +51,8 @@ extern void initMissionInfo(void); extern char *getTranslatedString(char *string); extern void updateStarSystemMissions(void); extern void updateChallengeMissions(void); +extern char *getBackgroundTexture(int n); +extern char *getPlanetTexture(int n); extern Battle battle; extern Entity *player; diff --git a/src/system/init.c b/src/system/init.c index b66c6ae..2274910 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -132,7 +132,8 @@ void initGameSystem(void) initChallenges, initStats, initBattle, - initModalDialog + initModalDialog, + initBackground }; numInitFuns = sizeof(initFuncs) / sizeof(void*); @@ -290,6 +291,8 @@ void cleanup(void) destroyGalacticMap(); destroyWidgets(); + + destroyBackground(); TTF_Quit(); diff --git a/src/system/init.h b/src/system/init.h index fe79f25..f04014d 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -47,6 +47,7 @@ extern void initStats(void); extern void initStarSystems(void); extern void initChallenges(void); extern void initWidgets(void); +extern void initBackground(void); extern void destroyLookups(void); extern void destroyFonts(void); extern void destroySounds(void); @@ -60,6 +61,7 @@ extern void destroyBattle(void); extern void destroyTextures(void); extern void destroyGalacticMap(void); extern void destroyWidgets(void); +extern void destroyBackground(void); extern void expireTexts(int all); extern void initInput(void); extern void initModalDialog(void);