home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TUTORC.ZIP / TUT16.C < prev    next >
C/C++ Source or Header  |  1994-10-30  |  7KB  |  235 lines

  1. /* 
  2.   10/30/94
  3.   tut16.c
  4.   from tutprog16.pas
  5.   Adapted from Denthor's tutprog16.pas
  6.   Translated into C, from Denthor's VGA Trainer, by
  7.   Steve Pinault, scp@ohm.att.com
  8.   Compiled with Microsoft Visual C++ 1.5 (Microsoft C 8.0)
  9.   To compile:
  10.   First compile the subroutines in tutsubs.c with the batch file 
  11.   cltutsub.bat
  12.   Then compile any of the tutor programs with the batch file
  13.   cltut.bat
  14.   Example: C:>cltutsub
  15.            C:>cltut tut16.c
  16.            to compile this program.
  17. */
  18.  
  19. #include "tutheadr.h"
  20.  
  21. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  22. // Procedure LoadCELPal (FileName : String; Var Palette : Pallette);
  23. //   { This loads in the pallette of the .CEL file into the variable Palette }
  24. void loadcelpal(char* fname, char palette[][3])
  25. {
  26.   FILE* fptr;
  27.   if((fptr=fopen(fname,"rb"))==NULL)
  28.   {
  29.     SetText();
  30.     printf("Error opening file %s\n",fname);
  31.     exit(1);
  32.   }
  33.   fseek(fptr,32,SEEK_SET);
  34.   fread(palette,sizeof(char),768,fptr);
  35.   fclose(fptr);                    
  36. }
  37.  
  38. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  39. // Procedure Init;
  40. void init()
  41. {
  42.   int loop1,loop2;
  43.   char tpal[256][3];
  44.   
  45.   SetUpVirtual();
  46.   Cls (0,Vaddr2);
  47.   Cls (0,Vaddr);
  48.   loadcelpal ("to.cel",tpal);
  49.   for(loop1=0;loop1<256;loop1++)
  50.     Pal ((char)loop1,tpal[loop1][0],tpal[loop1][1],tpal[loop1][2]);
  51.   LoadCEL ("to.cel",Virtual);
  52.   for(loop1=0;loop1<320;loop1++)
  53.     for(loop2=0;loop2<200;loop2++)
  54.       if(GetPixel(loop1,loop2,Vaddr)==0)
  55.         PutPixel(loop1,loop2,(char)((loop1+loop2)%256),Vaddr);
  56. }        
  57.  
  58. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  59. // Procedure Scale (x,y,w,h,origw,origh,source,dest:word); assembler;
  60. //  { This scales the picture to the size of w and h, and places the result
  61. //    at x , y. Origw and origh are the origional width and height of the
  62. //    bitmap. The bitmap must start at the beginning of a segment, with
  63. //    source being the segment value. The image is placed in screen at dest}
  64. void scale(int x,int y,int w,int h,int origw,int origh,int source,int dest)
  65. {
  66.   int jx,jy,depth,temp;
  67.   __asm
  68.   {
  69.   push  ds
  70.  
  71.   mov   ax,source
  72.   mov   ds,ax
  73.   mov   ax,dest
  74.   mov   es,ax
  75.   mov   depth,0
  76.   dec   h
  77.  
  78.   xor   dx,dx
  79.   mov   ax,origw
  80.   shl   ax,6
  81.   mov   bx,w
  82.   div   bx
  83.   shl   ax,2
  84.   mov   jx,ax    ; { jx:=origw*256/w }
  85.  
  86.   xor   dx,dx
  87.   mov   ax,origh
  88.   shl   ax,6
  89.   mov   bx,h
  90.   div   bx
  91.   shl   ax,2
  92.   mov   jy,ax    ; { jy:=origh*256/h }
  93.  
  94.   xor   cx,cx
  95. ALoop2 :         ; { vertical loop }
  96.   push  cx
  97.   mov   ax,depth
  98.   add   ax,jy
  99.   mov   depth,ax
  100.  
  101.   xor   dx,dx
  102.   mov   ax,depth
  103.   shr   ax,8
  104.   mov   bx,origw
  105.   mul   bx
  106.   mov   temp,ax  ; { temp:=depth shr 8*origw;}
  107.  
  108.  
  109.   mov   di,y
  110.   add   di,cx
  111.   mov   bx,di
  112.   shl   di,8
  113.   shl   bx,6
  114.   add   di,bx
  115.   add   di,x     ; { es:di = dest ... di=(loop1+y)*320+x }
  116.  
  117.   mov   cx,w
  118.   xor   bx,bx
  119.   mov   dx,jx
  120.   mov   ax,temp
  121. ALoop1 :         ; { horizontal loop }
  122.   mov   si,bx
  123.   shr   si,8
  124.   add   si,ax    ; { ax = temp = start of line }
  125.  
  126.   movsb          ; { si=temp+(si shr 8) }
  127.   add   bx,dx
  128.  
  129.   dec   cx
  130.   jnz   ALoop1   ; { horizontal loop }
  131.  
  132.   pop   cx
  133.   inc   cx
  134.   cmp   cx,h
  135.   jl    ALoop2   ; { vertical loop }
  136.  
  137.   pop   ds        
  138.   }
  139. }
  140.  
  141. //{DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  142. //Procedure Play;
  143. void play()
  144. {
  145.   int x,y,z,loop1;
  146.   z=114;
  147.   while(1)
  148.   {
  149.     for(loop1=0;loop1<100;loop1++)
  150.     {
  151.       z--;
  152.       x=(16<<8)/z; 
  153.       y=(10<<8)/z;
  154.       // { Perspective transforms ... makes the zoom smoother }
  155.       Cls (0,Vaddr2);
  156.       scale (160-(x >> 1),100-(y >> 1),x,y,320,200,Vaddr,Vaddr2);
  157.       WaitRetrace();
  158.       Flip2 (Vaddr2,VGA);
  159.     }
  160.     //   { Scale towards you }
  161.     for(loop1=0;loop1<100;loop1++)
  162.     {
  163.       z++;
  164.       x=(16<<8)/z; 
  165.       y=(10<<8)/z;
  166.       // { Perspective transforms ... makes the zoom smoother }
  167.       Cls (0,Vaddr2);
  168.       scale (160-(x >> 1),100-(y >> 1),x,y,320,200,Vaddr,Vaddr2);
  169.       WaitRetrace();    
  170.       Flip2 (Vaddr2,VGA);
  171.     }
  172.     if(_bios_keybrd(_KEYBRD_READY)){getch();break;}
  173.   }
  174. }
  175.  
  176. void main()
  177.   SetUpVirtual();
  178.   SetMCGA();
  179.   init();
  180.   play();
  181.   SetText();
  182. }          
  183.  
  184. /*
  185. BEGIN
  186.   clrscr;
  187.   writeln ('Hokay! Here is the sixteenth tutorial! This one is on nice fast 2d');
  188.   writeln ('scaling, for any size bitmap. Just hit any key and it will scale a');
  189.   writeln ('picture up and down. Clipping is NOT performed, so the destination');
  190.   writeln ('pic MUST fit in the screen boundaries. In one zoom towards and away');
  191.   writeln ('from you there is 100 frames.');
  192.   writeln;
  193.   Writeln ('You can make many nice effects with scaling, this "bouncing" is just');
  194.   writeln ('one of them ... go on, amaze everyone with your ingenuity ;-) Also,');
  195.   writeln ('why not test your coding mettle, so to speak, by implementing clipping?');
  196.   Writeln;
  197.   writeln ('The routine could greatly be speeded up with 386 extended registers, but');
  198.   writeln ('for the sake of compatability I have kept it to 286 code. Also, this');
  199.   writeln ('routine isn''t fully optimised .. you may be able to get some speedups');
  200.   writeln ('out of it... (probably by moving the finding of DI out of the loop and');
  201.   writeln ('just adding a constant for each line ... hint hint) ;)');
  202.   writeln;
  203.   writeln ('The pic was drawn by me for Tut11, I am reusing it because I am at varsity..');
  204.   writeln ('without a mouse. :(');
  205.   writeln;
  206.   writeln;
  207.   writeln ('Hit any key to continue ... ');
  208.   readkey;
  209.   setupvirtual;
  210.   setmcga;
  211.   init;
  212.   play;
  213.   settext;
  214.   shutdown;
  215.   freemem (virscr2,sizeof(virscr2^));
  216.   Writeln ('All done. This concludes the sixteenth sample program in the ASPHYXIA');
  217.   Writeln ('Training series. You may reach DENTHOR under the names of GRANT');
  218.   Writeln ('SMITH/DENTHOR/ASPHYXIA on the ASPHYXIA BBS.I also occasinally');
  219.   Writeln ('RSAProg, comp.lang.pascal and comp.sys.ibm.pc.demos. E-mail me at :');
  220.   Writeln ('    denthor@beastie.cs.und.ac.za');
  221.   Writeln ('The numbers are available in the main text. You may also write to me at:');
  222.   Writeln ('             Grant Smith');
  223.   Writeln ('             P.O. Box 270');
  224.   Writeln ('             Kloof');
  225.   Writeln ('             3640');
  226.   Writeln ('             Natal');
  227.   Writeln ('             South Africa');
  228.   Writeln ('I hope to hear from you soon!');
  229.   Writeln; Writeln;
  230.   Write   ('Hit any key to exit ...');
  231.   readkey;
  232. END.
  233. */
  234.