home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / SCRNSAVE.C < prev    next >
C/C++ Source or Header  |  1992-07-03  |  3KB  |  87 lines

  1. /*
  2. **  Save and restore a text screen
  3. **  public domain by Jeff Dunlop
  4. */
  5.  
  6. #ifdef __TURBOC__
  7.  #include <alloc.h>
  8. #else
  9.  #include <malloc.h>
  10.  #define farcalloc(a,b) _fcalloc((a),(b))
  11.  #define farfree(a) _ffree(a)
  12. #endif
  13. #include <string.h>
  14.  
  15. #define MAX_X 79 /* Actually, should be determined on program startup */
  16.  
  17. /*------------------------[ vlWinSave ]-------------------------*/
  18. /*     Save the screen with passed coordinates to a buffer      */
  19. /*--------------------------------------------------------------*/
  20. /* input:                                                       */
  21. /*      x1, y1 = upper left coordinates                         */
  22. /*      x2, y2 = lower right coordinates, 0-based               */
  23. /*      vid_ram = address of screen (or screen buffer)          */
  24. /* return:                                                      */
  25. /*      address of the buffer allocated and filled, or NULL     */
  26. /*      on error                                                */
  27. /* note:                                                        */
  28. /*      vlWinRestore must be called to release the memory.      */
  29. /*      These routines will work with video ram or virtual      */
  30. /*      windows.                                                */
  31. /*--------------------------------------------------------------*/
  32.  
  33. char far *vlWinSave(int x1, int y1, int x2, int y2, char far *vid_ram)
  34. {
  35.     int i;
  36.  
  37.     char far *win_ptr;
  38.  
  39.     if ( (win_ptr = farcalloc((x2 - x1 + 1) * (y2 - y1 + 1) * 2, 1)) == NULL )
  40.         return NULL;
  41.  
  42.     for (i = y1; i <= y2; i++)
  43.         f_memcpy(win_ptr + (x2 - x1 + 1) * 2 * (i - y1),
  44.             vid_ram + 2 * (MAX_X + 1) * i + 2 * x1, (x2 - x1 + 1) * 2);
  45.     return(win_ptr);
  46. }
  47.  
  48. /*------------------------[ vlWinRestore ]----------------------*/
  49. /*               Restore a previously saved screen              */
  50. /*--------------------------------------------------------------*/
  51. /* input:                                                       */
  52. /*      x1, y1 = upper left coordinates                         */
  53. /*      x2, y2 = lower right coordinates, 0-based               */
  54. /*      win_ptr = address of screen saved with winsave          */
  55. /*      vid_ram = address of video buffer                       */
  56. /* note:                                                        */
  57. /*      With care, it's possible to 'move' a window as long as  */
  58. /*      the resulting width & length are the same.              */
  59. /*--------------------------------------------------------------*/
  60.  
  61. void vlWinRestore(int x1, int y1, int x2, int y2, char far *win_ptr
  62.                  char far *vid_ram)
  63. {
  64.     char far *temp_ptr = win_ptr;
  65.  
  66.     int win_wid = 2 * (x2 - x1 + 1),
  67.         max_x_attr = 2 * (MAX_X + 1),
  68.         i;
  69.  
  70.     vid_ram += 2 * ((MAX_X + 1) * y1 + x1);
  71.     for (i = y1; i <= y2; i++)
  72.     {
  73.         f_memcpy(vid_ram, win_ptr, (unsigned)win_wid);
  74.         win_ptr += win_wid;
  75.         vid_ram += max_x_attr;
  76.     }
  77.     farfree(temp_ptr);
  78.     return;
  79. }
  80.  
  81. void f_memcpy(char far *dest, char far *src, size_t size)
  82. {
  83.     int i;
  84.  
  85.     movedata(FP_SEG(src), FP_OFF(src), FP_SEG(dest), FP_OFF(dest), size);
  86. }
  87.