BASIC

Author Topic: Animation strip/tile loading function  (Read 4991 times)

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4144
  • On the shoulders of giants.
    • View Profile
    • My Apps
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<start_num+num_sprites

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

  • Guest
Animation strip/tile loading function
« Reply #1 on: 2007-Dec-31 »
That's handy. :) Thank you.

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5014
  • O Rly?
    • View Profile
Animation strip/tile loading function
« Reply #2 on: 2007-Dec-31 »
I think there a many better ways to do that !
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4144
  • On the shoulders of giants.
    • View Profile
    • My Apps
Animation strip/tile loading function
« Reply #3 on: 2007-Dec-31 »
Quote from: Schranz0r
I 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.

Offline PeeJay

  • Mr. Polyvector
  • ***
  • Posts: 244
    • View Profile
    • PeeJays Remakes
Animation strip/tile loading function
« Reply #4 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?
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5014
  • O Rly?
    • View Profile
Animation strip/tile loading function
« Reply #5 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
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Lazarus

  • Guest
Animation strip/tile loading function
« Reply #6 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?

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5014
  • O Rly?
    • View Profile
Animation strip/tile loading function
« Reply #7 on: 2007-Dec-31 »
it`s not cut off ! ;)
I <3 DGArray's :D

PC:
AMD RYzen 7 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Animation strip/tile loading function
« Reply #8 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.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Animation strip/tile loading function
« Reply #9 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.

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5014
  • O Rly?
    • View Profile
Animation strip/tile loading function
« Reply #10 on: 2008-Jan-01 »
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 1700 @3.9Ghz, 16GB HyperX Fury 2666Mhz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10697
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Animation strip/tile loading function
« Reply #11 on: 2008-Jan-25 »
New Command: DRAWANIM.