diff --git a/src/defs.h b/src/defs.h index fa637e2..fbe2e59 100644 --- a/src/defs.h +++ b/src/defs.h @@ -59,6 +59,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MAX_FILENAME_LENGTH 1024 #define NUM_TEXTURE_BUCKETS 32 +#define NUM_ATLAS_BUCKETS 64 #define MAP_WIDTH 200 #define MAP_HEIGHT 200 diff --git a/src/system/atlas.c b/src/system/atlas.c index 2ee9405..7aaa6b7 100644 --- a/src/system/atlas.c +++ b/src/system/atlas.c @@ -20,31 +20,25 @@ 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; +static Atlas atlases[NUM_ATLAS_BUCKETS]; void initAtlas(void) { - memset(&atlasHead, 0, sizeof(Atlas)); - atlasTail = &atlasHead; - - loadAtlasTexture(); + memset(&atlases, 0, sizeof(Atlas) * NUM_ATLAS_BUCKETS); loadAtlasData(); - - dev.debug = dev.showFPS = 1; } Atlas *getImageFromAtlas(char *filename) { Atlas *a; + unsigned long i; + + i = hashcode(filename) % NUM_ATLAS_BUCKETS; - for (a = atlasHead.next ; a != NULL ; a = a->next) + for (a = atlases[i].next ; a != NULL ; a = a->next) { if (strcmp(a->filename, filename) == 0) { @@ -55,19 +49,13 @@ Atlas *getImageFromAtlas(char *filename) 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; + Atlas *atlas, *a; int x, y, w, h; cJSON *root, *node; char *text, *filename; + unsigned long i; text = readFile("data/atlas/atlas.json"); @@ -80,11 +68,20 @@ static void loadAtlasData(void) y = cJSON_GetObjectItem(node, "y")->valueint; w = cJSON_GetObjectItem(node, "w")->valueint; h = cJSON_GetObjectItem(node, "h")->valueint; + + i = hashcode(filename) % NUM_ATLAS_BUCKETS; + + a = &atlases[i]; + + /* horrible bit to look for the tail */ + while (a->next) + { + a = a->next; + } atlas = malloc(sizeof(Atlas)); memset(atlas, 0, sizeof(Atlas)); - atlasTail->next = atlas; - atlasTail = atlas; + a->next = atlas; STRNCPY(atlas->filename, filename, MAX_FILENAME_LENGTH); atlas->rect.x = x; diff --git a/src/system/atlas.h b/src/system/atlas.h index a190743..b5c37e3 100644 --- a/src/system/atlas.h +++ b/src/system/atlas.h @@ -23,5 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern Texture *getTexture(const char *filename); extern char *readFile(const char *filename); +extern unsigned long hashcode(const char *str); extern Dev dev;