From 2ff46e2582b09fc2c274e88c46270395e9da6e8b Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 4 Jan 2023 20:43:10 +0100 Subject: [PATCH] Pause Menu added and smal debug view --- .vscode/settings.json | 3 ++- Makefile | 5 ++++- Ui/button.c | 4 ++++ Ui/button.h | 2 ++ Ui/debug.c | 29 +++++++++++++++++++++++++++++ Ui/debug.h | 11 +++++++++++ Ui/uiContainer.c | 12 +++++++++++- main.c | 21 +++++++++------------ 8 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 Ui/debug.c create mode 100644 Ui/debug.h diff --git a/.vscode/settings.json b/.vscode/settings.json index f0c1e26..059e6ac 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,7 +12,8 @@ "buttons.h": "c", "string.h": "c", "uicontainer.h": "c", - "button.h": "c" + "button.h": "c", + "cstdio": "c" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/Makefile b/Makefile index da13a5c..63f0ea9 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 button.o uiContainer.o +OBJS = main.o sprite.o inputHandler.o isometricMap.o list.o game.o textureatlas.o animation.o animationHandler.o bucket.o mergeSort.o button.o uiContainer.o debug.o spiel: $(OBJS) $(CC) -o spiel $(OBJS) $(FLAGS) @@ -44,5 +44,8 @@ button.o: Ui/button.c uiContainer.o: Ui/uiContainer.c $(CC) -c Ui/uiContainer.c $(FLAGS) +debug.o: Ui/debug.c + $(CC) -c Ui/debug.c $(FLAGS) + clean: rm *.o spiel diff --git a/Ui/button.c b/Ui/button.c index e62a877..fc84a6b 100644 --- a/Ui/button.c +++ b/Ui/button.c @@ -32,6 +32,10 @@ void ButtonExecuteButton(Button *button, Game *game){ game->screen = SCREEN_GAME; } break; + case BUTTON_ID_EXIT: + // wahrscheinlich kein guter stil, es muss noch zeug freigegeben werden oder soos... keine Ahnung + exit(1); + break; } } diff --git a/Ui/button.h b/Ui/button.h index df33050..9d82a68 100644 --- a/Ui/button.h +++ b/Ui/button.h @@ -12,6 +12,7 @@ #define BUTTON_STATE_RELEASED 3 #define BUTTON_ID_CONTINUE 0 +#define BUTTON_ID_EXIT 1 typedef struct Button{ Texture2D textures[4]; // [0]: Normal [1]: Hovered [2]: Pressed [3]: Released @@ -22,6 +23,7 @@ typedef struct Button{ int id; } Button; +// Textures: [0]: Normal [1]: Hovered [2]: Pressed [3]: Released Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int id); // executes the logic of one button of certain id - huge switch? diff --git a/Ui/debug.c b/Ui/debug.c new file mode 100644 index 0000000..9862e86 --- /dev/null +++ b/Ui/debug.c @@ -0,0 +1,29 @@ +#include "debug.h" +#include "../game.h" +#include "uiContainer.h" +#include "string.h" +#include +#include "../Input/inputHandler.h" + +void DebugDraw(Game *game, UiContainer *uiContainer){ + + // Platz für bis zu 10 Strings der Länge 40 + char strings[10][41]; + + int lineamount = 0; + + sprintf(strings[lineamount++], "Screen: %d", game->screen); + sprintf(strings[lineamount++], "MouseScreenX: %d", GetMouseX()); + sprintf(strings[lineamount++], "MouseScreenY: %d", GetMouseY()); + sprintf(strings[lineamount++], "MouseWorldX: %d", (int)game->inputHandler->cursorWorldPos.x); + sprintf(strings[lineamount++], "MouseWorldY: %d", (int)game->inputHandler->cursorWorldPos.y); + + DrawRectangleLines(0, 0, 200, lineamount * DEBUG_FONT_SIZE + 5, (Color){0, 0, 0, 255}); + DrawRectangle(0, 0, 200, lineamount * DEBUG_FONT_SIZE + 5, (Color){0, 0, 0, 150}); + + int counter = 0; + while(counter < lineamount){ + DrawText(strings[counter], 2, counter * DEBUG_FONT_SIZE + 2, DEBUG_FONT_SIZE, RED); + counter++; + } +} \ No newline at end of file diff --git a/Ui/debug.h b/Ui/debug.h new file mode 100644 index 0000000..d2e50f0 --- /dev/null +++ b/Ui/debug.h @@ -0,0 +1,11 @@ +#ifndef DEBUG_H_ +#define DEBUG_H_ + +#include "../game.h" +#include "uiContainer.h" + +#define DEBUG_FONT_SIZE 18 + +void DebugDraw(Game *game, UiContainer *uiContainer); + +#endif \ No newline at end of file diff --git a/Ui/uiContainer.c b/Ui/uiContainer.c index 264e180..972e633 100644 --- a/Ui/uiContainer.c +++ b/Ui/uiContainer.c @@ -12,12 +12,22 @@ UiContainer * UiContainerInitPauseUiContainer(){ LoadTexture("assets/button_hovered.png"), //HOVERED LoadTexture("assets/button_pressed.png"), //PRESSED LoadTexture("assets/button_pressed.png")}; //RELEASED - Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 + 150}; + Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 300}; Button *continuebutton = ButtonInitButton(textures, &position, "Continue", 9, BUTTON_ID_CONTINUE); uiContainer->buttons[0] = continuebutton; uiContainer->buttonCounter = 1; + Texture2D textures2[4] = { LoadTexture("assets/button.png"), //DEFAULT + LoadTexture("assets/button_hovered.png"), //HOVERED + LoadTexture("assets/button_pressed.png"), //PRESSED + LoadTexture("assets/button_pressed.png")}; //RELEASED + + Vector2 position2 = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50}; + Button *exitButton = ButtonInitButton(textures2, &position2, "EXIT", 5, BUTTON_ID_EXIT); + uiContainer->buttons[1] = exitButton; + uiContainer->buttonCounter = 2; + // Methode funktioniert wieso auch immer auch ohne dieses return. C returned Implizit odder was O_o return uiContainer; } diff --git a/main.c b/main.c index 39b276b..b5ba365 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,7 @@ #include "Ui/screenIDs.h" #include "Ui/button.h" #include "Ui/uiContainer.h" +#include "Ui/debug.h" int main(){ @@ -35,6 +36,7 @@ int main(){ game->cursorSprite->x = GetMousePosition().x; game->cursorSprite->y = GetMousePosition().y; + BeginDrawing(); switch(game->screen){ case SCREEN_EXIT: return 0; @@ -49,7 +51,6 @@ int main(){ ListActAllSprites(game); ClearBackground(RAYWHITE); - BeginDrawing(); BeginMode2D(*(game->camera)); @@ -58,17 +59,12 @@ int main(){ IsometricMapDraw(game); EndMode2D(); - DrawSpriteToScreen(game->cursorSprite); // User Input Handling mouseInput(game); keyboardInput(game->inputHandler, game->camera); - - DrawFPS(GetScreenWidth() - 95, 10); - - EndDrawing(); - + if(IsKeyPressed(KEY_P)){ game->screen = SCREEN_PAUSE; } @@ -77,9 +73,6 @@ int main(){ case SCREEN_PAUSE: ClearBackground(RAYWHITE); - - BeginDrawing(); - BeginMode2D(*(game->camera)); IsometricMapDraw(game); EndMode2D(); @@ -91,10 +84,10 @@ int main(){ UiContainerUpdateUiContainer(pauseScreenUiContainer, game); UiContainerDrawUiContainer(pauseScreenUiContainer); + DebugDraw(game, pauseScreenUiContainer); + DrawSpriteToScreen(game->cursorSprite); - DrawFPS(GetScreenWidth() - 95, 10); - EndDrawing(); if(IsKeyPressed(KEY_P)){ game->screen = SCREEN_GAME; @@ -105,6 +98,10 @@ int main(){ return 1; break; } + BeginDrawing(); + DebugDraw(game, pauseScreenUiContainer); + DrawFPS(GetScreenWidth() - 95, 10); + EndDrawing();