/**********************************************************************
functions for saving screen contents
programming - Hafees
feel free to modify according to ur needs. and if any help is needed just pm me.
***********************************************************************/
#define SEGMENT 0xB000
#define OFFSET 0x8000
#include <dos.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include "screen.h"
int savescreen(int x1,int y1,int x2,int y2,char **buff)
{
int i,j,countx,county;
char *buffer;
//add code to check whether co-ordinates are valid
buffer=new char[(((x2-x1+1)*(y2-y1+1)*2)+1)];
//buffer=(char*)malloc(((x2-x1+1)*(y2-y1+1)*2)+1); //allocates memory
if(buffer==NULL)
return -1; //returns -1 if memory is not allocated
//reads and stores window contents to buffer
countx=0;
county=0;
i= ((y1-1)*160) + ((x1-1)*2);
for(j=0;;j++)
{
*(buffer+j)=peekb(SEGMENT,OFFSET+i);
i++;
countx++;
if(countx/2>=x2-x1+1)
{
county++;
countx=0;
if(county>=y2-y1+1)
break;
i=((y1-1+county)*160+((x1-1)*2));
}
}
*(buffer+j+1)=0; //terminates buffer
*buff=buffer; //points to the new buffer
return strlen(buffer);
}
int restore_screen(int x1,int y1,int x2,int y2,char *buffer)
{
int i,j,countx,county;
if(buffer==NULL)
return -1; //returns -1 if buffer is NULL
//reads and re stores buffer contents to screen
countx=0;
county=0;
i= ((y1-1)*160) + ((x1-1)*2);
for(j=0;buffer[j];j++)
{
pokeb(SEGMENT,OFFSET+i,*(buffer+j));
i++;
countx++;
if(countx/2>=x2-x1+1)
{
county++;
countx=0;
if(county>=y2-y1+1)
break;
i=((y1-1+county)*160)+((x1-1)*2);
if(!buffer[j])
break;
}
}
return j/2; //returns number of characters restored
}
//function to restore screen with new attribute
int restore2(int x1,int y1,int x2,int y2,int attrib,char *buffer)
{
int i,j,countx,county;
if(buffer==NULL)
return -1; //returns -1 if buffer is NULL
//reads and re stores buffer contents to screen
countx=0;
county=0;
i= ((y1-1)*160) + ((x1-1)*2);
for(j=0;buffer[j];j++)
{
if(i%2) //if it is the attribute byte change the attribute
pokeb(SEGMENT,OFFSET+i,attrib);
else
pokeb(SEGMENT,OFFSET+i,*(buffer+j));
i++;
countx++;
if(countx/2>=x2-x1+1)
{
county++;
countx=0;
if(county>=y2-y1+1)
break;
i=((y1-1+county)*160)+((x1-1)*2);
}
}
return j/2; //returns number of characters restored
}
int make_shadow(int x1,int y1,int x2,int y2)
{
int i,j,countx,county;
int bg,fg,attrib=0x0f;
countx=0;
county=0;
i= ((y1-1)*160) + ((x1-1)*2);
for(j=0;;j++)
{
if(i%2) //if it is the attribute byte change the attribute
pokeb(SEGMENT,OFFSET+i,(char)attrib);
i++;
countx++;
if(countx/2>=x2-x1+1)
{
county++;
countx=0;
if(county>=y2-y1+1)
break;
i=((y1-1+county)*160)+((x1-1)*2);
}
}
return j/2; //returns number of characters restored
}
// to check save screen & restore screen
/*main()
{
char *b,i;
//saves entire screen
savescreen(10,3,20,10,&b);
clrscr();
printf("\n Press any key to restore the screen with YELLOW text...");
getch();
restore2(10,3,20,10,0x3,b);
getch();
}
*/