I searched alot on this forums for solution for this and found only some snippets. Then today i worked almost whole day on this and got it working. Im creating GTA style game and such plain texture animation is must have in such game. So i thought i must share this with others. Because such example is requested alot but there is almost always no answers how to actually do this.
I have attached RAR with complete code and example BMP.
And the source only is here:
`======================================================
`Plain texture animation
`Created: 13.09.2009 by Mulderman (A.V)
`Credits and many thanks goes to:
` 1) dark coder for SetObjectSpriteFrame
` 2) Grog Grueslayer for timing code
` 3) Van-B for motivation
` 4) Grand Theft Auto 2 for it's awesomeness
`======================================================
sync on: sync rate 60
`Load spritesheet
`Change this bmp file and it's path to your own values!
load image "xped.bmp", 2
`Create animation from it
`Change this bmp file and it's path to your own values!
create animated sprite 1, "xped.bmp", 9, 1, 1
`Make simple and small plain
make object plain 1, 0.2, 0.2
`Texture our plain with the spritesheet
texture object 1, 2
`Current image frame
global Frame = 1
` Set a timer for frame switching
time = timer()
`Make our plain transparent
set object transparency 1, 2
`Main loop
do
`Uncomment these 2 lines to see sprite animation at X 40, Y 80
`play sprite 1, 2, 9, 190
`sprite 1, 40, 80, 1
`Check if 190 milliseconds is past
if timer() > time + 190
`Change to the next frame number
inc Frame
`Check if the frames are within 1 to 9
if Frame = 9 then Frame = 1
`Show the frame switch
`Reset timer
time = timer()
endif
`This does the hard work, thanks goes to "dark coder"
`http://forum.thegamecreators.com/?m=forum_view&t=105132&b=1
SetObjectSpriteFrame( 1 , Frame , 9, 1)
`Display FPS and current frame
text 40,40, "FPS "+str$(screen fps())
text 40,60, "Switch to Frame "+str$(Frame)
`Update
sync
loop
`Free the resources
delete image 1
delete object 1
`End of program
end
` ###########################################################
` ###########################################################
` http://forum.thegamecreators.com/?m=forum_view&t=105132&b=1
` ###########################################################
` ###########################################################
Function SetObjectSpriteFrame( Object As Dword , Tile As Integer , SpriteX As Float , SpriteY As Float )
Local ShiftX As Float
Local ShiftY As Float
Local U As Float
Local V As Float
Local Across As Integer
ShiftX = 1 / SpriteX
ShiftY = 1 / SpriteY
If Tile
Across = Int ( Tile / SpriteX )
V = ShiftY * Across
U = Tile MOD SpriteX
U = U * ShiftX
Else
U = 0
V = 0
Endif
Lock VertexData For Limb Object , 0
Set VertexData UV 0 , U + ShiftX , V
Set VertexData UV 1 , U , V
Set VertexData UV 2 , U + ShiftX , V + ShiftY
Set VertexData UV 3 , U , V
Set VertexData UV 4 , U , V + ShiftY
Set VertexData UV 5 , U + ShiftX , V + ShiftY
Unlock VertexData
EndFunction
All comments, suggestions for improving this etc are very welcome.