From 8a4ac55c909f7bcfe00356f94726d1c0ff265d0e Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Sun, 27 Nov 2022 22:17:56 +0100 Subject: [PATCH] TextureAtlas and some Loader Methods added --- Animation/animationHandler.h | 14 ++++++++++++++ Makefile | 7 +++++-- Textures/textureatlas.c | 34 ++++++++++++++++++++++++++++++++++ Textures/textureatlas.h | 7 ++++++- game.c | 12 +++++------- game.h | 2 +- 6 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 Animation/animationHandler.h create mode 100644 Textures/textureatlas.c diff --git a/Animation/animationHandler.h b/Animation/animationHandler.h new file mode 100644 index 0000000..b36c932 --- /dev/null +++ b/Animation/animationHandler.h @@ -0,0 +1,14 @@ +#ifndef ANIMATIONHANDLER_H_ +#define ANIMATIONHANDLER_H_ +#include "raylib.h" + + +struct AnimationHandler; + +typedef struct AnimationHandler{ + Texture2D *textures; + + +} AnimationHandler; + +#endif \ No newline at end of file diff --git a/Makefile b/Makefile index 9a013e4..34c9843 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ CC = gcc FLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -spiel: main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o game.o - $(CC) -o spiel main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o game.o $(FLAGS) +spiel: main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o game.o textureatlas.o + $(CC) -o spiel main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o game.o textureatlas.o $(FLAGS) main.o: main.c $(CC) -c main.c $(FLAGS) @@ -28,5 +28,8 @@ tile.o: IsometricMap/tile.c game.o: game.c $(CC) -c game.c $(FLAGS) +textureatlas.o: Textures/textureatlas.c + $(CC) -c Textures/textureatlas.c $(FLAGS) + clean: rm *.o spiel diff --git a/Textures/textureatlas.c b/Textures/textureatlas.c new file mode 100644 index 0000000..b2c3e4f --- /dev/null +++ b/Textures/textureatlas.c @@ -0,0 +1,34 @@ +#include "textureatlas.h" +#include "stdlib.h" +#include "raylib.h" + +TextureAtlas * TextureAtlasInit(){ + TextureAtlas *textures = (TextureAtlas *) malloc(sizeof(TextureAtlas)); + + LoadCursorTextures(textures->cursorTextures); + LoadWorkerTextures(textures->workerTextures); + + return textures; +} + +void LoadCursorTextures(Texture2D *cursorTextures){ + *cursorTextures = LoadTexture("assets/cursor.gif"); + *(cursorTextures + 1) = LoadTexture("assets/cursor_down.gif"); +} + +void LoadWorkerTextures(Texture2D *workerTextures){ + Image worker1flip = LoadImage("assets/worker/worker-1.png"); + ImageFlipHorizontal(&worker1flip); + Image worker2flip = LoadImage("assets/worker/worker-2.png"); + ImageFlipHorizontal(&worker2flip); + Image worker3flip = LoadImage("assets/worker/worker-3.png"); + ImageFlipHorizontal(&worker3flip); + textures->workerTextures[6] = LoadTexture("assets/worker/worker-0.png"); + textures->workerTextures[5] = LoadTexture("assets/worker/worker-1.png"); + textures->workerTextures[7] = LoadTextureFromImage(worker1flip); + textures->workerTextures[4] = LoadTexture("assets/worker/worker-2.png"); + textures->workerTextures[0] = LoadTextureFromImage(worker2flip); + textures->workerTextures[3] = LoadTexture("assets/worker/worker-3.png"); + textures->workerTextures[1] = LoadTextureFromImage(worker3flip); + textures->workerTextures[2] = LoadTexture("assets/worker/worker-4.png"); +} diff --git a/Textures/textureatlas.h b/Textures/textureatlas.h index 9c843ea..f7a442a 100644 --- a/Textures/textureatlas.h +++ b/Textures/textureatlas.h @@ -4,8 +4,13 @@ typedef struct TextureAtlas{ Texture2D cursorTextures[2]; - Texture2D workerTextures[7]; + Texture2D workerTextures[8][5]; //Texture2D[] mapTextures; } TextureAtlas; +// Initialize the full TextureAtlas struct with all Textures used in the game +TextureAtlas * TextureAtlasInit(); +void LoadCursorTextures(Texture2D *cursorTextures); +void LoadWorkerTextures(Texture2D *workerTextures); + #endif \ No newline at end of file diff --git a/game.c b/game.c index edf1d14..5636e1f 100644 --- a/game.c +++ b/game.c @@ -5,18 +5,16 @@ #include "List/list.h" #include "Input/inputHandler.h" #include "IsometricMap/isometricMap.h" +#include "Textures/textureatlas.h" #include "stdio.h" Game *GameInit() { Game *game = (Game *)malloc(sizeof(Game)); - game->cursorTextures[0] = LoadTexture("assets/cursor.gif"); - game->cursorTextures[1] = LoadTexture("assets/cursor_down.gif"); - - // game->cursorSprite = {&(cursorTextures[0]), 450, 225}; - // game->cursorSprite = (Sprite *) malloc(sizeof(Sprite)); - game->cursorSprite = SpriteCreate(game->cursorTextures, 450, 225); + game->textures = TextureAtlasInit(); + + game->cursorSprite = SpriteCreate(game->textures->cursorTextures, 450, 225); Image worker1flip = LoadImage("assets/worker/worker-1.png"); ImageFlipHorizontal(&worker1flip); @@ -44,7 +42,7 @@ Game *GameInit() game->inputHandler->cursorWorldTile.x = 0; game->inputHandler->cursorWorldTile.y = 0; game->inputHandler->selectedLayer = -1; - game->inputHandler->cursorTextures = game->cursorTextures; + game->inputHandler->cursorTextures = game->textures->cursorTextures; game->inputHandler->cursorSprite = game->cursorSprite; game->camera = (Camera2D *)malloc(sizeof(Camera2D)); diff --git a/game.h b/game.h index 3722fc5..190ac8e 100644 --- a/game.h +++ b/game.h @@ -5,8 +5,8 @@ // So kann man die includes umgehen, also keine Circular dependencies mehr :) typedef struct Game{ - Texture2D cursorTextures[2]; Texture2D worker[8]; + struct TextureAtlas *textures; struct Sprite *cursorSprite; struct List *sprites; struct InputHandler *inputHandler;