What you will need is some kind of object structure. Nothing too fancy just a list of structures that hold information about your sprites.
Maybe xpos, ypos, sprite pointer maybe something to hold a sprite animation frame. Then you could just step through that list and update all of your objects?
You could have a type variable instead of a sprite pointer that you could use to index *another* table with sprite pointers and sprite sizes.
Just a couple of ideas?
Couple possibly whip up some example code if you like.
EDIT:
I managed to whip this small example up during my break today :
Not much to look at (xor'd sprites over background :X), but it demonstrates a player controlled object along with 2 other objects of different sizes, each executing a simple predefined animation.
Code: ;------------------------------------------------------------
; tr1p1ea - Simple Engine Demo
;------------------------------------------------------------
; [url="http://img36.imageshack.us/img36/4885/simpler.gif"]http://img36.imageshack.us/img36/4885/simpler.gif[/url]
.nolist
#include "ion.inc"
#include "keys.inc"
#define OBJECTSIZE 4 ; each object is 4 bytes
#define OBJECTX 0
#define OBJECTY 1
#define OBJECTTYPE 2
#define OBJECTANIMFRAME 3
.list
#ifdef TI83P
.org progstart-2
.db $BB,$6D
#else
.org progstart
#endif
ret
jr nc,begin
.db "Simple Engine Demo",0
begin:
mainLoop:
call processInput ; process input
call updateObjects ; update objects
call drawBackground ; draw simple background
call drawObjects ; draw objects
call ionFastCopy
jr mainLoop
;------------------------------------------------------------
; processInput
;------------------------------------------------------------
;
processInput:
ld a,(activeObject) ; active object
ld l,a
ld h,0
ld e,l
ld d,h
add hl,hl
add hl,hl ; * 4
ex de,hl
ld ix,objectTable+1
add ix,de ; ix points to current object
ld a,Group2
out (1), a
nop \ nop
in a,(1)
cp KClear
jp z,Quit
ld a,Group1
out (1), a
nop \ nop
in a,(1)
cp KUp
jp z,Up
cp KDown
jp z,Down
cp KLeft
jp z,Left
cp KRight
jp z,Right
ret
Quit:
pop af ; clear call
ret ; return
Up:
ld a,(ix + OBJECTY + 0)
add a,-1
ret nc ; only checking screen bounds
ld (ix + OBJECTY + 0),a ; really should be checking against
ret ; object size!
Down:
ld a,(ix + OBJECTY + 0)
add a,1
cp 64-8+1
ret z
ld (ix + OBJECTY + 0),a
ret
Left:
ld a,(ix + OBJECTX + 0)
add a,-1
ret nc
ld (ix + OBJECTX + 0),a
ret
Right:
ld a,(ix + OBJECTX + 0)
add a,1
cp 96-8+1
ret z
ld (ix + OBJECTX + 0),a
ret
ret
;------------------------------------------------------------
; updateObjects
;------------------------------------------------------------
;
updateObjects:
ld ix,objectTable
ld b,(ix + 0)
inc ix
updateObjectsLoop:
push bc
push ix
ld a,(ix + OBJECTTYPE + 0)
add a,a ; 2 bytes per table entry
ld e,a
ld d,0
ld hl,objectAnimationTable
add hl,de ; offset animation table
ld e,(hl)
inc hl
ld d,(hl) ; de points to animation
ld a,(de)
inc de
ld b,a ; b = frames in animation
ld a,(ix + OBJECTANIMFRAME + 0)
add a,a ; each frame is 2 bytes
ld l,a
ld h,0
add hl,de ; hl points to animation frame data
ld a,(ix + OBJECTX + 0)
add a,(hl)
ld (ix + OBJECTX + 0),a
inc hl
ld a,(ix + OBJECTY + 0)
add a,(hl)
ld (ix + OBJECTY + 0),a
ld a,(ix + OBJECTANIMFRAME + 0)
inc a
cp b
jr nz,$+3
xor a ; reset frame if end is reached
ld (ix + OBJECTANIMFRAME + 0),a ; update animation frame
pop ix
ld de,OBJECTSIZE
add ix,de ; ix points to next object
pop bc
djnz updateObjectsLoop
ret
;------------------------------------------------------------
; drawBackground
;------------------------------------------------------------
;
drawBackground:
bcall(_grBufClr) ; clear buffer
ld hl,plotsScreen
ld a,%10000000
ld bc,(6*256)+32
drawBackgroundLoop:
ld (hl),a ; draw some dots as the background :X
inc hl
inc hl
djnz drawBackgroundLoop
xor %10000001
ld de,32
add hl,de
ld b,6
dec c ; yes i know but its easier to read :)
jr nz,drawBackgroundLoop
ret
;------------------------------------------------------------
; drawObjects
;------------------------------------------------------------
;
drawObjects:
ld ix,objectTable
ld b,(ix + 0) ; b = total number of objects
inc ix
drawObjectsLoop:
push bc
push ix
ld a,(ix + OBJECTTYPE + 0) ; use object type as table index
add a,a ; 2 bytes per table entry
ld e,a
ld d,0
ld hl,objectSpriteSizeTable
add hl,de
ld c,(hl) ; sprite width
inc hl
ld b,(hl) ; sprite height
ld hl,objectSpriteTable
add hl,de
ld e,(hl)
inc hl
ld d,(hl) ; de = sprite pointer
ld a,(ix + OBJECTX + 0) ; a = x
ld l,(ix + OBJECTY + 0) ; l = y
push de
pop ix
call ionLargeSprite ; draw sprite
pop ix
ld de,OBJECTSIZE
add ix,de ; ix points to next object
pop bc
djnz drawObjectsLoop
ret
;------------------------------------------------------------
; variables, tables & data
;------------------------------------------------------------
;
activeObject:
.db 0 ; player controlled object
objectTable: ; list of in-game objects
.db 3 ; total number of objects
; object 0
.db 48-4 ; x pos
.db 32-4 ; y pos
.db 0 ; object type
.db 0 ; animation frame
; object 1
.db 48-4-28 ; x pos
.db 32-4 ; y pos
.db 1 ; object type
.db 0 ; animation frame
; object 2
.db 48-4+28 ; x pos
.db 32-8 ; y pos
.db 2 ; object type
.db 0 ; animation frame
objectSpriteTable:
.dw sprite0 ; object0 sprite pointer
.dw sprite1 ; object1 sprite pointer
.dw sprite2 ; object2 sprite pointer
objectSpriteSizeTable:
.db (8/8),8 ; width/8, height - sprite0: 8 * 8
.db (8/8),8 ; width/8, height - sprite0: 8 * 8
.db (16/8),16 ; width/8, height - sprite0: 16 * 16
objectAnimationTable:
.dw idle ; object0 will idle
.dw moveUpDown ; object1 will move up and down
.dw moveLeftRight ; object2 will move left and right
idle:
.db 1 ; number of frames
.db 0,0 ; +x0,+y0,+x1,+y1,+x2,+y2,+x3,+y3 ...
moveUpDown:
.db 24 ; number of frames
.db 0,-1,0,-1,0,-1,0,-1 ; +x0,+y0,+x1,+y1,+x2,+y2,+x3,+y3 ...
.db 0,-1,0,-1,0,-1,0,-1
.db 0,-1,0,-1,0,-1,0,-1
.db 0,1,0,1,0,1,0,1
.db 0,1,0,1,0,1,0,1
.db 0,1,0,1,0,1,0,1
moveLeftRight:
.db 16 ; number of frames
.db -1,0,-1,0,-1,0,-1,0 ; +x0,+y0,+x1,+y1,+x2,+y2,+x3,+y3 ...
.db -1,0,-1,0,-1,0,-1,0
.db 1,0,1,0,1,0,1,0
.db 1,0,1,0,1,0,1,0
sprite0: ; sprite0 data
.db %10101010
.db %01010101
.db %10101010
.db %01010101
.db %10101010
.db %01010101
.db %10101010
.db %01010101
sprite1: ; sprite1 date
.db %11111111
.db %10000001
.db %10000001
.db %10000001
.db %10000001
.db %10000001
.db %10000001
.db %11111111
sprite2: ; sprite2 data
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.db %11111111,%11111111
.end
END
I know your looking at IX thinking O_o, but it makes things easier to understand .
You can add/remove objects by adding an entry to 'objectTable', then changing the total, which is currently '.db 3':
You can reuse existing object types 0,1,2 or if you want to create your own you would:
create a new entry to objectSpriteTable, objectSpriteSizeTable and objectAnimationTable
Alternatively you could reserve another variable for each object which denotes what animation each object should have. That way any object can perform any animation you wish, and it can even be changed at runtime .
Last edited by Guest on 17 Mar 2010 02:06:22 am; edited 1 time in total |