Load sprites.

This commit is contained in:
Steve 2018-01-28 17:23:49 +00:00
parent 8bd0244a33
commit 73c0ef66b2
5 changed files with 84 additions and 9 deletions

View File

@ -370,9 +370,9 @@ struct Quadtree {
struct Sprite { struct Sprite {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
SDL_Rect frames[MAX_SPRITE_FRAMES]; int *times;
int times[MAX_SPRITE_FRAMES]; char **filenames;
char filenames[MAX_SPRITE_FRAMES][MAX_FILENAME_LENGTH]; SDL_Rect *frames;
int currentFrame; int currentFrame;
float currentTime; float currentTime;
int w; int w;

View File

@ -71,7 +71,8 @@ void initGameSystem(void)
initGraphics, initGraphics,
initFonts, initFonts,
initAtlas, initAtlas,
initSounds initSounds,
initSprites
}; };
numInitFuns = sizeof(initFuncs) / sizeof(void*); numInitFuns = sizeof(initFuncs) / sizeof(void*);

View File

@ -30,6 +30,7 @@ extern void initGraphics(void);
extern void initFonts(void); extern void initFonts(void);
extern void initAtlas(void); extern void initAtlas(void);
extern void initSounds(void); extern void initSounds(void);
extern void initSprites(void);
extern void destroyLookups(void); extern void destroyLookups(void);
extern void destroyFonts(void); extern void destroyFonts(void);
extern void destroyTextures(void); extern void destroyTextures(void);

View File

@ -21,10 +21,21 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "sprites.h" #include "sprites.h"
static void animateSprite(Sprite *s); static void animateSprite(Sprite *s);
static void loadGameSprites(void);
static void loadSpriteList(char *filename);
static void loadSprites(cJSON *root);
static Tuple spriteMapHead; static Tuple spriteMapHead;
static Sprite spriteHead; static Sprite spriteHead;
/*static Sprite *spriteTail;*/ static Sprite *spriteTail;
void initSprites(void)
{
memset(&spriteHead, 0, sizeof(Sprite));
spriteTail = &spriteHead;
loadGameSprites();
}
int getSpriteIndex(char *key) int getSpriteIndex(char *key)
{ {
@ -117,18 +128,78 @@ SDL_Rect getCurrentFrame(Sprite *s)
return s->frames[s->currentFrame]; 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]);
} }
void loadSpriteList(char *filename) free(filenames);
{
} }
void loadSprites(cJSON *root) 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);
}
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) void destroySprites(void)

View File

@ -22,3 +22,5 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../json/cJSON.h" #include "../json/cJSON.h"
extern float wrap(float value, float low, float high); extern float wrap(float value, float low, float high);
extern char *readFile(const char *filename);
char **getFileList(const char *dir, int *count);