|
1 | 1 | #include "ncurses-view.h" |
2 | 2 | #include <ncurses.h> |
| 3 | +#include <stdbool.h> |
3 | 4 | #include <string.h> |
4 | 5 |
|
5 | 6 | int maxWidth, maxHeight; |
6 | 7 |
|
7 | | -WINDOW *mainMenu; |
8 | | -const char mainMenuTitle[] = "Space Inviders"; |
9 | | -const unsigned mainMenuWidth = 18, mainMenuHeight = 7, nbMainMenuButtons = 4; |
10 | | -const char *mainMenuButtons[] = {"Play", "Settings", "About", "Quit"}; |
| 8 | +// Main Menu |
| 9 | +WINDOW *mainMenu = NULL; |
| 10 | +const char mainMenuTitle[] = "Space Invaders"; |
| 11 | +const unsigned mainMenuWidth = 18, mainMenuHeight = 8, nbMainMenuButtons = 4; |
11 | 12 | int mainMenuSelection = 0; |
12 | 13 |
|
| 14 | +// Game Window |
| 15 | +WINDOW *gameWin = NULL; |
| 16 | + |
| 17 | +void deleteMainMenu() { |
| 18 | + delwin(mainMenu); |
| 19 | + mainMenu = NULL; |
| 20 | +} |
| 21 | + |
13 | 22 | void drawMainMenu() { |
14 | 23 | wclear(mainMenu); |
15 | 24 | mvwin(mainMenu, (maxHeight - mainMenuHeight) / 2, |
16 | 25 | (maxWidth - mainMenuWidth) / 2); |
17 | | - refresh(); |
18 | 26 | box(mainMenu, 0, 0); |
19 | 27 | mvwprintw(mainMenu, 0, (mainMenuWidth - strlen(mainMenuTitle)) / 2, |
20 | 28 | mainMenuTitle); |
21 | | - for (int i = 0; i < nbMainMenuButtons; i++) { |
22 | | - wattrset(mainMenu, mainMenuSelection == i ? A_REVERSE : A_NORMAL); |
23 | | - mvwprintw(mainMenu, i + 2, (mainMenuWidth - strlen(mainMenuButtons[i])) / 2, |
24 | | - "%s", mainMenuButtons[i]); |
25 | | - } |
| 29 | + |
| 30 | + wattrset(mainMenu, mainMenuSelection == 0 ? A_REVERSE : A_NORMAL); |
| 31 | + mvwprintw(mainMenu, 2, gameWin == NULL ? 7 : 5, |
| 32 | + gameWin == NULL ? "Play" : "Continue"); |
| 33 | + |
| 34 | + wattrset(mainMenu, mainMenuSelection == 1 ? A_REVERSE : A_NORMAL); |
| 35 | + mvwprintw(mainMenu, 3, 5, "Settings"); |
| 36 | + |
| 37 | + wattrset(mainMenu, mainMenuSelection == 2 ? A_REVERSE : A_NORMAL); |
| 38 | + mvwprintw(mainMenu, 4, 6, "Credit"); |
| 39 | + |
| 40 | + wattrset(mainMenu, mainMenuSelection == 3 ? A_REVERSE : A_NORMAL); |
| 41 | + mvwprintw(mainMenu, 5, 7, "Quit"); |
| 42 | + |
26 | 43 | wattrset(mainMenu, A_NORMAL); |
27 | 44 | wrefresh(mainMenu); |
28 | 45 | } |
29 | 46 |
|
| 47 | +void createMainMenu() { |
| 48 | + mainMenu = |
| 49 | + newwin(mainMenuHeight, mainMenuWidth, (maxHeight - mainMenuHeight) / 2, |
| 50 | + (maxWidth - mainMenuWidth) / 2); |
| 51 | + mainMenuSelection = 0; |
| 52 | + drawMainMenu(); |
| 53 | +} |
| 54 | + |
| 55 | +void drawGameWindow(Model *model) { |
| 56 | + wclear(gameWin); |
| 57 | + wresize(gameWin, maxHeight, maxWidth); |
| 58 | + box(gameWin, 0, 0); |
| 59 | + |
| 60 | + if (model->currentGame != NULL) { |
| 61 | + Game *game = model->currentGame; |
| 62 | + |
| 63 | + mvwprintw(gameWin, 2, 2, "Score: %d", game->score); |
| 64 | + mvwprintw(gameWin, 2, maxWidth - 10, "Lives: %d", game->lives); |
| 65 | + } |
| 66 | + |
| 67 | + wrefresh(gameWin); |
| 68 | +} |
| 69 | + |
| 70 | +void createGameWindow(Model *model) { |
| 71 | + gameWin = newwin(maxHeight, maxWidth, 0, 0); |
| 72 | + drawGameWindow(model); |
| 73 | +} |
| 74 | + |
30 | 75 | void initViewNcurses() { |
31 | 76 | initscr(); |
32 | 77 | noecho(); |
33 | | - cbreak(); |
| 78 | + halfdelay(1); |
34 | 79 | curs_set(0); |
35 | 80 | keypad(stdscr, TRUE); |
36 | 81 |
|
37 | 82 | getmaxyx(stdscr, maxHeight, maxWidth); |
38 | 83 | refresh(); |
39 | 84 |
|
40 | | - mainMenu = |
41 | | - newwin(mainMenuHeight, mainMenuWidth, (maxHeight - mainMenuHeight) / 2, |
42 | | - (maxWidth - mainMenuWidth) / 2); |
43 | | - drawMainMenu(); |
| 85 | + createMainMenu(); |
44 | 86 | } |
45 | 87 |
|
46 | 88 | void closeViewNcurses() { |
47 | 89 | delwin(mainMenu); |
| 90 | + mainMenu = NULL; |
| 91 | + delwin(gameWin); |
| 92 | + gameWin = NULL; |
48 | 93 | endwin(); |
49 | 94 | } |
50 | 95 |
|
51 | 96 | void loopViewNcurses(Model *model) { |
52 | 97 | bool closeApp = FALSE; |
| 98 | + int key; |
53 | 99 | while (closeApp == FALSE) { |
54 | | - switch (getch()) { |
55 | | - case KEY_RESIZE: |
| 100 | + key = getch(); |
| 101 | + |
| 102 | + if (key == KEY_RESIZE) { |
56 | 103 | getmaxyx(stdscr, maxHeight, maxWidth); |
| 104 | + if (mainMenu != NULL) |
| 105 | + drawMainMenu(); |
| 106 | + if (gameWin != NULL) |
| 107 | + drawGameWindow(model); |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if (mainMenu != NULL) { |
| 112 | + switch (key) { |
| 113 | + case '\n': |
| 114 | + switch (mainMenuSelection) { |
| 115 | + case 0: |
| 116 | + // Play or Continue |
| 117 | + deleteMainMenu(); |
| 118 | + if (gameWin == NULL) { |
| 119 | + startGame(model); |
| 120 | + createGameWindow(model); |
| 121 | + } |
| 122 | + break; |
| 123 | + case 1: |
| 124 | + // Settings |
| 125 | + break; |
| 126 | + case 2: |
| 127 | + // About |
| 128 | + break; |
| 129 | + case 3: |
| 130 | + // Exit |
| 131 | + closeApp = TRUE; |
| 132 | + break; |
| 133 | + } |
| 134 | + break; |
| 135 | + case 'z': |
| 136 | + case KEY_UP: |
| 137 | + mainMenuSelection--; |
| 138 | + if (mainMenuSelection < 0) |
| 139 | + mainMenuSelection = nbMainMenuButtons - 1; |
| 140 | + break; |
| 141 | + case 's': |
| 142 | + case KEY_DOWN: |
| 143 | + mainMenuSelection = (mainMenuSelection + 1) % nbMainMenuButtons; |
| 144 | + break; |
| 145 | + } |
57 | 146 | drawMainMenu(); |
58 | | - break; |
59 | | - case '\n': |
60 | | - if (mainMenuSelection == nbMainMenuButtons - 1) |
61 | | - closeApp = TRUE; |
62 | | - break; |
63 | | - case 'z': |
64 | | - case KEY_UP: |
65 | | - mainMenuSelection--; |
66 | | - if (mainMenuSelection < 0) |
67 | | - mainMenuSelection = nbMainMenuButtons - 1; |
68 | | - drawMainMenu(); |
69 | | - break; |
70 | | - case 's': |
71 | | - case KEY_DOWN: |
72 | | - mainMenuSelection = (mainMenuSelection + 1) % nbMainMenuButtons; |
73 | | - drawMainMenu(); |
74 | | - break; |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + if (gameWin != NULL) { |
| 151 | + switch (key) { |
| 152 | + case 27: |
| 153 | + createMainMenu(); |
| 154 | + break; |
| 155 | + } |
| 156 | + drawGameWindow(model); |
| 157 | + continue; |
75 | 158 | } |
76 | 159 | } |
77 | 160 | } |
0 commit comments