From 76f54de298382fc900256bca29f6724b5077b180 Mon Sep 17 00:00:00 2001 From: Stefan Neumann Date: Thu, 18 Jan 2018 13:18:48 +0100 Subject: [PATCH 1/2] function added for button clicks in main menu --- gamestate.h | 2 +- startmenu.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/gamestate.h b/gamestate.h index 9e2f3d6..fca7225 100644 --- a/gamestate.h +++ b/gamestate.h @@ -1,6 +1,6 @@ #ifndef __gamestate_h__ #define __gamestate_h__ -typedef enum gameStateEnum { MainMenu = 1, Game = 2, LevelSelect = 3, Settings = 4, Highscores = 5 } GameState; +typedef enum gameStateEnum { MainMenu = 1, Game = 2, LevelSelect = 3, SkinSelect = 4, Settings = 5, Highscores = 6 } GameState; #endif diff --git a/startmenu.c b/startmenu.c index cfc51cf..13afc14 100644 --- a/startmenu.c +++ b/startmenu.c @@ -19,6 +19,10 @@ SDL_Rect SKINSBUTTON_Rect; SDL_Rect HIGHSCORESBUTTON_Rect; SDL_Rect QUITBUTTON_Rect; +int clickInRect(SDL_MouseButtonEvent b, SDL_Rect* area_rect) { + return (((b.x) >= (area_rect->x)) && ((b.x) <= ((area_rect->x) + (area_rect->w))) && ((b.y) >= (area_rect->y)) && ((b.y) <= ((area_rect->y) + (area_rect->h)))); +} + void Load_Textures (SDL_Renderer* renderer) { TITLE_Texture = IMG_LoadTexture(renderer, "assets/images/breaking_button.png"); TITLE_Rect = (SDL_Rect){.x = 685,.y = 50, .w=550, .h=250}; @@ -51,3 +55,19 @@ void Startmenu_Draw (SDL_Renderer* renderer) { SDL_RenderCopy(renderer, HIGHSCORESBUTTON_Texture, NULL, &HIGHSCORESBUTTON_Rect); SDL_RenderCopy(renderer, QUITBUTTON_Texture, NULL, &QUITBUTTON_Rect); } + +int button_clicked(SDL_MouseButtonEvent b) { + if(clickInRect(b, PLAYBUTTON_Rect) == 1) { + GAME_ChangeState(Game); + } else if (clickInRect(b, SKINSBUTTON_Rect) == 1) { + GAME_ChangeState(SkinSelect); + } else if (clickInRect(b, LEVELBUTTON_Rect) == 1) { + GAME_ChangeState(LevelSelect); + } else if (clickInRect(b, SETTINGSBUTTON_Rect) == 1) { + GAME_ChangeState(Settings); + } else if (clickInRect(b, HIGHSCORESBUTTON_Rect) == 1) { + GAME_ChangeState(Highscores); + } else if (clickInRect(b, QUITBUTTON_Rect) == 1) { + GAME_Escape(); + } +} From 04d0cfb994382d9a6e6ebd1bb4ff3aa41b5af2bb Mon Sep 17 00:00:00 2001 From: Stefan Neumann Date: Thu, 18 Jan 2018 15:19:04 +0100 Subject: [PATCH 2/2] main_menu as default start screen added and now you can click to change gamestate --- main.c | 8 +++++--- startmenu.c | 16 +++++++++------- startmenu.h | 4 ++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 107b1c3..49e0c10 100644 --- a/main.c +++ b/main.c @@ -47,12 +47,12 @@ SDL_Window * window; SDL_Renderer * renderer; SDL_Event event; bool running = true, fullscreen = false; -GameState gameState = Game; +GameState gameState = MainMenu; Scenery scenery; int main(int argc, char * args[]){ - printf("Spielbereiche\n\t- 1: Hauptmen"ue "\n\t- 2: Spiel\n\t- 3: Level Select\n\t- 4: Settings\n\t- 5: Highscores\n"); - GAME_ChangeState(readIntFromIO("W"ae "hle einen Spielbereich aus, den du testen m"oe "chtest:", "Fehlerhafte Eingabe!\n", "%d ist kein g"ue "ltiger Spielbereich!\n", 1, 5)); + //printf("Spielbereiche\n\t- 1: Hauptmen"ue "\n\t- 2: Spiel\n\t- 3: Level Select\n\t- 4: Settings\n\t- 5: Highscores\n"); + //GAME_ChangeState(readIntFromIO("W"ae "hle einen Spielbereich aus, den du testen m"oe "chtest:", "Fehlerhafte Eingabe!\n", "%d ist kein g"ue "ltiger Spielbereich!\n", 1, 5)); INITIALIZE(); while (running) { // Gameloop HandleSDLEvents(); @@ -64,6 +64,7 @@ int main(int argc, char * args[]){ break; case MainMenu: // Startmenu_Update(keystate); + Startmenu_Draw(renderer); break; case Highscores: @@ -111,6 +112,7 @@ void HandleSDLEvents(){ break; case SDL_MOUSEBUTTONDOWN: mousePress(event.button); + button_clicked(event.button); break; case SDL_WINDOWEVENT: windowChanged(event.window); diff --git a/startmenu.c b/startmenu.c index 13afc14..7eb677e 100644 --- a/startmenu.c +++ b/startmenu.c @@ -2,6 +2,8 @@ #include #include #include +#include "gamestate.h" + SDL_Texture* TITLE_Texture; SDL_Texture* PLAYBUTTON_Texture; @@ -56,18 +58,18 @@ void Startmenu_Draw (SDL_Renderer* renderer) { SDL_RenderCopy(renderer, QUITBUTTON_Texture, NULL, &QUITBUTTON_Rect); } -int button_clicked(SDL_MouseButtonEvent b) { - if(clickInRect(b, PLAYBUTTON_Rect) == 1) { +void button_clicked(SDL_MouseButtonEvent b) { + if(clickInRect(b, &PLAYBUTTON_Rect) == 1) { GAME_ChangeState(Game); - } else if (clickInRect(b, SKINSBUTTON_Rect) == 1) { + } else if (clickInRect(b, &SKINSBUTTON_Rect) == 1) { GAME_ChangeState(SkinSelect); - } else if (clickInRect(b, LEVELBUTTON_Rect) == 1) { + } else if (clickInRect(b, &LEVELBUTTON_Rect) == 1) { GAME_ChangeState(LevelSelect); - } else if (clickInRect(b, SETTINGSBUTTON_Rect) == 1) { + } else if (clickInRect(b, &SETTINGSBUTTON_Rect) == 1) { GAME_ChangeState(Settings); - } else if (clickInRect(b, HIGHSCORESBUTTON_Rect) == 1) { + } else if (clickInRect(b, &HIGHSCORESBUTTON_Rect) == 1) { GAME_ChangeState(Highscores); - } else if (clickInRect(b, QUITBUTTON_Rect) == 1) { + } else if (clickInRect(b, &QUITBUTTON_Rect) == 1) { GAME_Escape(); } } diff --git a/startmenu.h b/startmenu.h index ee4d6cd..82fff3d 100644 --- a/startmenu.h +++ b/startmenu.h @@ -12,4 +12,8 @@ void Load_Textures (SDL_Renderer* renderer); void Startmenu_Draw (SDL_Renderer* renderer); +void button_clicked(SDL_MouseButtonEvent b); + +int clickInRect(SDL_MouseButtonEvent b, SDL_Rect* area_rect); + #endif