Close Back

/*
*	typer.h
*
*****************************************************************************
*
*	The Matrix Typer for C
*     	~~~~~~~~~~~~~~~~~~~~~~
*****************************************************************************
*
*     	Author           :  Rajesh _ThE gReAt
*	Built on         :  19/01/2002
*	Last Modified on :  19/01/2002
*
*	Known errors : When the function returns it sets textcolor
*		       to LIGHTGRAY which may be undesirable .
*		       So double check your text colors after
*		       you have called any fuction in TYPER.H
*
*****************************************************************************
*	DISCLAIMER:
*****************************************************************************
*
*	Programmers may incorporate any or all code into their programs,
*	giving proper credit within the source. Publication of the
*	source routines is permitted so long as proper credit is given
*	to Rajesh _ThE gReAt.
*
*	Copyright (C) 1990, 1991 by Rajesh _ThE gReAt.  You may use this program, or
*	code or tables extracted from it, as desired without restriction.
*	I can not and will not be held responsible for any damage caused from
*	the use of this software.
*
*****************************************************************************
* This source works with Turbo C 2.0 and Turbo C++ 3.0 and above.
*****************************************************************************
*	Rajesh _ThE gReAt is a synonym of Rajesh Goli
*	Source freely distributable 
*****************************************************************************/

#ifndef __TYPER_H
#define __TYPER_H
#endif

#if !defined( __STDIO_H )
#include <stdio.h>
#endif	// __STDIO_H

#if !defined( __CONIO_H )
#include <conio.h>
#endif	// __CONIO_H

#define BACKCOLOR BLACK
#define CLR_WHILE_RETURN 0
#define CLR_WHILE_ERROR 1
#define UNGETCH_EXIT 0

int typer(char *,int ,int ,int ,int ,int );
int loop_typer(char *,int ,int ,int ,int ,int ,int );
void clr(int );

int dont_clear = 0;

int typer(char *msg,int highcolor,int normcolor,int delay_val,int x,int y)
{
	int i=0;
	char store;

	fflush(stdin);
	
	if(!dont_clear)
	{
		textcolor(BACKCOLOR);
		textbackground(BACKCOLOR);
		printf("asdf");
		clrscr();
	}

	gotoxy(x,y);
	while(msg[i])
	{
		textcolor(highcolor);
		if(msg[i] != '\n' && msg[i] != '\t')
		{
			cprintf("%c",msg[i]);
			delay(delay_val);
			printf("\b");
			textcolor(normcolor);
			cprintf("%c",msg[i]);
		}
		else
			printf("%c",msg[i]);

		if(kbhit())
			{

		#if( CLR_WHILE_ERROR == 1 )
				clrscr();
		#endif
				store = getch();
			
				/* We probably need to exit() if this is the case , ESC key is hit*/
				if(store == 0x1b)
				{
			#if( UNGETCH_EXIT == 1 )
					ungetch(store);
			#endif
					return -2;
				}

		#if( UNGETCH_EXIT == 1 )
				ungetch(store);   /* This could be of some use to other funcs */
		#endif
				return -1; /* We were interrupted at the wrong time */
			}
		i++;		
	}

	textcolor(LIGHTGRAY);
#if( CLR_WHILE_RETURN == 1 )
	clrscr();
#endif

	if(kbhit())
		store = getch();

	if(store == 0x1b) /* We probably need to exit() */
		return -2;
	else
		return 0;	
}

int loop_typer(char *msg,int highcolor,int normcolor,int delay_val,int x,int y,int clr_or_not)
{
	int i=0;
	char cleared = 0;
	char store;

	fflush(stdin);

	while(!kbhit())
	{
		
		if(clr_or_not || !cleared)
		{
			textcolor(BACKCOLOR);
			textbackground(BACKCOLOR);
			printf("asdf");
			if(!dont_clear)
				clrscr();
			cleared = 1;
		}

		gotoxy(x,y);

		i = 0;
		while(msg[i])
		{
			
			textcolor(highcolor);
			if(msg[i] != '\n' && msg[i] != '\t')
			{
				cprintf("%c",msg[i]);
				delay(delay_val);
				printf("\b");
				textcolor(normcolor);
				cprintf("%c",msg[i]);
			}
			
			else
				printf("%c",msg[i]);
			
			if(kbhit())
			{

		#if( CLR_WHILE_ERROR == 1 )
				clrscr();
		#endif

				store = getch();				

				/* We probably need to exit() if this is the case */
				if(store == 0x1b)
				{
			#if( UNGETCH_EXIT == 1 )
					ungetch(store);
			#endif
					return -2;
				}


		#if( UNGETCH_EXIT == 1 )
				ungetch(store);
		#endif
				return -1; /* We were interrupted at the wrong time */
			}
			i++;		
		}
	}
	
	textcolor(LIGHTGRAY);
#if( CLR_WHILE_RETURN == 1 )
	clrscr();
#endif

	if(kbhit())
		store = getch();

	if(store == 0x1b) /* We probably need to exit() */
		return -2;
	else
		return 0;	
}

/* Prevent the typers from clearing the screen initially
   if clr_or_not == 0
   else 
	it allows to clear initially
*/

void clr(int clr_or_not)
{
	dont_clear = !clr_or_not;
}