so://phie

freezo

A retro platform game
git clone git://soophie.de/freezo

blob: 09f671632cc616b64fb0ba2f3978f32e1d7a9c6b (2362 bytes)[doc][blame][raw]

1#include <stdlib.h>

2

3#include "../lib/raylib.h"

4#include "../include/menu.h"

5#include "../include/util.h"

6#include "../include/const.h"

7#include "../include/game.h"

8#include "../include/level.h"

9

10menu_t *menu_create(void) {

11 menu_t *menu = malloc(sizeof(menu_t));

12 menu->idx = 0;

13 return menu;

14}

15

16void menu_update(menu_t *menu, game_t *game) {

17 UNUSED(menu);

18 UNUSED(game);

19 if (IsKeyPressed(KEY_ESCAPE)) {

20 game->state = STATE_GAME;

21 }

22 if (IsKeyPressed(KEY_W)) {

23 if (menu->idx > 0) {

24 menu->idx--;

25 }

26 }

27 if (IsKeyPressed(KEY_S)) {

28 if (menu->idx < 2) {

29 menu->idx++;

30 }

31 }

32 if (IsKeyPressed(KEY_ENTER)) {

33 switch (menu->idx) {

34 case 0:

35 game->state = STATE_GAME;

36 break;

37 case 1: {

38 if (game->level != LEVEL_NULL) {

39 level_e level = game->level->type;

40 level_unload(game);

41 level_load(game, level);

42 game->victory = false;

43 game->defeat = false;

44 }

45 game->state = STATE_GAME;

46 break;

47 }

48 case 2:

49 game->quit = true;

50 break;

51 default:

52 break;

53 }

54 }

55}

56

57void menu_draw(menu_t *menu, game_t *game) {

58 UNUSED(menu);

59 UNUSED(game);

60 int shift = 20;

61 DrawRectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, (Color) { 0, 0, 0, 125 });

62 DrawRectangle(WINDOW_WIDTH / 2.0 - 100, WINDOW_HEIGHT / 2.0 - 75 + shift - 10, 200, 150, (Color) { 50, 50, 50, 255 });

63 DrawTextureRec(

64 game->assets.images,

65 (rect_t) { 0, 0, 56 * SCALE, 28 * SCALE },

66 pos_snap((pos_t) { WINDOW_WIDTH / 2.0 - 3.5 * TILE_WIDTH, WINDOW_HEIGHT / 2.0 - 115 }),

67 WHITE

68 );

69 text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + shift }, "RESUME", TEXT_ALIGNMENT_CENTER, game);

70 text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + shift }, "RESTART", TEXT_ALIGNMENT_CENTER, game);

71 text_draw((pos_t) { WINDOW_WIDTH / 2.0, WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 + 30 + shift }, "QUIT", TEXT_ALIGNMENT_CENTER, game);

72 pos_t cursor_pos = (pos_t) {

73 .x = WINDOW_WIDTH / 2.0 - 70,

74 .y = WINDOW_HEIGHT / 2.0 - FONT_HEIGHT / 2.0 - 30 + menu->idx * 30 + shift,

75 };

76 DrawTextureRec(

77 game->assets.font,

78 texture_rect(0, 4, FONT_WIDTH, FONT_HEIGHT),

79 pos_snap(cursor_pos),

80 WHITE

81 );

82}

83

84void menu_free(menu_t *menu) {

85 free(menu);

86}