// A Demonstration of the NCURSES library S. Murrell 15-11-99 // // (ncurses is almost the same as curses) // curses and ncurses allow you to control the whole screen and draw // on it using normal ascii characters. Editors like pico and jed use // curses because it gives you a standard system-independent AND // terminal-independent interface. // // Curses is a standard unix library, but it doesn't provide non-blocking // input (editors don't need it, but we do here). Ncurses is a special // version of curses with a number of extensions. It is almost a standard // and is quite widely available. // // This example has been tailored to work properly on rabbit. On other // computers you will probably have to play around with the libraries // a bit. // // You can download this file, compile and run it directly. Use as much // of it as you like. If you want to use any features that I don't // illustrate here, try the "man ncurses" command; you may be lucky. // // Assuming you have downloaded this and called the file "curses.cpp" // compile with: // CC -c curses.cpp // then link with: // CC -lncurses curses.o // then run with: // ./a.out // // if your program covers multiple .cpp files, compile each separately // with "CC -c", then link them all together like this: // CC -lncurses curses.o otherfile.o wawawa.o woooo.o // // if you are also using the Pthreads library (see the other example) // you will still need to use the -pthread switch when linking: // // CC -c curses.cpp // CC -c other.cpp // CC -pthread -lncurses curses.o other.o // ./a.out // Got it? #include #include #include #include #include WINDOW *w1, *w2, *w3; int screenW, screenH, windowW, windowH; char map[50][100]; char tempstr[100]; int num_moves=0; int random_in_range(int min, int max) { return min+random()%(max+1-min); } void main(void) { initscr(); // This is required to make curses work nonl(); // This stops it from screwing up the output raw(); // This stops it from playing with the input cbreak(); // this makes it deliver input one character at // a time, without waiting for you to press // ENTER noecho(); // This stops it from echoing keys you type nodelay(stdscr,1); // This makes it do NON-BLOCKING input. That is, // if you call getch() but there is no input // ready yet, it returns ERR immediately. getmaxyx(stdscr, screenH, screenW); if (screenW>99) windowW=99; else windowW=screenW; if (screenH>49) windowH=49; else windowH=screenH-1; w1=newwin(windowH,windowW,0,0); // create the main window (ht,wd,y0,x0) w2=newwin(1,windowW,windowH,0); // create the report window (ht,wd,y0,x0) w3=NULL; // create 3rd window when needed // we'll fill the map with solid '@'s... for (int x=0; x