home *** CD-ROM | disk | FTP | other *** search
/ Global Amiga Experience / globalamigaexperience.iso / compressed / development / heliosdemodisk3.dms / heliosdemodisk3.adf / Source / Demo3_SimpleSprite.src < prev    next >
Text File  |  1995-01-24  |  16KB  |  580 lines

  1.  
  2.   \ ***********************************************************
  3.   \
  4.   \                    SIMPLE SPRITE DEMO
  5.   \
  6.   \ ***********************************************************
  7.   \
  8.   \ This code demonstrates how to create a simple HeliOS single
  9.   \ sprite (a BOB to be precise) and move it around.
  10.   \
  11.   \ This code builds upon the following Demos:
  12.   \
  13.   \ Demo1_SinglePF.src
  14.   \ Demo2_SinglePFCopper.src
  15.   \
  16.   \ This code is then expanded in the following Demos:
  17.   \
  18.   \ Demo4_MultiSprites.src
  19.   \ Demo5_MultiSptCollide.src
  20.   \ Demo6_SimpleAnim.src
  21.   \ Demo7_MultiAnim.src
  22.   \
  23.   \ ***********************************************************
  24.  
  25.  
  26.   \ ***********************************************************
  27.   \ Re-initialise HeliOS dictionary to standard CORE vocabulary
  28.   \ ***********************************************************
  29.  
  30.   \ This should always be used at the start of any program which
  31.   \ is to be repeatedly recompiled.
  32.  
  33.   FORGET **CORE**
  34.  
  35.   \ *************************
  36.   \ Load include symbol files
  37.   \ *************************
  38.   \
  39.   \ These "include files" are pre-compiled (for speed) versions of the
  40.   \ Amiga includes and the Helios system includes.
  41.   \
  42.   \ Uncomment the lines below for standalone compilation, but otherwise
  43.   \ it is better to set these include files from the Helios Forth menus
  44.  
  45.   AMIGAINCLUDE Helios:Amiga_Include
  46.   USERINCLUDE  Helios:Helios_Include
  47.  
  48.   \ ****************************************
  49.   \ Create display imagery file name strings
  50.   \ ****************************************
  51.  
  52.   \ These files are ordinary IFF's (and may be "PowerPacked" if required)
  53.   \
  54.   \ The pictures here will be loaded into each of the display BitMaps.
  55.   \
  56.  
  57.   $CONSTANTL Slice1Pic $Helios:Source/Data/Pic1$
  58.  
  59.   \ **************************************
  60.   \ Create display configuration constants
  61.   \ **************************************
  62.   \
  63.   \ Collect all "display-specific" parameters here and generate "named"
  64.   \ constants which make references easier than using numeric values.
  65.   \
  66.   \ Collecting these together here makes it easier to adjust things at any
  67.   \ time without having to search source code to replace values individually.
  68.   \
  69.  
  70.  
  71.   256                      CONSTANT DisplayHeight      \ Full PAL display
  72.   320                      CONSTANT DisplayWidth       \ Lores display
  73.   44                       CONSTANT DisplayTopLine     \ Display start
  74.  
  75.   DisplayWidth             CONSTANT Slice1Width        \ Lores width
  76.   DisplayWidth 32 +        CONSTANT Slice1RasterWidth  \ Raster=SWidth+32
  77.   DisplayHeight            CONSTANT Slice1Height       \ Slice height
  78.   DisplayHeight 32 +       CONSTANT Slice1RasterHeight \ Slice Raster=DHgt+32
  79.   0                        CONSTANT Slice1Mode         \ Lores
  80.   3                        CONSTANT Slice1Planes       \ Slice bitplanes
  81.  
  82.   \ The calculation below takes the number of bitplanes and calculates
  83.   \ how many colours this represents.
  84.   \
  85.   \ One bitplane gives two colours.
  86.   \
  87.   \ Each additional bitplane multiplies the number of colours by two.
  88.   \
  89.   \ Performing a single LSL operation on any number multipies by 2, so we
  90.   \ have a quick method of multiplying by two for our colour calculation.
  91.   \
  92.   \ In this case, we need "2 to the power 3", which is 2*2*2=8.
  93.   \
  94.   \ e.g.                              2*2*2
  95.   \                                   ^ ^ ^
  96.   \                                   Total number of planes = 3
  97.   \
  98.   \
  99.   \ So, we take the first value two
  100.   \
  101.   \ e.g.                              2*2*2
  102.   \                                   ^
  103.   \                                  Initial start value of 2
  104.   \
  105.   \ and we now need to multiply it by two "the_number_of_planes minus_one"
  106.   \ more times.
  107.   \
  108.   \ e.g.                              2*2*2
  109.   \                                     ^ ^
  110.   \                                     Total planes minus one
  111.   \
  112.   \
  113.   \ So, Number of colours = 2 operated on by LSL NumberOfPlanes-1 times.
  114.   \
  115.  
  116.   2 Slice1Planes 1- LSL    CONSTANT Slice1Colours      \ A-slice colours
  117.  
  118.   \ ***********************
  119.   \ Error handling routines
  120.   \ ***********************
  121.  
  122.   \ This error handler allows all errors to be routed via a comprehensive
  123.   \ sequential closedown routine, which is associated with the HeliOS
  124.   \ system error handler word ERROR".
  125.   \
  126.   \ When ERROR" senses an error, it prints an associated error message
  127.   \ delimited by '"' characters, and then closes everything down using the
  128.   \ routine CLOSEDOWN below which you have supplied.
  129.   \
  130.   \ This simplifies errors checks to the use of a single word, ERROR", which
  131.   \ displays a text message and closes eveything down.
  132.   \
  133.  
  134.   0 VARIABLE (CLOSEDOWN)
  135.  
  136.   : ?CLOSEDOWNERROR
  137.  
  138.   IF
  139.     CR
  140.     CR
  141.     TYPE
  142.     CR
  143.     CR
  144.     ." Press <Space> to quit!"
  145.     CR
  146.     CR
  147.     WAITSPACE
  148.     (CLOSEDOWN) @EXECUTE
  149.     QUIT
  150.   ELSE
  151.     DDROP
  152.   THEN
  153.   ;
  154.  
  155.   LATESTCFA VARIABLE ERROR1
  156.  
  157.   \ ****************************************
  158.   \ Create display pointer storage variables
  159.   \ ****************************************
  160.  
  161.   \ Here we create a set of "pointers", initially set to a "null" value.
  162.   \
  163.   \ These "pointers" are set up as "long addresses" when various components
  164.   \ of the display system are allocated and initialised.
  165.   \
  166.   \ Note that initially these are all set to zero, and we clear them back
  167.   \ to zero when we de-allocate the associated resource.
  168.   \
  169.   \ These DPOINTERs are all initially set to "null" by using 'D0'.
  170.   \
  171.   \ When we allocate memory or Amiga system resources in the program at
  172.   \ run-time, these pointers are updated to contain the 32-bit address
  173.   \ of the newly allocated resource.
  174.   \
  175.   \ Subsequently the symbolic DPOINTER name can be used in your code to
  176.   \ represent the associated address.
  177.  
  178.   D0 DPOINTER Display1             \ Main Display structure pointer
  179.   D0 DPOINTER Slice1               \ Slice 1 Slice structure pointer
  180.  
  181.   D0 DPOINTER Slice1_ColorMap      \ Slice 1 ColourMap structure pointer
  182.  
  183.   D0 DPOINTER Slice1_RasInfo       \ Slice 1 RasInfo structure pointer
  184.  
  185.   D0 DPOINTER Slice1_BMap          \ Slice 1 BitMap structure pointer
  186.  
  187.   D0 DPOINTER Slice1_SliceControl  \ Slice 1 SliceControl structure pointer
  188.  
  189.   \ *************
  190.   \ Colour tables
  191.   \ *************
  192.  
  193.   \ Each colour entry requies 2 bytes of storage space
  194.  
  195.   CREATEL Slice1_ColorTable        \ Create longword pointer to table
  196.   Slice1Colours 2* 0 ALLOTFILL     \ Allocate Slice1 colours * 2 bytes
  197.  
  198.  
  199.   \ *************************************
  200.   \ Copper strip for graduated background
  201.   \ *************************************
  202.  
  203.   D0 DVARIABLE Copper              \ 32-bit CopperList pointer store
  204.   D0 DVARIABLE CopperLength        \ 32-bit CopperList length store
  205.  
  206.   : AddCopper                      \ Add custom copper list to display
  207.  
  208.   Display1                         \ We are adding CopperList to Display1
  209.   Copper D@
  210.   ADDCOPPERSTRIP
  211.   SORTSTRIPTABLE
  212.   LINKSTRIPS
  213.   DDROP
  214.   ;
  215.  
  216.   : RemCopper                      \ Remove custom copper list from display
  217.  
  218.   Display1                         \ We are removing CopperList from Display1
  219.   Copper D@
  220.   REMCOPPERSTRIP
  221.   LINKSTRIPS
  222.   DDROP
  223.   ;
  224.  
  225.   : Create_Copper
  226.  
  227.   COPPERSTART
  228.   Copper D!
  229.   0 [ DECIMAL ]  45 [ HEX ] FFFE COPPERWAIT  DROP   0100 18E COPPERMOVE  DROP
  230.   0 [ DECIMAL ]  60 [ HEX ] FFFE COPPERWAIT  DROP   0200 18E COPPERMOVE  DROP
  231.   0 [ DECIMAL ]  75 [ HEX ] FFFE COPPERWAIT  DROP   0300 18E COPPERMOVE  DROP
  232.   0 [ DECIMAL ]  90 [ HEX ] FFFE COPPERWAIT  DROP   0400 18E COPPERMOVE  DROP
  233.   0 [ DECIMAL ] 105 [ HEX ] FFFE COPPERWAIT  DROP   0500 18E COPPERMOVE  DROP
  234.   0 [ DECIMAL ] 120 [ HEX ] FFFE COPPERWAIT  DROP   0600 18E COPPERMOVE  DROP
  235.   0 [ DECIMAL ] 135 [ HEX ] FFFE COPPERWAIT  DROP   0700 18E COPPERMOVE  DROP
  236.   0 [ DECIMAL ] 150 [ HEX ] FFFE COPPERWAIT  DROP   0800 18E COPPERMOVE  DROP
  237.   0 [ DECIMAL ] 165 [ HEX ] FFFE COPPERWAIT  DROP   0900 18E COPPERMOVE  DROP
  238.   0 [ DECIMAL ] 180 [ HEX ] FFFE COPPERWAIT  DROP   0A00 18E COPPERMOVE  DROP
  239.   0 [ DECIMAL ] 195 [ HEX ] FFFE COPPERWAIT  DROP   0B00 18E COPPERMOVE  DROP
  240.   0 [ DECIMAL ] 210 [ HEX ] FFFE COPPERWAIT  DROP   0C00 18E COPPERMOVE  DROP
  241.   0 [ DECIMAL ] 225 [ HEX ] FFFE COPPERWAIT  DROP   0D00 18E COPPERMOVE  DROP
  242.   0 [ DECIMAL ] 240 [ HEX ] FFFE COPPERWAIT  DROP   0E00 18E COPPERMOVE  DROP
  243.   0 [ DECIMAL ] 255 [ HEX ] FFFE COPPERWAIT  DROP   0F00 18E COPPERMOVE  DROP
  244.   [ DECIMAL ]
  245.   COPPEREND
  246.   CopperLength D!   Copper D!
  247.   ADDCOPPER
  248.   ;
  249.  
  250.   : Free_Copper                  \ Closes down and frees copperlist memory
  251.  
  252.   RemCopper
  253.   Copper D@ FREEMEMORY
  254.   ;
  255.  
  256.   \ ***********************************
  257.   \ Create Display and Slice structures
  258.   \ ***********************************
  259.  
  260.   \ This routine simply makes blank structures, which then need to be
  261.   \ initialised later (in the CREATE_DISPLAY routine).
  262.  
  263.   : CREATE_DSLICES
  264.  
  265.   DS_SIZEOF MAKESTRUCTURE Display1 MAKEPOINTER  \ Main "Display" structure
  266.  
  267.   SL_SIZEOF MAKESTRUCTURE Slice1   MAKEPOINTER  \ Display "Slice" structure
  268.   ;
  269.  
  270.   : FREE_DSLICES
  271.  
  272.   Slice1    DDUP FREEMEMORY   CLEARPOINTER
  273.   Display1  DDUP FREEMEMORY   CLEARPOINTER
  274.   ;
  275.  
  276.   \ ******************************
  277.   \ Create RasInfo structures etc.
  278.   \ ******************************
  279.  
  280.   : CREATE_RASINFO
  281.  
  282.   \ First allocate and initialise complete RasInfo structures.
  283.   \
  284.   \ This routine automatically allocates all BitMaps etc.
  285.   \
  286.  
  287.   Slice1RasterWidth Slice1RasterHeight Slice1Planes  OPENRASINFO
  288.   DFLAG0= ERROR" Fail: RasInfo1"
  289.   Slice1_RasInfo MAKEPOINTER
  290.  
  291.   \ Set invisible area "sprite margins" for slice RasInfo
  292.  
  293.   16              Slice1_RasInfo         ri_RxOffset    INDEX!L
  294.   16              Slice1_RasInfo         ri_RyOffset    INDEX!L
  295.  
  296.   \ Store BitMap pointer - often useful for later reference
  297.  
  298.   Slice1_RasInfo  ri_BitMap INDEXD@L Slice1_BMap MAKEPOINTER
  299.   ;
  300.  
  301.   : FREE_RASINFO
  302.  
  303.   Slice1_RasInfo   DDUP CLOSERASINFO   CLEARPOINTER
  304.   ;
  305.  
  306.   \ ********************************
  307.   \ Create Display/Slice Copperlists
  308.   \ ********************************
  309.  
  310.   \ This function builds the main display copperlist by:
  311.   \
  312.   \ 1. Initialising the Slice data structure
  313.   \ 2. Calling MAKECOPSTRIP for the slice, to build a copperlist
  314.   \ 3. Calling MAKEDISPLAY to build the master Display copperlist
  315.   \
  316.  
  317.   : CREATE_DISPLAY
  318.  
  319.   \ First initialise main display structures
  320.  
  321.   Slice1                           Display1  DS_Slice     INDEXD!L
  322.  
  323.   Slice1Width                      Slice1    SL_DWidth    INDEX!L
  324.   Slice1Height                     Slice1    SL_DHeight   INDEX!L
  325.   DisplayTopLine                   Slice1    SL_DyOffset  INDEX!L
  326.   Slice1_RasInfo                   Slice1    SL_RasInfo   INDEXD!L
  327.   Slice1_ColorMap                  Slice1    SL_ColorMap  INDEXD!L
  328.   Slice1Mode                       Slice1    SL_Modes     INDEX!L
  329.  
  330.   \ Generate copper list information for each of the display slices
  331.  
  332.   Slice1 MAKECOPSTRIP
  333.   D0= ERROR" Fail: Slice1CopStrip"
  334.  
  335.   \ Make display
  336.  
  337.   Display1 MAKEDISPLAY
  338.   D0= ERROR" Fail: Display1"
  339.   ;
  340.  
  341.   : FREE_DISPLAY
  342.  
  343.   Display1                 FREEDISPLAY
  344.   Slice1                   FREECOPSTRIP
  345.   ;
  346.  
  347.   \ ********************************************************
  348.   \ Create SliceControl structures for double buffered slice
  349.   \ ********************************************************
  350.  
  351.   \ SliceControl structures are used to control any slices which perform
  352.   \ mapping or scrolling functions, or which require double or triple
  353.   \ playfield buffering.
  354.   \
  355.   \ In this case we have one slice which does not scroll, is not mapped,
  356.   \ but IS double buffered.
  357.   \
  358.  
  359.   : CREATE_SLICECONTROL
  360.  
  361.   \ Make SliceControl for double buffered bitmap display
  362.  
  363.   Slice1  0 0 MAKESLICECONTROL
  364.   DFLAG0= ERROR" Fail: SliceControl1"
  365.   Slice1_SliceControl MAKEPOINTER
  366.  
  367.   \ Install slice controls into HeliOS display control system
  368.  
  369.   Slice1_SliceControl  INSTALLSLICECONTROL
  370.   ;
  371.  
  372.   : FREE_SLICECONTROL
  373.  
  374.   CLEARSLICECONTROLS
  375.   Slice1_SliceControl  CLOSESLICECONTROL
  376.   ;
  377.  
  378.   \ These routines load an IFF picture into supplied BitMap, and correctly
  379.   \ initialises the supplied ColorTable.
  380.   \
  381.   \ The ColorTable is then used to create an initialised ColorMap structure.
  382.   \
  383.  
  384.   : CREATE_IMAGERY
  385.  
  386.   Slice1_BMap
  387.   Slice1_ColorTable
  388.   Slice1Pic
  389.   10 2 DOSLIB                        \ Call to internal HeliOS library
  390.   10 <> ERROR" Fail: Slice1Pic"
  391.  
  392.   Slice1_ColorTable  Slice1Colours MAKECOLORMAP  \ Allocate ColourMap
  393.   DFLAG0= ERROR" Fail: Slice1ColorMap"
  394.   Slice1_ColorMap MAKEPOINTER
  395.   ;
  396.  
  397.   : FREE_IMAGERY
  398.  
  399.   Slice1_ColorMap  DDUP FREECOLORMAP  CLEARPOINTER
  400.   ;
  401.  
  402.   \ ***********
  403.   \ Sprite Demo
  404.   \ ***********
  405.  
  406.   \ *********************
  407.   \ Sprite Demo constants
  408.   \ *********************
  409.  
  410.   \ Store the gun speed as a CONSTANT
  411.  
  412.   4  CONSTANT GunSpeed             \ Speed of Gun movement
  413.  
  414.   \ *************************************************
  415.   \ Create sprite demo pointers and storage variables
  416.   \ *************************************************
  417.  
  418.   D0 DPOINTER GunSpriteSet         \ Gun sprite image
  419.  
  420.   \ ***************************
  421.   \ User input response routine
  422.   \ ***************************
  423.  
  424.   : MoveGun
  425.  
  426.   RAWKEY @ 78 =
  427.   JOY1LEFTRIGHT 0>
  428.   OR
  429.   IF
  430.      GunSpeed
  431.      19 320
  432.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  433.      LIMIT+!L
  434.   ELSE
  435.     RAWKEY @ 79 =
  436.     JOY1LEFTRIGHT 0<
  437.     OR
  438.     IF
  439.      GunSpeed NEGATE
  440.      19 320
  441.      GunSpriteSet 8. D+ D@L SpriteCtrl_XPos D+
  442.      LIMIT+!L
  443.     THEN
  444.   THEN
  445.   ;
  446.  
  447.   \ ********************
  448.   \ Create sprite object
  449.   \ ********************
  450.  
  451.   : CREATE_SPRITE
  452.  
  453.   \ ----------
  454.   \ Gun sprite
  455.   \ ----------
  456.  
  457.   Slice1_BMap
  458.   154 272
  459.   13 15
  460.   1
  461.   MAKESPRITESET
  462.   DFLAG0= ERROR" Fail: Gun SpriteSet"
  463.   GunSpriteSet MAKEPOINTER
  464.  
  465.   GunSpriteSet 8. D+ D@L
  466.   DDUP  SpriteCtrl_XPos D+       170 -ROT !L
  467.         SpriteCtrl_YPos D+       239 -ROT !L
  468.  
  469.   Slice1_SliceControl   GunSpriteSet    INITSPRITESET
  470.   ;
  471.  
  472.   : FREE_SPRITE
  473.  
  474.   GunSpriteSet    DDUP FREESPRITESET CLEARPOINTER
  475.   ;
  476.  
  477.   \ *********************
  478.   \ Close down everything
  479.   \ *********************
  480.  
  481.   : CLOSEDOWN
  482.  
  483.   FREE_SPRITE
  484.   FREE_COPPER
  485.   FREE_SLICECONTROL
  486.   FREE_DISPLAY
  487.   FREE_IMAGERY
  488.   FREE_RASINFO
  489.   FREE_DSLICES
  490.   RESETERROR"
  491.   ;
  492.  
  493.   LATESTCFA (CLOSEDOWN) !
  494.  
  495.   : TestDisplay          \ Start of program
  496.  
  497.   SCRCLR
  498.   CR
  499.   ."        **********************************************************"
  500.   CR 6 FPENSET
  501.   ."                           SIMPLE SPRITE DEMO"
  502.   CR 1 FPENSET
  503.   ."        **********************************************************"
  504.   CR
  505.   CR
  506.   ."        This code demonstrates how to create a simple HeliOS single"
  507.   CR
  508.   ."        sprite (a BOB to be precise) and move it around."
  509.   CR
  510.   CR
  511.   ."        This code build upon the following Demos:"
  512.   CR
  513.   CR
  514.   ."        Demo1_SinglePF.src"
  515.   CR
  516.   ."        Demo2_SinglePFCopper.src"
  517.   CR
  518.   CR
  519.   ."        This code is then expanded in the following Demos:"
  520.   CR
  521.   CR
  522.   ."        Demo4_MultiSprites.src"
  523.   CR
  524.   ."        Demo5_MultiSptCollide.src"
  525.   CR
  526.   ."        Demo6_SimpleAnim.src"
  527.   CR
  528.   ."        Demo7_MultiAnim.src"
  529.   CR
  530.   ."        **********************************************************"
  531.   CR 6 FPENSET
  532.   ."                  Press <Space> or <L-Mouse> to see Demo          "
  533.   CR 3 FPENSET
  534.   ."         Use a Joystick to move the gun turret from left to right"
  535.   CR
  536.   ."        **********************************************************"
  537.   CR
  538.  
  539.   WAITSPACE
  540.  
  541.   SCRCLR
  542.  
  543.  
  544.   ERROR1 SETERROR"       \ Redirect system errors to our routine ERROR1
  545.  
  546.   CREATE_DSLICES
  547.   CREATE_RASINFO
  548.   CREATE_IMAGERY
  549.   CREATE_DISPLAY
  550.   CREATE_SLICECONTROL
  551.   CREATE_COPPER
  552.   CREATE_SPRITE
  553.  
  554.   HeliOS_On
  555.  
  556.   GunSpriteSet 8. D+ D@L INSTALLSPRITE
  557.  
  558.   1 FrameRate !L
  559.  
  560.   Display1 SHOWDISPLAY
  561.  
  562.   BEGIN
  563.     WAITFRAME
  564.  
  565.     MoveGun
  566.  
  567.     ?TERMINAL 27 =
  568.   UNTIL
  569.  
  570.   -3 GunSpriteSet 8. D+ D@L SpriteCtrl_Flags INDEX!L
  571.  
  572.   5 DELAY
  573.  
  574.   HeliOS_Off
  575.  
  576.   CLOSEDOWN
  577.   ;
  578.  
  579.   TestDisplay
  580.