From 73c0ef66b28d31cb67cf9f2343fd8d12e77041a1 Mon Sep 17 00:00:00 2001 From: Steve Date: Sun, 28 Jan 2018 17:23:49 +0000 Subject: [PATCH] Load sprites. --- src/structs.h | 6 ++-- src/system/init.c | 3 +- src/system/init.h | 1 + src/system/sprites.c | 81 +++++++++++++++++++++++++++++++++++++++++--- src/system/sprites.h | 2 ++ 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/structs.h b/src/structs.h index 01350f5..865f65c 100644 --- a/src/structs.h +++ b/src/structs.h @@ -370,9 +370,9 @@ struct Quadtree { struct Sprite { char name[MAX_NAME_LENGTH]; - SDL_Rect frames[MAX_SPRITE_FRAMES]; - int times[MAX_SPRITE_FRAMES]; - char filenames[MAX_SPRITE_FRAMES][MAX_FILENAME_LENGTH]; + int *times; + char **filenames; + SDL_Rect *frames; int currentFrame; float currentTime; int w; diff --git a/src/system/init.c b/src/system/init.c index 92f5145..3ff34a1 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -71,7 +71,8 @@ void initGameSystem(void) initGraphics, initFonts, initAtlas, - initSounds + initSounds, + initSprites }; numInitFuns = sizeof(initFuncs) / sizeof(void*); diff --git a/src/system/init.h b/src/system/init.h index 5269f37..b030ce2 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -30,6 +30,7 @@ extern void initGraphics(void); extern void initFonts(void); extern void initAtlas(void); extern void initSounds(void); +extern void initSprites(void); extern void destroyLookups(void); extern void destroyFonts(void); extern void destroyTextures(void); diff --git a/src/system/sprites.c b/src/system/sprites.c index 2e5acac..bfb253e 100644 --- a/src/system/sprites.c +++ b/src/system/sprites.c @@ -21,10 +21,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "sprites.h" static void animateSprite(Sprite *s); +static void loadGameSprites(void); +static void loadSpriteList(char *filename); +static void loadSprites(cJSON *root); static Tuple spriteMapHead; static Sprite spriteHead; -/*static Sprite *spriteTail;*/ +static Sprite *spriteTail; + +void initSprites(void) +{ + memset(&spriteHead, 0, sizeof(Sprite)); + spriteTail = &spriteHead; + + loadGameSprites(); +} int getSpriteIndex(char *key) { @@ -117,18 +128,78 @@ SDL_Rect getCurrentFrame(Sprite *s) return s->frames[s->currentFrame]; } -void loadGameSprites(void) +static void loadGameSprites(void) { - + char **filenames; + char path[MAX_FILENAME_LENGTH]; + int count, i; + + filenames = getFileList("data/sprites", &count); + + for (i = 0 ; i < count ; i++) + { + sprintf(path, "data/sprites/%s", filenames[i]); + + loadSpriteList(path); + + free(filenames[i]); + } + + free(filenames); } -void loadSpriteList(char *filename) +static void loadSpriteList(char *filename) { + cJSON *root, *node; + char *text; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename); + + text = readFile(filename); + root = cJSON_Parse(text); + + for (node = root->child ; node != NULL ; node = node->next) + { + loadSprites(node); + } + + cJSON_Delete(root); + + free(text); } -void loadSprites(cJSON *root) +static void loadSprites(cJSON *root) { + Sprite *s; + cJSON *frame; + int i; + s = malloc(sizeof(Sprite)); + memset(s, 0, sizeof(Sprite)); + spriteTail->next = s; + spriteTail = s; + + STRNCPY(s->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH); + + for (frame = cJSON_GetObjectItem(root, "frame")->child ; frame != NULL ; frame = frame->next) + { + s->numFrames++; + } + + s->times = malloc(sizeof(int) * s->numFrames); + s->filenames = malloc(sizeof(char*) * s->numFrames); + + i = 0; + + for (frame = cJSON_GetObjectItem(root, "frame")->child ; frame != NULL ; frame = frame->next) + { + 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); + + i++; + } } void destroySprites(void) diff --git a/src/system/sprites.h b/src/system/sprites.h index a9c66cb..81c6351 100644 --- a/src/system/sprites.h +++ b/src/system/sprites.h @@ -22,3 +22,5 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../json/cJSON.h" extern float wrap(float value, float low, float high); +extern char *readFile(const char *filename); +char **getFileList(const char *dir, int *count);