diff --git a/README.md b/README.md index b4b9834..dd6ddf9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Fantasy Welt oder Realistisch? [Sprite sortierung nach Nähe zur Kamera](https://gamedev.stackexchange.com/questions/8151/how-do-i-sort-isometric-sprites-into-the-correct-order) +[turn off EXIT on ESC Key, for later](https://github.com/raysan5/raylib/issues/520) ## TODO + LinkedList erweitern diff --git a/Ui/button.c b/Ui/button.c index fc84a6b..714617b 100644 --- a/Ui/button.c +++ b/Ui/button.c @@ -6,7 +6,7 @@ #include "raylib.h" #include "string.h" -Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int id){ +Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int fontSize, int id){ Button *button = malloc(sizeof(Button)); button->textures[0] = textures[0]; @@ -21,6 +21,8 @@ Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, button->state = BUTTON_STATE_DEFAULT; button->id = id; + button->fontSize = fontSize; + return button; } @@ -28,13 +30,17 @@ void ButtonExecuteButton(Button *button, Game *game){ button->state = BUTTON_STATE_DEFAULT; switch(button->id){ case BUTTON_ID_CONTINUE: // continue game - if(game->screen == SCREEN_PAUSE){ - game->screen = SCREEN_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); + game->screen = SCREEN_EXIT; + break; + case BUTTON_ID_START_GAME: + game->screen = SCREEN_GAME; + break; + default: + printf("\n\n\n\n\n\n Unsupported Button ID %d \n\n\n\n\n\n", button->id); break; } } @@ -42,7 +48,7 @@ void ButtonExecuteButton(Button *button, Game *game){ void ButtonDrawButton(Button * button){ // erst Button Texture, dann Text zentriert drauf 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); + DrawText(button->text, button->centerPosition.x - MeasureText(button->text, button->fontSize)/2, button->centerPosition.y - button->fontSize/2, button->fontSize, BLACK); } int ButtonUpdateButtonState(Button * button){ diff --git a/Ui/button.h b/Ui/button.h index 9d82a68..f8aa218 100644 --- a/Ui/button.h +++ b/Ui/button.h @@ -4,8 +4,6 @@ #include "raylib.h" #include "../game.h" -#define BUTTON_FONT_SIZE 36 - #define BUTTON_STATE_DEFAULT 0 #define BUTTON_STATE_HOVERED 1 #define BUTTON_STATE_PRESSED 2 @@ -13,18 +11,20 @@ #define BUTTON_ID_CONTINUE 0 #define BUTTON_ID_EXIT 1 +#define BUTTON_ID_START_GAME 2 typedef struct Button{ Texture2D textures[4]; // [0]: Normal [1]: Hovered [2]: Pressed [3]: Released - Vector2 position; - Vector2 centerPosition; - char text[20]; + Vector2 position; // Linke obere Ecke des Buttons + Vector2 centerPosition; // Die Mitte des Buttons + char text[20]; // Text, den der Button zentriert anzeigt, aktuell max. 19 Zeichen int state; // 0: default 1: hovered 2: pressed 3: released - int id; + int id; // Durch die ID wird dem Button eine Funktion zugeordnet + int fontSize; // FontSize kann für jeden Button individuell festgelegt werden } Button; // Textures: [0]: Normal [1]: Hovered [2]: Pressed [3]: Released -Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int id); +Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text, int textLEN, int fontSize, int id); // executes the logic of one button of certain id - huge switch? void ButtonExecuteButton(Button *button, Game * game); diff --git a/Ui/debug.c b/Ui/debug.c index 9862e86..8bead17 100644 --- a/Ui/debug.c +++ b/Ui/debug.c @@ -7,20 +7,24 @@ void DebugDraw(Game *game, UiContainer *uiContainer){ - // Platz für bis zu 10 Strings der Länge 40 - char strings[10][41]; - - int lineamount = 0; + char strings[10][41]; // Platz für bis zu 10 Strings der Länge 40 + int lineamount = 0; // sollte aktuell gehalten werden, wie viele Lines sind aktuell im Strings Array + // Hier die Debug Information in den Strings Array einfügen + // im Endeffekt einfach im Array an der Stelle lineamount++ die Elemente einfügen 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); + sprintf(strings[lineamount++], "Selected Layer: %d", game->inputHandler->selectedLayer); + sprintf(strings[lineamount++], "DEPTH: %d", (int)(game->inputHandler->cursorWorldPos.x + game->inputHandler->cursorWorldPos.y + game->inputHandler->selectedLayer)); - 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}); + // Drawed eine Box für die Debug Info + DrawRectangleLines(0, 0, 250, lineamount * DEBUG_FONT_SIZE + 5, (Color){0, 0, 0, 255}); + DrawRectangle(0, 0, 250, lineamount * DEBUG_FONT_SIZE + 5, (Color){0, 0, 0, 150}); + // Drawed den Inhalt des "strings" Arrays int counter = 0; while(counter < lineamount){ DrawText(strings[counter], 2, counter * DEBUG_FONT_SIZE + 2, DEBUG_FONT_SIZE, RED); diff --git a/Ui/debug.h b/Ui/debug.h index d2e50f0..cc1b90b 100644 --- a/Ui/debug.h +++ b/Ui/debug.h @@ -4,8 +4,9 @@ #include "../game.h" #include "uiContainer.h" -#define DEBUG_FONT_SIZE 18 +#define DEBUG_FONT_SIZE 20 +// Drawed das Debug Fenster in die obere Linke Ecke void DebugDraw(Game *game, UiContainer *uiContainer); #endif \ No newline at end of file diff --git a/Ui/uiContainer.c b/Ui/uiContainer.c index 972e633..2bd3527 100644 --- a/Ui/uiContainer.c +++ b/Ui/uiContainer.c @@ -12,19 +12,40 @@ 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 - 300}; + Vector2 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50}; + int buttonFontSize = 36; - Button *continuebutton = ButtonInitButton(textures, &position, "Continue", 9, BUTTON_ID_CONTINUE); + Button *continuebutton = ButtonInitButton(textures, &position, "Continue", 9, buttonFontSize, BUTTON_ID_CONTINUE); uiContainer->buttons[0] = continuebutton; uiContainer->buttonCounter = 1; - Texture2D textures2[4] = { LoadTexture("assets/button.png"), //DEFAULT + position.y += 250; + + Button *exitButton = ButtonInitButton(textures, &position, "EXIT", 5, buttonFontSize, 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; +} + +UiContainer * UiContainerInitMainMenuUiContainer(){ + UiContainer *uiContainer = malloc(sizeof(UiContainer)); + + Texture2D textures[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 position = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50}; + int buttonFontSize = 36; + + Button *continuebutton = ButtonInitButton(textures, &position, "Start Game", 11, buttonFontSize, BUTTON_ID_START_GAME); + uiContainer->buttons[0] = continuebutton; + uiContainer->buttonCounter = 1; + + position.y += 250; - Vector2 position2 = (Vector2){GetScreenWidth()/2 - textures[0].width/2, GetScreenHeight()/2 - 50}; - Button *exitButton = ButtonInitButton(textures2, &position2, "EXIT", 5, BUTTON_ID_EXIT); + Button *exitButton = ButtonInitButton(textures, &position, "EXIT", 5, buttonFontSize, BUTTON_ID_EXIT); uiContainer->buttons[1] = exitButton; uiContainer->buttonCounter = 2; diff --git a/Ui/uiContainer.h b/Ui/uiContainer.h index 16c9f25..fa85d1b 100644 --- a/Ui/uiContainer.h +++ b/Ui/uiContainer.h @@ -6,16 +6,15 @@ #include "../game.h" typedef struct UiContainer{ - Button *buttons[15]; - int buttonCounter; + Button *buttons[15]; // Platz für max. 15 Buttons + int buttonCounter; // Zeigt wie viele Buttons der UiContainer aktuell schon speichert } 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(); - +UiContainer * UiContainerInitMainMenuUiContainer(); #endif \ No newline at end of file diff --git a/game.c b/game.c index 88940df..d2d4f89 100644 --- a/game.c +++ b/game.c @@ -31,7 +31,7 @@ Game *GameInit() game->inputHandler->cursorTextures = game->textures->cursorTextures; game->inputHandler->cursorSprite = game->cursorSprite; - game->screen = SCREEN_GAME; + game->screen = SCREEN_MAINMENU; game->camera = malloc(sizeof(Camera2D)); game->camera->target.x = 0; diff --git a/main.c b/main.c index b5ba365..ac85d42 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,8 @@ int main(){ // TODO: Screen structs, die zum Beispiel die UiContainer enthalten? UiContainer *pauseScreenUiContainer = UiContainerInitPauseUiContainer(); + UiContainer *mainMenuScreenUiContainer = UiContainerInitMainMenuUiContainer(); + // Hides the operating systems own cursor HideCursor(); @@ -37,58 +39,57 @@ int main(){ game->cursorSprite->y = GetMousePosition().y; BeginDrawing(); + ClearBackground(RAYWHITE); switch(game->screen){ case SCREEN_EXIT: + printf("EXIT \n"); return 0; case SCREEN_MAINMENU: - printf("MAINMENU \n"); - return 0; + // MainMenu hat aktuell nur paar Buttons + UiContainerUpdateUiContainer(mainMenuScreenUiContainer, game); + UiContainerDrawUiContainer(mainMenuScreenUiContainer); + break; case SCREEN_OPTIONS: printf("OPTIONS \n"); return 0; case SCREEN_GAME: + // Updating Sprites ListActAllSprites(game); - ClearBackground(RAYWHITE); - - BeginMode2D(*(game->camera)); - - //IsometricRendererRenderIsometricMap(game); - //ListDrawAllSprites(game->sprites, game->layers, game->camera); + // Drawing IsometricMap + BeginMode2D(*(game->camera)); // Sorgt dafür, dass die Kameraposition beachtet wird IsometricMapDraw(game); - EndMode2D(); - DrawSpriteToScreen(game->cursorSprite); // User Input Handling mouseInput(game); keyboardInput(game->inputHandler, game->camera); - + if(IsKeyPressed(KEY_P)){ game->screen = SCREEN_PAUSE; } break; case SCREEN_PAUSE: - ClearBackground(RAYWHITE); - BeginMode2D(*(game->camera)); + // Still drawing isometric map, which is not updated atm + BeginMode2D(*(game->camera)); // Sorgt dafür, dass die Kameraposition beachtet wird IsometricMapDraw(game); EndMode2D(); + // darkened background + "Paused" Text 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); + // Button / UI stuff UiContainerUpdateUiContainer(pauseScreenUiContainer, game); UiContainerDrawUiContainer(pauseScreenUiContainer); + // Debug Menu DebugDraw(game, pauseScreenUiContainer); - DrawSpriteToScreen(game->cursorSprite); - - if(IsKeyPressed(KEY_P)){ game->screen = SCREEN_GAME; } @@ -98,9 +99,11 @@ int main(){ return 1; break; } - BeginDrawing(); + // Dinge die grundsätzlich immer gedrawed werden sollen + // Debug Menu, FPS anzeige, Cursor DebugDraw(game, pauseScreenUiContainer); DrawFPS(GetScreenWidth() - 95, 10); + DrawSpriteToScreen(game->cursorSprite); EndDrawing();