BRHSM
BRHSM

Reputation: 884

Ncurses lines not connected when using this kind of algorithm

I have an algorithm where i have a 3 X 3 box:

typedef struct _WIN_struct {

    int startx, starty;
    int height, width;
    WIN_BORDER border;
}WIN;

p_win->border.ls = '|';
p_win->border.rs = '|';
p_win->border.ts = '-';
p_win->border.bs = '-';
p_win->border.tl = '+';
p_win->border.tr = '+';
p_win->border.bl = '+';
p_win->border.br = '+';

so this looks like this:

+-+
| |
+-+

when a key is pressed the box moves in the direction it has to go then, I want draw a line behind it using a simple mvaddch(y,x+1,'*'); (in this example the box moves left so it makes the * at the right side of the box)

But when I change direction the lines do not connect. this happens probably because the box is 3 X 3 instead of 1 X 1 (I got it to work with a 1 X 1 box before but i tried to change the box to 3 X 3 and it messe up).

This is how it looks like when I change direction 4 times and try to create a square:

Image

You can see that the lines do not add up (I can't do a screen capture because I am on LTSP without administrator rights :( )

here is a kind of closeup on how it's made:

****
....
..*.
..*.

Here it's starting by going up then I go left. the points are just for formatting.

here is the full program: http://pastebin.com/EGabVfwY

Upvotes: 0

Views: 139

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54563

The lines do not connect because you are adding the offset from y,x in the wrong direction (but consistently). For instance, when moving up, the program does this:

mvaddch(y+1,x,'*');
y--;

However, moving *up), the y coordinate will decrease. If you change that pair of statements to

y--;
mvaddch(y,x,'*');

it will do what you intend. Also, since each of the 4 cases (up, down, left and right) ends with the same mvaddch, you could move that statement just past the switch statement, and simplify the program a little.

You can also make the program a little more interesting by using line-drawing characters. To start, add

#include <locale.h>

at the top, and call

setlocale(LC_ALL, "");

just before initscr(). Then you could replace the ASCII line-drawing codes with something like this:

p_win->border.ls = ACS_VLINE;
p_win->border.rs = ACS_VLINE;
p_win->border.ts = ACS_HLINE;
p_win->border.bs = ACS_HLINE;
p_win->border.tl = ACS_ULCORNER;
p_win->border.tr = ACS_URCORNER;
p_win->border.bl = ACS_LLCORNER;
p_win->border.br = ACS_LRCORNER;

If you link with the ncursesw library, that should be able to show nice lines in a UTF-8 locale.

Upvotes: 1

Related Questions