[Solved] NCurses and Typewriter Text (XCode,C++)
Posted: Mon Jul 26, 2010 6:34 pm
I'm trying to create a typewriter effect for a console app and I'm having some trouble with a part of it. I can get the text to display one letter at a time, but I'm having trouble ending the loop if a keypress is found and print the rest of the text. I started with a for loop then went to a while to try "while((ch = getch()) < 0) &&....)" but it just waits for input. I have the main function in raw() "mode" and I figured that would make it not wait for input, but I'm pretty new to ncurses and I'm stuck on where to go. Any help would be very appreciated.
Here's the function:
Working Code for future reference:
Here's the function:
Code: Select all
void twtext(char text[]) {
int twdelay = 35;
if (strlen(text) >= 25)
twdelay = 10;
else
twdelay = 35;
int cb;
int i = 0;
//Just waits at input. Without the first part it displays the text fine.
while (((cb = getch()) < 0) && (i < strlen(text))) {
if (cb > 0) { //If keypressed (attempt)
twdelay = 0;
}
printw("%c", text[i]);
refresh();
msleep(twdelay);
i++;
}
printw("\n");
refresh();
}
Code: Select all
void twtext(char text[]) {
raw();
int twdelay = 35;
if (strlen(text) >= 25)
twdelay = 10;
else
twdelay = 35;
int cb;
int i = 0;
nodelay(stdscr, true);
while ((cb = getch()) && (i < strlen(text))){
if (cb > 0) {
twdelay = 0;
}
printw("%c", text[i]);
refresh();
msleep(twdelay);
i++;
}
printw("\n");
refresh();
nodelay(stdscr, false);
}