MainMenu added

main
Jan 3 years ago
parent 2ff46e2582
commit e4560ac264

@ -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) [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 ## TODO
+ LinkedList erweitern + LinkedList erweitern

@ -6,7 +6,7 @@
#include "raylib.h" #include "raylib.h"
#include "string.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 *button = malloc(sizeof(Button));
button->textures[0] = textures[0]; button->textures[0] = textures[0];
@ -21,6 +21,8 @@ Button * ButtonInitButton(Texture2D textures[4], Vector2 *position, char *text,
button->state = BUTTON_STATE_DEFAULT; button->state = BUTTON_STATE_DEFAULT;
button->id = id; button->id = id;
button->fontSize = fontSize;
return button; return button;
} }
@ -28,13 +30,17 @@ void ButtonExecuteButton(Button *button, Game *game){
button->state = BUTTON_STATE_DEFAULT; button->state = BUTTON_STATE_DEFAULT;
switch(button->id){ switch(button->id){
case BUTTON_ID_CONTINUE: // continue game case BUTTON_ID_CONTINUE: // continue game
if(game->screen == SCREEN_PAUSE){ game->screen = SCREEN_GAME;
game->screen = SCREEN_GAME;
}
break; break;
case BUTTON_ID_EXIT: case BUTTON_ID_EXIT:
// wahrscheinlich kein guter stil, es muss noch zeug freigegeben werden oder soos... keine Ahnung // 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; break;
} }
} }
@ -42,7 +48,7 @@ void ButtonExecuteButton(Button *button, Game *game){
void ButtonDrawButton(Button * button){ void ButtonDrawButton(Button * button){
// erst Button Texture, dann Text zentriert drauf // erst Button Texture, dann Text zentriert drauf
DrawTexture(button->textures[button->state], button->position.x, button->position.y, WHITE); 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){ int ButtonUpdateButtonState(Button * button){

@ -4,8 +4,6 @@
#include "raylib.h" #include "raylib.h"
#include "../game.h" #include "../game.h"
#define BUTTON_FONT_SIZE 36
#define BUTTON_STATE_DEFAULT 0 #define BUTTON_STATE_DEFAULT 0
#define BUTTON_STATE_HOVERED 1 #define BUTTON_STATE_HOVERED 1
#define BUTTON_STATE_PRESSED 2 #define BUTTON_STATE_PRESSED 2
@ -13,18 +11,20 @@
#define BUTTON_ID_CONTINUE 0 #define BUTTON_ID_CONTINUE 0
#define BUTTON_ID_EXIT 1 #define BUTTON_ID_EXIT 1
#define BUTTON_ID_START_GAME 2
typedef struct Button{ typedef struct Button{
Texture2D textures[4]; // [0]: Normal [1]: Hovered [2]: Pressed [3]: Released Texture2D textures[4]; // [0]: Normal [1]: Hovered [2]: Pressed [3]: Released
Vector2 position; Vector2 position; // Linke obere Ecke des Buttons
Vector2 centerPosition; Vector2 centerPosition; // Die Mitte des Buttons
char text[20]; char text[20]; // Text, den der Button zentriert anzeigt, aktuell max. 19 Zeichen
int state; // 0: default 1: hovered 2: pressed 3: released 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; } Button;
// Textures: [0]: Normal [1]: Hovered [2]: Pressed [3]: Released // 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? // executes the logic of one button of certain id - huge switch?
void ButtonExecuteButton(Button *button, Game * game); void ButtonExecuteButton(Button *button, Game * game);

@ -7,20 +7,24 @@
void DebugDraw(Game *game, UiContainer *uiContainer){ void DebugDraw(Game *game, UiContainer *uiContainer){
// Platz für bis zu 10 Strings der Länge 40 char strings[10][41]; // Platz für bis zu 10 Strings der Länge 40
char strings[10][41]; int lineamount = 0; // sollte aktuell gehalten werden, wie viele Lines sind aktuell im Strings Array
int lineamount = 0;
// 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++], "Screen: %d", game->screen);
sprintf(strings[lineamount++], "MouseScreenX: %d", GetMouseX()); sprintf(strings[lineamount++], "MouseScreenX: %d", GetMouseX());
sprintf(strings[lineamount++], "MouseScreenY: %d", GetMouseY()); sprintf(strings[lineamount++], "MouseScreenY: %d", GetMouseY());
sprintf(strings[lineamount++], "MouseWorldX: %d", (int)game->inputHandler->cursorWorldPos.x); sprintf(strings[lineamount++], "MouseWorldX: %d", (int)game->inputHandler->cursorWorldPos.x);
sprintf(strings[lineamount++], "MouseWorldY: %d", (int)game->inputHandler->cursorWorldPos.y); 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}); // Drawed eine Box für die Debug Info
DrawRectangle(0, 0, 200, lineamount * DEBUG_FONT_SIZE + 5, (Color){0, 0, 0, 150}); 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; int counter = 0;
while(counter < lineamount){ while(counter < lineamount){
DrawText(strings[counter], 2, counter * DEBUG_FONT_SIZE + 2, DEBUG_FONT_SIZE, RED); DrawText(strings[counter], 2, counter * DEBUG_FONT_SIZE + 2, DEBUG_FONT_SIZE, RED);

@ -4,8 +4,9 @@
#include "../game.h" #include "../game.h"
#include "uiContainer.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); void DebugDraw(Game *game, UiContainer *uiContainer);
#endif #endif

@ -12,19 +12,40 @@ UiContainer * UiContainerInitPauseUiContainer(){
LoadTexture("assets/button_hovered.png"), //HOVERED LoadTexture("assets/button_hovered.png"), //HOVERED
LoadTexture("assets/button_pressed.png"), //PRESSED LoadTexture("assets/button_pressed.png"), //PRESSED
LoadTexture("assets/button_pressed.png")}; //RELEASED 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->buttons[0] = continuebutton;
uiContainer->buttonCounter = 1; 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_hovered.png"), //HOVERED
LoadTexture("assets/button_pressed.png"), //PRESSED LoadTexture("assets/button_pressed.png"), //PRESSED
LoadTexture("assets/button_pressed.png")}; //RELEASED 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(textures, &position, "EXIT", 5, buttonFontSize, BUTTON_ID_EXIT);
Button *exitButton = ButtonInitButton(textures2, &position2, "EXIT", 5, BUTTON_ID_EXIT);
uiContainer->buttons[1] = exitButton; uiContainer->buttons[1] = exitButton;
uiContainer->buttonCounter = 2; uiContainer->buttonCounter = 2;

@ -6,16 +6,15 @@
#include "../game.h" #include "../game.h"
typedef struct UiContainer{ typedef struct UiContainer{
Button *buttons[15]; Button *buttons[15]; // Platz für max. 15 Buttons
int buttonCounter; int buttonCounter; // Zeigt wie viele Buttons der UiContainer aktuell schon speichert
} UiContainer; } UiContainer;
// executes the logic of one button of certain id - huge switch?
void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game); void UiContainerUpdateUiContainer(UiContainer *uiContainer, Game *game);
void UiContainerDrawUiContainer(UiContainer *uiContainer); void UiContainerDrawUiContainer(UiContainer *uiContainer);
UiContainer * UiContainerInitPauseUiContainer(); UiContainer * UiContainerInitPauseUiContainer();
UiContainer * UiContainerInitGameUiContainer(); UiContainer * UiContainerInitGameUiContainer();
UiContainer * UiContainerInitMainMenuUiContainer();
#endif #endif

@ -31,7 +31,7 @@ Game *GameInit()
game->inputHandler->cursorTextures = game->textures->cursorTextures; game->inputHandler->cursorTextures = game->textures->cursorTextures;
game->inputHandler->cursorSprite = game->cursorSprite; game->inputHandler->cursorSprite = game->cursorSprite;
game->screen = SCREEN_GAME; game->screen = SCREEN_MAINMENU;
game->camera = malloc(sizeof(Camera2D)); game->camera = malloc(sizeof(Camera2D));
game->camera->target.x = 0; game->camera->target.x = 0;

@ -24,6 +24,8 @@ int main(){
// TODO: Screen structs, die zum Beispiel die UiContainer enthalten? // TODO: Screen structs, die zum Beispiel die UiContainer enthalten?
UiContainer *pauseScreenUiContainer = UiContainerInitPauseUiContainer(); UiContainer *pauseScreenUiContainer = UiContainerInitPauseUiContainer();
UiContainer *mainMenuScreenUiContainer = UiContainerInitMainMenuUiContainer();
// Hides the operating systems own cursor // Hides the operating systems own cursor
HideCursor(); HideCursor();
@ -37,58 +39,57 @@ int main(){
game->cursorSprite->y = GetMousePosition().y; game->cursorSprite->y = GetMousePosition().y;
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE);
switch(game->screen){ switch(game->screen){
case SCREEN_EXIT: case SCREEN_EXIT:
printf("EXIT \n");
return 0; return 0;
case SCREEN_MAINMENU: case SCREEN_MAINMENU:
printf("MAINMENU \n"); // MainMenu hat aktuell nur paar Buttons
return 0; UiContainerUpdateUiContainer(mainMenuScreenUiContainer, game);
UiContainerDrawUiContainer(mainMenuScreenUiContainer);
break;
case SCREEN_OPTIONS: case SCREEN_OPTIONS:
printf("OPTIONS \n"); printf("OPTIONS \n");
return 0; return 0;
case SCREEN_GAME: case SCREEN_GAME:
// Updating Sprites
ListActAllSprites(game); ListActAllSprites(game);
ClearBackground(RAYWHITE); // Drawing IsometricMap
BeginMode2D(*(game->camera)); // Sorgt dafür, dass die Kameraposition beachtet wird
BeginMode2D(*(game->camera));
//IsometricRendererRenderIsometricMap(game);
//ListDrawAllSprites(game->sprites, game->layers, game->camera);
IsometricMapDraw(game); IsometricMapDraw(game);
EndMode2D(); EndMode2D();
DrawSpriteToScreen(game->cursorSprite);
// User Input Handling // User Input Handling
mouseInput(game); mouseInput(game);
keyboardInput(game->inputHandler, game->camera); keyboardInput(game->inputHandler, game->camera);
if(IsKeyPressed(KEY_P)){ if(IsKeyPressed(KEY_P)){
game->screen = SCREEN_PAUSE; game->screen = SCREEN_PAUSE;
} }
break; break;
case SCREEN_PAUSE: 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); IsometricMapDraw(game);
EndMode2D(); EndMode2D();
// darkened background + "Paused" Text
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color){0, 0, 0, 150}); DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), (Color){0, 0, 0, 150});
int textWidthHalf = MeasureText("Paused", 28) / 2; int textWidthHalf = MeasureText("Paused", 28) / 2;
DrawText("Paused", GetScreenWidth()/2 - textWidthHalf, GetScreenHeight()/2 - 14, 28, WHITE); DrawText("Paused", GetScreenWidth()/2 - textWidthHalf, GetScreenHeight()/2 - 14, 28, WHITE);
// Button / UI stuff
UiContainerUpdateUiContainer(pauseScreenUiContainer, game); UiContainerUpdateUiContainer(pauseScreenUiContainer, game);
UiContainerDrawUiContainer(pauseScreenUiContainer); UiContainerDrawUiContainer(pauseScreenUiContainer);
// Debug Menu
DebugDraw(game, pauseScreenUiContainer); DebugDraw(game, pauseScreenUiContainer);
DrawSpriteToScreen(game->cursorSprite);
if(IsKeyPressed(KEY_P)){ if(IsKeyPressed(KEY_P)){
game->screen = SCREEN_GAME; game->screen = SCREEN_GAME;
} }
@ -98,9 +99,11 @@ int main(){
return 1; return 1;
break; break;
} }
BeginDrawing(); // Dinge die grundsätzlich immer gedrawed werden sollen
// Debug Menu, FPS anzeige, Cursor
DebugDraw(game, pauseScreenUiContainer); DebugDraw(game, pauseScreenUiContainer);
DrawFPS(GetScreenWidth() - 95, 10); DrawFPS(GetScreenWidth() - 95, 10);
DrawSpriteToScreen(game->cursorSprite);
EndDrawing(); EndDrawing();

Loading…
Cancel
Save