GLBasic forum

Codesnippets => Code Snippets => Topic started by: Ian Price on 2007-Dec-30

Title: Animation strip/tile loading function
Post by: Ian Price on 2007-Dec-30
This function allows you to load in a single image/sprite that contains a multiple of same size images (tiles etc.) and then spits out sprites ready to be used - this negates the need for lots and lots of similar image files within a folder and makes everything tidier and easier.

This code is public domain and may be used freely, although a thankyou would be nice.

USAGE -

AnimImage( graphic file name$, the required sprite start#, number of tiles/images, tile width, tile height]

Code (glbasic) Select
// Get tiles
FUNCTION AnimImage: name$, start_num, num_sprites, width, height

LOADSPRITE name$,1000

DRAWRECT 0,0,640,480,RGB(255,0,128)

DRAWSPRITE 1000,0,0

GETSPRITESIZE 1000,sizex,sizey

n=start_num
x=0
y=0

WHILE n
GRABSPRITE n,x,y,width,height

n=n+1

x=x+width

IF x>sizex-width
 x=0
 y=y+height
ENDIF

IF KEY(1) THEN RETURN

WEND

ENDFUNCTION
Title: Animation strip/tile loading function
Post by: Lazarus on 2007-Dec-31
That's handy. :) Thank you.
Title: Animation strip/tile loading function
Post by: Schranz0r on 2007-Dec-31
I think there a many better ways to do that !
Title: Animation strip/tile loading function
Post by: Ian Price on 2007-Dec-31
Quote from: Schranz0rI think there a many better ways to do that !
Thanks for the warm welcome :/

Can you show me a better way then please?
Title: Animation strip/tile loading function
Post by: PeeJay on 2007-Dec-31
I'm with you on that, Ian. Schranz0r - let me explain - both Ian and myself are both new to GL, and are learning as we go along - but you will have also noticed that we are more than willing to share our code. It is little benefit to either of us to have out code criticised, with no helpful alternatives suggested. I hope you can appreciate this, and perhaps suggest a better method?
Title: Animation strip/tile loading function
Post by: Schranz0r on 2007-Dec-31
Sorry, you have probably misunderstood :)
Thats not a bad code!!!
BUT ;)

You load any picture with the spritenumber 1000
-Load and draw the picture with the startsprite(start_num) !

And num_sprites.....(a way, but not a good one ;) )
-you can calculate it with the spritesize / tilewidth = Tiles


EDIT:

Thats my way:

Code (glbasic) Select
FUNCTION LoadSpriteSet: Picture_name$, Start_num, TileSize_X, TileSize_Y
LOCAL X,Y,XX,YY,TilesX,TilesY

LOADSPRITE Picture_name$, Start_num  // Load the SpriteSet
DRAWSPRITE Start_num,0,0 // Draw the Spriteset
GETSPRITESIZE Start_num,XX,YY // Get width an height of the SpriteSet

TilesX = XX / TileSize_X // Tiles in X-direction
TilesY = YY / TileSize_Y // Tiles in Y-direction

FOR X = 0 TO TilesX-1 // x to TilesX
FOR Y = 0 TO TilesY-1 // y to TilesY

GRABSPRITE Start_num,X*TileSize_X,Y*TileSize_Y,TileSize_X,TileSize_Y // Grab the Sprite

INC Start_num,1 // next Spritenumber

NEXT
NEXT

BLACKSCREEN  // Clear screen
ENDFUNCTION
Title: Animation strip/tile loading function
Post by: Lazarus on 2007-Dec-31
One... huge problem with both of these functions.
If the resolution for the program is set at say  - 640X480, and your animation image is larger than that... it'll get cut off.

Any solution?
Title: Animation strip/tile loading function
Post by: Schranz0r on 2007-Dec-31
it`s not cut off ! ;)
Title: Animation strip/tile loading function
Post by: Quentin on 2007-Dec-31
you could create your spriteset in that way that the sprites are arranged in one more line.

for example: if each sprite of your set has a width of 200 pixels you can put three sprites in each "line" of your sprite set

[sprite1][sprite2][sprite3]
[sprite4][sprite5][sprite6]
[sprite7]

but this will cause some more effort to your coding

However you'll have a problem if the height of the sprites is larger than 480 pixels ;)
In this case I'll prefer single sprites.
Title: Animation strip/tile loading function
Post by: Kitty Hello on 2008-Jan-01
For this problem I do a DRAWSPRITE before every GRABSPRITE and then offset the image, so that the grabbed area is always at 1,1, width, height.
Title: Animation strip/tile loading function
Post by: Schranz0r on 2008-Jan-01
ah OK that cut off ^^
yes, thats bad, can i use CREATESCREEN ? :blush:

@ all

:good: Happy new year !!! :good:
Title: Animation strip/tile loading function
Post by: Kitty Hello on 2008-Jan-25
New Command: DRAWANIM.