home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1988 / 02 / screen_c / screen.c < prev    next >
Text File  |  1987-12-14  |  6KB  |  249 lines

  1. /*-----------------------------------------------------------------------*/
  2. /*                             SCREEN.C                                  */
  3. /*   Direkte Bildschirmadressierung und einfache Windowhandling fuer     */
  4. /*   MS-DOS-Rechner (CGA und Monochrom) in C programmiert.               */
  5.  
  6. #include <alloc.h>
  7. #include <dos.h>
  8. #include "screen.h"
  9.  
  10. /*------------------Globale Variabeln------------------------------------*/
  11.  
  12.   /* Attribut fuer normale Schrift in's Highbyte */
  13.  
  14. int attribut = ATT_NORMAL << 8;
  15.  
  16.   /* Zeiger auf Bildschirmspeicher einrichten */
  17. int far *screenpointer = ((int far *)MK_FP(SCREEN_SEGMENT,0x0000));
  18.  
  19.   /* In dieser Struktur werden die Fenster gespeichert */
  20. struct windows {  int xa,ya,xe,ye ;
  21.                   int *screen_zeig ;
  22.                } ;
  23.  
  24. struct windows window[MAX_WINDOWS];
  25. int window_num = NOWIN;
  26.  
  27. /*-----------------------------------------------------------------------*/
  28.  
  29. void set_attribut(att)
  30.   unsigned char att;
  31.  
  32. {
  33.   attribut = att << 8;  /* In's Highbyte shiften */
  34. }
  35.  
  36. /*-----------------------------------------------------------------------*/
  37.  
  38. void make_relativ(x,y)
  39.   int *x,*y;
  40.  
  41. {
  42.   *x += window[window_num].xa;
  43.   *y += window[window_num].ya;
  44. }
  45.  
  46. /*-----------------------------------------------------------------------*/
  47.  
  48. void set_cursor(x,y)
  49.   int x,y;
  50.  
  51. { union REGS regs;
  52.  
  53.   if (window_num != NOWIN) make_relativ(&x,&y);
  54.   regs.h.ah = 2;
  55.   regs.h.dh = y-1;
  56.   regs.h.dl = x-1;
  57.   regs.h.bh = 0;
  58.   int86(0x10, ®s, ®s);
  59. }
  60.  
  61. /*-----------------------------------------------------------------------*/
  62.  
  63. void cursor(mode)
  64.   int mode;
  65.  
  66. { union REGS regs;
  67.  
  68.   switch(mode)
  69.   {
  70.     case 0  : {regs.h.cl = 0x00; regs.h.ch = 0x0F; break;}
  71.     case 1  : {regs.h.cl = 0x07; regs.h.ch = 0x07; break;}
  72.     case 2  : {regs.h.cl = 0x07; regs.h.ch = 0x06; break;}
  73.     case 8  : {regs.h.cl = 0x08; regs.h.ch = 0x00; break;}
  74.   }
  75.   regs.h.ah = 1;
  76.   int86(0x10, ®s, ®s);
  77. }
  78.  
  79. /*-----------------------------------------------------------------------*/
  80.  
  81. void printxy(x,y,c)
  82.   int x,y;
  83.   unsigned char c;
  84.  
  85. {
  86.   if (window_num != NOWIN) make_relativ(&x,&y);
  87.   *(screenpointer + SCREEN_OFFSET(x,y)) = (((int) c) | attribut);
  88. }
  89.  
  90. /*-----------------------------------------------------------------------*/
  91.  
  92. void printsxy(x,y,str)
  93.   int  x,y;
  94.   char *str;
  95.  
  96. {
  97.   for (; *str != '\0'; x++, str++)
  98.     printxy(x,y,(unsigned)*str);
  99. }
  100.  
  101. /*-----------------------------------------------------------------------*/
  102.  
  103. unsigned char readxy(x,y)
  104.   int x,y;
  105.  
  106. {
  107.   if (window_num != NOWIN) make_relativ(&x,&y);
  108.   return((unsigned char)*(screenpointer + SCREEN_OFFSET(x,y)));
  109. }
  110.  
  111. /*-----------------------------------------------------------------------*/
  112.  
  113. void readsxy (x,y,length,str)
  114.   int x,y,length;
  115.   char *str;
  116.  
  117. {
  118.   int i;
  119.  
  120.   for(i = 0; i <= length; i++, str++, x++ )
  121.     *str = (signed)readxy(x,y);
  122.   *str = '\0';
  123. }
  124.  
  125. /*------------------------------------------------------------------------*/
  126.  
  127. void clrscr(xanf,yanf,xend,yend)
  128.   int xanf,yanf,xend,yend;
  129.  
  130. {
  131.   register x,y;
  132.   int alt_attribut;
  133.  
  134.   alt_attribut = attribut;
  135.   attribut = 7*256;
  136.   for ( x = xanf; x <= xend; x++ )
  137.     for ( y = yanf; y <= yend ; y++ )
  138.         printxy(x,y,' ');
  139.   attribut = alt_attribut;
  140. }
  141.  
  142. /*-----------------------------------------------------------------------*/
  143.  
  144. void open_window(xa,ya,xe,ye,rahmen)
  145.   int xa,ya,xe,ye,rahmen;
  146.  
  147.  
  148. {
  149.   unsigned char oben_links, oben_rechts, unten_links,
  150.                 unten_rechts, oben_unten, seite;
  151.   register x,y;
  152.   int *zeiger;
  153.   int screen_length, alt_window_num;
  154.   char buffer[81];
  155.   char *bufp = buffer;
  156.  
  157.   switch (rahmen)
  158.   {
  159.     /* Die Zeichen-Nummer-Konstanten zum Rahmenzeichnen */
  160.     /* muessen gegebenenfalls an den Zeichensatz des    */
  161.     /* verwendeten Rechners angepaßt werden !           */
  162.              /* Umrahmung mit Leerzeichen */
  163.     case 0 : { oben_links = oben_rechts
  164.                = unten_links = unten_rechts
  165.                = oben_unten = seite = ' ';
  166.                break;
  167.              }
  168.              /* Einfache Umrahmung */
  169.     case 1 : { oben_links=218; oben_rechts=191; unten_links=192;
  170.                unten_rechts=217; oben_unten =196; seite =179;
  171.                break;
  172.              }
  173.              /* Doppelte Umrahmung */
  174.     case 2 : { oben_links=201; oben_rechts=187; unten_links=200;
  175.                unten_rechts=188; oben_unten =205; seite =186;
  176.                break;
  177.              }
  178.   }
  179.   window_num++;
  180.   window[window_num].xa = xa;
  181.   window[window_num].ya = ya;
  182.   window[window_num].xe = xe;
  183.   window[window_num].ye = ye;
  184.   /* Speicherplatz auf dem Heap allokieren */
  185.   screen_length = (xe-xa+1)*(ye-ya+1);
  186.   zeiger = window[window_num].screen_zeig = calloc(screen_length,2);
  187.  
  188.   alt_window_num = window_num;
  189.   window_num = NOWIN;
  190.   for (x = xa ; x <= xe ; x++)
  191.     for (y = ya ; y <= ye ; y++)
  192.       *(zeiger++) = readxy(x,y);
  193.  
  194.   /* String fuer Ober-/Unterkante-Fenster fertigmachen */
  195.   for (x = xa; x < xe; x++, bufp++) *bufp = oben_unten;
  196.   *bufp = '\0';
  197.   clrscr(xa+1,ya+1,xe-1,ye-1);
  198.  
  199.   /* Rahmen zeichnen */
  200.   printxy(xa,ya,oben_links);
  201.   printsxy(xa+1,ya,buffer);
  202.   printxy(xe,ya,oben_rechts);
  203.   for (x = ya+1; x < ye; x++)
  204.     { printxy(xa,x,seite);
  205.       printxy(xe,x,seite);
  206.     }
  207.   printxy(xa,ye,unten_links);
  208.   printsxy(xa+1,ye,buffer);
  209.   printxy(xe,ye,unten_rechts);
  210.  
  211.   window_num = alt_window_num;
  212. }
  213.  
  214. /*-----------------------------------------------------------------------*/
  215.  
  216. void close_window()
  217. {
  218.   int *zeiger;
  219.   register x,y;
  220.   int xa,ya,xe,ye,alt_window_num;
  221.  
  222.   xa=window[window_num].xa;
  223.   ya=window[window_num].ya;
  224.   xe=window[window_num].xe;
  225.   ye=window[window_num].ye;
  226.   zeiger=window[window_num].screen_zeig;
  227.  
  228.   alt_window_num = window_num;
  229.   window_num = NOWIN;
  230.  
  231.   for (x = xa ; x <= xe ; x++)
  232.     for (y = ya ; y <= ye ; y++)
  233.       printxy(x,y,*(zeiger++));
  234.  
  235.   window_num = alt_window_num;
  236.   free(window[window_num].screen_zeig);
  237.   window_num--;
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.