home *** CD-ROM | disk | FTP | other *** search
/ The Equalizer BBS / equalizer-bbs-collection_2004.zip / equalizer-bbs-collection / DEMOSCENE-STUFF / BLACDEMO.ZIP / STAND2.INC < prev    next >
Text File  |  1995-12-30  |  9KB  |  392 lines

  1. ; *************************************************************************
  2. ; ** Filename : "STAND2.INC"                         **
  3. ; ** Purpose  : Include file of standard ASM procedures (version 2)     **
  4. ; *************************************************************************
  5.  
  6. ; Contents:
  7. ; ---------
  8.  
  9. ; (P) = PROC  (M) = MACRO
  10.  
  11. ; VIDEO MODE CONTROL
  12. ;    Set13h      (P) Sets standard 320x200 256 colour mode
  13. ;    SetText        (P) Returns to standard text mode
  14. ;    SetPal        (P) Set a palette
  15.  
  16. ; MEMORY ALLOCATION
  17. ;    MemInit        (M) Set up variables before memory allocation
  18. ;                NB - MUST be the first thing after .startup
  19. ;                   - calls ResizeMem automatically
  20. ;    ResizeMem    (P) Frees up all unused memory for allocation
  21. ;    GetMem        (P) Allocates a memory block
  22. ;    FreeMem        (P) Deallocates a memory block
  23.  
  24. ; FILE ACCESS
  25. ;    LoadData    (P) Load graphics info from an uncompressed file
  26. ;              NB - Autodetects a sprite or background file,
  27. ;                the size of the file and deals with files
  28. ;                over 64k in size automatically. The buffer
  29. ;                must be large enough though.
  30. ;    LoadPal        (P) Load palette information
  31.  
  32. ; GENERAL
  33. ;    WaitKey        (P) Wait for a keypress
  34. ;    Finish        (M) Exits cleanly to DOS
  35.  
  36.              
  37. ; All code Copyright Steve Streeting 1995
  38. ;************************************************************************
  39.  
  40. .data
  41. ProgStartSeg    dw    0
  42. ProgEndSeg    dw    0
  43. OldInt6Seg    dw    0
  44. OldInt6Off    dw    0
  45. OutMemFlag    dw    0
  46.  
  47.  
  48. ;*** Load graphics info
  49. GFXHeader    LABEL BYTE
  50. WidthL        db    ?    ;separate high/low bytes as loaded in in
  51. WidthH        db    ?    ; little endian format, low byte first
  52. HeightL        db    ?
  53. HeightH        db    ?
  54. SpriteWidthL    db    ?
  55. SpriteWidthH    db    ?
  56. SpriteHeightL    db    ?
  57. SpriteHeightH    db    ?
  58. NoSpritesL    db    ?
  59. NoSpritesH    db    ?
  60. FileType    db    ?
  61. Empty        db    5 dup(?)
  62.  
  63. .code
  64.  
  65. FileSize    dd    ?
  66.  
  67. ;************************ PROCEDURES ***************************
  68.  
  69. ;------------------ Free up memory grabbed by EXE-------------
  70. ; *** MUST SET UP SEGS FIRST *****        
  71. ResizeMem        PROC NEAR
  72.  
  73.  
  74.     mov     bx,[ProgEndSeg]
  75.         sub     bx,[ProgStartSeg]
  76.         mov     es,[ProgStartSeg]
  77.         mov     ah,4ah            ;resize mem block (EXE's grabbed all
  78.         int     21h            ;the memory! The git!! 8-))
  79.     ret
  80. ResizeMem    ENDP
  81.  
  82.  
  83. ; ----------------- ALLOCATE MEMORY BLOCK ----------------------
  84. ; *** PRE-LOAD BX WITH SIZE OF BLOCK IN PARAS (BYTES / 16) ***
  85. ; *** RETURNS AX = SEG OF ALLOCATED BLOCK ***
  86. ; *** IMPORTANT - CHECK CARRY FLAG ON RETURN
  87.  
  88. GetMem        PROC NEAR
  89.     
  90.  
  91.     mov    ah, 48h            ;DOS mem allocate
  92.     int    21h
  93.     ret
  94. GetMem    ENDP
  95.  
  96.  
  97. ; ------------------- FREE A MEMORY BLOCK --------------------
  98. ; *** PRE-LOAD ES WITH SEG OF BLOCK TO FREE ***
  99. FreeMem        PROC NEAR
  100.  
  101.     mov    ah, 49h
  102.     int 21h
  103.     ret
  104. FreeMem        ENDP
  105.  
  106.  
  107. ; --------------- LOAD IN GFX ------------------------------ 
  108. ; *** PRE-LOAD :
  109. ;        DX -  offset of filename
  110. ;        AX -  segment of buffer for graphics
  111. ;        DI -  offset of buffer for graphics
  112.     
  113. LoadData    PROC NEAR
  114.     push    ax
  115.     mov ax, 3d00h                ;Open file, read only
  116.                     ;DX already loaded with name off
  117.     int 21h
  118.     mov bx, ax            ;store the handle in BX
  119.     
  120.     mov    dx, offset GFXHeader
  121.     mov    cx, 16            ;read header
  122.     mov ah, 3Fh              
  123.     int 21h
  124.  
  125.     cmp    FileType, '2'
  126.     je    SpriteFile
  127.     cmp    FileType, '4'
  128.     je    SpriteFile
  129.         
  130.     xor    eax, eax
  131.     xor    ecx, ecx
  132.     mov    ah, [WidthH]
  133.     mov    al, [WidthL]
  134.     mov    ch, [HeightH]
  135.     mov    cl, [HeightL]
  136.     mul    ecx            ;size of the file
  137.     jmp    loadit
  138. SpriteFile:
  139.     xor    eax, eax
  140.     xor    ecx, ecx
  141.     mov    ah, [SpriteWidthH]
  142.     mov    al, [SpriteWidthL]
  143.     mov    ch, [SpriteHeightH]
  144.     mov    cl, [SpriteHeightL]
  145.     mul    ecx            ;size of the Sprite
  146.     xor    ecx, ecx        
  147.     mov    cl, [NoSpritesL]
  148.     mov    ch, [NoSpritesH]
  149.     mul    ecx            ; * number of sprites    
  150.                     ; = size of file
  151. loadit:    
  152.     mov    CS:[FileSize], eax
  153.     
  154.     pop    ax
  155.     push    ds
  156.     mov    ds, ax            ;load the destination segment
  157.     mov    dx, di            ;& offset 
  158.     
  159.     
  160.     
  161.   readloop:        
  162.     cmp    CS:[FileSize], 38400
  163.     jb    lastone
  164.     sub    CS:[FileSize], 38400
  165.     mov     cx, 38400                  ;standard load size
  166.     jmp    load
  167.    lastone:
  168.        mov    ecx, CS:[FileSize]        ;the remainder of the file
  169.        mov    CS:[FileSize], 0
  170.    load:        
  171.     mov ah, 3Fh              ;read file
  172.     int 21h
  173.     
  174.     mov    ax, ds
  175.     add    ax, (38400 / 16)    ;shift up the seg (in paras)
  176.     mov    ds, ax
  177.     mov    dx, di            ;keep using the offset
  178.  
  179.     cmp    CS:[FileSize], 0
  180.     jne    readloop        ;continue if there's some left
  181.  
  182.     mov ah, 3Eh               ;close file (BX already set)
  183.     int 21h
  184.  
  185.     pop    ds            ;get DS back
  186.     ret
  187.  
  188. LoadData    ENDP        
  189.  
  190.  
  191. ; ----------------- LOAD A PALETTE -------------------------------------
  192. LoadPal         PROC NEAR
  193.     
  194. ;*** PRE-LOAD:
  195. ;        DX = Offset of Palette file name
  196. ;        CX = Offset of Palette buffer
  197.  
  198. ; (Palettes are assumed to be in the data segment)
  199.  
  200.     push ds
  201.     mov ax, @data
  202.     mov ds, ax
  203.     
  204.     mov ax, 3d00h                ;Open file, read only
  205.                     ;DX already loaded with name off
  206.     int 21h
  207.     mov bx, ax            ;store the handle in BX
  208.     
  209.     mov dx, cx            ;put palette buffer offset in DX
  210.     
  211.     mov cx, 768                  ;size of palette file
  212.         
  213.     mov ah, 3Fh              ;read file
  214.     int 21h
  215.     
  216.     mov ah, 3Eh               ;close file (BX already set)
  217.     int 21h
  218.     
  219.     pop ds
  220.     ret
  221. LoadPal         ENDP
  222.  
  223. ; ---------------- SET A PALETTE ----------------------------------------
  224. ;*** PRE-LOAD:
  225. ;        SI = offset of palette to be used
  226.  
  227. SetPal          PROC NEAR
  228.                         ;set the palette up
  229.     cld
  230.  
  231.     mov     dx, 3c8h                        ;colour NO selector
  232.     xor     al, al                          ;zero AL
  233.     out     dx, al                          ;select it
  234.     inc     dx                              ;DX+1 = RGB writing location
  235.  
  236.     ;mov     si, offset palette              ;back to start of palette
  237.     mov     cx, 768                         ;256*3 colours again
  238.     cli                                     ;clear interrupts
  239.     rep     outsb                           ;write all colours
  240.     sti                                     ;allow interrupts
  241.     ret
  242. SetPal          ENDP
  243.  
  244. ;----------------- FADE A PALETTE DOWN -------------------------------------
  245. ; *** PRE-LOAD:
  246. ;        SI = Offset of palette to fade
  247. ;        BX = Value to decrement by
  248. DecPalette    PROC NEAR
  249.     mov    dx, DAC_INDEX
  250.     xor    ax, ax
  251.     out    dx, al        ;select colour 0
  252.     inc    dx        ;where we set RGB values    
  253.  
  254.     mov    cx, 768    
  255.   decloop:    
  256.     xor    ax, ax
  257.     mov    al, [si]
  258.     sub    ax, bx        ;word value as no byte penalty
  259.     jnc    StillOK        ;if carry, has gone negative
  260.     xor    ax, ax
  261.   StillOk:        
  262.     mov    [si], al
  263.     out    dx, al         ;set the colour    
  264.     inc    si
  265.   loop decloop
  266.       ret
  267. DecPalette    ENDP
  268.  
  269. ;----------------- FADE A PALETTE UP -------------------------------------
  270. ; *** PRE-LOAD:
  271. ;        SI = Offset of temp palette (BLACK)
  272. ;        DI = Offset of destination palette
  273. IncPalette    PROC NEAR
  274.     mov    dx, DAC_INDEX
  275.     xor    ax, ax
  276.     out    dx, al        ;select colour 0
  277.     inc    dx        ;where we set RGB values    
  278.  
  279.     mov    cx, 768    
  280.   incloop:    
  281.     xor    ax, ax
  282.     mov    al, [si]
  283.     cmp    [di], al
  284.     je    NoInc
  285.     inc    al
  286.  
  287. NoInc:
  288.     mov    [si], al    ;save it
  289.     out    dx, al         ;set the colour    
  290.     inc    si
  291.     inc    di
  292.   loop incloop
  293.       ret
  294. IncPalette    ENDP
  295.  
  296.  
  297. ; ---------------- SET A BLACK PALETTE ----------------------------------------
  298. ;*** PRE-LOAD:    None
  299.  
  300. BlackPal          PROC NEAR
  301.                         ;set the palette up
  302.     cld
  303.  
  304.     mov     dx, 3c8h                        ;colour NO selector
  305.     xor     al, al                          ;zero AL
  306.     out     dx, al                          ;select it
  307.     inc     dx                              ;DX+1 = RGB writing location
  308.  
  309.     mov     cx, 768                         ;256*3 colours again
  310.     xor    ax, ax
  311.     cli                                     ;clear interrupts
  312.     rep     out dx, al                      ;write all colours
  313.     sti                                     ;allow interrupts
  314.     ret
  315. BlackPal          ENDP
  316.  
  317. ; ---------------------- WAIT FOR A VERTICAL RETRACE ---------------------
  318. WaitRetrace    PROC NEAR
  319.     mov    dx, 3dah
  320.   ret1:
  321.       in    al, dx
  322.       and    al, 08h
  323.       jnz    ret1
  324.   ret2:
  325.       in    al, dx
  326.       and    al, 08h
  327.       jz    ret2
  328.  
  329.     ret
  330. WaitRetrace    ENDP
  331.  
  332. ; ------------------ WAIT FOR A KEYPRESS ---------------------------------
  333. WaitKey        PROC NEAR
  334.     waiting:
  335.     mov     ah, 1            ;Wait for keypress
  336.     int     16h
  337.     jz      waiting
  338.     
  339.     mov    ah, 0            ;discard it from buffer
  340.     int    16h
  341.     
  342.     ret
  343. WaitKey        ENDP
  344.         
  345. ; ------------------- SET VIDEO MODES ------------------------
  346. Set13h          PROC NEAR
  347.     mov     ax, 13h
  348.     int     10h
  349.     ret
  350. Set13h          ENDP
  351.  
  352. SetText         PROC NEAR
  353.     mov     ax, 03h
  354.     int     10h
  355.     ret
  356. SetText         ENDP
  357.  
  358. ;--------------------CLEAR SCREEN--------------------------------
  359. ClearScreen    PROC NEAR
  360.     mov    ax, 0a000h
  361.     mov    es, ax
  362.     xor    eax, eax
  363.     xor    edi, edi
  364.     mov    cx, 65535        ;clear the whole VGA
  365.     rep    stosd            ;386 only
  366.     ret
  367. ClearScreen    ENDP
  368.  
  369.  
  370.  
  371.  
  372. ; ************************* MACROS ****************************
  373.  
  374. ; ------------------ CLEAN EXIT TO DOS ------------------------
  375. Finish    MACRO
  376.     mov    ax, 4c00h
  377.     int    21h
  378.     ENDM
  379.  
  380. ; ------------------- SET UP DETAILS FOR RESIZE MEM -------------
  381. ;***  Must be called STRAIGHT AFTER startup!!
  382. MemInit        MACRO        
  383.     mov    [ProgStartSeg], ES        ;save the PSP
  384.     mov    ax, ss                ;stack seg
  385.     mov    bx, sp                ;stack pointer
  386.     shr    bx, 4                ;in Paragraphs
  387.     inc    bx                ;plus 1 (safe)
  388.     add    ax, bx                ;make ONE good one!
  389.     mov    [ProgEndSeg], ax        ;store it
  390.     call    ResizeMem
  391.         ENDM
  392.