Created
August 9, 2012 21:44
-
-
Save jangler/3308310 to your computer and use it in GitHub Desktop.
idiotically simple ncurses ascii art program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Controls: | |
// - Arrow keys - move cursor | |
// - Esc - toggle reprint of last character on movement | |
// - ^C - exit program, printing the screen to standard output | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <ncurses.h> | |
int x = 0, y = 0, maxx = 0, maxy = 0; | |
int lastc = ' '; | |
bool repeat = false; | |
void init(void) { | |
initscr(); | |
raw(); | |
keypad(stdscr, true); | |
noecho(); | |
getmaxyx(stdscr, maxy, maxx); | |
} | |
void print_screen(void) { | |
for (y = 0; y < maxy; y++) { | |
int endx = maxx - 1; | |
while (endx > 0 && mvinch(y, endx) == 32) | |
endx--; | |
for (x = 0; x <= endx; x++) | |
printf("%c", mvinch(y, x)); | |
printf("\n"); | |
} | |
} | |
bool handle_input(void) { | |
int c = getch(); | |
switch(c) { | |
case KEY_UP: | |
if (y > 0) y--; | |
break; | |
case KEY_DOWN: | |
if (y + 1 < maxy) y++; | |
break; | |
case KEY_LEFT: | |
if (x > 0) x--; | |
break; | |
case KEY_RIGHT: | |
if (x + 1 < maxx) x++; | |
break; | |
case 27: // escape key | |
repeat = !repeat; | |
break; | |
case 3: // ^C | |
endwin(); | |
print_screen(); | |
return false; | |
default: | |
addch(c); | |
lastc = c; | |
break; | |
} | |
move(y, x); | |
if (repeat) { | |
addch(lastc); | |
move(y, x); | |
} | |
refresh(); | |
return true; | |
} | |
int main() { | |
init(); | |
while (handle_input()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment