Index

ncurses-minesweeper / 13c4747

Terminal game of Minesweeper, implemented in C with ncurses.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
4717 Oct 2021 18:2013c4747Add about page; Update input methods; Add central strings fileJosh Stockin1103G

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

text/plain2090 bytesdownload raw
1/* ncurses-minesweeper Copyright (c) 2021 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 "../strings.h"
16#include "pages.h"
17
18int draw(game_state *state, int ch) {
19 int ret = 0;
20
21 // Map input
22 if (ch == 'h' || ch == 'H') ch = KEY_LEFT;
23 if (ch == 'j' || ch == 'J') ch = KEY_DOWN;
24 if (ch == 'k' || ch == 'K') ch = KEY_UP;
25 if (ch == 'l' || ch == 'L') ch = KEY_RIGHT;
26 if (ch == 'q' || ch == 'Q') ch = 27;
27 if (ch == 10 || ch == ' ') ch = KEY_ENTER;
28
29 switch (state->page) {
30 case Title: {
31 ret = draw_title_screen(state, ch);
32 break;
33 }
34 case Game: {
35 ret = draw_game(state, ch);
36 break;
37 }
38 case Options: {
39 ret = draw_options_screen(state, ch);
40 break;
41 }
42 case Help: {
43 ret = draw_help_screen(state, ch);
44 break;
45 }
46 case About: {
47 ret = draw_about_screen(state, ch);
48 break;
49 }
50 default:
51 return 1;
52 }
53
54 if (ch != -1) mvprintw(LINES - 1, 0, "%04i", ch); // print most recent character press
55
56 if (LINES < (state->board->height + 1) || LINES < 17 || COLS/2 < (state->board->width) || COLS < 20) attron(COLOR_PAIR(3));
57 mvprintw(LINES - 1, COLS - 6, "%03ix%02i", COLS/2, LINES); // print screen resolution
58 if (LINES < (state->board->height + 1) || LINES < 17 || COLS/2 < (state->board->width) || COLS < 20) attroff(COLOR_PAIR(3));
59
60 refresh();
61 return ret;
62}
63
64void draw_loop(game_state *state) {
65 int ch = 0;
66 while ((ch = getch())) {
67 if (draw(state, ch)) break; // break loop on non-zero return
68 }
69}
70