home *** CD-ROM | disk | FTP | other *** search
/ PC Action 1997 July / CITE.ISO / menue / privat / 97070049.txt < prev    next >
Encoding:
Text File  |  1991-05-26  |  11.1 KB  |  430 lines

  1. 99
  2. Grafik unter TP 7.0
  3. CHROME
  4. PROGRAMMIEREN
  5.   Hallo Leute ...
  6.  
  7. Heute habe ich mir mal
  8. etwas Arbeit gemacht und
  9. für die unter euch, die
  10. sich noch nicht so gut
  11. mit VGA Programmierung unter
  12. DOS auskennen eine kleine
  13. Unit geschrieben mit der
  14. man den Modus 13h ( 320*200
  15. mit 256 Farben ) ansprechen
  16. kann.
  17.  
  18. Jetzt fragt ihr euch wie
  19. ihr diese Datei von der CD
  20. runterkriegt.
  21.  
  22. 1. Wenn ihr den Norton
  23.    Commander habt geht das
  24.    ganz einfach.
  25.  
  26.  - Geht in das Verzeichnis
  27.    \MENUE\PRIVAT auf der
  28.    PCA CD
  29.  - drückt Alt+F7 ( für Datei
  30.    suchen )
  31.  - gebt bei Dateien suchen
  32.    "*.txt" ein und bei
  33.    Enthält Text "MCGA"
  34.  - nun drückt ihr Alt+N und
  35.    dann Alt+S
  36.  - nun müßten zwei Dateien
  37.    gefunden werden
  38.  - nun drückt ihr F und
  39.    habt diese beiden Dateien
  40.    in einem Fenster und
  41.    könnt sie kopieren
  42.  
  43. 2. Wenn ihr ihn nicht habt
  44.    müßt ihr in eben diesem
  45.    Verzeichnis nach einer
  46.    Datei mit der Größe
  47.    ca. 11.400 bytes suchen.
  48.  
  49. Nun ladet ihr diese Datei
  50. mit eurem TP Editor und
  51. schneidet den oberen Teil
  52. ab. Die Datei muß nur
  53. noch unter den Namen
  54. "MCGA.PAS" abgespeichert
  55. werden un schon könnt ihr 
  56. die Unit kompilieren und 
  57. verwenden wie sie ist oder 
  58. sie einfach ausschlachten, 
  59. wie ihr wollt.
  60.  
  61.   brought to ya by
  62.  
  63.        CHROME
  64.  
  65. -- nicht weiterscrollen ----
  66. ---- sonst ärgert ihr ------
  67. ------- euch noch ----------
  68.  
  69.  
  70. ------- cut here -----------
  71. unit mcga;
  72. interface
  73. {$G+} (*  286-instructions  *)
  74.  
  75. const
  76.   maxx   = 319;
  77.   maxy   = 199;
  78.  
  79.   clipx1 : word = 0;
  80.   clipy1 : word = 0;
  81.   clipx2 : word = maxx;
  82.   clipy2 : word = maxy;
  83.  
  84. type
  85.   screen  = array[ 0..maxy, 0..maxx ] of byte;
  86.   pscreen = ^screen;
  87.   palette = array[ 0..255, 0..2 ] of byte;
  88.   sprite = record
  89.              width  : word;
  90.              height : word;
  91.              data   : pointer;
  92.            end;
  93.  
  94. var
  95.   vmem   : pscreen; (* real screen *)
  96.  
  97.  
  98. procedure init13h;
  99. procedure done13h;
  100. procedure clearscreen( pscr  : pscreen; color  : byte );
  101. procedure copyscreen( source, dest  : pscreen );
  102. procedure setpalette( var new  : palette );
  103. procedure getpalette( var pal  : palette );
  104. procedure plotpixel( x, y  : word; color  : byte; pscr  : pscreen );
  105. function getpixel(  x, y  : word; pscr  : pscreen ) :  byte;
  106. function getsprite( var s  : sprite; xs, ys, width, height : word; pscr  : pscreen ) :  boolean;
  107. procedure putsprite( s  : sprite; xs, ys  : integer; pscr  : pscreen );
  108. procedure putspritemasked( s  : sprite; xs, ys  : integer; pscr : pscreen; maskcolor : byte );
  109. procedure setclipping( x1, y1, x2, y2 : word );
  110. procedure vsync;
  111.  
  112. function pcxshow( pcxname  : string; xs, ys  : word; pscr  : pscreen; truecolors  : boolean ) :  boolean;
  113.  
  114. implementation
  115.  
  116. procedure init13h;
  117. begin
  118.   asm
  119.     mov  ax, 0013h
  120.     int  10h
  121.   end;
  122.   vmem := ptr( $a000, 0 );
  123. end;
  124.  
  125. procedure done13h; assembler;
  126. asm
  127.   mov  ax, 03
  128.   int  10h
  129. end;
  130.  
  131. procedure clearscreen( pscr  : pscreen; color  : byte ); assembler;
  132. asm
  133.   cld
  134.   les  di, pscr
  135.   mov  al, color
  136.   mov  ah, color
  137.   mov  cx, ( 320 * 200 ) / 2
  138.   stosw
  139. end;
  140.  
  141. procedure copyscreen( source, dest  : pscreen ); assembler;
  142. asm
  143.   push ds
  144.   lds  si, source
  145.   les  di, dest
  146.   mov  cx, ( 320 * 200 ) / 4
  147. db 66h
  148.   rep  movsw  (* db 66h + rep movsw = rep movsd *)
  149.   pop  ds
  150. end;
  151.  
  152. procedure setpalette( var new  : palette ); assembler;
  153. asm
  154.   push ds
  155.   xor  al, al
  156.   mov  dx, 03c8h
  157.   out  dx, al
  158.   inc  dx
  159.   cld
  160.   mov  cx, 768
  161.   lds  si, new
  162.   rep  outsb
  163.   pop  ds
  164. end;
  165.  
  166. procedure getpalette( var pal  : palette ); assembler;
  167. asm
  168.   cld
  169.   xor  al, al
  170.   mov  dx, 03c7h
  171.   out  dx, al
  172.   mov  dx, 03c9h
  173.   mov  cx, 768
  174.   les  di, pal
  175.   rep  insb
  176. end;
  177.  
  178. procedure plotpixel( x, y  : word; color  : byte; pscr  : pscreen );
  179. begin
  180.   pscr^[ y, x ] := color;
  181. end;
  182.  
  183. function getpixel(  x, y  : word; pscr  : pscreen ) :  byte;
  184. begin
  185.   getpixel := pscr^[ y, x ];
  186. end;
  187.  
  188. {$I-}
  189. function pcxshow( pcxname  : string; xs, ys  : word; pscr  : pscreen; truecolors  : boolean ) :  boolean;
  190. var
  191.   pcxfile     : file;
  192.   pcxsize     : longint;  (*  filesize w/o the 256-color palette *)
  193.   pcxfileptr  : longint;
  194.   pcx         : record
  195.                  id            : byte;    (*  0ah  *)
  196.                  version       : byte;
  197.                  compressed    : boolean;
  198.                  bpp           : byte;    (*  bits per pixel *)
  199.                  xstart        : word;
  200.                  ystart        : word;
  201.                  xend          : word;
  202.                  yend          : word;
  203.                  hdpi          : word;    (*  horizontal dots per inch *)
  204.                  vdpi          : word;    (*  vertical dots per inch   *)
  205.                  colors16      : array[ 0..15, 0..2 ] of byte;
  206.                  reserved      : byte;
  207.                  planes        : byte;
  208.                  scanlinewidth : word;    (*  bytes per line *)
  209.                  palettetype   : word;
  210.                  fillup        : array[ 0..57 ] of byte;
  211.                 end;
  212.  
  213.   x, y        : word;
  214.   w, h        : word;
  215.   databyte    : byte;
  216.   count       : byte;
  217.   color       : byte;
  218.   i           : byte;
  219.  
  220.   colors256   : palette;
  221.  
  222.   data        : pointer;
  223.   datasize    : word;
  224.   maxdatasize : word;
  225.   dataptr     : word;
  226.  
  227. procedure readdata;
  228. var
  229.   dataread  :  word;
  230. begin
  231.   if pcxfileptr < pcxsize then
  232.     begin
  233.       datasize := maxdatasize;
  234.       if ( pcxsize - pcxfileptr ) < datasize then datasize := pcxsize - pcxfileptr;
  235.       blockread( pcxfile, data^, datasize, dataread );
  236.       if ioresult <> 0 then begin pcxshow := false; exit; end;
  237.       if dataread < datasize then datasize := dataread;
  238.       inc( pcxfileptr, dataread );
  239.       dataptr := 0;
  240.     end else datasize := 0;
  241. end;
  242.  
  243. begin
  244. { open the pcxfile  }
  245.   assign( pcxfile, pcxname );
  246.   reset( pcxfile, 1 );
  247.   if ioresult <> 0 then begin pcxshow := false; exit; end;
  248.   pcxsize := filesize( pcxfile );
  249. { read the header }
  250.   blockread( pcxfile, pcx, sizeof( pcx ) );
  251.   if ioresult <> 0 then begin pcxshow := false; exit; end;
  252. { look at header }
  253.   with pcx do
  254.     begin
  255.       if (( id <> $0a ) or
  256.           ( version <> 5 ) or
  257.           ( not compressed ) or
  258.           ( bpp <> 8 ) or
  259.           ( planes <> 1 )) then
  260.         begin
  261.           pcxshow := false;
  262.           exit;
  263.         end;
  264. { read palette }
  265.       if truecolors then
  266.         begin
  267.           seek( pcxfile, pcxsize - 769 );
  268.           blockread( pcxfile, databyte, 1 );
  269.           if ioresult <> 0 then begin pcxshow := false; exit; end;
  270.           if databyte <> $0c then begin pcxshow := false; exit; end;
  271.           blockread( pcxfile, colors256, sizeof( palette ) );
  272.           if ioresult <> 0 then begin pcxshow := false; exit; end;
  273. { shrink palette ; i don't know why but it funx ( i read it anywhere ) }
  274.           for color := 0 to 255 do
  275.             for count := 0 to 2 do
  276.               colors256[ color, count ] := colors256[ color, count ] shr 2;
  277.           setpalette( colors256 );
  278.           seek( pcxfile, 128 );
  279.         end;
  280.       pcxsize := pcxsize - 769;
  281.       pcxfileptr := 128;
  282. { allocate memory }
  283.       if (( pcxsize - pcxfileptr ) < 65536 ) then maxdatasize := pcxsize - pcxfileptr
  284.       else maxdatasize := $ffff;
  285.       if maxavail < maxdatasize then maxdatasize := maxavail;
  286.       getmem( data, maxdatasize );
  287. { calculate some }
  288.       y := ys;
  289.       x := xs;
  290.       w := xend - xstart + 1;
  291.       h := yend - ystart + 1;
  292. { main loop }
  293.       repeat
  294.         readdata;
  295.         if datasize = 0 then begin pcxshow := false; exit; end;
  296.         repeat
  297.           databyte := mem[ seg(data^):ofs(data^) + dataptr ];
  298.           inc( dataptr );
  299.           if ( databyte and 192 ) = 192 then
  300.             begin
  301.               if dataptr >= datasize then
  302.                 begin
  303.                   readdata;
  304.                   if datasize = 0 then begin pcxshow := false; exit; end;
  305.                 end;
  306.               count := databyte and 63;
  307.               color := mem[ seg(data^):ofs(data^) + dataptr ];
  308.               inc( dataptr );
  309.             end else
  310.             begin
  311.               count := 1;
  312.               color := databyte;
  313.             end;
  314.           for i := 1 to count do
  315.             begin
  316.               plotpixel( x, y, color, pscr );
  317.               inc( x );
  318.               dec( w );
  319.               if w = 0 then
  320.                 begin
  321.                   x := xs;
  322.                   w := xend - xstart + 1;
  323.                   inc( y );
  324.                   dec( h );
  325.                   if h = 0 then dataptr := datasize;
  326.                 end;
  327.             end;
  328.         until dataptr >= datasize;
  329.       until h = 0;
  330.     end;
  331. { shutdown }
  332.   freemem( data, maxdatasize );
  333.   close( pcxfile );
  334.   pcxshow := true;
  335. end;
  336. {$I+}
  337.  
  338. function getsprite( var s  : sprite; xs, ys, width, height : word; pscr  : pscreen ) :  boolean;
  339. var p    : pointer;
  340.     size : word;
  341.     c    : word;
  342.     y    : word;
  343. begin
  344.   size := width * height;
  345.   if maxavail < size then begin getsprite := false; exit; end;
  346.   getmem( p, size );
  347.   c := 0;
  348.   for y := ys to ys+height-1 do
  349.     begin
  350.       move( pscr^[y, xs], ptr( seg(p^), ofs(p^)+c)^, width );
  351.       c := c + width;
  352.     end;
  353.   s.width   := width;
  354.   s.height  := height;
  355.   s.data    := p;
  356.   getsprite := true;
  357. end;
  358.  
  359. procedure putsprite( s  : sprite; xs, ys  : integer; pscr  : pscreen );
  360. var c        : word;
  361.     y        : word;
  362.     rwidth   : word;
  363.     rheight  : word;
  364.     rxs, rys : word;
  365. begin
  366.   if xs < clipx1 then rxs := clipx1 else rxs := xs;
  367.   if ys < clipy1 then rys := clipy1 else rys := ys;
  368.   c := (( rys - ys ) * s.width) + ( rxs-xs );
  369.   rwidth  := s.width - ( rxs - xs );
  370.   rheight := s.height - ( rys - ys );
  371.   if ( rxs+rwidth-1 )  > clipx2 then rwidth  := clipx2-rxs+1;
  372.   if ( rys+rheight-1 ) > clipy2 then rheight := clipy2-rys+1;
  373.   for y := rys to rys+rheight-1 do
  374.     begin
  375.       move( ptr( seg(s.data^), ofs(s.data^)+c)^, pscr^[y, rxs], rwidth );
  376.       c := c + s.width;
  377.     end;
  378. end;
  379.  
  380. procedure putspritemasked( s  : sprite; xs, ys  : integer; pscr : pscreen; maskcolor : byte );
  381. var x        : word;
  382.     y        : word;
  383.     c, d     : word;
  384.     rwidth   : word;
  385.     rheight  : word;
  386.     rxs, rys : word;
  387. begin
  388.   if xs < clipx1 then rxs := clipx1 else rxs := xs;
  389.   if ys < clipy1 then rys := clipy1 else rys := ys;
  390.   c := (( rys - ys ) * s.width) + ( rxs-xs );
  391.   rwidth  := s.width - ( rxs - xs );
  392.   rheight := s.height - ( rys - ys );
  393.   if ( rxs+rwidth-1 )  > clipx2 then rwidth  := clipx2-rxs+1;
  394.   if ( rys+rheight-1 ) > clipy2 then rheight := clipy2-rys+1;
  395.   for y := rys to rys+rheight-1 do
  396.     begin
  397.       d := 0;
  398.       for x := rxs to rxs+rwidth-1 do
  399.         begin
  400.           if mem[ seg(s.data^):ofs(s.data^)+c ] <> maskcolor then pscr^[ y, x ] := mem[ seg(s.data^):ofs(s.data^)+c ];
  401.           inc( c );
  402.           inc( d );
  403.         end;
  404.       c := c + ( s.width - d );
  405.     end;
  406. end;
  407.  
  408. procedure setclipping( x1, y1, x2, y2  : word );
  409. begin
  410.   clipx1 := x1;
  411.   clipy1 := y1;
  412.   clipx2 := x2;
  413.   clipy2 := y2;
  414. end;
  415.  
  416. procedure vsync; assembler;
  417. label _loop_one, _loop_two;
  418. asm
  419.   mov  dx, 3dah
  420. _loop_one :
  421.   in   al, dx
  422.   test al, 08
  423.   jnz  _loop_one
  424. _loop_two :
  425.   in   al, dx
  426.   test al, 08
  427.   jz   _loop_two
  428. end;
  429.  
  430. end.