breakout/startmenu.c

67 lines
2.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "gamestate.h"
#include "main.h"
extern float XScale, YScale;
extern SDL_Rect Return_Button_rect;
SDL_Texture * TITLE_Texture;
SDL_Texture * PLAYBUTTON_Texture;
SDL_Texture * SETTINGSBUTTON_Texture;
SDL_Texture * HIGHSCORESBUTTON_Texture;
SDL_Texture * QUITBUTTON_Texture;
SDL_Rect TITLE_Rect;
SDL_Rect PLAYBUTTON_Rect;
SDL_Rect SETTINGSBUTTON_Rect;
SDL_Rect HIGHSCORESBUTTON_Rect;
SDL_Rect QUITBUTTON_Rect;
int clickInRect(SDL_MouseButtonEvent b, SDL_Rect * area_rect) {
int clickx, clicky;
clickx = (int)roundf((float)b.x / XScale);
clicky = (int)roundf((float)b.y / YScale);
return ((clickx >= (area_rect->x)) && (clickx <= ((area_rect->x) + (area_rect->w))) && (clicky >= (area_rect->y)) && (clicky <= ((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 };
PLAYBUTTON_Texture = IMG_LoadTexture(renderer, "assets/images/play_button.png");
PLAYBUTTON_Rect = (SDL_Rect) {.x = 772, .y = 420, .w = 376, .h = 214 };
SETTINGSBUTTON_Texture = IMG_LoadTexture(renderer, "assets/images/settings_button.png");
SETTINGSBUTTON_Rect = (SDL_Rect) {.x = 772, .y = 720, .w = 376, .h = 214 };
HIGHSCORESBUTTON_Texture = IMG_LoadTexture(renderer, "assets/images/highscores_button.png");
HIGHSCORESBUTTON_Rect = (SDL_Rect) {.x = 1557, .y = 120, .w = 313, .h = 178 };
QUITBUTTON_Texture = IMG_LoadTexture(renderer, "assets/images/quit_button.png");
QUITBUTTON_Rect = (SDL_Rect) {.x = 50, .y = 896, .w = 235, .h = 134 };
} /* Load_Textures */
void Startmenu_Draw(SDL_Renderer * renderer) {
SDL_RenderCopy(renderer, TITLE_Texture, NULL, &TITLE_Rect);
SDL_RenderCopy(renderer, PLAYBUTTON_Texture, NULL, &PLAYBUTTON_Rect);
SDL_RenderCopy(renderer, SETTINGSBUTTON_Texture, NULL, &SETTINGSBUTTON_Rect);
SDL_RenderCopy(renderer, HIGHSCORESBUTTON_Texture, NULL, &HIGHSCORESBUTTON_Rect);
SDL_RenderCopy(renderer, QUITBUTTON_Texture, NULL, &QUITBUTTON_Rect);
}
void STARTMENU_ButtonClicked(SDL_MouseButtonEvent b, GameState gameState) {
if (clickInRect(b, &PLAYBUTTON_Rect) == 1) {
GAME_ChangeState(Game);
} 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();
}
}