1 | /* ncurses-minesweeper Copyright (c) 2020 Joshua 'joshuas3' Stockin |
2 | * <https://joshstock.in> |
3 | * <https://github.com/JoshuaS3/ncurses-minesweeper> |
4 | * |
5 | * This software is licensed and distributed under the terms of the MIT License. |
6 | * See the MIT License in the LICENSE file of this project's root folder. |
7 | * |
8 | * This comment block and its contents, including this disclaimer, MUST be |
9 | * preserved in all copies or distributions of this software's source. |
10 | */ |
11 |
|
12 | #include <ncurses.h> |
13 |
|
14 | #include "../state.h" |
15 | #include "pages.h" |
16 |
|
17 | int draw(game_state *state, int ch) { |
18 | int ret = 0; |
19 |
|
20 | // Map input |
21 | if (ch == 'h' || ch == 'H') ch = KEY_LEFT; |
22 | if (ch == 'j' || ch == 'J') ch = KEY_DOWN; |
23 | if (ch == 'k' || ch == 'K') ch = KEY_UP; |
24 | if (ch == 'l' || ch == 'L') ch = KEY_RIGHT; |
25 |
|
26 | switch (state->page) { |
27 | case Title: { |
28 | ret = draw_title_screen(state, ch); |
29 | break; |
30 | } |
31 | case Game: { |
32 | ret = draw_game(state, ch); |
33 | break; |
34 | } |
35 | case Options: { |
36 | ret = draw_options_screen(state, ch); |
37 | break; |
38 | } |
39 | case Help: { |
40 | ret = draw_help_screen(state, ch); |
41 | break; |
42 | } |
43 | default: |
44 | return 1; |
45 | } |
46 |
|
47 | if (ch != -1) mvprintw(LINES - 1, 0, "%04i", ch); // print most recent character press |
48 |
|
49 | if (LINES < 20 || COLS < 80) attron(COLOR_PAIR(3)); |
50 | mvprintw(LINES - 1, COLS - 7, "%03ix%03i", COLS, LINES); // print screen resolution |
51 | if (LINES < 20 || COLS < 80) attroff(COLOR_PAIR(3)); |
52 |
|
53 | refresh(); |
54 | return ret; |
55 | } |
56 |
|
57 | void draw_loop(game_state *state) { |
58 | int ch = 0; |
59 | while ((ch = getch())) { |
60 | if (draw(state, ch)) break; // break loop on non-zero return |
61 | } |
62 | } |
63 |
|