Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: A Question on Curses  (Read 3086 times)

palverz

  • Escaped Lunatic
    • View Profile
    • My dev
A Question on Curses
« on: April 01, 2012, 07:34:47 pm »

I am working on using curses for a game of my own and have run into a snag that is frustrating me to no end, I am sure its just an armature mistake.

I have a function to get a number from the player based on the way Tarn's last official update to LCS was using mvgetstr() and atoi()

Well I have it laid out where the left side of the screen you select an item using my function that calls mvgetstr() and after I display some options on the right hand side, while the left side is still there, both the selection portions are in a loop to catch invalid selections and the problem is once the first function call to mvgetstr is made and carriage return is hit by the player it doesn't wait for input at the second mvgetstr(). I tried throwing a getch() in there to catch it but no luck.... Any idea what I may be doing wrong.... Ive tried using getstr() and have the commands from my function directly in my other code and no luck.

How do I clear that buffer to make it stop for getstr() calls?  Ive tested and debugged enough to be 80% sure my problem is something to do with multiple calls to getstr()...... will erase() fix this if so that means Ill have to redraw the right hand side because I want both sides to be displayed.
Logged

Jonathan S. Fox

  • Bay Watcher
    • View Profile
    • http://www.jonathansfox.com/
Re: A Question on Curses
« Reply #1 on: April 01, 2012, 10:15:36 pm »

I did a bit of Google searching, and there appears to be a flushinp() function in Curses to flush the input buffer. I would start there and see if it solves the problem.
Logged

palverz

  • Escaped Lunatic
    • View Profile
    • My dev
Re: A Question on Curses
« Reply #2 on: April 02, 2012, 08:04:36 pm »

Well unfortunately that didn't seem to work or do anything at all..... I double checked that the looping isn't caused by me accidentally and concluded that I am even more sure its a problem with the getstr() I will do even more googling I remember seeing someone else with this problem before. I tried erase() and refresh() no luck...... :( 
Logged

palverz

  • Escaped Lunatic
    • View Profile
    • My dev
Re: A Question on Curses
« Reply #3 on: April 02, 2012, 08:28:39 pm »

Ok in order to test a little more I did this ditty:

getstr(str1);
getstr(str2);

addstr(str1)
addstr(str2)



and that works just fine which means it have to a problem with me turning off the echo and stuff on the in my function this is my function:

int getINT(int x, int y)
{
      int val;
      char str[50];
  //   keypad(stdscr,FALSE);
  //  raw_output(FALSE);
      echo();
      curs_set(1);
      mvgetstr(x,y ,str); << Modified this doh! I had my x and y crossed double doh
      val=atoi(str);
      curs_set(0);
      noecho();
  //    raw_output(TRUE);
  //    keypad(stdscr,TRUE);
      return val;   
}

So now my 85% sure it wasnt me I am 100% it is me somewhere lol.......

EDIT:
I was able to get it to kinda work the way I want for now although it has anomalies when typeing by commenting out the lines i commented on here
« Last Edit: April 02, 2012, 09:35:51 pm by palverz »
Logged

Karlito

  • Bay Watcher
    • View Profile
Re: A Question on Curses
« Reply #4 on: April 02, 2012, 10:23:29 pm »

You might have more luck asking in the coding thread or stack overflow, or something.
Logged
This sentence contains exactly threee erors.

Jonathan S. Fox

  • Bay Watcher
    • View Profile
    • http://www.jonathansfox.com/
Re: A Question on Curses
« Reply #5 on: April 03, 2012, 05:47:37 am »

Speaking entirely as an armchair programmer here, that seems an imperfect solution -- by not toggling keypad(), you may be getting degenerate characters (like arrow keys...) fed into input stream and then echoed to the screen. You mentioned typing anomalies, so you may have already seen something like this.

Speculatively, the problem may be a result of incorrect handling of Windows newlines when you disable raw output in PDCurses. I would intuitively expect that to only affect the echo text though, not the input itself, and I really don't see how enabling (rather than disabling) translation would cause problems. My best suggestion would be to try to isolate it to one of the two changes -- either the raw_output(FALSE) or the keypad(stdscr, FALSE). Or have you already determined that it's specifically both, and commenting out only one or the other of them doesn't fix it?

The other thing I would try is making two string reads inside all the code sandwich for enabling/disabling echo and changing the translation settings. Given that you have unexpected behavior, and without knowing how the library was coded, I would test this before ruling out the possibility that merely toggling those settings back and forth between reads is messing something up.

One thing I can say with actual knowledge to back it up (rather than all the derpy "I don't actually know this stuff either, but here are some ideas" advice I've been offering up) is that your x and y are crossed in mvgetstr(). I know you acknowledge this, but I'm reading that acknowledgement as saying that you saw the y parameter first and mistakenly thought this was an error, so you switched them to make x first... when in fact, the y parameter does come first in Curses. I see a couple bad reasons and no good reasons to make the y parameter first, but apparently whoever wrote the original library thought otherwise.
Logged

palverz

  • Escaped Lunatic
    • View Profile
    • My dev
Re: A Question on Curses
« Reply #6 on: April 03, 2012, 04:48:47 pm »

I am at a training right now so my "work on this project" time is much more limited than when I am home


Pt 1: Yeah its a horrible way of fixing it like I said it does cause anomalies and I will for sure take a shot at your theory to test both individually. But it makes that part of my code work at least for now.....

Pt 2: I think you are right once again

Pt 3: String reads will probably help lol I am not the best programmer, and I have been out of the loop for a long time with the exception of bash scripts.

pt 4: they are crossed but I had them crossed two when calling the function and I was ending up with my input way up on top when I was expecting it low.... (My call to the function happens in a loop listing vector items)

Now that I have this semi working though I noticed I am having a major problem (CTD type ones) with my understanding of using vectors and addressing for custom class objects and will need to take those questions up with a coding forum like was mentioned above, (I am trying to take a string each  from two separate class types stored each in its own vector to build one string a prefix and a main word, lol I am 100% sure there is an easier way to do it)

(If I ever go fully open source I know the people who look at this garbage will cringe)

Thanks a lot for your help and Ideas when I have a chance to try I will post results in case someone else ever ends up in the same place.
Logged

Jonathan S. Fox

  • Bay Watcher
    • View Profile
    • http://www.jonathansfox.com/
Re: A Question on Curses
« Reply #7 on: April 03, 2012, 05:53:08 pm »

By "string reads" I just mean try calling mvgetstr() twice right after one another, with all the echo and raw_output stuff around them, and see if it causes the same problem. Just as a further way to try to figure out what the problem is, since I'm not certain myself. :)
Logged

palverz

  • Escaped Lunatic
    • View Profile
    • My dev
Re: A Question on Curses
« Reply #8 on: April 08, 2012, 11:17:53 am »

Ha this is interesting.... I moved along fixed some problems with my vectors which weren't as bad as I thought they would be, then I went back uncommented those lines and it works just fine I am not sure what I changed that would have caused that issue. So my function now works as it was intended originally :) .
Logged

Jonathan S. Fox

  • Bay Watcher
    • View Profile
    • http://www.jonathansfox.com/
Re: A Question on Curses
« Reply #9 on: April 08, 2012, 05:22:15 pm »

Nice. Glad to hear it.
Logged