PRODUCT : Borland C++ NUMBER : 1045 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 1/5 TITLE : Switching Video Mode without clearing Video Memory. /* ----------------------------------------------------------- *\ | The purpose of this program is to demonstrate how to switch | | from graphics mode to text mode and back without having all of| | the video memory being cleared. | | | | If bit 7 of the AX register is set then video memory is not | | cleared when function 0 of interrupt 10 is called. | | | | The top portion of the graphics screen may be corrupted by | | switching to text mode. The function getimage is used to | | preserve this area of the screen. | | | | If you are running this in the IDE be sure to allocate enough | | memory for Program heap size: Under Options-Debugger. | \* ----------------------------------------------------------- */ #include #include #include #include #include void totext (unsigned char&); void textfunction (void); void tographics (unsigned char&); // For holding a copy of the top portion of the graphics screen. void far* image = NULL; // Display prompt #1 void displayprompt1 () { // Display prompt settextjustify (CENTER_TEXT,CENTER_TEXT); outtextxy (getmaxx()/2,getmaxy()/2, "In graphics mode. Press any key"); getch (); // Erase the text so that it won't be displayed upon PRODUCT : Borland C++ NUMBER : 1046 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 2/5 TITLE : Switching Video Mode without clearing Video Memory. // returning to graphics mode. setfillstyle (SOLID_FILL,0); bar (1,getmaxy()/2 - 10, getmaxx()-1, getmaxy()/2+10); } // displayprompt1 // Display prompt #2 void displayprompt2 () { outtextxy (getmaxx()/2,getmaxy()/2, "Back in graphics mode. Press any key to exit."); getch (); } // displayprompt2 // Purpose: Switch to text mode without clearing video memory // // oldgrmode is needed so that the graphics mode can be restored. // image will hold the image of the first ROW_SAVE rows upon exit // of totext. void totext (unsigned char& oldgrmode) { // The value of ROW_SAVE was determined by trial and error. // 110 is a a little beyond the rows that get corrupted when // the switch to text mode is made. The 110 was found for // 640 x 480 mode. const ROW_SAVE = 110; // Need the size of the image unsigned imgsize = imagesize (0,0,getmaxx(),ROW_SAVE); image = farmalloc (imgsize); // Check that memory allocation was successful // If you are running this in the IDE be sure to allocate // enough memory for Program heap size: Under // Options-Debugger. if (!image) { closegraph (); PRODUCT : Borland C++ NUMBER : 1046 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 3/5 TITLE : Switching Video Mode without clearing Video Memory. cout << "Error: Could not allocate memory for " "the image." << endl; exit (1); } // if // Store the image getimage (0,0,getmaxx(),110,image); displayprompt1 (); // Get the current graphics mode from the BIOS _AX= 0x0f00; asm int 10h // Save the mode # oldgrmode = _AL; // Set bit 7 of AX to prevent video memory from being // cleared during mode switch. _AX = 0x0080; // Video mode #3 which is a text mode. _AX |= 0x0003; // Switch modes. asm int 10h } // totext // Purpose: To return to graphics mode without clearing video // memory oldgrmode holds the graphics mode the system was in // before switching to text mode. // image holds the image of top portion of the screen. void tographics (unsigned char& oldgrmode) { // _AX will hold the the video mode that will be entered. _AX = (int) oldgrmode; // Set bit 7 of AX to prevent video memory from being // cleared. _AX |= 0x0080; PRODUCT : Borland C++ NUMBER : 1046 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 4/5 TITLE : Switching Video Mode without clearing Video Memory. // Switch modes asm int 10h // Restore the image to the screen. putimage (0,0,image,COPY_PUT); displayprompt2 (); farfree (image); } // tographics // Perfom output in text mode. void textfunction () { clrscr (); gotoxy (0,12); cout << "\n\n\n\n\n\n\n\n\n\n"; cout.fill (' '); cout.width (55); cout << "Now in text mode. Press any key."; getch (); } // textfunction int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics mode */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { cout << "Graphics error: " << grapherrormsg(errorcode) << endl; cout << "Press any key to halt:"; PRODUCT : Borland C++ NUMBER : 1046 VERSION : 3.1 OS : DOS DATE : October 19, 1993 PAGE : 5/5 TITLE : Switching Video Mode without clearing Video Memory. getch(); clrscr (); exit(1); /* return with error code */ } setcolor (getmaxcolor()); // Draw a full screen image for (int i=0;i