Best way to animate Sprite ?

Previous topic - Next topic

Gilles

Hi,
I’m looking for the best way to animate sprite.
Must I define a sprite for each picture in a sprite ?
Example:
LOADSPRITE "sprt1 image1",10
LOADSPRITE "sprt1 image2",11
LOADSPRITE "sprt1 image3",12
LOADSPRITE "sprt1 image4",13

boucle:
FOR anim=0 TO 3
SPRITE 10+anim,100,100
SHOWSCREEN
NEXT
GOTO boucle


Or there is an easy way ?

Thank
Gilles

Kitty Hello

It seems you have to do it this way. Hmm... What about these functions: (untested)


Code (glbasic) Select
LOCAL an$, ani[]
DIM an$[3]
   an$[0] = "img1.bmp"
   an$[1] = "img2.bmp"
   an$[2] = "img3.bmp"

   LoadAnim(an$[], ani[])

   WHILE TRUE
      SPRITE AnimFrame(ani[]), 100, 100
      SHOWSCREEN
   WEND


FUNCTION AnimFrame: anim[]
LOCAL pos
   pos = MOD(GETTIMERALL()/100, BOUNDS(anim[]) )
   RETURN anim[pos]
ENDFUNCTION

FUNCTION LoadAnim: filenames$[], anim[]
LOCAL i
   DIM anim[BOUNDS(filename$[],0)]
   FOR i=0 TO BOUNDS(filename$[], 0)-1
      anim[i] = LoadSpr(filename$[i])
   NEXT
ENDFUNCTION

FUNCTION LoadSpr: filename$
GLOBAL gnFiles
   gnFiles=gnFiles+1
   LOADSPRITE filename$, gnFiles
   RETURN gnFiles
ENDFUNCTION

Gilles

Ok, thank.

I sought a sprite editor and found Tile Studio:
http://tilestudio.sourceforge.net/

But he can generate only bmp file with multiple tiles. He can't generate individual bmp file for each tiles.

For future versions, do you plan to add the ability to manage BMP file with multiple tiles ? (like Blitzbasic)
It could be like this:
LOAD SPRITE "Human.bmp", Sprt_ID, Sprt_width, Sprt_height
SPRITE Sprt_ID, Tile Number, x, y

Kitty Hello

You can do that with POLYVECTOR, however there was a bug in POLYVECTOR, so better get the beta SDK which fixes it:
http://www.glbasic.com/beta/glbasic_sdk.exe

Here I load "smalfont.bmp" which is a 16x8 tile bitmap, right? Then I use PSprite function to draw the ASC(MID$(a$, i,1) ) index block of that set. For width/height I give the size of each tile. Tiles are numbered as:
0 1 2
3 4 5
6 7 8

Is this what you need? With POLYVECTOR you can also change the color of the sprite, rotate, zoom, rotozoom, misfit any way you want it! Sure, it's a bit more work than in Blitz, but it's really worth if you want to dig deep inside GLBasic.

The other alternative would be to LOADBMP the tileset and then use GETSPRITE to get each tile as a single SPRITE - I'd prefer POLYSPRITE.

Code (glbasic) Select
// 272x176 = 17x22 pixels per char
LOADSPRITE "smalfont.bmp", 0

a$ = "Hello PSprite"
FILLRECT 0,0, 800,600,RGB(0,255,255)
FOR i=0 TO LEN(a$)-1
PSprite(0, ASC(MID$(a$, i, 1)), 17,22, i*17, 100)
NEXT

SHOWSCREEN
MOUSEWAIT

FUNCTION PSprite: id, tile, width, height, x, y
LOCAL sx, sy, tx, ty, dx, dy
LOCAL p2x, p2y
LOCAL cols, c
GETSPRITESIZE id, sx, sy
cols = INTEGER(sx / width)
tx = MOD(tile, cols) * width
ty = INTEGER(tile/cols) * height
c=RGB(255,255,255)

width=width-1
height=height-1
STARTPOLY id
POLYVECTOR x,      y       ,tx      ,ty       ,c
POLYVECTOR x,      y+height,tx      ,ty+height,c
POLYVECTOR x+width,y+height,tx+width,ty+height,c
POLYVECTOR x+width,y       ,tx+width,ty       ,c
ENDPOLY
ENDFUNCTION

Gilles

Yes that seem to be wath i'm looking for.
many thanks !