Thread: saving screen to file

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    Unhappy saving screen to file

    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.

    Code:
    /* 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 */
    Last edited by z0diac; 02-24-2003 at 01:02 AM.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Usos Coda Tagos.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Code:
    /* 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 */

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Moved to the DOS board
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM