-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.c
70 lines (57 loc) · 2.03 KB
/
messages.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <curses.h>
#include "board_model.h"
#include "messages.h"
#include "worm.h"
#include "worm_model.h"
// Clear an entire line on the display
void clearLineInMessageArea(const int row) {
move(row, 0);
for (int i = 1; i <= COLS; i++) {
addch(' ');
}
}
// Display status about the game in the message area
void showStatus(struct Board *board, struct Worm *worm) {
int pos_line1 = LINES - MA_ROWS_RESERVED + 1;
int pos_line2 = LINES - MA_ROWS_RESERVED + 2;
int pos_line3 = LINES - MA_ROWS_RESERVED + 3;
struct Pos headpos = getWormHeadPos(worm);
mvprintw(pos_line1, 1, "Anzahl verbleibender Futterbrocken: %2d ",
getNumberOfFoodItems(board));
mvprintw(pos_line2, 1, "Wurm ist an Position: y=%3d x=%3d", headpos.y,
headpos.x);
mvprintw(pos_line3, 1, "Laenge des Wurms: %3d", getWormLength(worm));
}
// Display a dialog in the message area and wait for confirmation
// String prompt1 is displayed in the second line of the message area
// String prompt2 is displayed in the third line of the message area
int showDialog(char *prompt1, char *prompt2) {
const int pos_line1 = LINES - MA_ROWS_RESERVED + 1;
const int pos_line2 = LINES - MA_ROWS_RESERVED + 2;
const int pos_line3 = LINES - MA_ROWS_RESERVED + 3;
if (prompt1 == NULL) {
return RES_FAILED;
}
// Delete lines in the message area
clearLineInMessageArea(pos_line1);
clearLineInMessageArea(pos_line2);
clearLineInMessageArea(pos_line3);
// Display message
mvprintw(pos_line2, 1, "%s", prompt1);
if (prompt2 != NULL) {
mvprintw(pos_line3, 1, "%s", prompt2);
}
refresh();
napms(1000);
nodelay(stdscr, FALSE);
const int ch = getch(); // Wait for user to press an arbitrary key
nodelay(stdscr, TRUE);
// Delete lines in the message area
clearLineInMessageArea(pos_line1);
clearLineInMessageArea(pos_line2);
clearLineInMessageArea(pos_line3);
// Display changes
refresh();
// Return code of key pressed
return ch;
}