Author Topic: nCurses "mvaddchstr" Function  (Read 9540 times)

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
nCurses "mvaddchstr" Function
« on: May 04, 2012, 07:00:11 AM »
Konnichiwa,
Just wanted  to ask a question about nCurses functions and how they are really messing me up.  I want to save my screen, print some information (in this case, old messages), then restore the old screen.  To do some tests, I started off with this (note this is the C programming language):


unsigned int line1;
unsigned int *ptr1 = &line1;    
int count1;
int count2;


I made the pointer since both mvinchstr and mvaddchstr require a pointer for some reason. The two counts are for the nested for loops. Then:


mvinchstr(0, 0, ptr1);
/*Bleh put up old info blah whatever*/
for (count1 = 0; count1 < 34; count1++) {
    for (count2 = 0; count2 < 142; count2++) {
        mvaddch(count1, count2, ' ');
    }
}
mvaddchstr(0, 0, ptr1);


So, in theory, this should copy the first line of the screen, put up the old messages, blank the screen, then put back the first line of the old screen again.  However, I get this (I'm just testing it with a simple line for now):

Old Screen:
                 #
New Screen:
                                            #

I've checked the output from both mvinchstr and mvaddchstr, and neither are erring.  Mvinchstr is copying 142 characters (the length of the line I'm copying) and mvaddchstr is returning 0 (it would return -1 if it encountered an error).  Anybody know what's going on?  

If I run it piece by piece, mvinchstr runs fine, the old messages pop up fine, the screen clears fine, but mvaddchstr messes up.
« Last Edit: May 04, 2012, 07:03:46 AM by Pueo »
{O.o}
 |)__)
   ” ”   o RLY?

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: nCurses "mvaddchstr" Function
« Reply #1 on: May 04, 2012, 03:25:20 PM »
New Info:
(Wall of text ahead! Skip to the bottom for a summary, or read the full version for in-depth info)

To see if maybe the copying was not going correctly (and to see if the problem was on a certain line), I decided to go through the whole copy/print series of functions.  

If I copy the first line (and only the first line), then only print the first line, then it will print fine.  
However, if I copy specifically the first two lines, then print the first two lines, the first line is skewed, and the second line is correct.  
If I copy three lines, then print three lines, the third line is correct, the second line is skewed, and the first line is even more skewed.  
However, (here's where it gets weird) if I copy line four, which is a full line (it acts as a separator), then when I print it, line 4 is correct, but lines 3, 2, and 1 are skewed, except now they also are full lines (which they should not be).  
Then, if I copy 5 lines, line 5 is correct, but the lines are again skewed, and line four is only skewed by one column, whereas the others are all skewed up to 50 lines. Line 4 is also wrong (content-wise) and only shows one '#' where it should show a full line.
Copying line 6, lines 5 and 4 are skewed by 1 and 2 lines, respectively, while line 6 is correct and lines 3, 2, and 1 are skewed again.
And so on, for 34 lines.

Summary!
Whenever I print "x" amount of lines, line "x" is correct in every way, but the other lines are incorrect in myriad ways.  Look at the next post for a visual.
{O.o}
 |)__)
   ” ”   o RLY?

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: nCurses "mvaddchstr" Function
« Reply #2 on: May 04, 2012, 03:26:12 PM »
Visuals!
It should look like this (this is what it copies (I cut off a little of the full lines so it wouldn't loop)):
                 #
                 #
                 #
                 #############################################################################
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #
                 #############################################################################
                 #

It does look like this (what it prints):
                                            #
                                 #
                      #
                #
               #
              #
             #
            #
           #
                                           #
                                          #
                                         #
                                        #
                                       #
                                      #
                                     #
                                    #
                                   #
                                  #
                                #
                               #
                              #
                             #
                            #
                           #
                          #
                         #
                        #
                       #
                     #
                    #
                   #
                  #
                 #

Note how the very last line is correct.
« Last Edit: May 04, 2012, 03:30:07 PM by Pueo »
{O.o}
 |)__)
   ” ”   o RLY?

Ancient

  • Rogueliker
  • ***
  • Posts: 453
  • Karma: +0/-0
    • View Profile
Re: nCurses "mvaddchstr" Function
« Reply #3 on: May 04, 2012, 06:43:08 PM »
As I see it you call mvaddchstr correctly. Your mistake lies in usage of mvinchstr.

First, a couple of comments. Use chtype instead of unsigned int. Uint is likely to stab you in the back when you port to another platform. To store screen data you need an array. Pointer to single integer is not going to cut it. I am surprised your program prints anything instead of just crashing.

I think most instructive would be a snippet that loads screen into an array. Tested in PRIME environment and appears to be working. A C99 strict code excerpt for capturing for 64 columns and 20 lines main window follows.

Code: [Select]
    chtype buffer[20][64 + 1];
    for (int y = 0; y < 20; ++y) {
        mvwinchstr (mMainWin, y, 0, buffer[y]);
    }
Michał Bieliński, reviewer for Temple of the Roguelike

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: nCurses "mvaddchstr" Function
« Reply #4 on: May 04, 2012, 07:46:22 PM »
@Ancient,

Thanks a lot. I think this makes this the second time you helped me with my code  :D

Your little code snippet works, after I adjust the dimensions for my screen.  Since my code-fu obviously needs some help, a couple questions:

Why do you initialize the buffer with [64+1] if the screen is only 64 columns?
I've always used the '++' shortcut after the variable, is there a difference when you use it before the variable?
« Last Edit: May 05, 2012, 02:35:56 PM by Pueo »
{O.o}
 |)__)
   ” ”   o RLY?

Ancient

  • Rogueliker
  • ***
  • Posts: 453
  • Karma: +0/-0
    • View Profile
Re: nCurses "mvaddchstr" Function
« Reply #5 on: May 06, 2012, 09:05:29 PM »
No problem!

Why do you initialize the buffer with [64+1] if the screen is only 64 columns?
According to ncurses manual inchstr () returns NULL terminated array of chtype. That one last cell is precisely for the trailing sentinel.

Quote
I've always used the '++' shortcut after the variable, is there a difference when you use it before the variable?
In for loop there is not. Otherwise standard pre-incrementation and post-incrementation differences are in effect. Good explanation is here: http://www.gamedev.net/topic/337133-i-vs-i/

I learned C from K&R book and everywhere they used pre-incrementation unless the other was explicitly needed. I find it slightly easier to read. It also used to be faster. Today compilers are smart enough to detect useless i++ and change them all to ++i.
Michał Bieliński, reviewer for Temple of the Roguelike

Pueo

  • Rogueliker
  • ***
  • Posts: 263
  • Karma: +0/-0
    • View Profile
    • Email
Re: nCurses "mvaddchstr" Function
« Reply #6 on: May 08, 2012, 05:54:46 AM »
According to ncurses manual inchstr () returns NULL terminated array of chtype. That one last cell is precisely for the trailing sentinel.
Ok, that makes sense.

In for loop there is not. Otherwise standard pre-incrementation and post-incrementation differences are in effect.
Thanks for the link  :)

I learned C from K&R book and everywhere they used pre-incrementation unless the other was explicitly needed. I find it slightly easier to read. It also used to be faster. Today compilers are smart enough to detect useless i++ and change them all to ++i.
I'm learn(ing) from C for Dummies (Vol. 1 and 2, from 1994 and 97), so I guess I either didn't get there yet or just missed it.
{O.o}
 |)__)
   ” ”   o RLY?