home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / MISC / MAG07.ZIP / MODE13H.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-04-04  |  6.8 KB  |  307 lines

  1. Unit Mode13h;
  2.  
  3. Interface
  4.  
  5. Const VGA=$A000;
  6.       Npages=2;
  7.  
  8. Type RgbItem=Record
  9.                    R,G,B:Byte;
  10.              End;
  11.      RgbList=Array[0..255] of RgbItem;
  12.      Table=Array[0..1799] Of Real;
  13.      PTable=^Table;
  14.  
  15. Var Sines:Ptable;
  16.     Cosines:Ptable;
  17.     Virt:Array[1..Npages] Of Pointer;
  18.     VP:Array[1..Npages] Of Word;
  19.  
  20. Procedure Initgraph;
  21. Procedure Closegraph;
  22. Procedure PutPixel(X,Y:word;Col:Byte;Where:Word);
  23. Procedure Cls(col:byte);
  24. Procedure WaitVBL;
  25. Procedure GetColor(Col:Byte;Var R,G,B:Byte);
  26. Procedure SetColor(Col,R,G,B:Byte);
  27. Procedure GetPalette(Var Pal:RgbList);
  28. Procedure SetPalette(Pal:RgbList);
  29. Procedure RotatePal(Var Pal:RgbList;First,Last:Byte);
  30. Procedure Fade(Target:RgbList);
  31. Procedure Circle(X,Y,R:Integer;Col:Byte;Where:Word);
  32. Function Sgn(A:Real):Integer;
  33. Procedure Line(X1,Y1,X2,Y2,Col:Integer;Where:Word);
  34. Procedure InitTables;
  35. Procedure ClearTables;
  36. Procedure InitVirt;
  37. Procedure CopyPage(From,Too:Word);
  38. Procedure LoadPCX(Filename:String;Where:Word);
  39.  
  40. Implementation
  41.  
  42. Procedure Initgraph; Assembler;
  43. Asm
  44.    mov ah,0
  45.    mov al,13h
  46.    int 10h
  47. End;
  48.  
  49. Procedure Closegraph; Assembler;
  50. Asm
  51.    mov ah,0
  52.    mov al,03h
  53.    int 10h
  54. End;
  55.  
  56. Procedure PutPixel(X,Y:word;Col:Byte;Where:Word);
  57. Begin
  58.      Mem[Where:(y*320)+x]:=Col;
  59. End;
  60.  
  61. Procedure Cls(col:byte);
  62. Begin
  63.      Fillchar(Mem[$A000:0000],64000,col);
  64. End;
  65.  
  66. Procedure WaitVBL; Assembler;
  67. Label A1,A2;
  68. Asm
  69.    Mov DX,3DAh
  70.    A1:
  71.       In AL,DX
  72.       And AL,08h
  73.       Jnz A1
  74.    A2:
  75.       In AL,DX
  76.       And AL,08h
  77.       Jz A2
  78. End;
  79.  
  80. Procedure GetColor(Col:Byte;Var R,G,B:Byte);
  81. Begin
  82.      Port[$3C7]:=Col;
  83.      R:=Port[$3C9];
  84.      G:=Port[$3C9];
  85.      B:=Port[$3C9];
  86. End;
  87.  
  88. Procedure SetColor(Col,R,G,B:Byte);
  89. Begin
  90.      Port[$3C8]:=Col;
  91.      Port[$3C9]:=R;
  92.      Port[$3C9]:=G;
  93.      Port[$3C9]:=B;
  94. End;
  95.  
  96. Procedure GetPalette(Var Pal:RgbList);
  97. Var A:Byte;
  98. Begin
  99.      For A:=0 To 255 do GetColor(A,Pal[A].R,Pal[A].G,Pal[A].B);
  100. End;
  101.  
  102. Procedure SetPalette(Pal:RgbList);
  103. Var A:Byte;
  104. Begin
  105.      WaitVBL;
  106.      For A:=0 To 255 do SetColor(A,Pal[A].R,Pal[A].G,Pal[A].B);
  107. End;
  108.  
  109. Procedure RotatePal(Var Pal:RgbList;First,Last:Byte);
  110. Var Temp:RgbItem;
  111.     A:Byte;
  112. Begin
  113.      Temp:=Pal[Last];
  114.      For A:=Last-1 DownTo First Do Pal[A+1]:=Pal[A];
  115.      Pal[First]:=Temp;
  116. End;
  117.  
  118. Procedure Fade(Target:RgbList);
  119. Var Tmp:RgbList;
  120.     Flag:Boolean;
  121.     Loop:Integer;
  122. Begin
  123.      Repeat
  124.            Flag:=True;
  125.            GetPalette(Tmp);
  126.            For Loop:=0 To 255 Do
  127.            Begin
  128.                 If Tmp[Loop].R>Target[Loop].R Then
  129.                 Begin
  130.                      Dec(Tmp[Loop].R);
  131.                      Flag:=False;
  132.                 End;
  133.                 If Tmp[Loop].G>Target[Loop].G Then
  134.                 Begin
  135.                      Dec(Tmp[Loop].G);
  136.                      Flag:=False;
  137.                 End;
  138.                 If Tmp[Loop].B>Target[Loop].B Then
  139.                 Begin
  140.                      Dec(Tmp[Loop].B);
  141.                      Flag:=False;
  142.                 End;
  143.                 If Tmp[Loop].R<Target[Loop].R Then
  144.                 Begin
  145.                      Inc(Tmp[Loop].R);
  146.                      Flag:=False;
  147.                 End;
  148.                 If Tmp[Loop].G<Target[Loop].G Then
  149.                 Begin
  150.                      Inc(Tmp[Loop].G);
  151.                      Flag:=False;
  152.                 End;
  153.                 If Tmp[Loop].B<Target[Loop].B Then
  154.                 Begin
  155.                      Inc(Tmp[Loop].B);
  156.                      Flag:=False;
  157.                 End;
  158.            End;
  159.            SetPalette(Tmp);
  160.      Until Flag;
  161. End;
  162.  
  163. Procedure Circle(X,Y,R:Integer;Col:Byte;Where:Word);
  164. Var Px,Py:Integer;
  165.     Deg:Word;
  166. Begin
  167.      For Deg:=0 to 1799 Do
  168.      Begin
  169.           Px:=Trunc(R*Sines^[Deg]+X);
  170.           Py:=Trunc(R*Cosines^[Deg]+Y);
  171.           PutPixel(Px,Py,Col,Where);
  172.      End;
  173. End;
  174.  
  175. Function Sgn(A:Real):Integer;
  176. Begin
  177.      If A<0 then Sgn:=-1;
  178.      If A=0 then Sgn:=0;
  179.      If A>0 then Sgn:=+1;
  180. End;
  181.  
  182. Procedure Line(X1,Y1,X2,Y2,Col:Integer;Where:Word);
  183. Var Deltax,S,Deltay,Dx1,Dy1,Dx2,Dy2,S1,S2:Real;
  184.     I:Integer;
  185. Begin
  186.      Deltax:=X2-X1;
  187.      Deltay:=Y2-Y1;
  188.      Dx1:=Sgn(Deltax);
  189.      Dy1:=Sgn(Deltay);
  190.      Dx2:=Sgn(Deltax);
  191.      Dy2:= 0;
  192.      S1:=Abs(Deltax);
  193.      S2:=Abs(Deltay);
  194.      If Not (S1>S2) Then
  195.      Begin
  196.           Dx2:=0;
  197.           Dy2:=Sgn(Deltay);
  198.           S1:=Abs(Deltay);
  199.           S2:=Abs(Deltax);
  200.      End;
  201.      S:=Int(S1/2);
  202.      For I:=0 To Round(S1) Do
  203.      Begin
  204.           PutPixel(X1,Y1,Col,Where);
  205.           S:=S+S2;
  206.           If Not (S<S1) Then
  207.           Begin
  208.                S:=S-S1;
  209.                X1:=X1+Round(Dx1);
  210.                Y1:=Y1+Round(Dy1);
  211.           End
  212.           Else
  213.           Begin
  214.                X1:=X1+Round(dx2);
  215.                Y1:=Y1+Round(Dy2);
  216.           End;
  217.      End;
  218. End;
  219.  
  220. Procedure InitTables;
  221. Var A:Word;
  222.     B:Real;
  223. Begin
  224.      Getmem(Sines,Sizeof(Sines^));
  225.      Getmem(Cosines,Sizeof(Cosines^));
  226.      B:=0;
  227.      For A:=0 To 1799 Do
  228.      Begin
  229.           Sines^[A]:=Sin(B);
  230.           Cosines^[A]:=Cos(B);
  231.           B:=B+0.005;
  232.      End;
  233. End;
  234.  
  235. Procedure ClearTables;
  236. Begin
  237.      Freemem(Sines,Sizeof(Sines^));
  238.      Freemem(Cosines,Sizeof(Cosines^));
  239. End;
  240.  
  241. Procedure InitVirt;
  242. Var A:Byte;
  243. Begin
  244.      For A:=1 To Npages Do
  245.      Begin
  246.           GetMem(Virt[A],64000);
  247.           VP[A]:=Seg(Virt[A]^);
  248.      End;
  249. End;
  250.  
  251. Procedure CopyPage(From,Too:Word);
  252. Begin
  253.      WaitVbl;
  254.      Move(Mem[From:0],Mem[Too:0],64000);
  255. End;
  256.  
  257. Procedure LoadPCX(Filename:String;Where:Word);
  258. Var Fil:File;
  259.     Dx,Dy:Word;
  260.     J,M:Byte;
  261.     Ph:Word;
  262.     Buff:Array[0..127] of byte;
  263.     PCXPal:RgbList;
  264. Begin
  265.      Assign(Fil,Filename);
  266.      Reset(Fil,1);
  267.      Blockread(Fil,Buff,128);
  268.      Dy:=0;
  269.      Repeat
  270.            Dx:=0;
  271.            Repeat
  272.                  BlockRead(Fil,J,1);
  273.                  If J>192 Then
  274.                  Begin
  275.                       BlockRead(Fil,M,1);
  276.                       Dec(J,192);
  277.                       For Ph:=1 To J Do
  278.                       Begin
  279.                            PutPixel(Dx,Dy,M,Where);
  280.                            Inc(Dx);
  281.                       End;
  282.                  End
  283.                  Else
  284.                  Begin
  285.                       PutPixel(Dx,Dy,J,Where);
  286.                       Inc(Dx);
  287.                  End;
  288.            Until Dx>=320;
  289.            Inc(Dy);
  290.      Until Dy=200;
  291.      BlockRead(Fil,M,1);
  292.      If M=12 Then
  293.      Begin
  294.           BlockRead(Fil,PCXPal,768);
  295.           For M:=0 To 255 Do
  296.           Begin
  297.                PCXPal[M].R:=PCXPal[M].R Div 4;
  298.                PCXPal[M].G:=PCXPal[M].G Div 4;
  299.                PCXPal[M].B:=PCXPal[M].B Div 4;
  300.           End;
  301.           SetPalette(PCXPal);
  302.      End;
  303.      Close(Fil);
  304. End;
  305.  
  306. Begin
  307. End.