Ncursesでスクリーン制御
Ncurses(*1)を使ってスクリーン制御してみる。
(*1)
CUIでスクリーン、キー入力、カーソルなどの制御機能を提供するライブラリのこと。
make menuconfig とか実行すると出てくるアレを実現できる。
目次
ソースコード
sample.c
#include <curses.h>
#include <stdlib.h>
#define ESCAPE 27
typedef enum {
COLOR_TYPE_INVALID = 0,
COLOR_TYPE_BACK_GROUND = 1
} color_type_t;
void init_curses()
{
initscr();
start_color();
init_pair(COLOR_TYPE_BACK_GROUND, COLOR_RED, COLOR_BLACK);
curs_set(0);
noecho();
keypad(stdscr,TRUE);
}
int main()
{
init_curses();
bkgd(COLOR_PAIR(COLOR_TYPE_BACK_GROUND));
move(2,1);
printw("Press ESC key, you will exit.");
refresh();
int key;
while(1) {
int key = getch();
if (key == ESCAPE) {
break;
}
refresh();
};
endwin();
return 0;
}
実行結果
$ gcc sample.c -o sample -lcurses
$ ./sample

Ncursesを使ってテトリスぐらいなら作れそう。
コメント