diff options
Diffstat (limited to 'src/menu.c')
| -rw-r--r-- | src/menu.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/menu.c b/src/menu.c new file mode 100644 index 0000000..2bbc4df --- /dev/null +++ b/src/menu.c @@ -0,0 +1,108 @@ +#include "app.h" +#include "font.h" + +#define MENU_ITEM_TOP_YPOS_RELATIVE 0.3 +#define MENU_ITEM_YOFFSET_RELATIVE 0.03 +#define UNSELECTED_ITEM_SCALE 1 +#define SELECTED_ITEM_SCALE 1.2 + +typedef enum { + timed_mode_option = 0, + exit_option +} menu_option; + +menu_option selected_option; +sprite_font text_font; + +extern SDL_Window* window; +extern application_state app_state; + +static inline float get_window_relative_xpos(float vp) { + int width; + SDL_GetWindowSizeInPixels(window, &width, NULL); + + return width * vp; +} + +static inline float get_window_relative_ypos(float vp) { + int height; + SDL_GetWindowSizeInPixels(window, NULL, &height); + + return height * vp; +} + +void init_menu(SDL_Renderer* renderer) { + if (text_font.spritesheet == NULL) { + load_sprite_font(renderer, + "assets/Fonts/Peaberry-Font-v2.0/Peaberry Bitmap Fonts/1. WhitePeaberry/2. White Peaberry/WhitePeaberry.txt", + &text_font); + } + + selected_option = timed_mode_option; +} + +void draw_menu(SDL_Renderer* renderer) { + SDL_RenderClear(renderer); + + const char* labels[] = { + "Timed Mode", + "Exit", + }; + + float yp = MENU_ITEM_TOP_YPOS_RELATIVE; + for (menu_option i = timed_mode_option; i <= exit_option; i++) { + float scale = i == selected_option ? SELECTED_ITEM_SCALE : UNSELECTED_ITEM_SCALE; + + draw_text_with_spritefont_extended(renderer, + &text_font, + (unsigned char*)labels[i], + get_window_relative_xpos(0.5), + get_window_relative_ypos(yp), + scale + ); + yp += MENU_ITEM_YOFFSET_RELATIVE; + } + + SDL_RenderPresent(renderer); +} + +static inline void select_next_option() { + selected_option = SDL_min(selected_option+1, exit_option); +} + +static inline void select_previous_option() { + if (selected_option > timed_mode_option) { + selected_option--; + } +} + +SDL_AppResult menu_execute_action() { + if (selected_option == exit_option) { + return SDL_APP_SUCCESS; + } if (selected_option == timed_mode_option) { + init_timed_game(NULL, NULL, 40); + app_state = game_state; + } + + return SDL_APP_CONTINUE; +} + +SDL_AppResult menu_event(SDL_Event* e) { + if (e->type == SDL_EVENT_KEY_DOWN) { + SDL_Scancode pressed_button = e->key.scancode; + + if (pressed_button == SDL_SCANCODE_RETURN) { + return menu_execute_action(); + } else if (pressed_button == SDL_SCANCODE_DOWN) { + select_next_option(); + } else if (pressed_button == SDL_SCANCODE_UP) { + select_previous_option(); + } + } + + return SDL_APP_CONTINUE; +} + +void free_menu() { + free_sprite_font(&text_font); +} |
