From 94a3b03a1a4bb8ce10a26bd3b9e49938c8a59a84 Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 3 Jan 2023 16:58:14 +0100 Subject: [PATCH] Ui Container added, Sprite richtung bug komisch hmmmmm --- .vscode/settings.json | 9 +++-- Makefile | 9 +++-- README.md | 20 ---------- Ui/{buttons.c => button.c} | 51 +++++++++++++++++--------- Ui/button.h | 31 ++++++++++++++++ Ui/buttons.h | 26 ------------- Ui/uiContainer.c | 42 +++++++++++++++++++++ Ui/uiContainer.h | 21 +++++++++++ main.c | 75 ++++++-------------------------------- 9 files changed, 152 insertions(+), 132 deletions(-) rename Ui/{buttons.c => button.c} (53%) create mode 100644 Ui/button.h delete mode 100644 Ui/buttons.h create mode 100644 Ui/uiContainer.c create mode 100644 Ui/uiContainer.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 2644e13..f0c1e26 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,4 @@ { - "C_Cpp.errorSquiggles": "Enabled", "files.associations": { "isometricrenderer.h": "c", "sprite.h": "c", @@ -10,6 +9,10 @@ "tile.h": "c", "raylib.h": "c", "game.h": "c", - "buttons.h": "c" - } + "buttons.h": "c", + "string.h": "c", + "uicontainer.h": "c", + "button.h": "c" + }, + "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/Makefile b/Makefile index 3d670b6..da13a5c 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 buttons.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 spiel: $(OBJS) $(CC) -o spiel $(OBJS) $(FLAGS) @@ -38,8 +38,11 @@ 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) +button.o: Ui/button.c + $(CC) -c Ui/button.c $(FLAGS) + +uiContainer.o: Ui/uiContainer.c + $(CC) -c Ui/uiContainer.c $(FLAGS) clean: rm *.o spiel diff --git a/README.md b/README.md index 69f32e3..b4b9834 100644 --- a/README.md +++ b/README.md @@ -22,29 +22,9 @@ Fantasy Welt oder Realistisch? + LinkedList erweitern + Sprites Animationen etc improven + Die Inputs sollten den Kamera Zoom beachten, aktuell geht noch alles kaputt wenn man den zoom umstellt -+ Funktion, um die ganzen Sprites nach ihrer y-Koordinaten sortiert zu drawen -+ Drawable Container machen, die sortiert werden können, dort kommen alle Tiles und Sprites rein, damit sie dann sortiert werden können + Maps in eigenen Dateien speichern + Parser für Map-Dateien + MapEditor -* Rendering Reihenfolge: layer 0, Sprites auf layer 0, layer 1, Sprites auf layer 1; Theoretisch müssen die einzelnen Layer Reihenweise gedrawed werden mit den Sprites zwischendrin + IsometricMap struct erstellen, das den IsometricMap(+Layer) Array speichert ? -+ Beim rendern müssen die map tiles und die sprites nach ihrer depth (d = x + y + ~0.05*z) sortiert werden, dafür sollten wir ein bucket sorting system implementieren. Buckets sollten erstmal nur tiles und sprites unterstützen. -+ Wir können auch die Sprites in der Liste nach d sortieren, dann geht das gut mit "nur in der Kamera sichtbare Sprites rendern". d ist nicht optimal, vielleicht auch einfach über die ganze Liste gehen und nur die sprites mit $ x y \subset camBounds $ in das Bucket sorting system einfügen. Buckets können auch mit MergeSort oder so sortiert werden -[Vererbung in C, drawable superStruct?](https://de.wikibooks.org/wiki/C%2B%2B-Programmierung:_Vererbung) - -+ Mit der Map geht das schon recht einfach, weil wir wissen welche tiles in der Kamera sind. d -+ TODO: Das rendern der IsometricMap wird bei größeren Maps sehr ineffizient; -+ Add offset x and y to each tile to be calculated ONCE, not every frame -+ Alle gehardcodeten screen bounds (450 225 800 400) durch GetScreenWidth() GetScreenHeight() ersetzen -+ Kameraposition abhängiges drawen auch für Sprites implementieren - -### WiP - -+ Dokumentation aufholen - -### Done - -+ Movement speed der Sprites an delta time orientieren diff --git a/Ui/buttons.c b/Ui/button.c similarity index 53% rename from Ui/buttons.c rename to Ui/button.c index 6d92156..4e77dac 100644 --- a/Ui/buttons.c +++ b/Ui/button.c @@ -1,10 +1,29 @@ -#include "buttons.h" +#include "button.h" #include "../game.h" #include "screenIDs.h" #include "stdio.h" +#include "stdlib.h" #include "raylib.h" +#include "string.h" -void executeButton(Button *button, Game *game){ +Button * InitButton(Texture2D textures[3], Vector2 *position, char *text, int textLEN, int id){ + Button *button = malloc(sizeof(Button)); + + button->textures[0] = textures[0]; + button->textures[1] = textures[1]; + button->textures[2] = textures[2]; + + button->position = (Vector2){position->x, position->y}; + button->centerPosition = (Vector2){position->x + textures[0].width/2, position->y + textures[0].height/2}; + + strncpy(button->text, text, textLEN); + button->state = 0; + button->id = id; + + return button; +} + +void ButtonExecuteButton(Button *button, Game *game){ button->state = 0; switch(button->id){ case 0: // continue game @@ -15,19 +34,17 @@ void executeButton(Button *button, Game *game){ } } -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"); +void ButtonDrawButton(Button * button){ + ButtonUpdateButtonState(button); + DrawTexture(button->textures[button->state], button->position.x, button->position.y, WHITE); + DrawText(button->text, button->centerPosition.x - MeasureText(button->text, BUTTON_FONT_SIZE)/2, button->centerPosition.y - BUTTON_FONT_SIZE/2, BUTTON_FONT_SIZE, BLACK); } -int updateButtonState(Button * button){ +int ButtonUpdateButtonState(Button * button){ if(GetMouseX() > button->position.x && - GetMouseX() < button->position.x + button->textures[button->state]->width && + GetMouseX() < button->position.x + button->textures[button->state].width && GetMouseY() > button->position.y && - GetMouseY() < button->position.y + button->textures[button->state]->height + GetMouseY() < button->position.y + button->textures[button->state].height ){ if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)){ button->state = 2; @@ -40,22 +57,22 @@ int updateButtonState(Button * button){ return 0; } -int isButtonHovered(Button * button){ +int ButtonIsButtonHovered(Button * button){ if(GetMouseX() > button->position.x && - GetMouseX() < button->position.x + button->textures[0]->width && + GetMouseX() < button->position.x + button->textures[0].width && GetMouseY() > button->position.y && - GetMouseY() < button->position.y + button->textures[0]->height + GetMouseY() < button->position.y + button->textures[0].height ){ return button->state = 1; } return button->state = 0; } -int isButtonPressed(Button * button){ +int ButtonIsButtonPressed(Button * button){ if(GetMouseX() > button->position.x && - GetMouseX() < button->position.x + button->textures[0]->width && + GetMouseX() < button->position.x + button->textures[0].width && GetMouseY() > button->position.y && - GetMouseY() < button->position.y + button->textures[0]->height + GetMouseY() < button->position.y + button->textures[0].height ){ if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){ return button->state = 2; diff --git a/Ui/button.h b/Ui/button.h new file mode 100644 index 0000000..77673d9 --- /dev/null +++ b/Ui/button.h @@ -0,0 +1,31 @@ +#ifndef BUTTONS_H_ +#define BUTTONS_H_ + +#include "raylib.h" +#include "../game.h" + +#define BUTTON_FONT_SIZE 36 + +typedef struct Button{ + Texture2D textures[3]; // [0]: Normal [1]: Hovered [2]: Pressed + Vector2 position; + Vector2 centerPosition; + char text[20]; + int state; // 0: default 1: hovered 2: pressed + int id; +} Button; + +Button * InitButton(Texture2D textures[3], Vector2 *position, char *text, int textLEN, int id); + +// executes the logic of one button of certain id - huge switch? +void ButtonExecuteButton(Button *button, Game * game); + +int ButtonUpdateButtonState(Button * button); + +void ButtonDrawButton(Button * button); + +int ButtonisButtonPressed(Button * button); + +int ButtonisButtonHovered(Button * button); + +#endif \ No newline at end of file diff --git a/Ui/buttons.h b/Ui/buttons.h deleted file mode 100644 index 15d2a75..0000000 --- a/Ui/buttons.h +++ /dev/null @@ -1,26 +0,0 @@ -#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/uiContainer.c b/Ui/uiContainer.c new file mode 100644 index 0000000..d5f9a37 --- /dev/null +++ b/Ui/uiContainer.c @@ -0,0 +1,42 @@ +#include "uiContainer.h" +#include "../game.h" +#include "button.h" +#include "raylib.h" +#include "stdlib.h" +#include "stdio.h" + +UiContainer * UiContainerInitPauseUiContainer(){ + UiContainer *uiContainer = malloc(sizeof(UiContainer)); + + Texture2D textures[3] = { LoadTexture("assets/button.png"), + LoadTexture("assets/button_hovered.png"), + LoadTexture("assets/button_pressed.png")}; + Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 + 150}; + + Button *continuebutton = InitButton(textures, &position, "Continue", 9, 0); + uiContainer->buttons[0] = continuebutton; + uiContainer->buttonCounter = 1; + + // Methode funktioniert wieso auch immer auch ohne dieses return. C returned Implizit odder was O_o + return uiContainer; +} + +UiContainer * UiContainerInitGameUiContainer(){ + +} + +void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game){ + int i = 0; + for(i=0 ; i < uiContainer->buttonCounter; i++){ + ButtonUpdateButtonState(uiContainer->buttons[i]); + if(uiContainer->buttons[i]->state == 2){ + ButtonExecuteButton(uiContainer->buttons[i], game); + } + } +} +void UiContainerDrawUiContainer(UiContainer *uiContainer){ + int i = 0; + for(i=0 ; i < uiContainer->buttonCounter; i++){ + ButtonDrawButton(uiContainer->buttons[i]); + } +} \ No newline at end of file diff --git a/Ui/uiContainer.h b/Ui/uiContainer.h new file mode 100644 index 0000000..16c9f25 --- /dev/null +++ b/Ui/uiContainer.h @@ -0,0 +1,21 @@ +#ifndef UICONTAINER_H_ +#define UICONTAINER_H_ + +#include "raylib.h" +#include "button.h" +#include "../game.h" + +typedef struct UiContainer{ + Button *buttons[15]; + int buttonCounter; +} UiContainer; + +// executes the logic of one button of certain id - huge switch? +void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game); +void UiContainerDrawUiContainer(UiContainer *uiContainer); + +UiContainer * UiContainerInitPauseUiContainer(); +UiContainer * UiContainerInitGameUiContainer(); + + +#endif \ No newline at end of file diff --git a/main.c b/main.c index 3de0b84..39b276b 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include "raylib.h" +#include "string.h" #include #include #include "sprite.h" @@ -10,74 +11,24 @@ #include "DepthSorting/bucket.h" #include "DepthSorting/mergeSort.h" #include "Ui/screenIDs.h" -#include "Ui/buttons.h" +#include "Ui/button.h" +#include "Ui/uiContainer.h" int main(){ - InitWindow(1600, 900, "basic window"); + InitWindow(1280, 720, "basic window"); Game *game = GameInit(); + // TODO: Screen structs, die zum Beispiel die UiContainer enthalten? + UiContainer *pauseScreenUiContainer = UiContainerInitPauseUiContainer(); + // Hides the operating systems own cursor HideCursor(); SetTargetFPS(60); - return uitest(game); - // GAME MAIN ROUTINE - while(!WindowShouldClose()){ - - ListActAllSprites(game); - - ClearBackground(RAYWHITE); - BeginDrawing(); - - BeginMode2D(*(game->camera)); - - //IsometricRendererRenderIsometricMap(game); - //ListDrawAllSprites(game->sprites, game->layers, game->camera); - IsometricMapDraw(game); - - EndMode2D(); - - // Moving cursor Sprite to Mouse Pos and drawing it - game->cursorSprite->x = game->inputHandler->cursorPos.x; - game->cursorSprite->y = game->inputHandler->cursorPos.y; - DrawSpriteToScreen(game->cursorSprite); - - // User Input Handling - mouseInput(game); - keyboardInput(game->inputHandler, game->camera); - - DrawFPS(GetScreenWidth() - 95, 10); - - EndDrawing(); - - } - - CloseWindow(); - - return 0; - -} - - -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 @@ -137,12 +88,8 @@ int uitest(Game * game){ 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); - } + UiContainerUpdateUiContainer(pauseScreenUiContainer, game); + UiContainerDrawUiContainer(pauseScreenUiContainer); DrawSpriteToScreen(game->cursorSprite); DrawFPS(GetScreenWidth() - 95, 10); @@ -166,4 +113,6 @@ int uitest(Game * game){ CloseWindow(); return 0; -} \ No newline at end of file + +} +