home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1989 / 10 / titel / mcga.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-08  |  6.1 KB  |  343 lines

  1.  /*                     MCGA.C                    */
  2.  /*               (C) 1989 TOOLBOX                */
  3.  /*  Dieses Modul liefert die Grundlage für die   */
  4.  /*  Programmierung des Modus 13h der VGA- Karte  */
  5.  /*          Implementation in Turbo-C            */
  6.  
  7.  #include "mcga.h"
  8.  #include <stdio.h>
  9.  #include <dos.h>
  10.  #include <string.h>
  11.  #include <mem.h>
  12.  #include <io.h>
  13.  #include <math.h>
  14.  
  15.  char far *scrbase;
  16.  
  17.  typedef struct { char r,g,b;} onereg;
  18.  typedef onereg TColorRegBuf[256];
  19.  
  20.  
  21.  /* Setzt einen Punkt in der Farbe color */
  22.  
  23.  void plot(x,y,color)
  24.    int x,y,color;
  25.  {
  26.    *(scrbase+y*320+x) = (char)color;
  27.  ;}
  28.  
  29.  
  30.  /* Testet die Farbe eines Punktes */
  31.  int getdotcolor (x,y)
  32.    int x,y;
  33.  {
  34.     union REGS regs;
  35.  
  36.     regs.x.ax = 13;
  37.     regs.x.cx = x;
  38.     regs.x.dx = y;
  39.     int86(0*10,®s,®s);
  40.     return (regs.h.al);
  41.  }
  42.  
  43.  
  44.  /* Setzt den 320*200 Punkte Modus mit 256 Farben */
  45.  void initgraphic()
  46.  {
  47.    union REGS regs;
  48.  
  49.    scrbase = MK_FP(0xA000,0);
  50.    regs.h.ah = 0;
  51.    regs.h.al = 0x13;
  52.    int86(0x10,®s,®s);
  53.  }
  54.  
  55.  /* Zurück in den Textmodus */
  56.  void exitgraphic()
  57.  {
  58.    union REGS regs;
  59.  
  60.    regs.h.ah = 0;
  61.    regs.h.al = 3;
  62.    int86(0x10,®s,®s);
  63.  }
  64.  
  65.  
  66.  /* Schreibt einen String an die Cursorposition */
  67.  void print (line,color)
  68.    char *line;
  69.    int color;
  70.  
  71.  {
  72.    int i;
  73.    union REGS regs;
  74.  
  75.    for( i = 0; i < strlen(line); i++) {
  76.      regs.h.ah = 14;
  77.      regs.h.al = line[i];
  78.      regs.h.bl = color;
  79.      int86(0x10,®s,®s);
  80.    }
  81.  }
  82.  
  83.  
  84.  /* Setzt den Cursor auf x,y */
  85.  void setcursor (x,y)
  86.    int x,y;
  87.  {
  88.    union REGS regs;
  89.  
  90.    regs.h.ah = 2;
  91.    regs.h.bh = 0;
  92.    regs.h.dh = y;
  93.    regs.h.dl = x;
  94.    int86(0x10,®s,®s);
  95.  }
  96.  
  97.  
  98.  /* Liest x-Position des Cursors */
  99.  int cursorx(void)
  100.  {
  101.    union REGS regs;
  102.  
  103.    regs.h.ah = 3;
  104.    regs.h.bh = 0;
  105.    int86(0x10,®s,®s);
  106.    return(regs.h.dl);
  107.  }
  108.  
  109.  /* Liest y-Position des Cursors */
  110.  int cursory(void)
  111.  {
  112.    union REGS regs;
  113.  
  114.    regs.h.ah = 3;
  115.    regs.h.bh = 0;
  116.    int86(0x10,®s,®s);
  117.    return(regs.h.dh);
  118.  }
  119.  
  120.  
  121.  /* Löscht den Bildschirm mit der Farbe color */
  122.  void clearscreen(color)
  123.   int color;
  124.  
  125.  {
  126.    setmem(scrbase,32000,color);
  127.    setmem((scrbase+32000),32000,color);
  128.  }
  129.  
  130.  
  131.  /* Zeichnet gefüllte Box in der Farbe color */
  132.  void colorbox (x1,y1,x2,y2,color)
  133.    int x1,y1,x2,y2,color;
  134.  {
  135.    int i,d;
  136.  
  137.    d = x2-x1;
  138.    for(i = y1; i <= y2; i++) {
  139.      setmem((scrbase+i*320+x1),d,color);
  140.    }
  141.  }
  142.  
  143.  
  144.  /* Sichert den Bildschirm in die Datei "filename" */
  145.  void mcgasave (filename)
  146.    char *filename;
  147.  
  148.  {
  149.    FILE *file;
  150.    size_t res;
  151.  
  152.    file = fopen(filename,"wb");
  153.    if (file != NULL) {
  154.      res = fwrite(scrbase,(size_t)32000,(size_t)2,file);
  155.      fclose(file);
  156.    }
  157.  }
  158.  
  159.  
  160.  /* Lädt einen Bildschirm aus der Datei "filename" */
  161.  void mcgaload (filename)
  162.    char *filename;
  163.  {
  164.    FILE *file;
  165.    size_t res;
  166.  
  167.  
  168.    file = fopen(filename,"rb");
  169.    if (file != NULL) {
  170.      res = fread(scrbase,(size_t)32000,(size_t)2,file);
  171.      fclose(file);
  172.    }
  173.  }
  174.  
  175.  
  176. /* Linie in Farbe color zeichen */
  177. void line(x1,y1,x2,y2,color)
  178.  
  179. int x1,y1,x2,y2,color;
  180.  
  181. {
  182.  
  183.   int deltax,deltay,zaehler,abweichung,x,y;
  184.  
  185.   abweichung=0;
  186.   deltax=x2-x1;
  187.   deltay=y2-y1;
  188.   if (deltay<0)
  189.   {
  190.     swap(x1,x2);
  191.     swap(y1,y2);
  192.     deltay=-deltay;
  193.     deltax=-deltax;
  194.   }
  195.   plot(x1,y1,color);
  196.   x=x1;
  197.   y=y1;
  198.   if (deltax>=0)
  199.     if (deltax<deltay)
  200.       for (zaehler=1;zaehler<=deltay-1;zaehler++)
  201.         if (abweichung<0)
  202.         {
  203.           x++;
  204.           y++;
  205.       plot(x,y,color);
  206.           abweichung=abweichung+deltay-deltax;
  207.         }
  208.         else {
  209.           y++;
  210.       plot(x,y,color);
  211.           abweichung=abweichung-deltax;
  212.         }
  213.     else
  214.       for (zaehler=1;zaehler<=deltax-1;zaehler++)
  215.         if (abweichung<=0)
  216.         {
  217.           x++;
  218.       plot(x,y,color);
  219.           abweichung=abweichung+deltay;
  220.         }
  221.         else {
  222.           x++;
  223.           y++;
  224.       plot(x,y,color);
  225.           abweichung=abweichung+deltay-deltax;
  226.         }
  227.   else
  228.     if (abs(deltax)>=deltay)
  229.       for (zaehler=1;zaehler<=(abs(deltax)-1);zaehler++)
  230.         if (abweichung<=0)
  231.         {
  232.           --x;
  233.       plot(x,y,color);
  234.           abweichung=abweichung+deltay;
  235.         }
  236.         else {
  237.           --x;
  238.           y++;
  239.       plot(x,y,color);
  240.           abweichung=abweichung+deltax+deltay;
  241.         }
  242.     else
  243.       for (zaehler=1;zaehler<=deltay-1;zaehler++)
  244.         if (abweichung<0)
  245.         {
  246.           --x;
  247.           y++;
  248.       plot(x,y,color);
  249.           abweichung=abweichung+deltax+deltay;
  250.         }
  251.         else {
  252.           y++;
  253.       plot(x,y,color);
  254.           abweichung=abweichung+deltax;
  255.         }
  256.   plot(x2,y2,color);
  257. }
  258.  
  259.  
  260.  
  261.  /* Zeichnet Box in der Farbe color */
  262.  void box(x1,y1,x2,y2,color)
  263.  int x1,y1,x2,y2,color;
  264.  
  265.  {
  266.     line(x1,y1,x2,y1,color);
  267.     line(x1,y2,x2,y2,color);
  268.     line(x1,y1,x1,y2,color);
  269.     line(x2,y1,x2,y2,color);
  270.  }
  271.  
  272.  
  273.  /* Setzt ein Farbregister */
  274.  void setcolor(nr,red,green,blue)
  275.  int nr,red,green,blue;
  276.  
  277.  {
  278.    union REGS r;
  279.  
  280.      r.h.ah = 0x10;
  281.      r.h.al = 0x10;
  282.      r.x.bx = nr;
  283.      r.h.dh = red;
  284.      r.h.ch = green;
  285.      r.h.cl = blue;
  286.  
  287.    int86(0x10,&r,&r);
  288.  }
  289.  
  290.  
  291.  /* Liest ein Farbregister */
  292.  void readcolor(nr,red,green,blue)
  293.  int nr;
  294.  int *red,*green,*blue;
  295.  
  296.  {
  297.    union REGS r;
  298.  
  299.      r.h.ah = 0x10;
  300.      r.h.al = 0x15;
  301.      r.x.bx = nr;
  302.      int86(0x10,&r,&r);
  303.      *red = r.h.dh;
  304.      *green = r.h.ch;
  305.      *blue = r.h.cl;
  306.   }
  307.  
  308.  
  309.  /* Setzt einen Block von Farbregistern */
  310.  void setcolorblock(startnr,buf,nr)
  311.  int startnr, nr;
  312.  void *buf;
  313.  
  314.  {
  315.     struct REGPACK r;
  316.  
  317.       r.r_ax = 0x10*256 + 0x12;
  318.       r.r_bx = startnr;
  319.       r.r_es = FP_SEG(buf);
  320.       r.r_dx = FP_OFF(buf);
  321.       r.r_cx = nr;
  322.  
  323.     intr(0x10,&r);
  324.  }
  325.  
  326.  /* Liest einen Block von Farbregistern */
  327.  void readcolorblock(startnr,buf,nr)
  328.  int startnr, nr;
  329.  void *buf;
  330.  
  331.  {
  332.     struct REGPACK r;
  333.  
  334.       r.r_ax = 0x10*256 + 0x12;
  335.       r.r_bx = startnr;
  336.       r.r_es = FP_SEG(buf);
  337.       r.r_dx = FP_OFF(buf);
  338.       r.r_cx = nr;
  339.  
  340.     intr(0x10,&r);
  341.   }
  342.  
  343.