diff --git a/Makefile b/Makefile index 25a90f6..9b17783 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ FLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -spiel: main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o - $(CC) -o spiel main.o sprite.o inputHandler.o isometricRenderer.o isometricMap.o tile.o list.o $(FLAGS) +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) main.o: main.c $(CC) -c main.c $(FLAGS) @@ -27,5 +27,8 @@ isometricMap.o: IsometricMap/isometricMap.c tile.o: IsometricMap/tile.c $(CC) -c IsometricMap/tile.c $(FLAGS) +game.o: game.c + $(CC) -c game.c $(FLAGS) + clean: rm *.o spiel diff --git a/game.c b/game.c new file mode 100644 index 0000000..16dc8e1 --- /dev/null +++ b/game.c @@ -0,0 +1,112 @@ +#include "game.h" +#include "stdlib.h" +#include "raylib.h" +#include "sprite.h" +#include "List/list.h" +#include "Input/inputHandler.h" +#include "IsometricMap/isometricMap.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->texture = &(game->cursorTextures[0]); + game->cursorSprite->x = 450; + game->cursorSprite->y = 225; + + + + 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); + game->worker[6] = LoadTexture("assets/worker/worker-0.png"); + game->worker[5] = LoadTexture("assets/worker/worker-1.png"); + game->worker[7] = LoadTextureFromImage(worker1flip); + game->worker[4] = LoadTexture("assets/worker/worker-2.png"); + game->worker[0] = LoadTextureFromImage(worker2flip); + 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; + game->inputHandler->rectStart.y = 0; + game->inputHandler->cursorPos.x = 0; + game->inputHandler->cursorPos.y = 0; + game->inputHandler->cursorWorldPos.x = 0; + game->inputHandler->cursorWorldPos.y = 0; + game->inputHandler->cursorWorldTile.x = 0; + game->inputHandler->cursorWorldTile.y = 0; + game->inputHandler->selectedLayer = -1; + game->inputHandler->cursorTextures = game->cursorTextures; + game->inputHandler->cursorSprite = game->cursorSprite; + + game->camera = (Camera2D *) malloc(sizeof(Camera2D)); + game->camera->target = (Vector2){0, 0}; + game->camera->rotation = 0.0f; + game->camera->zoom = 1.0f; + + game->sprites = ListInit(); + + game->layers = ((IsometricMap ***) malloc(10*sizeof(IsometricMap *))); + + // Test Layers --- + int n = 0; + int i = 0; + int j = 0; + + for(n = 0; n < 10; n++){ + (*(game->layers))[n] = IsometricMapInit(n); + } + + for(n = 0; n <= 10; n++){ + for(i = 0; i < 100; i++){ + for(j = 0; j < 100; j++){ + switch(n){ + case 0: + IsometricMapAddTile((*(game->layers))[n], i, j, 0); + break; + case 1: + if(i > 35 && i < 50 && j > 45 && j < 60){ + IsometricMapAddTile((*(game->layers))[n], i, j, 0); + } + break; + case 2: + if(i > 40 && i < 44 && j > 50 && j < 54){ + IsometricMapAddTile((*(game->layers))[n], i, j, 1); + } + break; + } + + } + } + } + + printf("ALARM\n"); + for(n = 0; n <= 10; n++){ + for(i = 0; i < 20-n*2; i++){ + for(j = 0; j < 20-n*2; j++){ + IsometricMapAddTile((*(game->layers))[n], i, j, 0); + if(n == 9){ + IsometricMapAddTile((*(game->layers))[n], i, j, 1); + } + } + } + } + printf("ALARM\n"); + + // ------- + + return game; + +} \ No newline at end of file diff --git a/game.h b/game.h new file mode 100644 index 0000000..1ed88dd --- /dev/null +++ b/game.h @@ -0,0 +1,21 @@ +#ifndef GAME_H_ +#define GAME_H_ + +#include "raylib.h" +#include "sprite.h" +#include "List/list.h" +#include "Input/inputHandler.h" + +typedef struct Game { + Texture2D cursorTextures[2]; + Sprite *cursorSprite; + Texture2D worker[8]; + List *sprites; + InputHandler *inputHandler; + Camera2D *camera; + IsometricMap ***layers; +} Game; + +Game * GameInit(); + +#endif \ No newline at end of file diff --git a/game.o b/game.o new file mode 100644 index 0000000..9764eaa Binary files /dev/null and b/game.o differ diff --git a/inputHandler.o b/inputHandler.o new file mode 100644 index 0000000..0dfacfa Binary files /dev/null and b/inputHandler.o differ diff --git a/isometricMap.o b/isometricMap.o new file mode 100644 index 0000000..d0e6aa8 Binary files /dev/null and b/isometricMap.o differ diff --git a/isometricRenderer.o b/isometricRenderer.o new file mode 100644 index 0000000..8152738 Binary files /dev/null and b/isometricRenderer.o differ diff --git a/list.o b/list.o new file mode 100644 index 0000000..11ae7d0 Binary files /dev/null and b/list.o differ diff --git a/main.c b/main.c index 1e793c9..2e10499 100644 --- a/main.c +++ b/main.c @@ -7,105 +7,13 @@ #include "List/list.h" #include "IsometricMap/isometricRenderer.h" #include "IsometricMap/isometricMap.h" +#include "game.h" int main(){ InitWindow(800, 450, "basic window"); - Texture2D amulet; - amulet = LoadTexture("assets/amulet.png"); - - 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); - - Texture2D worker[8]; - worker[6] = LoadTexture("assets/worker/worker-0.png"); - worker[5] = LoadTexture("assets/worker/worker-1.png"); - worker[7] = LoadTextureFromImage(worker1flip); - worker[4] = LoadTexture("assets/worker/worker-2.png"); - worker[0] = LoadTextureFromImage(worker2flip); - worker[3] = LoadTexture("assets/worker/worker-3.png"); - worker[1] = LoadTextureFromImage(worker3flip); - worker[2] = LoadTexture("assets/worker/worker-4.png"); - - Texture2D cursorTextures[2]; - cursorTextures[0] = LoadTexture("assets/cursor.gif"); - cursorTextures[1] = LoadTexture("assets/cursor_down.gif"); - - List *sprites = ListInit(); - //ListPrintForward(sprites); - - Sprite cursorSprite = {&(cursorTextures[0]), 450, 225}; - - InputHandler inputHandler; - inputHandler.pressed = 0; - inputHandler.rectStart.x = 0; - inputHandler.rectStart.y = 0; - inputHandler.cursorPos.x = 0; - inputHandler.cursorPos.y = 0; - inputHandler.cursorWorldPos.x = 0; - inputHandler.cursorWorldPos.y = 0; - inputHandler.cursorWorldTile.x = 0; - inputHandler.cursorWorldTile.y = 0; - inputHandler.selectedLayer = -1; - inputHandler.cursorTextures = cursorTextures; - inputHandler.cursorSprite = &cursorSprite; - - Camera2D camera = { 0 }; - camera.target = (Vector2){0, 0}; - camera.rotation = 0.0f; - camera.zoom = 1.0f; - - IsometricMap **layers = (IsometricMap **) malloc(10*sizeof(IsometricMap *)); - - // Test Layers --- - - int n = 0; - int i = 0; - int j = 0; - - for(n = 0; n < 10; n++){ - layers[n] = IsometricMapInit(n); - } - - for(n = 0; n <= 10; n++){ - for(i = 0; i < 100; i++){ - for(j = 0; j < 100; j++){ - switch(n){ - case 0: - IsometricMapAddTile(layers[n], i, j, 0); - break; - case 1: - if(i > 35 && i < 50 && j > 45 && j < 60){ - IsometricMapAddTile(layers[n], i, j, 0); - } - break; - case 2: - if(i > 40 && i < 44 && j > 50 && j < 54){ - IsometricMapAddTile(layers[n], i, j, 1); - } - break; - } - - } - } - } - for(n = 0; n <= 10; n++){ - for(i = 0; i < 20-n*2; i++){ - for(j = 0; j < 20-n*2; j++){ - IsometricMapAddTile(layers[n], i, j, 0); - if(n == 9){ - IsometricMapAddTile(layers[n], i, j, 1); - } - } - } - } - - // ------- + Game *game = GameInit(); // Test of the IsometricMapUnproject Function //Vector2 asdf = {500, 600}; @@ -124,23 +32,22 @@ int main(){ ClearBackground(RAYWHITE); - BeginMode2D(camera); - + BeginMode2D(*(game->camera)); - IsometricRendererRenderIsometricMap(layers, &inputHandler); + IsometricRendererRenderIsometricMap(*game->layers, game->inputHandler); - ListDrawAllSprites(sprites, layers[0], &camera); + ListDrawAllSprites(game->sprites, *game->layers[0], game->camera); EndMode2D(); // Moving cursor Sprite to Mouse Pos and drawing it - cursorSprite.x = inputHandler.cursorPos.x; - cursorSprite.y = inputHandler.cursorPos.y; - DrawSpriteToScreen(&cursorSprite); + game->cursorSprite->x = game->inputHandler->cursorPos.x; + game->cursorSprite->y = game->inputHandler->cursorPos.y; + DrawSpriteToScreen(game->cursorSprite); // User Input Handling - mouseInput(&inputHandler, sprites, worker+4, &camera, layers); - keyboardInput(&inputHandler, &camera); + mouseInput(game->inputHandler, game->sprites, game->worker+4, game->camera, *game->layers); + keyboardInput(game->inputHandler, game->camera); //cursor Positions test //printf("Cursor Pos: %f %f\n", inputHandler.cursorPos.x, inputHandler.cursorPos.y); @@ -148,7 +55,7 @@ int main(){ // Sprites move towards their destination float movementSpeed = 10.0f; - Node *current = sprites->head; + Node *current = game->sprites->head; while (current != 0){ if(current->data.hasDestination == 1){ @@ -176,7 +83,7 @@ int main(){ f *= 3.5; f += 3.5; int index = (int) f; - current->data.texture = worker + index; + current->data.texture = game->worker + index; } } diff --git a/main.o b/main.o new file mode 100644 index 0000000..941bd31 Binary files /dev/null and b/main.o differ diff --git a/spiel b/spiel new file mode 100755 index 0000000..e6b51df Binary files /dev/null and b/spiel differ diff --git a/sprite.o b/sprite.o new file mode 100644 index 0000000..0c3146f Binary files /dev/null and b/sprite.o differ diff --git a/tile.o b/tile.o new file mode 100644 index 0000000..ab55776 Binary files /dev/null and b/tile.o differ