blobwarsAttrition/src/system/atlas.c

100 lines
2.2 KiB
C

/*
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 "atlas.h"
static void loadAtlasTexture(void);
static void loadAtlasData(void);
static Atlas atlasHead;
static Atlas *atlasTail;
static Texture *atlasTexture;
static int atlasSize;
void initAtlas(void)
{
memset(&atlasHead, 0, sizeof(Atlas));
atlasTail = &atlasHead;
loadAtlasTexture();
loadAtlasData();
dev.debug = dev.showFPS = 1;
}
Atlas *getImageFromAtlas(char *filename)
{
Atlas *a;
for (a = atlasHead.next ; a != NULL ; a = a->next)
{
if (strcmp(a->filename, filename) == 0)
{
return a;
}
}
return NULL;
}
static void loadAtlasTexture(void)
{
atlasTexture = getTexture("gfx/atlas/atlas.png");
SDL_QueryTexture(atlasTexture->texture, NULL, NULL, &atlasSize, &atlasSize);
}
static void loadAtlasData(void)
{
Atlas *atlas;
int x, y, w, h;
cJSON *root, *node;
char *text, *filename;
text = readFile("data/atlas/atlas.json");
root = cJSON_Parse(text);
for (node = cJSON_GetObjectItem(root, "images")->child ; node != NULL ; node = node->next)
{
filename = cJSON_GetObjectItem(node, "filename")->valuestring;
x = cJSON_GetObjectItem(node, "x")->valueint;
y = cJSON_GetObjectItem(node, "y")->valueint;
w = cJSON_GetObjectItem(node, "w")->valueint;
h = cJSON_GetObjectItem(node, "h")->valueint;
atlas = malloc(sizeof(Atlas));
memset(atlas, 0, sizeof(Atlas));
atlasTail->next = atlas;
atlasTail = atlas;
STRNCPY(atlas->filename, filename, MAX_FILENAME_LENGTH);
atlas->rect.x = x;
atlas->rect.y = y;
atlas->rect.w = w;
atlas->rect.h = h;
}
cJSON_Delete(root);
free(text);
}