2018-01-22 08:23:23 +01:00
|
|
|
/*
|
|
|
|
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 "sprites.h"
|
|
|
|
|
2018-01-24 08:44:45 +01:00
|
|
|
static void animateSprite(Sprite *s);
|
2018-01-28 18:23:49 +01:00
|
|
|
static void loadGameSprites(void);
|
|
|
|
static void loadSpriteList(char *filename);
|
2018-01-29 09:34:39 +01:00
|
|
|
void loadSprite(cJSON *root);
|
2018-01-24 08:44:45 +01:00
|
|
|
|
|
|
|
static Sprite spriteHead;
|
2018-01-28 18:23:49 +01:00
|
|
|
static Sprite *spriteTail;
|
|
|
|
|
|
|
|
void initSprites(void)
|
|
|
|
{
|
|
|
|
memset(&spriteHead, 0, sizeof(Sprite));
|
|
|
|
spriteTail = &spriteHead;
|
|
|
|
|
|
|
|
loadGameSprites();
|
|
|
|
}
|
2018-01-24 08:44:45 +01:00
|
|
|
|
|
|
|
Sprite *getSprite(char *name)
|
|
|
|
{
|
|
|
|
Sprite *s;
|
|
|
|
|
|
|
|
for (s = spriteHead.next ; s != NULL ; s = s->next)
|
|
|
|
{
|
|
|
|
if (strcmp(s->name, name) == 0)
|
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "No such sprite '%s'", name);
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void animateSprites(void)
|
|
|
|
{
|
|
|
|
Sprite *s;
|
|
|
|
|
|
|
|
for (s = spriteHead.next ; s != NULL ; s = s->next)
|
|
|
|
{
|
|
|
|
animateSprite(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void animateSprite(Sprite *s)
|
|
|
|
{
|
|
|
|
if (s->currentTime != -1)
|
|
|
|
{
|
|
|
|
s->currentTime--;
|
|
|
|
|
|
|
|
if (s->currentTime <= 0)
|
|
|
|
{
|
2018-02-03 00:01:51 +01:00
|
|
|
s->currentFrame = wrap(s->currentFrame + 1, 0, s->numFrames - 1);
|
2018-01-24 08:44:45 +01:00
|
|
|
s->currentTime = s->times[s->currentFrame];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-17 17:53:30 +01:00
|
|
|
SDL_Rect *getCurrentFrame(Sprite *s)
|
2018-01-24 08:44:45 +01:00
|
|
|
{
|
2018-02-17 17:53:30 +01:00
|
|
|
return &s->frames[s->currentFrame]->rect;
|
2018-01-24 08:44:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:23:49 +01:00
|
|
|
static void loadGameSprites(void)
|
2018-01-24 08:44:45 +01:00
|
|
|
{
|
2018-01-28 18:23:49 +01:00
|
|
|
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);
|
2018-01-24 08:44:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:23:49 +01:00
|
|
|
static void loadSpriteList(char *filename)
|
2018-01-24 08:44:45 +01:00
|
|
|
{
|
2018-01-28 18:23:49 +01:00
|
|
|
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)
|
|
|
|
{
|
2018-01-29 09:34:39 +01:00
|
|
|
loadSprite(node);
|
2018-01-28 18:23:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cJSON_Delete(root);
|
|
|
|
|
|
|
|
free(text);
|
2018-01-24 08:44:45 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 09:34:39 +01:00
|
|
|
void loadSprite(cJSON *root)
|
2018-01-24 08:44:45 +01:00
|
|
|
{
|
2018-01-28 18:23:49 +01:00
|
|
|
Sprite *s;
|
|
|
|
cJSON *frame;
|
2018-01-29 09:34:39 +01:00
|
|
|
char *filename;
|
2018-01-28 18:23:49 +01:00
|
|
|
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);
|
2018-01-31 22:50:49 +01:00
|
|
|
s->frames = malloc(sizeof(Atlas*) * s->numFrames);
|
2018-01-24 08:44:45 +01:00
|
|
|
|
2018-01-28 18:23:49 +01:00
|
|
|
i = 0;
|
|
|
|
|
|
|
|
for (frame = cJSON_GetObjectItem(root, "frame")->child ; frame != NULL ; frame = frame->next)
|
|
|
|
{
|
|
|
|
s->times[i] = cJSON_GetObjectItem(frame, "time")->valueint;
|
|
|
|
|
2018-01-29 09:34:39 +01:00
|
|
|
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));
|
2018-01-31 22:50:49 +01:00
|
|
|
|
|
|
|
s->frames[i] = getImageFromAtlas(filename);
|
2018-01-29 09:34:39 +01:00
|
|
|
}
|
2018-01-28 18:23:49 +01:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
2018-01-24 08:44:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroySprites(void)
|
|
|
|
{
|
|
|
|
}
|