Index

ncurses-minesweeper / 868e8df

Terminal game of Minesweeper, implemented in C with ncurses.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
3230 Sep 2020 15:18ac05185Add arrow key supportJosh Stockin170G

Blob @ ncurses-minesweeper / src / draw / draw.c

text/plain1625 bytesdownload raw
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
17int 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 mvprintw(LINES - 1, COLS - 7, "%03ix%03i", COLS, LINES); // print screen resolution
49
50 refresh();
51 return ret;
52}
53
54void draw_loop(game_state *state) {
55 int ch = 0;
56 while ((ch = getch())) {
57 if (draw(state, ch)) break; // break loop on non-zero return
58 }
59}
60