blobwarsAttrition/src/system/atlas.c

102 lines
2.2 KiB
C
Raw Normal View History

2018-01-24 09:43:08 +01:00
/*
2019-06-02 17:13:34 +02:00
Copyright (C) 2018-2019 Parallel Realities
2018-01-24 09:43:08 +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 "atlas.h"
static void loadAtlasData(void);
2018-02-19 20:14:03 +01:00
static Atlas atlases[NUM_ATLAS_BUCKETS];
2018-01-24 09:43:08 +01:00
void initAtlas(void)
{
2018-02-19 20:14:03 +01:00
memset(&atlases, 0, sizeof(Atlas) * NUM_ATLAS_BUCKETS);
2018-01-24 09:43:08 +01:00
loadAtlasData();
}
Atlas *getImageFromAtlas(char *filename)
{
Atlas *a;
2018-02-19 20:14:03 +01:00
unsigned long i;
i = hashcode(filename) % NUM_ATLAS_BUCKETS;
2019-06-02 17:13:34 +02:00
2018-02-19 20:14:03 +01:00
for (a = atlases[i].next ; a != NULL ; a = a->next)
2018-01-24 09:43:08 +01:00
{
if (strcmp(a->filename, filename) == 0)
{
return a;
}
}
2019-06-02 17:13:34 +02:00
2018-03-13 09:24:41 +01:00
if (!strstr(filename, "/tiles/"))
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "No such atlas image '%s'", filename);
}
2019-06-02 17:13:34 +02:00
2018-01-31 09:08:41 +01:00
return NULL;
2018-01-24 09:43:08 +01:00
}
static void loadAtlasData(void)
{
2018-02-19 20:14:03 +01:00
Atlas *atlas, *a;
2018-01-28 17:14:17 +01:00
int x, y, w, h;
2018-01-28 13:01:39 +01:00
cJSON *root, *node;
2018-01-28 17:14:17 +01:00
char *text, *filename;
2018-02-19 20:14:03 +01:00
unsigned long i;
2019-06-02 17:13:34 +02:00
2018-01-28 13:01:39 +01:00
text = readFile("data/atlas/atlas.json");
2018-01-24 09:43:08 +01:00
2018-01-28 13:01:39 +01:00
root = cJSON_Parse(text);
2019-06-02 17:13:34 +02:00
2018-02-15 08:59:59 +01:00
for (node = root->child ; node != NULL ; node = node->next)
2018-01-28 13:01:39 +01:00
{
2018-01-28 17:14:17 +01:00
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;
2018-02-19 20:14:03 +01:00
i = hashcode(filename) % NUM_ATLAS_BUCKETS;
a = &atlases[i];
/* horrible bit to look for the tail */
while (a->next)
{
a = a->next;
}
2019-06-02 17:13:34 +02:00
2018-01-28 17:14:17 +01:00
atlas = malloc(sizeof(Atlas));
memset(atlas, 0, sizeof(Atlas));
2018-02-19 20:14:03 +01:00
a->next = atlas;
2019-06-02 17:13:34 +02:00
2018-01-28 17:14:17 +01:00
STRNCPY(atlas->filename, filename, MAX_FILENAME_LENGTH);
atlas->rect.x = x;
atlas->rect.y = y;
atlas->rect.w = w;
atlas->rect.h = h;
2018-01-28 13:01:39 +01:00
}
2019-06-02 17:13:34 +02:00
2018-01-28 13:01:39 +01:00
cJSON_Delete(root);
2019-06-02 17:13:34 +02:00
2018-01-28 13:01:39 +01:00
free(text);
2018-01-24 09:43:08 +01:00
}