home *** CD-ROM | disk | FTP | other *** search
/ The Party 1994: Try This At Home / disk_image.bin / source / crosfade / total.txt < prev   
Text File  |  1995-04-18  |  9KB  |  354 lines

  1. #include "alloc.h"
  2. #include "io.h"
  3. #include "fcntl.h"
  4. #include "conio.h"
  5. #include "sys/stat.h"
  6.  
  7.  
  8.  
  9.  
  10. /*The following are the prototypes for the routine sin FADE.ASM */
  11. void setPal(void far *pal);
  12. void fill_pal(void far *pal,char red,char green,char blue);
  13. void copy_pal(void far *pal,void far *pal_dest);
  14. void sub_palette(void far *pal,void far *pal_dest);
  15. void fade_between_once(void far *pal,void far *pal_dest);
  16.  
  17.  
  18. char pal[768];
  19. char pal2[768];
  20.  
  21.  
  22.  
  23. const char *image="image.raw";
  24. const char *palf1="pal1.pal";
  25. const char *palf2="pal2.pal";
  26.  
  27. int readstuff(const char *filename,void far *buf,unsigned length)
  28. {
  29.    int handle, bytes;
  30.  
  31.  
  32.    if ((handle =
  33.       sopen(filename, O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
  34.    {
  35.       printf("Error Opening File\n");
  36.       exit(1);
  37.    }
  38.  
  39.    if ((bytes = read(handle, buf, length)) == -1) {
  40.       printf("Read Failed.\n");
  41.       exit(1);
  42.    }
  43.    return 0;
  44. }
  45.  
  46.  
  47. void fade_between_screen(void far *pal,void far *paldest)
  48. /* This routine actually morph one palette to another */
  49. {  int n;
  50.    char far *buff,*buff2;
  51.    buff=farmalloc(768*2);
  52.    buff2=farmalloc(768);
  53.    copyPal(paldest,buff2);
  54.    copyPal(pal,buff);
  55.    sub_palette(buff,buff2);
  56.    for(n=0;n<63;++n)
  57.     fade_between_once(buff,buff2);
  58.    farfree(buff2);
  59.    farfree(buff);
  60. }
  61.  
  62.  
  63.  
  64. void main()
  65. {  int n;
  66.    asm{           /* set mode 13h video mode*/
  67.      mov  ax,13h
  68.      int  10h
  69.    }
  70.  
  71.   /* read 320*200 raw image to video buffer*/
  72.  
  73.    fill_pal(&pal,0,0,0);  /* fill pal[768] with 0*/
  74.    setPal(&pal);
  75.    readstuff(image,(void far *)0xa0000000,64000);
  76.  
  77.   /*read palette file */
  78.    readstuff(palf1,&pal,768);
  79.    readstuff(palf2,&pal2,768);
  80.  
  81.    setPal(&pal);
  82.  
  83.    getch();
  84.  
  85.    fade_between_screen(&pal,&pal2);
  86.  
  87.    getch();
  88.  
  89.    fade_between_screen(&pal2,&pal);
  90.  
  91.    getch();
  92.  
  93.    asm{         /* return to text mode */
  94.      mov ax,3h
  95.      int 10h
  96.    }
  97.    printf("crosfade.exe written by Esak 1994");
  98. }
  99. %TITLE "moduleShell"
  100. ;---------------------------------------------------------------------
  101. ; CAUTION!
  102. ;      This module is written in Ideal model, meaning you need
  103. ;      Borland Turbo Assembler to compile it.
  104. ;------------------------------------------------------------------------
  105.  
  106.        IDEAL
  107.        P286
  108.        include "model.inc"
  109.  
  110.       DATASEG
  111.  
  112.  
  113. PAL_SIZE EQU 768
  114.  
  115.  
  116.        CODESEG
  117.  
  118.  
  119.        PUBLIC   _setpal
  120.        PUBLIC   _sub_palette,_fade_between_once
  121.        PUBLIC   _fill_pal,_copyPal
  122.  
  123.  
  124.  
  125. PROC _setpal
  126. ;-----------------------------------------------------------------
  127. ; void setpal(void far *pal)
  128. ; This function simply updates hardware palette using the data
  129. ; stored in char pal[PAL_SIZE]
  130. ;  (PAL_SIZE=768)
  131. ;-----------------------------------------------------------------
  132. ARG pal:dword
  133.  
  134.      push      bp
  135.      mov       bp,sp
  136.      push      ds
  137.      push      di
  138.      push      si
  139.      lds      si,[pal]
  140.      call      SETPAL
  141.      pop       si
  142.      pop       di
  143.      pop       ds
  144.      pop       bp
  145.      ret
  146. ENDP _setpal
  147.  
  148.  
  149. PROC _sub_palette
  150. ;----------------------------------------------------------------------
  151. ; sub_palette(void *pal,void far *pal_dest)
  152. ; This routine prepares the palettes for fade_between_once()
  153. ; function.
  154. ; WARNING: pal points to char array of 768*2 bytes, not 768 bytes
  155. ;----------------------------------------------------------------------
  156. ARG pal:dword,pal_dest:dword
  157.       push   bp
  158.       mov    bp,sp
  159.       push   ds
  160.       push   di
  161.       push   si
  162.  
  163.       lds    si,[pal]          ;ds:si points to pal[]
  164.       les    di,[pal_dest]     ;es:di points to pal_dest[]
  165.  
  166.       mov    cx,PAL_SIZE       ; the following loop is basically
  167. @@l:  lodsb                    ; for(n=0;n<768;++n)
  168.       sub    [es:di],al        ;  pal_dest[n]=pal_dest[n]-pal[n];
  169.       inc    di                ;
  170.       loop   @@l
  171.  
  172.       mov    cx,PAL_SIZE/2     ; the following loop is equivalent to
  173.       xor    ax,ax
  174. @@l2: mov    [ds:si],ax        ; for(n=0;n<768;++n)
  175.       inc    si                ;  pal[n+PAL_SIZE]=0;
  176.       inc    si
  177.       loop   @@l2
  178.  
  179.       pop     si
  180.       pop     di
  181.       pop     ds
  182.       pop     bp
  183.       ret
  184. ENDP _sub_palette
  185.  
  186.  
  187.  
  188.  
  189. PROC _fade_between_once
  190. ;-------------------------------------------------------------------
  191. ; fade_between_once(void far *pal,void far *pal_dest)
  192. ; call sub_palette first
  193. ; Which will destroy original values of pal_dest,
  194. ;    but it's a necessary step.
  195. ; After calling it 63 times,the content of pal[768] will become
  196. ; identical to the original values of pal_dest anyway :)
  197. ;-------------------------------------------------------------------
  198. ARG  pal:dword,pal_dest:dword
  199.      push   bp
  200.      mov    bp,sp
  201.      push   ds
  202.      push   di
  203.      push   si
  204.      lds    si,[pal]
  205.      les    di,[pal_dest]
  206.  
  207.      cld
  208.      push   si
  209.  
  210.       mov     cx,PAL_SIZE
  211. fl2:                         ; increase the value of pallettes
  212.       mov     bl,[es:di]     ; es:di = dest_pal
  213.       cmp     bl,0
  214.       jl      fl4            ; if bl>0 then goto fl4
  215.  
  216.       add     [PAL_SIZE+si],bl
  217.       jmp     fl3
  218.  
  219. fl4:  neg     bl
  220.       add     [PAL_SIZE+si],bl
  221. fl5:  cmp     [byte ptr PAL_SIZE+si],63
  222.       jb      fl7
  223.       dec     [byte ptr si]
  224.       sub     [byte ptr PAL_SIZE+si],63
  225.       jmp     fl7
  226.  
  227. fl3:  cmp     [byte ptr PAL_SIZE+si],63
  228.       jb      fl7      ;
  229.       inc     [byte ptr si] ; increase by one
  230.       sub     [byte ptr PAL_SIZE+si],63
  231. fl7:  inc     di
  232.       inc     si
  233.       loop    fl2
  234.  
  235.  
  236.      pop     si
  237.      call    Setpal
  238.  
  239.      pop     si
  240.      pop     di
  241.      pop     ds
  242.      pop     bp
  243.      ret
  244. ENDP _fade_between_once
  245.  
  246.  
  247.  
  248. ;-------------------------------------------------------------------------
  249. ; update the hardware palette
  250. ; input: ds:si points to palette array
  251. ;-------------------------------------------------------------------------
  252.  
  253.  
  254. PROC SETPAL
  255.       mov       bh,0               ; bh=# of the first palette color to
  256.                    ; update.
  257.       mov       bl,2               ;This is the loop index.
  258.                    ;In the following codes, we are updating
  259.                    ;128 color at a time. Therefore, we need
  260.                    ;a loop that runs twice in order to update
  261.                    ;all 256 colors.
  262.  
  263.     mov     cx,128*3         ; di=128(the number of colors to update)*3
  264.  
  265. PROC SETPAL2
  266. s:
  267.     mov dx, 03DAh               ; CRT controller input status 1 register
  268. v1:
  269.     in    al, dx
  270.     test   al,08h
  271.     jnz    v1                     ; wait until vertical retrace starts
  272. v2:
  273.     in     al, dx
  274.     test   al,08h                 ; wait until vertical retrace ends
  275.         jz     v2
  276.  
  277.  ;--------- We have done waiting. Now let's update the palette
  278.     mov     al,bh            ; get first color # to process into al
  279.     mov     dx, 03c8h        ; DAC palette index register
  280.  
  281.     push    cx
  282.     out     dx, al           ; Write the register number to the dac
  283.     inc     dx
  284.     rep     outsb
  285.     pop     cx
  286.  
  287.     add     bh,128           ; color index=color index+128
  288.     dec     bl
  289.     jnz     s
  290.  
  291.       ret
  292. ;----------------------------------------
  293.  
  294. ENDP SETPAL2
  295.  
  296.  
  297.  
  298.  
  299. PROC _fill_pal
  300. ;-------------------------------------------------------------
  301. ; fill_pal(void far *pal, char red, char green, char blue);
  302. ; fills the palette array with the color data given
  303. ; does not update hardware palette
  304. ;-------------------------------------------------------------
  305. ARG pal:dword, red:byte, green:byte,blue:byte
  306.       push   bp
  307.       mov    bp,sp
  308.       push   di
  309.       les    di,[pal]
  310.       cld
  311.       mov    cx,PAL_SIZE/3
  312.       mov    al,[red]
  313.       mov    ah,[green]
  314.       mov    bl,[blue]
  315. @@fpl:mov    [es:di],al
  316.       mov    [es:di+1],ah
  317.       mov    [es:di+2],bl
  318.       add    di,3
  319.       loop   @@fpl
  320.       pop    di
  321.       pop    bp
  322.       ret
  323. ENDP _fill_pal
  324.  
  325.  
  326. PROC _copyPal
  327. ;----------------------------------------------------------------
  328. ; copyPal(void far *source,void far *dest:dword)
  329. ;
  330. ; both *source and *dest points to arrays of 768 bytes (chars)
  331. ; I know there are C functions out there to copy strings. But
  332. ; I wrote this anyway. What the heck, it only took me a minute.
  333. ;----------------------------------------------------------------
  334. ARG source:dword,dest:dword
  335.       push   bp
  336.       mov    bp,sp
  337.       push   ds
  338.       push   di
  339.       push   si
  340.  
  341.       cld
  342.       les    di,[dest]
  343.       lds    si,[source]
  344.       mov    cx,PAL_SIZE
  345.       rep    movsb
  346.       pop    si
  347.       pop    di
  348.       pop    ds
  349.       pop    bp
  350.       ret
  351. ENDP _copyPal
  352.  
  353.       END                     ; End of module
  354.