blobwarsAttrition/src/system/sprites.c

174 lines
3.5 KiB
C
Raw Permalink Normal View History

2018-01-22 08:23:23 +01:00
/*
2019-06-02 17:13:34 +02:00
Copyright (C) 2018-2019 Parallel Realities
2018-01-22 08:23:23 +01:00
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;
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
loadGameSprites();
}
2018-01-24 08:44:45 +01:00
Sprite *getSprite(char *name)
{
Sprite *s;
2019-06-02 17:13:34 +02:00
2018-01-24 08:44:45 +01:00
for (s = spriteHead.next ; s != NULL ; s = s->next)
{
if (strcmp(s->name, name) == 0)
{
return s;
}
}
2019-06-02 17:13:34 +02:00
2018-01-24 08:44:45 +01:00
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "No such sprite '%s'", name);
exit(1);
return NULL;
}
void animateSprites(void)
{
Sprite *s;
2019-06-02 17:13:34 +02:00
2018-01-24 08:44:45 +01:00
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);
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
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
}
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
cJSON_Delete(root);
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
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;
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
s = malloc(sizeof(Sprite));
memset(s, 0, sizeof(Sprite));
spriteTail->next = s;
spriteTail = s;
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
STRNCPY(s->name, cJSON_GetObjectItem(root, "name")->valuestring, MAX_NAME_LENGTH);
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
for (frame = cJSON_GetObjectItem(root, "frame")->child ; frame != NULL ; frame = frame->next)
{
s->numFrames++;
}
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
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);
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
i = 0;
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
for (frame = cJSON_GetObjectItem(root, "frame")->child ; frame != NULL ; frame = frame->next)
{
s->times[i] = cJSON_GetObjectItem(frame, "time")->valueint;
2019-06-02 17:13:34 +02:00
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));
2019-06-02 17:13:34 +02:00
2018-01-31 22:50:49 +01:00
s->frames[i] = getImageFromAtlas(filename);
2018-01-29 09:34:39 +01:00
}
2019-06-02 17:13:34 +02:00
2018-01-28 18:23:49 +01:00
i++;
}
2019-06-02 17:13:34 +02:00
2018-04-24 20:04:08 +02:00
s->w = s->frames[0]->rect.w;
s->h = s->frames[0]->rect.h;
2018-01-24 08:44:45 +01:00
}
void destroySprites(void)
{
}