home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / NCSATELN / TEL23SRC.ZIP / ENGINE / SCR_REST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-19  |  2.9 KB  |  134 lines

  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <malloc.h>
  4. #include <graph.h>
  5.  
  6. #include "externs.h"
  7.  
  8. #define VDD_SEG 0x40
  9. #define VDD_ROWS_OFF 0x84    /* This points to a BYTE    */
  10. #define VDD_COLS_OFF 0x4a    /* This points to a WORD (int) */
  11.  
  12. #define MK_FP(__o,__s) (void far *)(((unsigned long)(__s)<<16)|(unsigned)(__o))
  13.  
  14. #ifdef DEBUG
  15. # define BUG(__s) fprintf(stderr,__s); getch()
  16. #else
  17. # define BUG(__s) ;
  18. #endif
  19.  
  20. static void restore_screen(void);
  21. static void saveScreen(void);
  22. static void find_screen(void);
  23.  
  24. extern void exit(int);
  25. extern int getch(void);
  26. extern int save_screen;
  27.  
  28. static int rows, cols;
  29. static char far *vid_buffer=NULL;
  30. static char far *vidstatebuffer=NULL;
  31. static char *old_vid;
  32. static int old_page;
  33. static short old_color;
  34. static struct rccoord old_pos;
  35.     
  36. void init_text(void)
  37. {
  38.     setvbuf(stdout,NULL,_IONBF,1);
  39.     if ((old_page = _getactivepage()) != 0)
  40.         _setactivepage(0);
  41.     old_pos = _gettextposition();
  42.     old_color = _gettextcolor();
  43.     find_screen();
  44.     if (save_screen)
  45.         saveScreen();
  46. }
  47.  
  48.  
  49. void end_text(void)
  50. {
  51.     BUG("We are in end_text - Press a Key");
  52.     if (old_page)
  53.         _setactivepage(old_page);
  54.  
  55.     set_mode();
  56.     fix_vid();
  57.     restore_screen();
  58.     _settextposition(old_pos.row,old_pos.col);
  59.     _settextcolor(old_color);
  60.  
  61. }
  62.  
  63. static void find_screen(void)
  64. {
  65.     struct rccoord pos;
  66.     unsigned char far *mono, far *color;
  67.     unsigned char old_mono, old_color, test_char;
  68.  
  69.     mono = (char far *)MK_FP(0,0xb000);
  70.     color = (char far *)MK_FP(0,0xb800);
  71.  
  72.     pos = _settextposition(0,0);
  73.  
  74.     old_mono = *mono;
  75.     old_color = *color;
  76.  
  77.     test_char = 0x88;
  78.     for(test_char=0x88;(test_char==old_mono)||(test_char==old_color);
  79.       test_char--);
  80.  
  81.     putchar(test_char);
  82.  
  83.     _settextposition(pos.row, pos.col);
  84.     if (*mono == test_char) {
  85.         vid_buffer = mono;
  86.         _settextposition(0,0);
  87.         putchar(old_mono);
  88.         BUG("We are mono - Press a Key\n");
  89.     } else if (*color == test_char) {
  90.         vid_buffer = color;
  91.         _settextposition(0,0);
  92.         putchar(old_color);
  93.         BUG("We are color - Press a Key\n");
  94.     } else {
  95.         int result;
  96.  
  97.         fprintf(stderr, "Cannot Recognize video type - cannot save screen\n");
  98.         fprintf(stderr, "Press ESC to exit, any other key to continue\n");
  99.         result = getch();
  100.         if (result == 0x1b)
  101.             exit(-1);
  102.         save_screen = FALSE;
  103.     }
  104.  
  105.     _settextposition(pos.row, pos.col);
  106. }
  107.  
  108. static void saveScreen(void)
  109. {
  110.     int size;
  111.  
  112.     rows = *(char far *)MK_FP(VDD_ROWS_OFF,VDD_SEG) + 1;
  113.     cols = *(int far *)MK_FP(VDD_COLS_OFF,VDD_SEG);
  114.  
  115.     if ((old_vid = (char *) malloc((rows*cols)<<1)) == NULL) {
  116.         printf("Couldn't get enough memory - exiting\n");
  117.         exit(-1);
  118.     }
  119.     _fmemcpy(old_vid,vid_buffer,(rows*cols)<<1);
  120.  
  121.     size = get_size();
  122.     size *=64;
  123.     vidstatebuffer=malloc(size);
  124.     get_mode(vidstatebuffer);
  125. }
  126.  
  127. static void restore_screen(void)
  128. {    
  129.     _fmemcpy(vid_buffer, old_vid, (rows*cols)<<1);
  130.     free(old_vid);
  131.     free(vidstatebuffer);
  132. }
  133.  
  134.