Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ncurses novice - starting out with GNU C

A simpler life

I am returning to C to help reinvigorate my programming lobe. The last time I learned to program I was at college working with Turbo C on MSDOS. Simple ASCII animations became an obsession once I found Borland's friendly <conio.h> one include away. A lot of fun was had with the gotoxy and textcolor functions and it wasn't long before I was writing games like snake and pong. It was a very rewarding way to learn a language, and each game pushed me further as I got more ambitious.

ncurses

I'd like start with similar game type projects. Today though, I'm on a mac with a dusty linux machine in the corner. I could launch my beloved Turbo C in dosbox (an ubiquitous enough platform) but I want to learn C such that I can develop something that compiles naturally on any BSD or unix platform. I've been told that ncurses is the way forward but the GNU site has largely gone over my head. Back in the day I had my friendly textmode function and I was aprint expression away from pong. ncurses seems a lot more powerful.

whoosh

There must be many people who've been in this situation. I'm looking for a relevant tutorial or resource that will help me grapple with what ncurses is and how to work with it. Any tips or similar stories would be of great interest too!

like image 888
deau Avatar asked Oct 27 '09 16:10

deau


1 Answers

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

NAME

move, wmove - move curses window cursor

SYNOPSIS

   #include <curses.h>

   int move(int y, int x);
   int wmove(WINDOW *win, int y, int x);

DESCRIPTION

These routines move the cursor associated with the window to line y and column x. This routine does not move the physical cursor of the terminal until refresh is called. The position specified is relative to the upper left-hand corner of the window, which is (0,0).

Addendum:

In your comment you ask about curses windows - I don't think I can really improve upon what the ncurses man page says on this, so I'll just quote it:

The ncurses library permits manipulation of data structures, called windows, which can be thought of as two-dimensional arrays of characters representing all or part of a CRT screen. A default window called stdscr, which is the size of the terminal screen, is supplied. Others may be created with newwin.

Note that curses does not handle overlapping windows, that's done by the panel(3CURSES) library. This means that you can either use stdscr or divide the screen into tiled windows and not using stdscr at all. Mixing the two will result in unpredictable, and undesired, effects.

Windows are referred to by variables declared as WINDOW *. These data structures are manipulated with routines described here and elsewhere in the ncurses manual pages. Among those, the most basic routines are move and addch. More general versions of these routines are included with names beginning with w, allowing the user to specify a window. The routines not beginning with w affect stdscr.

After using routines to manipulate a window, refresh is called, telling curses to make the user's CRT screen look like stdscr. The characters in a window are actually of type chtype, (character and attribute data) so that other information about the character may also be stored with each character.

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

like image 62
caf Avatar answered Oct 11 '22 01:10

caf