home *** CD-ROM | disk | FTP | other *** search
/ HOT Scene Stuff / hotscenestuffzyklop1996.iso / demos / sunknown / xgraf.cpp < prev    next >
C/C++ Source or Header  |  1994-04-06  |  7KB  |  388 lines

  1. // XGRAF.CPP ////////////////////////////////////////////////////////////////
  2.  
  3. // Thomas H.
  4.  
  5. // INCLUDES /////////////////////////////////////////////////////////////////
  6.  
  7. #include <dos.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <stdlib.h>
  11.  
  12. #include "global.h"
  13. #include "template.h"
  14. #include "xgraf.h"
  15. #include "timer.h"
  16.  
  17. // DEFINES //////////////////////////////////////////////////////////////////
  18.  
  19. #define NUMBLOCKS 10
  20. #define BLOCKLEN   12
  21. #define PAUSE      30
  22.  
  23. // EXTERNALS ////////////////////////////////////////////////////////////////
  24.  
  25. extern timer_C timer;
  26. extern byte rgb[768];
  27.  
  28. // FUNCTIONS ////////////////////////////////////////////////////////////////
  29.  
  30. // MODEX
  31.  
  32. void modex(int not13)
  33. {
  34.     frame();
  35.     if (!not13)
  36.     {
  37.         _AX=0x0013;
  38.         VGAINT;
  39.     }
  40.     frame();
  41.  
  42.     outportb(0x3c8,0);
  43.     for (int count=0; count<768; count++)
  44.         outportb(0x3c9,0);
  45.     outport(SC_INDEX,0x0604);
  46.     outport(SC_INDEX,0x0100);
  47.     outportb(MISC_OUTPUT,0xe3);
  48.     outport(SC_INDEX,0x0300);
  49.     outportb(CRTC_INDEX,0x11);
  50.     _AL=inportb(CRTC_INDEX+1)&0x7f;
  51.     outportb(CRTC_INDEX+1,_AL);
  52.     outport(CRTC_INDEX,0x0d06);
  53.     outport(CRTC_INDEX,0x3e07);
  54.     outport(CRTC_INDEX,0x4109);
  55.     outport(CRTC_INDEX,0xea10);
  56.     outport(CRTC_INDEX,0xac11);
  57.     outport(CRTC_INDEX,0xdf12);
  58.     outport(CRTC_INDEX,0x0014);
  59.     outport(CRTC_INDEX,0xe715);
  60.     outport(CRTC_INDEX,0x0616);
  61.     outport(CRTC_INDEX,0xe317);
  62.     outport(SC_INDEX,0x0f02);
  63.  
  64.     asm mov ax,0xa000
  65.     asm mov es,ax
  66.     asm xor ax,ax
  67.     asm xor di,di
  68.     asm mov cx,32768
  69.     asm rep stosw
  70. }
  71.  
  72. // TEXTMODE
  73.  
  74. void textmode(void)
  75. {
  76.     _AX=3;
  77.     VGAINT;
  78. }
  79.  
  80. // PUTPIXEL
  81.  
  82. void putpixel(int x,int y,byte color)
  83. {
  84.     if (x<0 || x>=320)
  85.         return;
  86.     asm mov ax,80
  87.     asm mul y
  88.     asm mov bx,x
  89.     asm mov cx,bx
  90.     asm shr bx,1
  91.     asm shr bx,1
  92.     asm add bx,ax
  93.     asm mov ax,0xa000
  94.     asm mov es,ax
  95.  
  96.     asm and cl,0x03
  97.     asm mov ax,0x0100+MAP_MASK
  98.     asm shl ah,cl
  99.     asm mov dx,SC_INDEX
  100.     asm out dx,ax
  101.  
  102.     asm mov al,color
  103.     asm mov es:[bx],al
  104. }
  105.  
  106. // GETPIXEL
  107.  
  108. byte getpixel(int x,int y)
  109. {
  110.     if (x<0 || x>=320)
  111.         return 0;
  112.     asm mov ax,80
  113.     asm mul y
  114.     asm mov bx,x
  115.     asm mov cx,bx
  116.     asm shr bx,1
  117.     asm shr bx,1
  118.     asm add bx,ax
  119.     asm mov ax,0xa000
  120.     asm mov es,ax
  121.     asm mov ah,cl
  122.     asm and ah,0x03
  123.     asm mov al,READ_MAP
  124.     asm mov dx,GC_INDEX
  125.     asm out dx,ax
  126.     asm mov al,es:[bx]
  127.   return _AL; // Return value already in AL
  128. }
  129.  
  130. // UPDATEPALETTE
  131.  
  132. void updatepalette(byte *pal)
  133. {
  134.     outportb(0x3c8,0);
  135.     for (int count=0; count<768; count++)
  136.     {
  137.         if (!(count&15))
  138.             frame();
  139.         outportb(0x3c9,pal[count]);
  140.     }
  141. }
  142.  
  143. // YOFFSET
  144.  
  145. void yoffset(int yoff)
  146. {
  147.     static int lastyoff=28712;
  148.     if (yoff==lastyoff)
  149.     return;
  150.   lastyoff=yoff;
  151.   asm mov ax,80
  152.   asm mul yoff
  153.   asm mov bh,al
  154.   asm mov ch,ah
  155.   asm mov bl,0x0d
  156.     asm mov cl,0x0c
  157.     asm mov ah,1
  158.  
  159.     asm mov dx,0x3da
  160. vent:
  161.     asm in al,dx
  162.     asm test al,ah
  163.     asm jnz vent
  164.  
  165.     asm mov dx,CRTC_INDEX
  166.     asm mov ax,bx
  167.     asm cli
  168.     asm out dx,ax
  169.     asm mov ax,cx
  170.     asm out dx,ax
  171.     asm sti
  172. }
  173.  
  174. // FRAME
  175.  
  176. void frame(void)
  177. {
  178.   while (inportb(0x3da)&8);
  179.   while (!(inportb(0x3da)&8));
  180. }
  181.  
  182. // SETRGB
  183.  
  184. void setrgb(byte color, byte red,byte green,byte blue)
  185. {
  186.     outportb(0x3c8,color);
  187.     outportb(0x3c9,red);
  188.   outportb(0x3c9,green);
  189.   outportb(0x3c9,blue);
  190. }
  191.  
  192. // BESTFIT
  193.  
  194. byte bestfit(int resr, int resg, int resb)
  195. {
  196.     long best=100000000l;
  197.     byte hit,*rp2=rgb;
  198.     for (int test=0; test<256; test++)
  199.     {
  200.         int r2=*(rp2++);
  201.         r2<<=8;
  202.         long bomr=resr-r2;
  203.  
  204.         int g2=*(rp2++);
  205.         g2<<=8;
  206.         long bomg=resg-g2;
  207.  
  208.         int b2=*(rp2++);
  209.         b2<<=8;
  210.         long bomb=resb-b2;
  211.  
  212.         bomb*=bomb;
  213.         bomr*=bomr;
  214.         bomg*=bomg;
  215.         long sumbom=bomr+bomg+bomb;
  216.         if (sumbom<best)
  217.         {
  218.             best=sumbom;
  219.             hit=test;
  220.             if (!best)
  221.                 test=256;
  222.         }
  223.     }
  224.     return hit;
  225. }
  226.  
  227. // SETPALETTE
  228.  
  229. void setpalette(char *filnavn)
  230. {
  231.     FILE *infile=fopen(filnavn,"rb");
  232.     fread(rgb,1,768,infile);
  233.     updatepalette(rgb);
  234.     fclose(infile);
  235. }
  236.  
  237. // PUTABSPIXEL
  238.  
  239. void putabspixel(word adr,byte plan, byte color,word sseg)
  240. {
  241.     asm mov ax,sseg
  242.     asm mov es,ax
  243.     asm mov cl,plan
  244.  
  245.     asm and cl,0x03
  246.     asm mov ax,0x0100+MAP_MASK
  247.     asm shl ah,cl
  248.     asm mov dx,SC_INDEX
  249.     asm out dx,ax
  250.     asm mov al,color
  251.     asm mov bx,adr
  252.     asm mov es:[bx],al
  253. }
  254.  
  255. // BYTT0
  256.  
  257. void switch0(word from, word to)
  258. {
  259.     to=19200;
  260.     from=0;
  261.     yoffset(to/80);
  262.     int full[NUMBLOCKS];
  263.     for (int count=0; count<NUMBLOCKS; count++)
  264.         full[count]=1*(count-NUMBLOCKS-1);
  265.     asm pusha
  266.     asm push ds
  267.   asm cld
  268.     do
  269.     {
  270.         word adr=to;
  271.         word fromadr=from;
  272.         for (int BLOCK=0; BLOCK<NUMBLOCKS; BLOCK++)
  273.         {
  274.             int f=full[BLOCK];
  275.             if (f>=0 && f<BLOCKLEN)
  276.             {
  277.                 if (f)
  278.                 {
  279.                     asm mov ax,0xa000
  280.                     asm mov es,ax
  281.                     asm mov ds,ax
  282.  
  283.                     word tadr=adr+80*(BLOCKLEN-f-1);
  284.                     int ff=f*80;
  285.                     asm mov di,tadr
  286.                     asm mov si,di
  287.                     asm add si,80
  288.                     asm mov cx,ff
  289.                     asm rep movsb
  290.                 }
  291.                 word pl=fromadr+80*f;
  292.                 asm mov ax,0xa000
  293.                 asm mov es,ax
  294.                 asm mov ds,ax
  295.                 asm mov di,adr
  296.                 asm add di,80*(BLOCKLEN-1);
  297.                 asm mov si,pl
  298.                 asm mov cx,80
  299.                 asm rep movsb
  300.             }
  301.             full[BLOCK]++;
  302.             adr+=80*BLOCKLEN;
  303.             fromadr+=80*BLOCKLEN;
  304.         }
  305.         frame();
  306.         frame();
  307.     }while (full[0]<BLOCKLEN);
  308.     asm pop ds
  309.     asm popa
  310. }
  311.  
  312. void switch1(word from, word to)
  313. {
  314.     to=19200;
  315.     from=0;
  316.     yoffset(to/80);
  317.     int full[NUMBLOCKS];
  318.     for (int count=0; count<NUMBLOCKS; count++)
  319.         full[count]=1*(BLOCKLEN+count);
  320.     asm pusha
  321.     asm push ds
  322.     asm cld
  323.     do
  324.     {
  325.         long tid=timer.readtimer();
  326.         word adr=to;
  327.         word fromadr=from;
  328.         for (int BLOCK=0; BLOCK<NUMBLOCKS; BLOCK++)
  329.         {
  330.             int f=full[BLOCK];
  331.             if (f>0 && f<=BLOCKLEN)
  332.             {
  333.                 for (int ned=0; ned<f-1; ned++)
  334.                 {
  335.                     asm mov ax,0xa000
  336.                     asm mov es,ax
  337.                     asm mov ds,ax
  338.  
  339.                     word tadr=adr+80*(BLOCKLEN-ned)-80;
  340.                     asm mov di,tadr
  341.                     asm mov si,di
  342.                     asm add si,-80
  343.                     asm mov cx,80
  344.                     asm rep movsb
  345.                 }
  346.                 word pl=fromadr+80*(BLOCKLEN-f);
  347.                 word pl0=adr+80*(BLOCKLEN-f);
  348.                 asm mov ax,0xa000
  349.                 asm mov es,ax
  350.                 asm mov ds,ax
  351.                 asm mov di,pl0
  352.                 asm mov si,pl
  353.                 asm mov cx,80
  354.                 asm rep movsb
  355.             }
  356.             full[BLOCK]--;
  357.             adr+=80*BLOCKLEN;
  358.             fromadr+=80*BLOCKLEN;
  359.         }
  360.         while (timer.elapsed(tid,timer.readtimer())<PAUSE);
  361.     }while (full[NUMBLOCKS-1]);
  362.     asm pop ds
  363.     asm popa
  364. }
  365.  
  366. // SWITCHPAGE
  367.  
  368. void switchpage(word from,word to,byte method)
  369. {
  370.     asm mov dx,GC_INDEX
  371.     asm mov ax,BIT_MASK
  372.     asm out dx,ax
  373.     asm mov dx,SC_INDEX+1
  374.     asm mov al,0x0f
  375.     asm out dx,al
  376.  
  377.     switch(method)
  378.     {
  379.         case 0: switch0(from,to); break;
  380.         case 1: switch1(from,to); break;
  381.     }
  382.  
  383.     asm mov dx,GC_INDEX+1
  384.     asm mov al,0xff
  385.     asm out dx,al
  386. }
  387.  
  388.