PDA

View Full Version : saving screen to file



z0diac
02-23-2003, 05:07 AM
This is a program to draw images, and save the contents of the screen to a file. A file of the specified size is being created, but it cannot be read back. I use Turbo C++ compiler in MS-DOS. Please help.



/* ARTIST.C */

#include <stdio.h>
#include <graphics.h>
#include <math.h>

int main(void)
{
int in, index = 1;
int xloc = 0, yloc = 0, x1 = 0, y1 = 0;
FILE *fpointer;
void *gpointer;
/* the void pointer which will be pointed to the area of screen memory, here B800:0000 */
char fillarray[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
/* setting the fill pattern */
int g_driver, g_mode;

g_driver = CGA; /* using the cga mode */
g_mode = CGAC3;

initgraph(&g_driver, &g_mode, "..\\bgi");

setgraphmode(CGAC3);

setcolor(index);

setfillpattern(fillarray, 1);

while((in = getch()) != 'q') /* loop quits if 'q' is entered */
{
if (!in)
in = getch() + 128; /* for using the arrow keys */

switch (in)
{
case 208: /* up */
yloc++;
break;

case 203: /* left */
xloc--;
break;

case 205: /* right */
xloc++;
break;

case 200: /* down */
yloc--;
break;

case 'p': /* put pixel */
putpixel(xloc, yloc, index);
moveto(xloc, yloc);
x1 = xloc;
y1 = yloc;
break;

case 'l': /* draw line */
lineto(xloc, yloc);
break;

case 'r': /* draw a rectangle */
rectangle(x1, y1, xloc, yloc);
break;

case 'e': /* draw ellipse */
ellipse((x1+xloc)/2, (y1+yloc)/2, 0, 360, abs(xloc-x1)/2, abs(yloc-y1)/2);
break;

case 'f': /* fill */
floodfill(xloc, yloc, index);
break;

case ' ': /* change color */
(index >= 3 ? index = 1 : index++); /* switches thru the colours available in cga mode */
setcolor(index);
break;

case 'd': /* delete */
index = 0;
setcolor(index); /* draw in the background colour */
break;


/* HERE!!! the saving code >> */


case 's': /* save screen */
if ((fpointer = fopen("..\\output\\screen.dmp", "wb")) != NULL)
{
gpointer = (void *)0xb8000000;
/* works only if the cast (void *) is used. used to work without cast. B800:0000 is the starting location for the screen memory in cga mode. */
fwrite(gpointer, 16*1024, 1, fpointer);
/* stores the screen into a file. the file is being created */
fclose(fpointer);
}
else
outtextxy(10, 10, "Error writing screen.dmp");
break;

case 'o': /* restore from file */
if ((fpointer = fopen("..\\output\\screen.dmp", "rb")) != NULL)
{
gpointer = (void *)0xb8000000;
/* works only if the cast (void *) is used. used to work without cast. B800:0000 is the starting location for the screen memory in cga mode. */
fread(gpointer, 16*1024, 1, fpointer);
/* reads the file into the screen memory. it seems the reading doesn't work */
fclose(fpointer);
}
else
outtextxy(10, 10, "Error reading screen.dmp");
break;


/* << HERE!!! the saving code */


default:
;
} /* end of switch */
} /* end of while */

return 0;
}

/* EOF */

Travis Dane
02-23-2003, 09:27 AM
Usos Coda Tagos.

powinda
02-23-2003, 11:54 AM
/* ARTIST.C */

#include <stdio.h>
#include <graphics.h>
#include <math.h>

int main(void)
{
int in, index = 1;
int xloc = 0, yloc = 0, x1 = 0, y1 = 0;
FILE *fpointer;
void *gpointer;
char fillarray[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55};
int g_driver, g_mode;

g_driver = CGA;
g_mode = CGAC3;

initgraph(&g_driver, &g_mode, "..\\bgi");

setgraphmode(CGAC3);

setcolor(index);

setfillpattern(fillarray, 1);

while((in = getch()) != 'q')
{
if (!in)
in = getch() + 128;
switch (in)
{
case 208: /* up */
yloc++;
break;
case 203: /* left */
xloc--;
break;
case 205: /* right */
xloc++;
break;
case 200: /* down */
yloc--;
break;
case 'p': /* put pixel */
putpixel(xloc, yloc, index);
moveto(xloc, yloc);
x1 = xloc;
y1 = yloc;
break;
case 'l': /* draw line */
lineto(xloc, yloc);
break;
case 'r': /* draw a rectangle */
rectangle(x1, y1, xloc, yloc);
break;
case 'e': /* draw ellipse */
ellipse((x1+xloc)/2, (y1+yloc)/2, 0, 360, abs(xloc-x1)/2, abs(yloc-y1)/2);
break;
case 'f': /* fill */
floodfill(xloc, yloc, index);
break;
case ' ': /* change color */
(index >= 3 ? index = 1 : index++);
setcolor(index);
break;
case 'd': /* delete */
index = 0;
setcolor(index);
break;


/* HERE!!! >> */


case 's': /* save screen */
if ((fpointer = fopen("..\\output\\screen.dmp", "wb")) != NULL)
{
gpointer = (void *)0xb8000000; /* works only if the cast (void *) is used. used to work without cast */
fwrite(gpointer, 16*1024, 1, fpointer); /* stores the screen into a file. the file is being created */
fclose(fpointer);
}
else
outtextxy(10, 10, "Error writing screen.dmp");
break;
case 'o': /* restore from file */
if ((fpointer = fopen("..\\output\\screen.dmp", "rb")) != NULL)
{
gpointer = (void *)0xb8000000; /* works only if the cast (void *) is used. used to work without cast */
fread(gpointer, 16*1024, 1, fpointer); /* reads the file into the screen memory. it seems the reading doesn't work */
fclose(fpointer);
}
else
outtextxy(10, 10, "Error reading screen.dmp");
break;


/* << HERE!!! */
default:
;
}
}

return 0;
}

/* EOF */

Hammer
02-23-2003, 07:00 PM
Moved to the DOS board