menu.c (2237B)
1 #include <stdlib.h> 2 3 #include "libs/raylib.h" 4 #include "menu.h" 5 #include "util.h" 6 #include "const.h" 7 #include "game.h" 8 #include "level.h" 9 10 menu_t *menu_create(void) { 11 menu_t *menu = malloc(sizeof(menu_t)); 12 menu->idx = 0; 13 return menu; 14 } 15 16 void menu_update(menu_t *menu, game_t *game) { 17 UNUSED(menu); 18 UNUSED(game); 19 if (IsKeyPressed(KEY_W)) { 20 if (menu->idx > 0) { 21 menu->idx--; 22 } 23 } 24 if (IsKeyPressed(KEY_S)) { 25 if (menu->idx < 2) { 26 menu->idx++; 27 } 28 } 29 if (IsKeyPressed(KEY_ENTER)) { 30 switch (menu->idx) { 31 case 0: 32 game->state = STATE_GAME; 33 break; 34 case 1: { 35 if (game->level != LEVEL_NULL) { 36 level_e level = game->level->type; 37 level_unload(game); 38 level_load(game, level); 39 game->victory = false; 40 game->defeat = false; 41 } 42 game->state = STATE_GAME; 43 break; 44 } 45 case 2: 46 game->quit = true; 47 break; 48 default: 49 break; 50 } 51 } 52 } 53 54 void menu_draw(menu_t *menu, game_t *game) { 55 UNUSED(menu); 56 UNUSED(game); 57 int shift = 20; 58 DrawRectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, (Color) { 0, 0, 0, 125 }); 59 DrawRectangle(WINDOW_WIDTH / 2.0 - 100, WINDOW_HEIGHT / 2.0 - 75 + shift - 10, 200, 150, (Color) { 50, 50, 50, 255 }); 60 DrawTextureRec( 61 game->assets.images, 62 (rect_t) { 0, 0, 56 * SCALE, 28 * SCALE }, 63 pos_snap((pos_t) { WINDOW_WIDTH / 2.0 - 3.5 * TILE_WIDTH, WINDOW_HEIGHT / 2.0 - 115 }), 64 WHITE 65 ); 66 text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + shift }, "RESUME", TEXT_ALIGNMENT_CENTER, game); 67 text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + shift }, "RESTART", TEXT_ALIGNMENT_CENTER, game); 68 text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + 30 + shift }, "QUIT", TEXT_ALIGNMENT_CENTER, game); 69 pos_t cursor_pos = (pos_t) { 70 .x = WINDOW_WIDTH / 2.0 - 70, 71 .y = WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + menu->idx * 30 + shift, 72 }; 73 DrawTextureRec( 74 game->assets.font, 75 texture_rect(0, 4, FONT_WIDTH, FONT_HEIGHT), 76 pos_snap(cursor_pos), 77 WHITE 78 ); 79 } 80 81 void menu_free(menu_t *menu) { 82 free(menu); 83 }