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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

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

Post 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);
}
Last edited by TheThirdL3g on Mon Jul 26, 2010 7:38 pm, edited 2 times in total.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

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

Post 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.
TheThirdL3g
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Wed Apr 15, 2009 6:40 pm

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

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