diff --git a/.vscode/settings.json b/.vscode/settings.json index 9e6e03e..2644e13 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,7 @@ "textureids.h": "c", "tile.h": "c", "raylib.h": "c", - "game.h": "c" + "game.h": "c", + "buttons.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 5daf646..3d670b6 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc FLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -OBJS = main.o sprite.o inputHandler.o isometricMap.o list.o game.o textureatlas.o animation.o animationHandler.o bucket.o mergeSort.o +OBJS = main.o sprite.o inputHandler.o isometricMap.o list.o game.o textureatlas.o animation.o animationHandler.o bucket.o mergeSort.o buttons.o spiel: $(OBJS) $(CC) -o spiel $(OBJS) $(FLAGS) @@ -38,5 +38,8 @@ bucket.o: DepthSorting/bucket.c mergeSort.o: DepthSorting/mergeSort.c $(CC) -c DepthSorting/mergeSort.c $(FLAGS) +buttons.o: Ui/buttons.c + $(CC) -c Ui/buttons.c $(FLAGS) + clean: rm *.o spiel diff --git a/Ui/buttons.c b/Ui/buttons.c new file mode 100644 index 0000000..6d92156 --- /dev/null +++ b/Ui/buttons.c @@ -0,0 +1,66 @@ +#include "buttons.h" +#include "../game.h" +#include "screenIDs.h" +#include "stdio.h" +#include "raylib.h" + +void executeButton(Button *button, Game *game){ + button->state = 0; + switch(button->id){ + case 0: // continue game + if(game->screen == SCREEN_PAUSE){ + game->screen = SCREEN_GAME; + } + break; + } +} + +void drawButton(Button * button){ + printf("%d\n", button->state); + updateButtonState(button); + printf("DRAW JETZT\n"); + DrawTexture(*button->textures[button->state], button->position.x, button->position.y, WHITE); + printf("funktioniert\n"); +} + +int updateButtonState(Button * button){ + if(GetMouseX() > button->position.x && + GetMouseX() < button->position.x + button->textures[button->state]->width && + GetMouseY() > button->position.y && + GetMouseY() < button->position.y + button->textures[button->state]->height + ){ + if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ + button->state = 2; + return 2; + } + button->state = 1; + return 1; + } + button->state = 0; + return 0; +} + +int isButtonHovered(Button * button){ + if(GetMouseX() > button->position.x && + GetMouseX() < button->position.x + button->textures[0]->width && + GetMouseY() > button->position.y && + GetMouseY() < button->position.y + button->textures[0]->height + ){ + return button->state = 1; + } + return button->state = 0; +} + +int isButtonPressed(Button * button){ + if(GetMouseX() > button->position.x && + GetMouseX() < button->position.x + button->textures[0]->width && + GetMouseY() > button->position.y && + GetMouseY() < button->position.y + button->textures[0]->height + ){ + if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ + return button->state = 2; + } + } + return button->state = 0; +} + diff --git a/Ui/buttons.h b/Ui/buttons.h new file mode 100644 index 0000000..15d2a75 --- /dev/null +++ b/Ui/buttons.h @@ -0,0 +1,26 @@ +#ifndef BUTTONS_H_ +#define BUTTONS_H_ + +#include "raylib.h" +#include "../game.h" + +typedef struct Button{ + Texture2D *textures[3]; // [0]: Normal [1]: Hovered [2]: Pressed + Vector2 position; + //char text[20]; + int state; // 0: default 1: hovered 2: pressed + int id; +} Button; + +// executes the logic of one button of certain id - huge switch? +void executeButton(Button *button, Game * game); + +int updateButtonState(Button * button); + +void drawButton(Button * button); + +int isButtonPressed(Button * button); + +int isButtonHovered(Button * button); + +#endif \ No newline at end of file diff --git a/Ui/screenIDs.h b/Ui/screenIDs.h index bf95efd..e55f992 100644 --- a/Ui/screenIDs.h +++ b/Ui/screenIDs.h @@ -1,5 +1,5 @@ -#ifndef BUTTONIDS_H_ -#define BUTTONIDS_H_ +#ifndef SCREENIDS_H_ +#define SCREENIDS_H_ #define SCREEN_EXIT 0 #define SCREEN_MAINMENU 1 diff --git a/assets/button.png b/assets/button.png new file mode 100644 index 0000000..0ea26fa Binary files /dev/null and b/assets/button.png differ diff --git a/assets/button_hovered.png b/assets/button_hovered.png new file mode 100644 index 0000000..32a0d04 Binary files /dev/null and b/assets/button_hovered.png differ diff --git a/assets/button_pressed.png b/assets/button_pressed.png new file mode 100644 index 0000000..7715efa Binary files /dev/null and b/assets/button_pressed.png differ diff --git a/main.c b/main.c index 389fe9b..3de0b84 100644 --- a/main.c +++ b/main.c @@ -10,10 +10,11 @@ #include "DepthSorting/bucket.h" #include "DepthSorting/mergeSort.h" #include "Ui/screenIDs.h" +#include "Ui/buttons.h" int main(){ - InitWindow(800, 450, "basic window"); + InitWindow(1600, 900, "basic window"); Game *game = GameInit(); @@ -64,6 +65,19 @@ int main(){ int uitest(Game * game){ + Texture2D button = LoadTexture("assets/button.png"); + Texture2D buttonHovered = LoadTexture("assets/button_hovered.png"); + Texture2D buttonPressed = LoadTexture("assets/button_pressed.png"); + + Button continuebutton; + continuebutton.id = 0; + continuebutton.position = (Vector2){GetScreenWidth()/2, GetScreenHeight()/2}; + continuebutton.state = 0; + + continuebutton.textures[0] = &button; + continuebutton.textures[1] = &buttonHovered; + continuebutton.textures[2] = &buttonPressed; + while(!WindowShouldClose()){ // Moving cursor Sprite to Mouse Pos @@ -111,9 +125,25 @@ int uitest(Game * game){ break; case SCREEN_PAUSE: ClearBackground(RAYWHITE); + BeginDrawing(); + BeginMode2D(*(game->camera)); + IsometricMapDraw(game); + EndMode2D(); + + DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color){0, 0, 0, 150}); + int textWidthHalf = MeasureText("Paused", 28) / 2; + DrawText("Paused", GetScreenWidth()/2 - textWidthHalf, GetScreenHeight()/2 - 14, 28, WHITE); + + printf("Vorher \n"); + drawButton(&continuebutton); + printf("Nachher \n"); + if(continuebutton.state == 2){ + executeButton(&continuebutton, game); + } + DrawSpriteToScreen(game->cursorSprite); DrawFPS(GetScreenWidth() - 95, 10); @@ -123,6 +153,10 @@ int uitest(Game * game){ game->screen = SCREEN_GAME; } break; + default: + printf("\n\n\n\n\n\n Wir haben ein problematisches Problem! Die Screen-ID [%d] ist nicht definiert. Hmmpf... früher bescheid wisse!\n\n\n\n\n\n", game->screen); + return 1; + break; }