I'm making my way through some ncurses tutorials as I want to improve some of my C programming skills. I'm at a point where the tutorial is emulating a simple tennis game.
What confuses me is this:
* think of posX as relating to rows, the bottom of the screen will be at
maximum Y value, so subtract 2, to set the racket near the bottom */
p.posX = SCREEN_HEIGHT - 2;
/* think of posY as relating to a column, the middle of the screen will be
at the middle of the screen_width, divided by 2, to set racket at center */
p.posY = SCREEN_WIDTH / 2;
and say, the racket is moved left by the user:
void moveRacketLeft() {
if (p.posY - 2 > 0) { // ensure racket is still on screen
mvprintw(p.posX, p.posY-1, " "); // if true, it will print an empty space, deleting the racket
p.posY --; // remember to decrement, as we are going to the left
}
}
What confuses me about this, is that the X and Y positions have seem to be reversed and I don't know how to logically 'get it'.
Is there some kind of grey area between conventional coordinates in C programming and the use of the ncurses library? Or is the tutorial making a mistake?
Many thanks
The x
and y
coordinates in curses are as you expect: x
is the horizontal coordinate, corresponding to columns. y
is the vertical coordinate, cooresponding to rows.
But in curses, these coordinates are usually given in the order y
, x
. For example, the prototype to mfprintw
mentioned in your code snippet is:
int mvprintw(int y, int x, const char *fmt, ...);
And there are even functions (or macros. rather) that have yx
in their name, e.g. getyx
.
I imagine that this notation was chosen, because it reflects the way that two-dimensional arrays are addressed in C: array[row][col]
.