home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 339.lha / xspecs.library_v0 / ThreeBallTrick.asm < prev    next >
Assembly Source File  |  1990-02-08  |  14KB  |  531 lines

  1. ********************************************
  2.  
  3. *  BallTrick.asm by Richard Horne
  4. *  Requires xspecs.library version 0 in df0:libs
  5. *  Freely distributable in the public domain
  6.  
  7. ********************************************
  8.  
  9.    INCLUDE     "exec/types.i"
  10.    INCLUDE     "exec/memory.i"
  11.    INCLUDE     "intuition/intuition.i"
  12.  
  13.    XREF  _AbsExecBase
  14.    XREF  _LVOAllocMem
  15.    XREF  _LVOFreeMem
  16.    XREF  _LVOOpenLibrary
  17.    XREF  _LVOCloseLibrary
  18.    XREF  _LVOGetMsg
  19.    XREF  _LVOReplyMsg
  20.    XREF  _LVOWaitTOF
  21.  
  22. _LVOXSpecsAllocateView        EQU   -30
  23. _LVOXSpecsDeallocateView      EQU   -36
  24. _LVOXSpecsOn                  EQU   -42
  25. _LVOXSpecsOff                 EQU   -48
  26. _LVOXSpecsSetAPen             EQU   -54
  27. _LVOXSpecsSetRGB4             EQU   -60
  28. _LVOXSpecsLoadRGB4            EQU   -66
  29. _LVOXSpecsText                EQU   -72
  30. _LVOXSpecsWritePixel          EQU   -78
  31. _LVOXSpecsDraw                EQU   -84
  32. _LVOXSpecsChangeSprite        EQU   -90
  33. _LVOXSpecsMoveSprite          EQU   -96
  34. _LVOXSpecsLeftXY              EQU   -102
  35. _LVOXSpecsRightXY             EQU   -108
  36. _LVOXSpecsLeftRastPort        EQU   -114
  37. _LVOXSpecsRightRastPort       EQU   -120
  38. _LVOXSpecsLeftViewPort        EQU   -126
  39. _LVOXSpecsRightViewPort       EQU   -132
  40. _LVOXSpecsSetDrMd             EQU   -138
  41. _LVOXSpecsPolyDraw            EQU   -144
  42. _LVOXSpecsMove                EQU   -150
  43. _LVOXSpecsGetSprite           EQU   -156
  44. _LVOXSpecsFreeSprite          EQU   -162
  45. _LVOXSpecsTime                EQU   -168
  46.  
  47. ***************************************
  48.  
  49. Start:
  50.  
  51. *  open Graphics library
  52.  
  53.    move.l  _AbsExecBase,a6
  54.    move.l   #GraphicsName,a1
  55.    clr.l    d0
  56.    jsr      _LVOOpenLibrary(a6)
  57.    tst.l    d0
  58.    beq      Abort
  59.    move.l   d0,GfxBase
  60.  
  61. *  open XSpecs library
  62.  
  63.    move.l  _AbsExecBase,a6
  64.    move.l   #XSpecsName,a1
  65.    clr.l    d0                         ;Version 0
  66.    jsr      _LVOOpenLibrary(a6)
  67.    beq      Abort
  68.    move.l   d0,XSpecsBase
  69.  
  70. *  Allocate Sprite Chip Memory.
  71. *  In order to use sprites later, we must set up sprite image data in
  72. *  chip memory.  We are going to use three sprites, each 16 pixels wide
  73. *  by 12 lines high (14 lines including top and bottom headers).
  74.  
  75.    move.l  _AbsExecBase,a6             ;Allocate chip memory
  76.    move.l   #168,d0                    ;3 sprites, each 14 lines (56 bytes)
  77.    move.l   #MEMF_CHIP!MEMF_CLEAR,d1
  78.    jsr      _LVOAllocMem(a6)
  79.    move.l   d0,Sprite2CM               ;CM address of Sprite2 image
  80.    add.l    #56,d0
  81.    move.l   d0,Sprite4CM               ;CM address of Sprite4 image
  82.    add.l    #56,d0
  83.    move.l   d0,Sprite6CM               ;CM address of Sprite6 image
  84.  
  85.    move.l   #SpriteData,a0             ;Sprite shape into chip memory
  86.    move.l   Sprite2CM,a1
  87.    move.l   #13,d1
  88. CS1:
  89.    move.l   (a0)+,(a1)+
  90.    dbra     d1,CS1
  91.  
  92.    move.l   #SpriteData,a0             ;Sprite shape into chip memory
  93.    move.l   Sprite4CM,a1
  94.    move.l   #13,d1
  95. CS2:
  96.    move.l   (a0)+,(a1)+
  97.    dbra     d1,CS2
  98.  
  99.    move.l   #SpriteData,a0             ;Sprite shape into chip memory
  100.    move.l   Sprite6CM,a1
  101.    move.l   #13,d1
  102. CS3:
  103.    move.l   (a0)+,(a1)+
  104.    dbra     d1,CS3
  105.  
  106.  
  107. ********************** XSpecs Code *********************
  108.  
  109. *  Allocate XSpecs View.  This function allocates memory and initializes
  110. *  data structures required for XSpecs left and right bitmaps, rastports,
  111. *  and viewports.
  112.  
  113.    move.l   XSpecsBase,a6
  114.    move.l   #V_HIRES!V_SPRITES,d0         ;ViewModes
  115.    jsr      _LVOXSpecsAllocateView(a6)
  116.    cmpi.l   #0,d0                         ;Successful?
  117.    bne      Abort
  118.  
  119. *  Turn On XSpecs.  This function starts the vertical blank interrupt
  120. *  which switches the XSpecs lenses.
  121.  
  122.    move.l   XSpecsBase,a6
  123.    move.l   #RAWKEY,d0                    ;Our IDCMP messages
  124.    jsr      _LVOXSpecsOn(a6)
  125.    move.l   d0,UserPort                   ;Our message port for IDCMP
  126.    cmpi.l   #1,d0                         ;Successful?
  127.    beq      Close
  128.  
  129. *  Set XSpecs View Colors
  130.  
  131.    move.l   XSpecsBase,a6
  132.    move.l   #Colors,a0
  133.    move.l   #32,d0
  134.    jsr      _LVOXSpecsLoadRGB4(a6)
  135.  
  136.    move.l   XSpecsBase,a6
  137.    move.l   #1,d0
  138.    jsr      _LVOXSpecsSetAPen(a6)
  139.  
  140. *  Draw XSpecs Cube #200 units each side, centered around 0,0,0
  141.  
  142.    move.l   XSpecsBase,a6
  143.    move.l   #Points,a0                 ;Array of (x,y,z) points to connect
  144.    move.l   #16,d0                     ;Number of points
  145.    jsr      _LVOXSpecsPolyDraw(a6)
  146.    cmpi.l   #1,d0                      ;Successful?
  147.    beq      Close
  148.  
  149. *  Write XSpecsText at position x,y,z
  150.  
  151.    move.l   #-78,d0                    ;x
  152.    move.l   #120,d1                    ;y
  153.    move.l   #-160,d2                   ;z
  154.    move.l   #24,d3                     ;count
  155.    move.l   #TestText,a0               ;Address of text string
  156.    move.l   XSpecsBase,a6
  157.    jsr      _LVOXSpecsText(a6)
  158.    cmpi.l   #1,d0                      ;Successful?
  159.    beq      Close
  160.  
  161. *  Get XSpecs Sprites
  162.  
  163.    move.l   XSpecsBase,a6
  164.    move.l   #2,d0                      ;SpriteNumber 2
  165.    move.l   #Sprite,a0                 ;Simple Sprite Structure
  166.    move.l   Sprite2CM,a1               ;Sprite Image Address
  167.    jsr      _LVOXSpecsGetSprite(a6)
  168.    cmpi.l   #-1,d0                     ;Successful?
  169.    beq      Close
  170.  
  171.    move.l   Sprite2CM,a0               ;Sprite Image Address
  172.    move.l   #2,d0                      ;Sprite Number
  173.    move.l   XSpecsBase,a6
  174.    jsr      _LVOXSpecsChangeSprite(a6)
  175.    cmpi.l   #1,d0                      ;Successful?
  176.    beq      Close
  177.  
  178.    move.l   XSpecsBase,a6
  179.    move.l   #4,d0                      ;SpriteNumber 4
  180.    move.l   #Sprite,a0                 ;Simple Sprite Structure
  181.    move.l   Sprite4CM,a1               ;Sprite Image Address
  182.    jsr      _LVOXSpecsGetSprite(a6)
  183.    cmpi.l   #-1,d0                     ;Successful?
  184.    beq      Close
  185.  
  186.    move.l   Sprite4CM,a0               ;Sprite Image Address
  187.    move.l   #4,d0                      ;Sprite Number
  188.    move.l   XSpecsBase,a6
  189.    jsr      _LVOXSpecsChangeSprite(a6)
  190.    cmpi.l   #1,d0                      ;Successful?
  191.    beq      Close
  192.  
  193.    move.l   XSpecsBase,a6
  194.    move.l   #6,d0                      ;SpriteNumber 6
  195.    move.l   #Sprite,a0                 ;Simple Sprite Structure
  196.    move.l   Sprite6CM,a1               ;Sprite Image Address
  197.    jsr      _LVOXSpecsGetSprite(a6)
  198.    cmpi.l   #-1,d0                     ;Successful?
  199.    beq      Close
  200.  
  201.    move.l   Sprite6CM,a0               ;Sprite Image Address
  202.    move.l   #6,d0                      ;Sprite Number
  203.    move.l   XSpecsBase,a6
  204.    jsr      _LVOXSpecsChangeSprite(a6)
  205.    cmpi.l   #1,d0                      ;Successful?
  206.    beq      Close
  207.  
  208. *  Begin BallTrick.  Now that everything is set up, we can begin
  209. *  the program loop to move the sprites.
  210.  
  211. BallLoop:
  212.  
  213.    move.l   XSpecsBase,a6              ;Get current time
  214.    jsr      _LVOXSpecsTime(a6)
  215.    add.l    #2,d0
  216.    move.l   d0,CycleTime               ;Time for start of next cycle
  217.  
  218. *  Update Ball Position and Velocity.  Each ball (sprite) has a position
  219. *  (X,Y,Z) and a velocity (Vx,Vy,Vz).  This function calcultes a new position
  220. *  for each ball based on its current velocity.  If a ball strikes a
  221. *  wall of the box, the velocity perpindicular to that wall is reversed
  222. *  causing the ball to bounce away from the wall.
  223.  
  224.    move.l   XVxSprite2,d0
  225.    move.l   YVySprite2,d1
  226.    move.l   ZVzSprite2,d2
  227.    jsr      GetNewPosition
  228.    move.l   d0,XVxSprite2
  229.    move.l   d1,YVySprite2
  230.    move.l   d2,ZVzSprite2
  231.  
  232.    move.l   XVxSprite4,d0
  233.    move.l   YVySprite4,d1
  234.    move.l   ZVzSprite4,d2
  235.    jsr      GetNewPosition
  236.    move.l   d0,XVxSprite4
  237.    move.l   d1,YVySprite4
  238.    move.l   d2,ZVzSprite4
  239.  
  240.    move.l   XVxSprite6,d0
  241.    move.l   YVySprite6,d1
  242.    move.l   ZVzSprite6,d2
  243.    jsr      GetNewPosition
  244.    move.l   d0,XVxSprite6
  245.    move.l   d1,YVySprite6
  246.    move.l   d2,ZVzSprite6
  247.  
  248. *  Move XSpecs Sprites to New Positions.  We have calculated the new ball
  249. *  (sprite) positions in the XSpecs (x,y,z) three dimensional coordinate
  250. *  system.  Now we need to move the balls (sprites) to corresponding
  251. *  positions in the XSpecs left and right viewports.  Switching of the
  252. *  XSpecs lenses will give the illusion of movement in three dimensions.
  253.  
  254.    move.w   XVxSprite2,d0              ;x position, 16 bits
  255.    move.w   YVySprite2,d1              ;y position, 16 bits
  256.    move.w   ZVzSprite2,d2              ;z position, 16 bits
  257.    move.l   #-8,d3                     ;x pixel offset
  258.    move.l   #-6,d4                     ;y pixel offset
  259.    move.l   #2,d5                      ;SpriteNumber
  260.    move.l   XSpecsBase,a6
  261.    jsr      _LVOXSpecsMoveSprite(a6)
  262.    cmpi.l   #1,d0                      ;Successful?
  263.    beq      Close
  264.  
  265.    move.w   XVxSprite4,d0              ;x position, 16 bits
  266.    move.w   YVySprite4,d1              ;y position, 16 bits
  267.    move.w   ZVzSprite4,d2              ;z position, 16 bits
  268.    move.l   #-8,d3                     ;x pixel offset
  269.    move.l   #-6,d4                     ;y pixel offset
  270.    move.l   #4,d5                      ;SpriteNumber
  271.    move.l   XSpecsBase,a6
  272.    jsr      _LVOXSpecsMoveSprite(a6)
  273.    cmpi.l   #1,d0                      ;Successful?
  274.    beq      Close
  275.  
  276.    move.w   XVxSprite6,d0              ;x position, 16 bits
  277.    move.w   YVySprite6,d1              ;y position, 16 bits
  278.    move.w   ZVzSprite6,d2              ;z position, 16 bits
  279.    move.l   #-8,d3                     ;x pixel offset
  280.    move.l   #-6,d4                     ;y pixel offset
  281.    move.l   #6,d5                      ;SpriteNumber
  282.    move.l   XSpecsBase,a6
  283.    jsr      _LVOXSpecsMoveSprite(a6)
  284.    cmpi.l   #1,d0                      ;Successful?
  285.    beq      Close
  286.  
  287. *  Check Time
  288.  
  289. TimeLoop:
  290.    move.l   GfxBase,a6
  291.    jsr      _LVOWaitTOF(a6)
  292.  
  293. *  Compare Time against Cycle Time
  294.  
  295.    move.l   XSpecsBase,a6
  296.    jsr      _LVOXSpecsTime(a6)
  297.    move.l   CycleTime,d1
  298.    cmp.l    d1,d0                      ;If Time < = CycleTime, then wait.
  299.    blt      TimeLoop
  300.  
  301. *  Get IDCMP messages
  302.    move.l  UserPort,a0
  303.    move.l  _AbsExecBase,a6
  304.    jsr      _LVOGetMsg(a6)
  305.    tst.l    d0
  306.    beq      BallLoop
  307.    move.l   d0,IDCMPMessage
  308.  
  309. *  Reply to message
  310.    move.l   _AbsExecBase,a6
  311.    move.l   IDCMPMessage,a1
  312.    jsr      _LVOReplyMsg(a6)
  313.  
  314. *  Find Message Class
  315.    move.l  IDCMPMessage,a0
  316.    move.l   im_Class(a0),MessageClass
  317.  
  318. *  Find Messsage Code
  319.    move.l  IDCMPMessage,a0
  320.    move.w   im_Code(a0),MessageCode
  321.  
  322. *  is message rawkey?
  323.  
  324.    cmpi.l   #RAWKEY,MessageClass
  325.    bne      BallLoop
  326.  
  327.  
  328. ***************** Close *******************
  329.  
  330. Close:
  331.  
  332.    move.l  XSpecsBase,a6
  333.    move.l   #2,d0
  334.    jsr      _LVOXSpecsFreeSprite(a6)
  335.  
  336.    move.l  XSpecsBase,a6
  337.    move.l   #4,d0
  338.    jsr      _LVOXSpecsFreeSprite(a6)
  339.  
  340.    move.l  XSpecsBase,a6
  341.    move.l   #6,d0
  342.    jsr      _LVOXSpecsFreeSprite(a6)
  343.  
  344.    move.l   XSpecsBase,a6
  345.    jsr      _LVOXSpecsOff(a6)
  346.  
  347.    move.l   XSpecsBase,a6
  348.    jsr      _LVOXSpecsDeallocateView(a6)
  349.  
  350. *  Free Sprite Memory
  351.  
  352.    move.l   _AbsExecBase,a6
  353.    move.l   Sprite2CM,a1
  354.    move.l   #168,d0
  355.    jsr      _LVOFreeMem(a6)
  356.  
  357. *  Close Libraries
  358.  
  359.    move.l  _AbsExecBase,a6
  360.    move.l   XSpecsBase,a1
  361.    jsr      _LVOCloseLibrary(a6)
  362.  
  363.    move.l  _AbsExecBase,a6
  364.    move.l   GfxBase,a1
  365.    jsr      _LVOCloseLibrary(a6)
  366.  
  367. Abort:
  368.    rts
  369.  
  370. ******************* Subroutines ***********************
  371.  
  372. GetNewPosition:
  373.    move.l   d0,d3          ;X,Vx in d3
  374.    swap     d0             ;Vx,X in d0
  375.    add.w    d3,d0          ;Vx,X+Vx in d0
  376.    cmpi.w   #100,d0
  377.    bge      ReverseVx
  378.    cmpi.w   #-100,d0
  379.    ble      ReverseVx
  380.    swap     d0
  381.    bra      YCal
  382. ReverseVx:
  383.    move.l   d3,d0
  384.    neg.w    d0
  385.  
  386. YCal:
  387.    move.l   d1,d3          ;Y,Vy in d3
  388.    swap     d1             ;Vy,Y in d1
  389.    add.w    d3,d1          ;Vy,Y+Vy in d1
  390.    cmpi.w   #100,d1
  391.    bge      ReverseVy
  392.    cmpi.w   #-100,d1
  393.    ble      ReverseVy
  394.    swap     d1
  395.    bra      ZCal
  396. ReverseVy:
  397.    move.l   d3,d1
  398.    neg.w    d1
  399.  
  400. ZCal:
  401.    move.l   d2,d3          ;Z, Vz in d3
  402.    swap     d2             ;Vz, Z in d2
  403.    add.w    d3,d2          ;Vz, Z+Vz in d2
  404.    cmpi.w   #100,d2
  405.    bge      ReverseVz
  406.    cmpi.w   #-100,d2
  407.    ble      ReverseVz
  408.    swap     d2
  409.    bra      EndGetNewPosition
  410. ReverseVz:
  411.    move.l   d3,d2
  412.    neg.w    d2
  413.  
  414. EndGetNewPosition:
  415.    rts
  416.  
  417. ****************** Data ********************
  418.  
  419.    SECTION  data,DATA
  420.  
  421. GraphicsName:
  422.    dc.b     'graphics.library',0
  423.    cnop     0,2
  424.  
  425. XSpecsName:
  426.    dc.b     'xspecs.library',0
  427.    cnop     0,2
  428.  
  429. GfxBase:
  430.    ds.l     1
  431.  
  432. XSpecsBase:
  433.    ds.l     1
  434.  
  435. TestText:
  436.    dc.b     'xspecs.library version 0',0
  437.    cnop     0,2
  438.  
  439. UserPort:                        ;Address of XSpecs IDCMP Message Port
  440.    ds.l     1
  441.  
  442. IDCMPMessage:
  443.    ds.l     1
  444.  
  445. MessageClass:
  446.    ds.l     1
  447.  
  448. MessageCode:
  449.    ds.w     1
  450.  
  451. Points:
  452.    dc.w     -100,100,-100
  453.    dc.w     -100,-100,-100
  454.    dc.w     100,-100,-100
  455.    dc.w     100,-100,100
  456.    dc.w     -100,-100,100
  457.    dc.w     -100,-100,-100
  458.    dc.w     100,-100,-100
  459.    dc.w     100,100,-100
  460.    dc.w     -100,100,-100
  461.    dc.w     -100,100,100
  462.    dc.w     -100,-100,100
  463.    dc.w     100,-100,100
  464.    dc.w     100,100,100
  465.    dc.w     -100,100,100
  466.    dc.w     100,100,100
  467.    dc.w     100,100,-100
  468.  
  469. ****************** Sprite **********************
  470.  
  471. Sprite:                 ;Simple Sprite Structure
  472.    dc.l     0
  473.    dc.w     12          ;Height
  474.    dc.w     0
  475.    dc.w     0
  476.    dc.w     0
  477.  
  478. Sprite2CM:                 ;Chip Memory address of sprite image
  479.    ds.l     1
  480.  
  481. Sprite4CM:                 ;Chip Memory address of sprite image
  482.    ds.l     1
  483.  
  484. Sprite6CM:                 ;Chip Memory address of sprite image
  485.    ds.l     1
  486.  
  487. SpriteData:
  488.   dc.l  $00000000
  489.   dc.l  $00000000,$00000000,$00000000,$00000000
  490.   dc.l  $01800780,$0CC003C0,$0CE013E0,$00E01FE0
  491.   dc.l  $11E01FE0,$0FC00FC0,$07800780,$00000000
  492.   dc.l  $00000000
  493.  
  494. XVxSprite2:                ;Initial ball (sprite) position and velocity
  495.    dc.w     20,8           ;16 bits position, 16 bits velocity
  496. YVySprite2:
  497.    dc.w     10,7
  498. ZVzSprite2:
  499.    dc.w     -40,6
  500.  
  501. XVxSprite4:
  502.    dc.w     10,9
  503. YVySprite4:
  504.    dc.w     12,4
  505. ZVzSprite4:
  506.    dc.w     -20,-7
  507.  
  508. XVxSprite6:
  509.    dc.w     8,3
  510. YVySprite6:
  511.    dc.w     9,-8
  512. ZVzSprite6:
  513.    dc.w     10,-9
  514.  
  515. CycleTime:
  516.    ds.l     1
  517.  
  518. Colors:
  519.    dc.w     $0000,$026F,$0000,$0000       ;0-3
  520.    dc.w     $0000,$0000,$0000,$0000       ;4-7
  521.    dc.w     $0000,$0000,$0000,$0000       ;8-11
  522.    dc.w     $0000,$0000,$0000,$0000       ;12-15
  523.    dc.w     $0000,$0000,$0000,$0000       ;16-19
  524.    dc.w     $0000,$0FCA,$0E86,$0D22       ;20-23
  525.    dc.w     $0000,$0AC8,$08A6,$0282       ;24-27
  526.    dc.w     $0000,$0ACF,$068E,$022D       ;28-31
  527.  
  528.  
  529.    END
  530.  
  531.