Ncurses novice - starting with GNU C

Simple life

I am returning to C to help energize my programming contribution. The last time I learned to program, I went to college, working with Turbo C on MSDOS. Simple ASCII animations became obsessive when I found that Borland friendly <conio.h> alone is included. There was a lot of fun with gotoxy and textcolor , and shortly before I wrote games like snake and pong. It was a very useful way to learn the language, and each game pushed me further as I became more ambitious.

Ncurses

I would like to start with similar projects like a game. Today, though, I am on a mac with a dusty linux machine in the corner. I could run my favorite Turbo C in dosbox (a fairly ubiquitous platform), but I want to learn C so that I can develop something that compiles naturally on any BSD or unix platform. I was told that ncurses is the way forward, but the GNU site pretty much went through my head. That day I had a friendly textmode function, and I was a print expression away from the pong. ncurses seems a lot more powerful.

Whistling

There must be many people who were in this situation. I am looking for a suitable tutorial or resource that will help me deal with what ncurses is and how to work with it. Any advice or similar stories would also be of great interest!

+7
c gcc animation ncurses
source share
4 answers

Yup, ncurses is the library you are looking for. As an example, here (n) curses the gotoxy equivalent:

NAME

move, wmove - move cursor cursor

SYNTAX

  #include <curses.h> int move(int y, int x); int wmove(WINDOW *win, int y, int x); 

DESCRIPTION

These procedures move the cursor associated with the window to row y and column x. This procedure does not move the physical cursor of the terminal until an update is called. The specified position relative to the upper left corner of the window, which is (0,0).

Addendum:

In your comment, you are asking about curses windows - I don't think I can really improve what ncurses man says on this page, so I will simply quote this:

The ncurses library allows the manipulation of data structures called windows, which can be like two-dimensional arrays of characters representing all or part of a CRT screen. The default window called stdscr, which is the size of the terminal screen. others can be created using newwin.

Note that curses are not handled by overlapping windows that pane (3CURSES). This means that you can use stdscr or split the screen into tiled windows and not use stdscr at all. Mixing two unpredictable and undesirable effects.

Windows refers to variables declared as WINDOW *. This data structure manipulates the procedures described here and elsewhere in the ncurses manual pages. Among those, basic procedures are moved and added. More general versions of these routines include names starting with w, allowing the user to specify a window. Procedures not starting with w affect stdscr.

After using the routines to control the window, update, curse to make the user's CRT screen look like stdscr. The characters in the window are indeed of type chtype, (character and attribute data), so other character information can also be stored with each character.

So, in general, you can safely ignore the whole thing of the window and just use the stdscr window.

+5
source share

Well, on UNIX-like systems like BSD and Linux, you definitely want to use ncurses , since terminal emulators make your life very miserable if you don't.

You should probably write yourself a simple wrapper containing the necessary functionality and implement it differently for different operating systems. There are several functions on Windows that you can use so that the part is not too complicated.

+2
source share

Here is a small program to get you started.

 #include <curses.h> int main(void) { int ch; /* The whole program needs error-checking */ initscr(); noecho(); cbreak(); printw("Hit Ctrl+C to exit ...\n\n"); for (;;) { ch = getch(); printw("Value of char: %d (%02x)\n", ch, ch); } endwin(); return 0; } 

Remember to tell the linker to add the libcurses library.

 gcc ... -lcurses 
+1
source share

There is a library that provides Turbo C as an IO console for Linux: TurboC Also linked: CONIO for DevC ++

+1
source share

All Articles