-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcurDraw.c
90 lines (77 loc) · 3.19 KB
/
curDraw.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* -*- Mode:C; Coding:us-ascii-unix; fill-column:132 -*- */
/**********************************************************************************************************************************/
/**
@file curDraw.c
@author Mitch Richling <https://www.mitchr.me>
@Copyright Copyright 1999 by Mitch Richling. All rights reserved.
@Revision $Revision$
@SCMdate $Date$
@brief Interactively draw in a terminal window.@EOL
@Keywords ncurses
@Std C89
Demonstrates how to draw single characters with various attributes, how to get window max and min coordinates, how to clear the
screen, and move the cursor around. In addition, various ncurses concepts are illustrated.
***********************************************************************************************************************************/
#include <ncurses.h> /* Popular Curses ???? */
#include <stdlib.h> /* Standard Lib C89 */
/**********************************************************************************************************************************/
int main() {
int ch, drCh;
int x, y, maxX, maxY, minX, minY;
unsigned int underline, reverse, bold;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
/* We make the cursor very visible.. */
curs_set(2);
/* Print the instructions. */
printw("To Move: arrow keys\n");
printw("To Clear: 'c'\n");
printw("Underline: 'u'\n");
printw("Reverse: 'r'\n");
printw("Bold: 'b'\n");
printw("Normal: 'n'\n");
printw("Change Char: Any printable char\n");
printw("To Quit: 'q'\n");
refresh();
/* Macro to get minX and minY */
getbegyx(stdscr, minY, minX);
/* Macro to get maxX and maxY */
getmaxyx(stdscr, maxY, maxX);
/* Set x and y to the center of the screen */
x = (minX+maxX)/2;
y = (minY+maxY)/2;
/* Print our initial character. */
drCh = '*';
mvaddch(y, x, drCh);
/* Move the cursor to x,y */
move(y, x);
/* Loop for input */
underline = reverse = bold = 0;
while(1) {
ch = getch();
switch(ch) {
case KEY_DOWN : y=y+1; break;
case KEY_UP : y=y-1; break;
case KEY_LEFT : x=x-1; break;
case KEY_RIGHT : x=x+1; break;
case 'c' : clear(); break;
case 'u' : underline = underline - A_UNDERLINE; break;
case 'r' : reverse = reverse - A_REVERSE; break;
case 'b' : bold = bold - A_BOLD; break;
case 'n' : underline = reverse = bold = 0; break;
case 'q' : endwin(); exit(1); break;
default : drCh = ch; break;
} /* end switch */
/* Clip x and y to fit the window. */
x=(x<minX?minX:x); x=(x>maxX?maxX:x);
y=(y<minY?minY:y); y=(y>maxY?maxY:y);
/* Draw things */
mvaddch(y, x, drCh | underline | reverse | bold);
move(y, x);
refresh();
} /* end while */
/* We never get here, but we still "return" to quiet the compiler */
return 0;
} /* end func main */