From 7f237e3c8f5727610c58c659e75a223d08d4cca0 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 29 Jan 2018 08:34:39 +0000 Subject: [PATCH] Start of world loading. --- common.mk | 2 +- src/entities/entities.c | 2 +- src/structs.h | 9 ++- src/system/lookup.c | 3 +- src/system/sprites.c | 15 +++-- src/test/atlasTest.c | 2 + src/test/atlasTest.h | 1 + src/world/worldLoader.c | 145 ++++++++++++++++++++++++++++++++++++++++ src/world/worldLoader.h | 30 +++++++++ 9 files changed, 200 insertions(+), 9 deletions(-) create mode 100644 src/world/worldLoader.c create mode 100644 src/world/worldLoader.h diff --git a/common.mk b/common.mk index bbd5bd1..969be5a 100644 --- a/common.mk +++ b/common.mk @@ -46,7 +46,7 @@ OBJS += quadtree.o OBJS += sound.o sprites.o OBJS += tankCommander.o tankTrack.o teeka.o teleporter.o text.o textures.o title.o triggers.o OBJS += unit.o util.o -OBJS += weapons.o weaponPickup.o widgets.o world.o +OBJS += weapons.o weaponPickup.o widgets.o world.o worldLoader.o # top-level rule to create the program. all: $(PROG) $(LOCALE_MO) diff --git a/src/entities/entities.c b/src/entities/entities.c index 3c511da..18af66e 100644 --- a/src/entities/entities.c +++ b/src/entities/entities.c @@ -87,7 +87,7 @@ Entity *createEntity(int type) world.entityTail = e; e->type = type; - e->uniqueId = game.entityCounter++; + e->uniqueId = world.entityCounter++; return e; } diff --git a/src/structs.h b/src/structs.h index 865f65c..745aba1 100644 --- a/src/structs.h +++ b/src/structs.h @@ -334,7 +334,6 @@ typedef struct { int enemiesKilled; int missionsPlayed; long timePlayed; - unsigned long entityCounter; char worldId[MAX_NAME_LENGTH]; int isResumingMission; int isComplete; @@ -410,7 +409,15 @@ struct Bullet { typedef struct { char id[MAX_NAME_LENGTH]; + char name[MAX_NAME_LENGTH]; + int numEnemyTypes; + char **enemyTypes; + char tileset[MAX_NAME_LENGTH]; + char music[MAX_FILENAME_LENGTH]; + char background[MAX_FILENAME_LENGTH]; int state; + int minEnemySpawnTime, maxEnemySpawnTime; + unsigned long entityCounter; Bob *bob; Boss *boss; Map map; diff --git a/src/system/lookup.c b/src/system/lookup.c index 6423d88..6721e77 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -30,7 +30,8 @@ void initLookups(void) memset(&head, 0, sizeof(Lookup)); tail = &head; - addLookup("DUMMY", 0); + addLookup("FACING_LEFT", FACING_LEFT); + addLookup("FACING_RIGHT", FACING_RIGHT); } static void addLookup(const char *name, long value) diff --git a/src/system/sprites.c b/src/system/sprites.c index bfb253e..f63ed38 100644 --- a/src/system/sprites.c +++ b/src/system/sprites.c @@ -23,7 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static void animateSprite(Sprite *s); static void loadGameSprites(void); static void loadSpriteList(char *filename); -static void loadSprites(cJSON *root); +void loadSprite(cJSON *root); static Tuple spriteMapHead; static Sprite spriteHead; @@ -160,7 +160,7 @@ static void loadSpriteList(char *filename) for (node = root->child ; node != NULL ; node = node->next) { - loadSprites(node); + loadSprite(node); } cJSON_Delete(root); @@ -168,10 +168,11 @@ static void loadSpriteList(char *filename) free(text); } -static void loadSprites(cJSON *root) +void loadSprite(cJSON *root) { Sprite *s; cJSON *frame; + char *filename; int i; s = malloc(sizeof(Sprite)); @@ -195,8 +196,12 @@ static void loadSprites(cJSON *root) { s->times[i] = cJSON_GetObjectItem(frame, "time")->valueint; - s->filenames[i] = malloc(MAX_NAME_LENGTH); - STRNCPY(s->filenames[i], cJSON_GetObjectItem(frame, "content")->valuestring, MAX_NAME_LENGTH); + if (cJSON_GetObjectItem(frame, "content") != NULL) + { + filename = cJSON_GetObjectItem(frame, "content")->valuestring; + s->filenames[i] = malloc(strlen(filename) + 1); + STRNCPY(s->filenames[i], filename, strlen(filename)); + } i++; } diff --git a/src/test/atlasTest.c b/src/test/atlasTest.c index be1a875..334e8e3 100644 --- a/src/test/atlasTest.c +++ b/src/test/atlasTest.c @@ -39,6 +39,8 @@ void initAtlasTest(void) atlasTexture = getTexture("gfx/atlas/atlas.png"); loadMapData("data/maps/raw/beachApproach.raw"); + + loadWorld("data/maps/underground2.json"); } static void logic(void) diff --git a/src/test/atlasTest.h b/src/test/atlasTest.h index 88e2a51..0c89f18 100644 --- a/src/test/atlasTest.h +++ b/src/test/atlasTest.h @@ -26,5 +26,6 @@ extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int extern void loadMapData(char *filename); extern void initGame(void); extern void initHub(void); +extern void loadWorld(char *filename); extern App app; diff --git a/src/world/worldLoader.c b/src/world/worldLoader.c new file mode 100644 index 0000000..8451a5c --- /dev/null +++ b/src/world/worldLoader.c @@ -0,0 +1,145 @@ +/* +Copyright (C) 2018 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 "worldLoader.h" + +static void loadEnemyTypes(char *enemyTypes); +static void loadTriggers(cJSON *root); +static void loadSprites(cJSON *root); +static void loadBob(cJSON *root); +static void loadEntities(cJSON *root); +static void loadObjectives(cJSON *root); + +void loadWorld(char *filename) +{ + cJSON *root; + char *text; + + memset(&world, 0, sizeof(World)); + + world.entityTail = &world.entityHead; + world.triggerTail = &world.triggerHead; + world.objectiveTail = &world.objectiveHead; + + text = readFile(filename); + + root = cJSON_Parse(text); + + STRNCPY(world.id, cJSON_GetObjectItem(root, "id")->valuestring, MAX_NAME_LENGTH); + STRNCPY(world.name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); + world.minEnemySpawnTime = cJSON_GetObjectItem(root, "minEnemySpawnTime")->valueint; + world.maxEnemySpawnTime = cJSON_GetObjectItem(root, "maxEnemySpawnTime")->valueint; + STRNCPY(world.music, cJSON_GetObjectItem(root, "music")->valuestring, MAX_FILENAME_LENGTH); + STRNCPY(world.tileset, cJSON_GetObjectItem(root, "tileset")->valuestring, MAX_NAME_LENGTH); + STRNCPY(world.background, cJSON_GetObjectItem(root, "background")->valuestring, MAX_FILENAME_LENGTH); + world.entityCounter = cJSON_GetObjectItem(root, "entityCounter")->valueint; + + loadEnemyTypes(cJSON_GetObjectItem(root, "enemyTypes")->valuestring); + + loadTriggers(cJSON_GetObjectItem(root, "triggers")); + + loadSprites(cJSON_GetObjectItem(root, "sprites")); + + loadBob(cJSON_GetObjectItem(root, "bob")); + + loadEntities(cJSON_GetObjectItem(root, "entities")); + + loadObjectives(cJSON_GetObjectItem(root, "objectives")); +} + +static void loadEnemyTypes(char *enemyTypes) +{ + int i; + char *enemyType; + + for (i = 0 ; i < strlen(enemyTypes) ; i++) + { + if (enemyTypes[i] == '|') + { + world.numEnemyTypes++; + } + } + + world.enemyTypes = malloc(world.numEnemyTypes * sizeof(char*)); + + i = 0; + enemyType = strtok(enemyTypes, "|"); + while (enemyType) + { + world.enemyTypes[i] = malloc(strlen(enemyType) + 1); + strcpy(world.enemyTypes[i], enemyType); + + enemyType = strtok(NULL, "|"); + + i++; + } +} + +static void loadTriggers(cJSON *root) +{ + cJSON *node; + Trigger *t; + + for (node = root->child ; node != NULL ; node = node->next) + { + t = malloc(sizeof(Trigger)); + memset(t, 0, sizeof(Trigger)); + world.triggerTail->next = t; + world.triggerTail = t; + + STRNCPY(t->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(t->targetNames, cJSON_GetObjectItem(node, "targetNames")->valuestring, MAX_DESCRIPTION_LENGTH); + STRNCPY(t->message, cJSON_GetObjectItem(node, "message")->valuestring, MAX_DESCRIPTION_LENGTH); + t->x = cJSON_GetObjectItem(node, "x")->valueint; + t->y = cJSON_GetObjectItem(node, "y")->valueint; + t->w = cJSON_GetObjectItem(node, "w")->valueint; + t->h = cJSON_GetObjectItem(node, "h")->valueint; + } +} + +static void loadSprites(cJSON *root) +{ + cJSON *node; + + for (node = root->child ; node != NULL ; node = node->next) + { + loadSprite(node); + } +} + +static void loadBob(cJSON *root) +{ + Bob *b; + + b = (Bob*)createEntity(ET_BOB); + b->x = cJSON_GetObjectItem(root, "x")->valueint; + b->y = cJSON_GetObjectItem(root, "y")->valueint; + b->facing = lookup(cJSON_GetObjectItem(root, "facing")->valuestring); + + initBob(b); +} + +static void loadEntities(cJSON *root) +{ +} + +static void loadObjectives(cJSON *root) +{ +} diff --git a/src/world/worldLoader.h b/src/world/worldLoader.h new file mode 100644 index 0000000..3b97654 --- /dev/null +++ b/src/world/worldLoader.h @@ -0,0 +1,30 @@ +/* +Copyright (C) 2018 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" +#include "../json/cJSON.h" + +extern char *readFile(const char *filename); +extern void loadSprite(cJSON *root); +extern Entity *createEntity(int type); +extern long lookup(const char *name); +extern void initBob(Bob *b); + +extern World world;