

#include <stdio.h>
#define XD 30
#define YD 5
#include <dos.h>
#include <conio.h>
#include<process.h>

char txt[YD][XD]={"Stan","Brani","C++","Haskovo","Pernik"};
char col,curs;

void init(int *x,int *y)
{	int f,c,cr;

	printf("\n input background colour ");
	scanf("%d",&f);
    f&=0x7;
	printf("\n input character colour ");
	scanf("%d",&c);
    c &= 0x15;
	printf("\n input cursor colour");
	scanf("%d",&cr);
    cr&=0x7;
	printf("\n input X co = ");
	scanf("%d",x);
    if(*x+XD>79){printf("ERROR  out of range  x=%d",*x);exit(0);}

	printf("\n input Y co = ");
	scanf("%d",y);

    if(*y+YD>24){ printf("ERROR  OUt of range  y=%d",*y);exit(0);}
	col=(char)f*16+c;
	curs=(char)cr*16;
	curs|=col&0x8f;

	printf("\n %d   %d  ",(int)col,(int)curs);
}

int getkey()
{
	int a;
	a=getch();
	if(!a) a=getch()+256;
	return a;}

void owin(int x,int y,char *buf)
{
	unsigned char far *p;int a,i,j,k,ba;
	p=(unsigned char far*)MK_FP(0xb800,0);

	for(j=0;j<YD;j++)
  {	k=0;

	for(i=0;i<XD;i++)
      { k+=(txt[j][i]==NULL);
	a=(i+x)*2+(y+j)*160;
	ba=2*(i+j*XD);
	buf[ba]=p[a];buf[ba+1]=p[a+1];
	p[a+1]=col;
	if(k)
	p[a]=' ';
	else p[a]=txt[j][i];

	}}}

void cwin(int x,int y,char *buf)
{
	int i,j,a,ba;unsigned char far *p;
	p=(unsigned char far *)MK_FP(0xb800,0);

	for(j=0;j<YD;j++)
   {    for(i=0;i<XD;i++)
     {	a=(i+x)*2+(y+j)*160;
	ba=2*(i+j*XD);
	p[a]=buf[ba];p[a+1]=buf[ba+1];
			}}}

void mcurs(int x,int y,int k)
{	unsigned char far *p;int i,a;char c;
    p=(unsigned char far *)MK_FP(0xb800,0);

	for (i=0;i<XD;i++)
   {	a=(x+i)*2+y*160+1;
	p[a]=col;
	p[a+160*k]=curs;

	}}
int move(int x,int y)
{
	int k,yc,kl;

	yc=y;
	mcurs(x,y,0);

	while(kl!=13)
    {
	kl=getkey();

	k=0;
	k-=(kl==328)&&(yc!=y);
	k+=(kl==336)&&(yc!=YD+y-1);
	mcurs(x,yc,k);
	yc+=k;}

	return (yc);}


int main ()
{	char buf[XD*YD*2];unsigned char far *p,b[XD];
	int y,x,yc,i;
	p=(unsigned char far *)MK_FP(0xb800,0);
	b[XD]=NULL;

	init(&x,&y);

	owin(x,y,buf);
	yc=move(x,y);

	for(i=0;i<XD;i++)
    {	b[i]=p[(x+i)*2+yc*160];}
	cwin(x,y,buf);
	b[i]=NULL;
	printf(" \n  %s  \n",b);

	return (0);}


