PRODUCT : Borland C++ NUMBER : 1396 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 1/3 TITLE : How to save/load an entire screen image with BGI // Demo of techniques for saving and restoring a 640x480 graphics // screen to a file. Since the whole screen will take more than // 64K to save, and an image for getimage() or putimage() can be // 64K at most, we'll divide the screen into 4 sections for // saving and restoring // Limitations: For simplicity, this program does NOT check to // see if there is sufficient memory for its allocations. It // does not check to see if you have passed a valid file name to // the save/restore functions. It also assumes that you are in // the same graphics mode that you were when you saved the // screen. // compile in LARGE model (or compact or huge) and be sure to // link with GRAPHICS.LIB. Also, have EGAVGA.BGI in the same // directory. #include #include #include #include #include void save_screen(char *fileName); void restore_screen(char *fileName); int main(void) { int gdriver=DETECT, gmode, errorcode; int maxx, maxy; /* auto-detect the graphics driver and mode */ initgraph(&gdriver, &gmode, ""); errorcode = graphresult(); /* check for any errors */ if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } maxx = getmaxx(); PRODUCT : Borland C++ NUMBER : 1396 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 2/3 TITLE : How to save/load an entire screen image with BGI maxy = getmaxy(); /* draw an image on the screen */ rectangle(0, 0, maxx, maxy); line(0, 0, maxx, maxy); line(0, maxy, maxx, 0); save_screen("GRSAVE"); /* save the current screen */ getch(); /* pause program before redoing screen */ restore_screen("GRSAVE"); /* restore the screen */ getch(); /* pause program before leaving */ closegraph(); /* shut down graphics and leave */ return 0; } void save_screen(char *fileName) { void far *buf; int yincr, xwidth, block; unsigned size; FILE *fid; xwidth=getmaxx(); yincr = (getmaxy()+1) / 4; // divide screen into // four chunks size = imagesize(0, 0, xwidth, yincr); // get byte size of a // chunk fid = fopen(fileName, "wb"); // allocate a temporary buffer the size of one screen chunk buf = farmalloc(size); // get 4 chunks and save them to the specified file as we go for (block=0; block<4; block++) { getimage(0, yincr*block, xwidth, yincr*(block+1), buf); fwrite(buf, size, 1, fid); } farfree (buf); PRODUCT : Borland C++ NUMBER : 1396 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 3/3 TITLE : How to save/load an entire screen image with BGI fclose(fid); } void restore_screen(char *fileName) { void far *buf[4]; unsigned size; int yincr, xwidth, block; FILE *fid; xwidth=getmaxx(); yincr = (getmaxy()+1) / 4; // divide screen into // four chunks size = imagesize(0, 0, xwidth, yincr); // get byte size of a // chunk fid = fopen(fileName, "rb"); cleardevice(); /* clear screen */ /* For better display speed, first read the 4 chunks of the image ... */ for (block=0; block<4; block++) { buf[block]=farmalloc(size); fread(buf[block], size, 1, fid); } // and then restore the image after we have brought all 4 // chunks into memory for (block=0; block<4; block++) { putimage(0, yincr*block, buf[block], COPY_PUT); farfree(buf[block]); } fclose(fid); } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.