#include "textureatlas.h" #include "stdlib.h" #include "raylib.h" #include "string.h" #include "stdio.h" TextureAtlas * TextureAtlasInit(){ TextureAtlas *new = malloc(sizeof(TextureAtlas)); TextureAtlasLoadTextures(new->textures); //LoadWorkerAnimations(textures->workerAnimations, textures->workerTextures); TextureAtlasLoadAnimations(new->animations, new->textures); return new; } void TextureAtlasLoadTextures(Texture2D **textures){ int i; for(i = 0; i < TE_AMOUNT; i++){ switch(i){ case TE_CURSOR: textures[TE_CURSOR] = malloc(2 * sizeof(Texture2D)); LoadCursorTextures(textures[TE_CURSOR]); break; case TE_WORKER: textures[TE_WORKER] = malloc(TE_ENTITY_LENGTH * sizeof(Texture2D)); LoadEntityTextures(textures[TE_WORKER], "worker"); break; case TE_BUILDING: textures[TE_BUILDING] = malloc(TE_MAPOBJECT_LENGTH * sizeof(Texture2D)); LoadMapObjectTextures(textures[TE_BUILDING], "building"); break; case TE_BAUSTELLE: textures[TE_BAUSTELLE] = malloc(TE_MAPOBJECT_LENGTH * sizeof(Texture2D)); LoadMapObjectTextures(textures[TE_BAUSTELLE], "baustelle"); break; case TE_PINETREE: textures[TE_PINETREE] = malloc(TE_MAPOBJECT_LENGTH * sizeof(Texture2D)); LoadMapObjectTextures(textures[TE_PINETREE], "pinetree"); break; case TE_LUMBERJACK: textures[TE_LUMBERJACK] = malloc(TE_ENTITY_LENGTH * sizeof(Texture2D)); LoadEntityTextures(textures[TE_LUMBERJACK], "lumberjack"); break; case TE_SELECTABLE_BACKGROUND: textures[TE_SELECTABLE_BACKGROUND] = malloc(TE_SELECTABLE_BACKGROUND_LENGTH * sizeof(Texture2D)); LoadSelectableBackgroundTextures(textures[TE_SELECTABLE_BACKGROUND]); break; case TE_BUTTON: textures[TE_BUTTON] = malloc(TE_BUTTON_LENGTH * sizeof(Texture2D)); LoadButtonTextures(textures[TE_BUTTON]); break; case TE_TILES: textures[TE_TILES] = malloc(TE_TILES_LENGTH * sizeof(Texture2D)); LoadTileTextures(textures[TE_TILES]); break; default: printf("WARNING: TEXTUREATLAS TRIED LOADING NON DEFINED TEXTUREID!!\n"); } } } void LoadEntityTextures(Texture2D *atlasrow, char *directoryPrefix){ // pathToFile wird erstellt. z.B. assets/entities/ + worker ---> assets/entities/worker/ char pathToFile[50] = "assets/entities/"; strcat(pathToFile, directoryPrefix); strcat(pathToFile, "/"); char animation[30] = "walk"; char filename[100] = ""; char number[3] = ""; char ending[5] = ".png"; // Since some Images need to be flipped, we have fewer pngs than textures later loaded. i counts textures // file counts the filename and is incremented manually when needed int i; int file = 0; for(i = 0; i < TE_ENTITY_LENGTH; i++){ // Concatenate the correct string for the filename assets/worker/ + walk + - + 5 + .png sprintf(number, "%d", file); strcat(filename, pathToFile); strcat(filename, animation); strcat(filename, "-"); strcat(filename, number); strcat(filename, ending); //printf("file:%s:file\n", filename); // Set correct values for next iteration int lol = i % 8; if(lol == 0){ atlasrow[i] = LoadTexture(filename); printf("\n"); file++; } else if(lol == 2){ Image tmp = LoadImage(filename); ImageFlipHorizontal(&tmp); atlasrow[i] = LoadTextureFromImage(tmp); printf("flipped\n"); file++; } else if(lol == 4){ Image tmp = LoadImage(filename); ImageFlipHorizontal(&tmp); atlasrow[i] = LoadTextureFromImage(tmp); printf("flipped\n"); file++; } else if(lol == 6){ Image tmp = LoadImage(filename); ImageFlipHorizontal(&tmp); atlasrow[i] = LoadTextureFromImage(tmp); printf("flipped\n"); file++; } else if(lol == 7){ atlasrow[i] = LoadTexture(filename); printf("\n"); file++; } else{ atlasrow[i] = LoadTexture(filename); printf("\n"); } strcpy(filename, ""); strcpy(number, ""); if(i == 39){ strcpy(animation, "arbeiten"); file = 0; } else if(i == 79){ strcpy(animation, "die"); file = 0; } } } void LoadMapObjectTextures(Texture2D *atlasrow, char *directoryPrefix){ // pathToFile wird erstellt. z.B. assets/mapobjects/ + building ---> assets/mapobjects/building/ char pathToFile[50] = "assets/mapobjects/"; strcat(pathToFile, directoryPrefix); strcat(pathToFile, "/"); //char animation[30] = "walk"; char filename[100] = ""; //char number[3] = ""; char ending[5] = ".png"; strcat(filename, pathToFile); strcat(filename, "idle"); strcat(filename, ending); atlasrow[0] = LoadTexture(filename); } void LoadSelectableBackgroundTextures(Texture2D *atlasrow){ atlasrow[0] = LoadTexture("assets/selectable_background.png"); atlasrow[1] = LoadTexture("assets/selectable_background_hovered.png"); atlasrow[2] = LoadTexture("assets/selectable_background_selected.png"); } void LoadButtonTextures(Texture2D *atlasrow){ atlasrow[0] = LoadTexture("assets/button.png"); atlasrow[1] = LoadTexture("assets/button_hovered.png"); atlasrow[2] = LoadTexture("assets/button_pressed.png"); } void LoadTileTextures(Texture2D *atlasrow){ int counter = 0; atlasrow[counter++] = LoadTexture("assets/tiles/grass.png"); atlasrow[counter++] = LoadTexture("assets/tiles/desert.png"); atlasrow[counter++] = LoadTexture("assets/tiles/desert_palm.png"); atlasrow[counter++] = LoadTexture("assets/tiles/tower.png"); atlasrow[counter++] = LoadTexture("assets/tiles/bigtower.png"); atlasrow[counter++] = LoadTexture("assets/tiles/grass_selected.png"); atlasrow[counter++] = LoadTexture("assets/tiles/ice.png"); atlasrow[counter++] = LoadTexture("assets/tiles/water.png"); atlasrow[counter++] = LoadTexture("assets/tiles/empty.png"); atlasrow[counter++] = LoadTexture("assets/tiles/water2.png"); atlasrow[counter++] = LoadTexture("assets/tiles/water3.png"); atlasrow[counter++] = LoadTexture("assets/tiles/water4.png"); } void LoadCursorTextures(Texture2D *cursorTextures){ cursorTextures[0] = LoadTexture("assets/cursor.gif"); cursorTextures[1] = LoadTexture("assets/cursor_down.gif"); } void TextureAtlasLoadAnimations(Animation ****animations, Texture2D **textures){ int i; for(i = 0; i < AN_AMOUNT; i++){ switch(i){ case AN_WORKER: animations[AN_WORKER] = malloc(AN_ENTITY_AMOUNT * sizeof(Animation *)); LoadEntityAnimations(animations[AN_WORKER], textures[TE_WORKER], AN_WORKER); break; case AN_LUMBERJACK: animations[AN_LUMBERJACK] = malloc(AN_ENTITY_AMOUNT * sizeof(Animation *)); LoadEntityAnimations(animations[AN_LUMBERJACK], textures[TE_LUMBERJACK], AN_LUMBERJACK); break; default: printf("WARNING: TEXTUREATLAS TRIED LOADING NON DEFINED ANIMATION!!\n"); } } } void LoadEntityAnimations(Animation ***animationsrow, Texture2D *atlasrow, int id){ int i; int j; int frame; for(i = 0; i < AN_ENTITY_AMOUNT; i++){ animationsrow[i] = malloc(DIRECTIONS_AMOUNT * sizeof(Animation)); int obergrenze; if(i == AN_ENTITY_DIE){ obergrenze = 3; } else{ obergrenze = 5; } for(j = 0; j < DIRECTIONS_AMOUNT; j++){ Animation *newAnimation = AnimationInit(); frame = i * DIRECTIONS_AMOUNT * 5 + j; int frameCounter; for(frameCounter = 0; frameCounter < obergrenze; frameCounter++){ AnimationInsertBack(newAnimation, (atlasrow + frame)); frame += 8; } animationsrow[i][j] = newAnimation; } } } void LoadWorkerAnimations(Animation **workerAnimations, Texture2D *workerTextures){ int i; int j; int frame = 0; for(i=0; i < 24; i++){ frame = i; Animation *newAnimation = AnimationInit(); int obergrenze; if(frame <= 79){ obergrenze = 5; } else{ obergrenze = 3; } for(j = 0; j < obergrenze; j++){ AnimationInsertBack(newAnimation, (workerTextures+frame)); frame += 8; } workerAnimations[i] = newAnimation; } }