Animation strip/tile loading function

Previous topic - Next topic

Ian Price

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
I came. I saw. I played.

Lazarus


Schranz0r

I think there a many better ways to do that !
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Ian Price

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?
I came. I saw. I played.

PeeJay

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?
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

Schranz0r

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
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Lazarus

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?

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Quentin

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.

Kitty Hello

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.

Schranz0r

ah OK that cut off ^^
yes, thats bad, can i use CREATESCREEN ? :blush:

@ all

:good: Happy new year !!! :good:
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Kitty Hello