Load sprites.
This commit is contained in:
parent
8bd0244a33
commit
73c0ef66b2
|
@ -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;
|
||||
|
|
|
@ -71,7 +71,8 @@ void initGameSystem(void)
|
|||
initGraphics,
|
||||
initFonts,
|
||||
initAtlas,
|
||||
initSounds
|
||||
initSounds,
|
||||
initSprites
|
||||
};
|
||||
|
||||
numInitFuns = sizeof(initFuncs) / sizeof(void*);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue