diff --git a/.vscode/settings.json b/.vscode/settings.json index 6c2e911..66e1870 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "isometricrenderer.h": "c", "sprite.h": "c", "map": "c", - "isometricmap.h": "c" + "isometricmap.h": "c", + "animationhandler.h": "c" } } \ No newline at end of file diff --git a/Input/inputHandler.c b/Input/inputHandler.c index 02720f3..0a49b35 100644 --- a/Input/inputHandler.c +++ b/Input/inputHandler.c @@ -123,7 +123,7 @@ void mouseInput(Game *game){ // Add Sprite if(width + height <= 1){ - ListInsertBack(sprites, SpriteCreate(texture, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); + ListInsertBack(sprites, SpriteCreate(game->textures, 1, inputHandler->cursorWorldPos.x, inputHandler->cursorWorldPos.y)); } // Berechnung, welche Sprites ausgewählt wurden, scuffed diff --git a/List/list.c b/List/list.c index 3e480e9..d4ab057 100644 --- a/List/list.c +++ b/List/list.c @@ -114,6 +114,8 @@ void ListActAllSprites(Game *game){ } } + SpriteUpdateAnimation(¤t->data); + current = current->next; } diff --git a/Makefile b/Makefile index 34c9843..05a12fa 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 textureatlas.o - $(CC) -o spiel main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o game.o textureatlas.o $(FLAGS) +spiel: main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o game.o textureatlas.o animation.o animationHandler.o + $(CC) -o spiel main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o game.o textureatlas.o animation.o animationHandler.o $(FLAGS) main.o: main.c $(CC) -c main.c $(FLAGS) @@ -31,5 +31,11 @@ game.o: game.c textureatlas.o: Textures/textureatlas.c $(CC) -c Textures/textureatlas.c $(FLAGS) +animation.o: Textures/animation.c + $(CC) -c Textures/animation.c $(FLAGS) + +animationHandler.o: Textures/animationHandler.c + $(CC) -c Textures/animationHandler.c $(FLAGS) + clean: rm *.o spiel diff --git a/Textures/animation.c b/Textures/animation.c index d0843a9..61f9eed 100644 --- a/Textures/animation.c +++ b/Textures/animation.c @@ -23,37 +23,58 @@ AnimationFrame * AnimationFrameCreate(Texture2D *texture){ void AnimationInsertFront(Animation *animation, Texture2D *texture){ AnimationFrame *new = AnimationFrameCreate(texture); - if(list->head == 0){ - list->head = new; - list->tail = new; + if(animation->head == 0){ + animation->head = new; + animation->tail = new; + + animation->head->prev = animation->tail; + animation->tail->next = animation->head->prev; + } - else if(list->head == list->tail){ - list->head = new; - list->head->next = list->tail; - list->tail->prev = list->head; + else if(animation->head == animation->tail){ + animation->head = new; + + animation->head->next = animation->tail; + animation->head->prev = animation->tail; + + animation->tail->prev = animation->head; + animation->tail->next = animation->head; } else{ - list->head->prev = new; - new->next = list->head; - list->head = new; + animation->head->prev = new; + + new->next = animation->head; + new->prev = animation->tail; + + animation->tail->next = new; + animation->head = new; } } void AnimationInsertBack(Animation *animation, Texture2D *texture){ AnimationFrame *new = AnimationFrameCreate(texture); - if(list->head == 0){ - list->head = new; - list->tail = new; + if(animation->head == 0){ + animation->head = new; + animation->tail = new; + + animation->head->prev = animation->tail; + animation->tail->next = animation->head->prev; } - else if(list->head == list->tail){ - list->tail = new; - list->head->next = list->tail; - list->tail->prev = list->head; + else if(animation->head == animation->tail){ + animation->tail = new; + + animation->head->next = animation->tail; + animation->head->prev = animation->tail; + + animation->tail->prev = animation->head; + animation->tail->next = animation->head; } else{ - list->tail->next = new; - new->prev = list->tail; - list->tail = new; + animation->tail->next = new; + new->prev = animation->tail; + new->next = animation->head; + animation->head->prev = new; + animation->tail = new; } -} \ No newline at end of file +} diff --git a/Textures/animation.h b/Textures/animation.h index 980b5cf..d2354c0 100644 --- a/Textures/animation.h +++ b/Textures/animation.h @@ -2,8 +2,8 @@ #define ANIMATION_H_ #include "raylib.h" -struct Animation; -struct AnimationFrame; +typedef struct Animation Animation; +typedef struct AnimationFrame AnimationFrame; typedef struct Animation { AnimationFrame *head; diff --git a/Textures/animationHandler.c b/Textures/animationHandler.c new file mode 100644 index 0000000..b750ae9 --- /dev/null +++ b/Textures/animationHandler.c @@ -0,0 +1,33 @@ +#include "animationHandler.h" +#include "animation.h" +#include "stdlib.h" +#include "stdio.h" + +AnimationHandler * AnimationHandlerInit(Animation **animations){ + AnimationHandler *new = (AnimationHandler *) malloc(sizeof(AnimationHandler)); + + new->animations = animations; + new->currentAnimation = 0; + //new->currentFrame = new->animations[new->currentAnimation]->head; + new->currentFrame = animations[0]->head; + printf("Hier geht noch\n"); + new->forward = 1; +} + +void AnimationUpdate(AnimationHandler *animationHandler){ + if(animationHandler->forward == 1){ + animationHandler->currentFrame = animationHandler->currentFrame->next; + } + else{ + animationHandler->currentFrame = animationHandler->currentFrame->prev; + } +} + +void AnimationReset(AnimationHandler *animationHandler){ + animationHandler->currentFrame = animationHandler->animations[animationHandler->currentAnimation]->head; +} + +void AnimationChangeAnimation(AnimationHandler *animationHandler, int newAnimation){ + animationHandler->currentAnimation = newAnimation; + AnimationReset(animationHandler); +} \ No newline at end of file diff --git a/Textures/animationHandler.h b/Textures/animationHandler.h new file mode 100644 index 0000000..3ab1e10 --- /dev/null +++ b/Textures/animationHandler.h @@ -0,0 +1,21 @@ +#ifndef ANIMATIONHANDLER_H_ +#define ANIMATIONHANDLER_H_ +#include "raylib.h" +#include "animation.h" +#include "stdio.h" + +typedef struct AnimationHandler AnimationHandler; + +typedef struct AnimationHandler{ + Animation **animations; + AnimationFrame *currentFrame; + int currentAnimation; + int forward; +} AnimationHandler; + +AnimationHandler * AnimationHandlerInit(Animation **animations); +void AnimationUpdate(AnimationHandler *animationHandler); +void AnimationReset(AnimationHandler *animationHandler); +void AnimationChangeAnimation(AnimationHandler *animationHandler, int newAnimation); + +#endif \ No newline at end of file diff --git a/Textures/textureIDs.h b/Textures/textureIDs.h new file mode 100644 index 0000000..bf421e2 --- /dev/null +++ b/Textures/textureIDs.h @@ -0,0 +1,7 @@ +#ifndef TEXTUREIDS_H_ +#define TEXTUREIDS_H_ + +#define cursor 0 +#define worker 1 + +#endif \ No newline at end of file diff --git a/Textures/textureatlas.c b/Textures/textureatlas.c index e24e296..362e56b 100644 --- a/Textures/textureatlas.c +++ b/Textures/textureatlas.c @@ -9,6 +9,7 @@ TextureAtlas * TextureAtlasInit(){ LoadCursorTextures(textures->cursorTextures); LoadWorkerTextures(textures->workerTextures); + LoadWorkerAnimations(textures->workerAnimations, textures->workerTextures); return textures; } @@ -16,6 +17,9 @@ TextureAtlas * TextureAtlasInit(){ void LoadCursorTextures(Texture2D *cursorTextures){ *cursorTextures = LoadTexture("assets/cursor.gif"); *(cursorTextures + 1) = LoadTexture("assets/cursor_down.gif"); + + Animation *new = AnimationInit(); + AnimationInsertBack(new, cursorTextures); } void LoadWorkerTextures(Texture2D *workerTextures){ @@ -53,35 +57,46 @@ void LoadWorkerTextures(Texture2D *workerTextures){ strcat(filename, number); strcat(filename, ending); - //*(workerTextures + i) = LoadTexture(filename); printf("lol: %s ", filename); // Set correct values for next iteration - int lol = i % 5; + int lol = i % 8; // TODO: walk und umackern läuft nicht bis 24 sondern nur 23 if(lol == 0){ + *(workerTextures + i) = LoadTexture(filename); printf("\n"); file++; } else if(lol == 2){ + Image tmp = LoadImage(filename); + ImageFlipHorizontal(&tmp); + *(workerTextures + i) = LoadTextureFromImage(tmp); printf("flipped\n"); file++; } else if(lol == 4){ + Image tmp = LoadImage(filename); + ImageFlipHorizontal(&tmp); + *(workerTextures + i) = LoadTextureFromImage(tmp); printf("flipped\n"); file++; } else if(lol == 6){ + Image tmp = LoadImage(filename); + ImageFlipHorizontal(&tmp); + *(workerTextures + i) = LoadTextureFromImage(tmp); printf("flipped\n"); file++; } else if(lol == 7){ + *(workerTextures + i) = LoadTexture(filename); printf("\n"); file++; } else{ + *(workerTextures + i) = LoadTexture(filename); printf("\n"); } @@ -98,3 +113,20 @@ void LoadWorkerTextures(Texture2D *workerTextures){ } } } + +void LoadWorkerAnimations(Animation **workerAnimations, Texture2D *workerTextures){ + int i; + int j; + int frame = 0; + + for(i=0; i < 24; i++){ + Animation *newAnimation = AnimationInit(); + + for(j = 0; j < 8; j++){ + AnimationInsertBack(newAnimation, (workerTextures+frame)); + frame++; + } + + workerAnimations[i] = newAnimation; + } +} diff --git a/Textures/textureatlas.h b/Textures/textureatlas.h index 7b9888f..7e13dd6 100644 --- a/Textures/textureatlas.h +++ b/Textures/textureatlas.h @@ -1,10 +1,17 @@ #ifndef TEXTUREATLAS_H_ #define TEXTUREATLAS_H_ #include "raylib.h" +#include "animation.h" + +typedef struct TextureAtlas TextureAtlas; typedef struct TextureAtlas{ Texture2D cursorTextures[2]; + Animation cursorAnimation; + Texture2D workerTextures[104]; + Animation *workerAnimations[24]; + //Texture2D[] mapTextures; } TextureAtlas; @@ -12,5 +19,6 @@ typedef struct TextureAtlas{ TextureAtlas * TextureAtlasInit(); void LoadCursorTextures(Texture2D *cursorTextures); void LoadWorkerTextures(Texture2D *workerTextures); +void LoadWorkerAnimations(Animation **workerAnimations, Texture2D *workerTextures); #endif \ No newline at end of file diff --git a/animation.o b/animation.o new file mode 100644 index 0000000..ad026d7 Binary files /dev/null and b/animation.o differ diff --git a/animationHandler.h b/animationHandler.h deleted file mode 100644 index b36c932..0000000 --- a/animationHandler.h +++ /dev/null @@ -1,14 +0,0 @@ -#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/animationHandler.o b/animationHandler.o new file mode 100644 index 0000000..f41671a Binary files /dev/null and b/animationHandler.o differ diff --git a/game.c b/game.c index 5636e1f..9136727 100644 --- a/game.c +++ b/game.c @@ -14,8 +14,8 @@ Game *GameInit() game->textures = TextureAtlasInit(); - game->cursorSprite = SpriteCreate(game->textures->cursorTextures, 450, 225); - + game->cursorSprite = SpriteCreate(game->textures, 0, 450, 225); +/* Image worker1flip = LoadImage("assets/worker/worker-1.png"); ImageFlipHorizontal(&worker1flip); Image worker2flip = LoadImage("assets/worker/worker-2.png"); @@ -30,7 +30,7 @@ Game *GameInit() game->worker[3] = LoadTexture("assets/worker/worker-3.png"); game->worker[1] = LoadTextureFromImage(worker3flip); game->worker[2] = LoadTexture("assets/worker/worker-4.png"); - +*/ game->inputHandler = (InputHandler *)malloc(sizeof(InputHandler)); game->inputHandler->pressed = 0; game->inputHandler->rectStart.x = 0; diff --git a/game.o b/game.o new file mode 100644 index 0000000..047f93c Binary files /dev/null and b/game.o differ diff --git a/inputHandler.o b/inputHandler.o new file mode 100644 index 0000000..250a841 Binary files /dev/null and b/inputHandler.o differ diff --git a/isometricMap.o b/isometricMap.o new file mode 100644 index 0000000..a630467 Binary files /dev/null and b/isometricMap.o differ diff --git a/isometricRenderer.o b/isometricRenderer.o new file mode 100644 index 0000000..e3faf8f Binary files /dev/null and b/isometricRenderer.o differ diff --git a/list.o b/list.o new file mode 100644 index 0000000..22b9aa2 Binary files /dev/null and b/list.o differ diff --git a/main.o b/main.o new file mode 100644 index 0000000..a3186b5 Binary files /dev/null and b/main.o differ diff --git a/spiel b/spiel new file mode 100755 index 0000000..b0199ef Binary files /dev/null and b/spiel differ diff --git a/sprite.c b/sprite.c index 6c31db1..00535c5 100644 --- a/sprite.c +++ b/sprite.c @@ -3,8 +3,13 @@ #include #include #include "IsometricMap/isometricMap.h" +#include "Textures/textureIDs.h" +#include "Textures/animationHandler.h" +#include "Textures/animation.h" +#include "Textures/textureatlas.h" void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, int y){ + /* if(*spriteAmount < 100){ (sprites + *spriteAmount) -> texture = texture; (sprites + *spriteAmount) -> x = x; @@ -18,6 +23,7 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in else{ printf("Voll\n"); } + */ } void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera){ @@ -52,12 +58,34 @@ void DrawSpriteToScreen(Sprite *sprite){ } } -Sprite * SpriteCreate(Texture2D *texture, int x, int y){ +void SpriteUpdateAnimation(Sprite *sprite){ + AnimationUpdate(sprite->animationHandler); + sprite->texture = sprite->animationHandler->currentFrame->texture; +} + +Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y){ Sprite *newSprite = (Sprite *) malloc(sizeof(Sprite)); - newSprite->texture = texture; - newSprite->x = x - texture->width / 2; - newSprite->y = y - texture->height / 2; + //AnimationHandler create + //Animation **animations = atlas->workerAnimations; + Animation **animations = 0; + + if(textureID == worker){ + animations = atlas->workerAnimations; + } + else if(textureID == cursor){ + // Nix + } + else{ + printf("\n\n\n\n\n\n\n\nSpriteCreate mit falscher ID aufgerufen oder ID nicht bekannt!!!\n\n\n\n\n\n\n\n"); + } + + AnimationHandler *newHandler = AnimationHandlerInit(animations); + + newSprite->animationHandler = newHandler; + newSprite->texture = newSprite->animationHandler->currentFrame->texture; + newSprite->x = x - newSprite->texture->width / 2; + newSprite->y = y - newSprite->texture->height / 2; newSprite->destX = x; newSprite->destY = y; newSprite->hasDestination = 0; diff --git a/sprite.h b/sprite.h index ddc0c65..1fb63de 100644 --- a/sprite.h +++ b/sprite.h @@ -2,8 +2,11 @@ #define SPRITE_H_ #include "raylib.h" #include "IsometricMap/isometricMap.h" +#include "Textures/animationHandler.h" +#include "Textures/textureatlas.h" typedef struct Sprite { + AnimationHandler *animationHandler; Texture2D *texture; float x; float y; @@ -17,7 +20,8 @@ void SpriteAdd(Sprite *sprites, int *spriteAmount, Texture2D *texture, int x, in void DrawSpriteToWorld(Sprite *sprite, IsometricMap *map, Camera2D *camera); void DrawSpriteToScreen(Sprite *sprite); +void SpriteUpdateAnimation(Sprite *sprite); -Sprite * SpriteCreate(Texture2D *texture, int x, int y); +Sprite * SpriteCreate(TextureAtlas *atlas, int textureID, int x, int y); #endif diff --git a/sprite.o b/sprite.o new file mode 100644 index 0000000..c71ea63 Binary files /dev/null and b/sprite.o differ diff --git a/textureatlas.o b/textureatlas.o new file mode 100644 index 0000000..38b6db9 Binary files /dev/null and b/textureatlas.o differ diff --git a/tile.o b/tile.o new file mode 100644 index 0000000..b7873fa Binary files /dev/null and b/tile.o differ