views:

374

answers:

4

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!

+1  A: 

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

You should probably write yourself a simple wrapper, containing the functionality you need and implement it differently for different OSes. On Windows there are a few console functions you can use so that part won't be too hard.

Joey
+3  A: 

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.

caf
thanks, pointing out the window aspect helped me realize what the question 'should' be. What is a window? Is it akin to a virtual page which is switched or streamed to the console? I am baffled by it.
deau
+1  A: 

Here's a little 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
pmg
well played! A most helpful program!
deau
+1  A: 

There is a library providing Turbo C like console IO to Linux: TurboC Also related: CONIO for DevC++

Amigable Clark Kant
nice, thanks very much
deau