Re: A REALLY verbose example of the EDO Structure Package by GT Hawkins
  Home FAQ Contact Sign in
comp.lang.forth only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: A REALLY verbose example of the EDO Structure Package by GT Hawkins         

Group: comp.lang.forth · Group Profile
Author: Tim Trussell
Date: Dec 7, 2006 10:18

\ ---[ OOF Test Conversion ]--------------------------[12/07/2006]---
\ by Timothy Trussell
\ -------------------------------------------------------------------
\ A recoding test to see if the MOOF.FS can be easier to implement in
\ a Sprite drawing routine
\
\ I need something a little more related to what I am actually coding
\ than the normal/bright text examples I've seen.
\
\ This will let me compare it to something that works in it's current
\ incarnation using the EDO structure package for the data references.
\ -------------------------------------------------------------------

exists [OOFExmp] [if]
forget [OOFExmp]
[then]
: [OOFExmp] ;

\ Define the sprite objects
\ These are basic packed sprites, in a 16x16 unpacked matrix

binary

create DiamondS[]
00000001 C, 10000000 C,
00000011 C, 11000000 C,
00000110 C, 01100000 C,
00001100 C, 00110000 C,
00011001 C, 10011000 C,
00110011 C, 11001100 C,
01100110 C, 01100110 C,
11001100 C, 00110011 C,
11001100 C, 00110011 C,
01100110 C, 01100110 C,
00110011 C, 11001100 C,
00011001 C, 10011000 C,
00000110 C, 01100000 C,
00000110 C, 01100000 C,
00000011 C, 11000000 C,
00000001 C, 10000000 C,

create Ovoid[]
00001111 C, 11110000 C,
00111111 C, 11111100 C,
01111111 C, 11111110 C,
01111111 C, 11111110 C,
11111000 C, 00011111 C,
11110000 C, 00001111 C,
11110000 C, 00001111 C,
11110000 C, 00001111 C,
11110000 C, 00001111 C,
11110000 C, 00001111 C,
11110000 C, 00001111 C,
11111000 C, 00011111 C,
01111111 C, 11111110 C,
01111111 C, 11111110 C,
00111111 C, 11111100 C,
00001111 C, 11110000 C,

decimal

13 constant NumberOfObjects
0 constant Plane0
1 constant Plane1
2 constant Plane2

0 [if]

\ ---[ EDO Version of animation sequence ]---------------------------

\ Define the animation control structure
\ [var] indicates that this value changes during program execution
\ [ref] indicates that this value is never changed.

S{
{1WORD}-DEF :: .delay \ var
{1WORD}-DEF :: .basedelay \ ref
{1WORD}-DEF :: .&image \ ref
{1WORD}-DEF :: .xcoord \ var
{1WORD}-DEF :: .xinc \ ref
{1WORD}-DEF :: .xmin \ ref
{1WORD}-DEF :: .xmax \ ref
{1WORD}-DEF :: .ycoord \ var
{1WORD}-DEF :: .yinc \ ref
{1WORD}-DEF :: .ymin \ ref
{1WORD}-DEF :: .ymax \ ref
{1WORD}-DEF :: .plane \ ref
}S object-obj

\ Define the animation control object and index

object-obj NumberOfObjects [] object[]-obj object-ndx

\ Create and allocate the data for the animation control block

create Objects[]
\ x x+ x< x> y y+ y< y> c
1 , 21 , DiamondS[] , 44 , 2 , 40 , 256 , 8 , 0 , 0 , 195 , Plane0
,
1 , 15 , Ovoid[] , 148 , 2 , 56 , 240 , 72 , 0 , 0 , 195 , Plane0
,
1 , 23 , DiamondS[] , 44 , 2 , 40 , 256 , 128 , 0 , 0 , 195 , Plane0
,
1 , 13 , Ovoid[] , 60 , 0 , 0 , 320 , 72 , 2 , 0 , 160 , Plane1
,
1 , 11 , DiamondS[] , 104 , 0 , 0 , 320 , 72 , 2 , 0 , 160 , Plane1
,
1 , 8 , Ovoid[] , 148 , 0 , 0 , 320 , 72 , 2 , 0 , 164 , Plane1
,
1 , 9 , DiamondS[] , 192 , 0 , 0 , 320 , 72 , 2 , 0 , 164 , Plane1
,
1 , 14 , Ovoid[] , 236 , 0 , 0 , 320 , 72 , 2 , 0 , 160 , Plane1
,
1 , 8 , DiamondS[] , 100 , 4 , 4 , 300 , 24 , 3 , 0 , 180 , Plane2
,
1 , 9 , Ovoid[] , 124 , 4 , 4 , 300 , 48 , 3 , 0 , 180 , Plane2
,
1 , 7 , DiamondS[] , 148 , 4 , 4 , 300 , 72 , 3 , 0 , 180 , Plane2
,
1 , 6 , Ovoid[] , 172 , 4 , 4 , 300 , 96 , 3 , 0 , 180 , Plane2
,
1 , 5 , DiamondS[] , 196 , 4 , 4 , 300 , 120 , 3 , 0 , 180 , Plane2
,

\ DrawObject draws the specified object to the screen

: DrawObject() ( &obj -- )
drop
;

\ Animation updates the delay for each object. If delay cycles down
\ to 0, then it's time to draw this object.

value %%currobj

: Animation ( -- )
begin
key? not
while
NumberOfObjects 0 do \ cycle thru the objects
Objects[] i object-ndx >R
R@ .delay @ 1- dup R@ .delay !
if \ if .delay=0, move this object
\ reset to .basedelay
R@ .basedelay @ R@ .delay !
\ inc x; reverse at xmin/xmax
R@ .xcoord @ dup
R@ .xmin @ < over
R@ .xmax @ > or if
R@ .xinc @ negate R@ .xinc !
then
\ add XInc to current XCoord
R@ .xinc @ + R@ .xcoord !
\ inc y; reverse at ymin/ymax
R@ .ycoord @ dup
R@ .ymin @ < over
R@ .ymax @ > or if
R@ .yinc @ negate R@ .yinc !
then
\ add YInc to current YCoord
R@ .yinc @ + R@ .ycoord !
\ Now draw the image at the new location
WaitRetrace
R@ DrawObject()
then
R> drop \ lose pointer and check next obj
loop
repeat
key drop
;

: SpriteDemo
$13 InitGraph
Animation
CloseGraph
;

[then]

\ ---[ MOOF.FS version of animation sequence ]-----------------------

\ --[ Object Oriented Forth - Bernd Paysan ]------
\ Mini-OOF 12apr98py
: method ( m v -- m' v ) Create over , swap cell+ swap
DOES> ( ... o -- ... ) @ over @ + @ execute ;
: var ( m v size -- m v' ) Create over , +
DOES> ( o -- addr ) @ + ;
: class ( class -- class methods vars ) dup 2@ ;
: end-class ( class methods vars -- )
Create here >r , dup , 2 cells ?DO ['] noop , 1 cells +LOOP
cell+ dup cell+ r> rot @ 2 cells /string move ;
: defines ( xt class -- ) ' >body @ + ! ;
: new ( class -- o ) here over @ allot swap over ! ;
: :: ( class "name" -- ) ' >body @ + @ compile, ;
Create object 1 cells , 2 cells ,

\ ---[ Now the conversion of the EDO to MOOF ]-----------------------
\ I'm not saying my recode is correct. If you see flaws - say so

object class
cell var .delay \ var
cell var .basedelay \ ref
cell var .&image \ ref
cell var .xcoord \ var
cell var .xinc \ ref
cell var .xmin \ ref
cell var .xmax \ ref
cell var .ycoord \ var
cell var .yinc \ ref
cell var .ymin \ ref
cell var .ymax \ ref
cell var .plane \ ref
method init
method draw
end-class anim

\ Now, implement the two methods, draw and init:

:noname ( .de .ba .&i .x .x+ .x< .x> .y .y+ .y< .y> .p obj -- )
>R
R@ .plane !
R@ .ymax !
R@ .ymin !
R@ .yinc !
R@ .ycoord !
R@ .xmax !
R@ .xmin !
R@ .xinc !
R@ .xcoord !
R@ .&image !
R@ .basedelay !
R> .delay !
; anim defines init

defer DrawObject

:noname ( o -- )
DrawObject
; anim defines draw

anim new constant Sprite0
anim new constant Sprite1
anim new constant Sprite2
anim new constant Sprite3
anim new constant Sprite4
anim new constant Sprite5
anim new constant Sprite6
anim new constant Sprite7
anim new constant Sprite8
anim new constant Sprite9
anim new constant Sprite10
anim new constant Sprite11
anim new constant Sprite12

1 21 DiamondS[] 44 2 40 256 8 0 0 195 Plane0 Sprite0 init
1 15 Ovoid[] 148 2 56 240 72 0 0 195 Plane0 Sprite1 init
1 23 DiamondS[] 44 2 40 256 128 0 0 195 Plane0 Sprite2 init
1 13 Ovoid[] 60 0 0 320 72 2 0 160 Plane1 Sprite3 init
1 11 DiamondS[] 104 0 0 320 72 2 0 160 Plane1 Sprite4 init
1 8 Ovoid[] 148 0 0 320 72 2 0 164 Plane1 Sprite5 init
1 9 DiamondS[] 192 0 0 320 72 2 0 164 Plane1 Sprite6 init
1 14 Ovoid[] 236 0 0 320 72 2 0 160 Plane1 Sprite7 init
1 8 DiamondS[] 100 4 4 300 24 3 0 180 Plane2 Sprite8 init
1 9 Ovoid[] 124 4 4 300 48 3 0 180 Plane2 Sprite9 init
1 7 DiamondS[] 148 4 4 300 72 3 0 180 Plane2 Sprite10 init
1 6 Ovoid[] 172 4 4 300 96 3 0 180 Plane2 Sprite11 init
1 5 DiamondS[] 196 4 4 300 120 3 0 180 Plane2 Sprite12 init

: (DrawObject) ( &obj -- )
\ access the data using the passed obj address and draw the image
cr ." Drawing object at address: " .
;

' (DrawObject) is DrawObject

: Animation ( -- )
\ stays the same?
Sprite0 DrawObject
Sprite1 DrawObject
Sprite2 DrawObject
Sprite3 DrawObject
Sprite4 DrawObject
;

\ Sprite0 64 dump
\ 1A161 45 A0 01 00 01 00 00 00 15 00 00 00 85 9D 01 00
\ 1A171 2C 00 00 00 02 00 00 00 28 00 00 00 00 01 00 00
\ 1A181 08 00 00 00 00 00 00 00 00 00 00 00 C3 00 00 00
\ 1A191 00 00 00 00
-- -- -- -- -- -- -- -- -- -- -- --

\ Is there a better way to recode this using the MOOF?
no comments
diggit! del.icio.us! reddit!