Page 1 of 1

[Solved] NCurses and Typewriter Text (XCode,C++)

Posted: Mon Jul 26, 2010 6:34 pm
by TheThirdL3g
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:

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();
	
}

Working Code for future reference:

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);
}

Re: NCurses and Typewriter Text (XCode,C++)

Posted: Mon Jul 26, 2010 6:53 pm
by wearymemory
Perhaps nodelay is what you're looking for. You can find information on the specific behavior that this applies to Input Processing under the Delay Mode section here.

Re: NCurses and Typewriter Text (XCode,C++)

Posted: Mon Jul 26, 2010 7:36 pm
by TheThirdL3g
Thanks a lot!
Worked perfectly.

I'll post the working code for future reference:

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();
	
}