Sprite sheet and strips info + sprite strip procedure

Advanced game related topics
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Sprite sheet and strips info + sprite strip procedure

Post by J. Baker »

Over the years of using PureBasic's 2D functions for gaming, I've learned a few things. Hopefully this info may be of some help.

You probably already know about sprite sheet and strips and how it can reduce the file size of your game. I recently started a new method on how I'm going to develop my game. It involves using sprite sheets for static sprites and sprite strips for animated sprites. This may sound odd but here's why...

Sprite sheets are great for loading a whole sheet of static images and clipping them before your repeat section. Not only does this save major space for your exe/app but all your static sprites are pre-clipped and ready to be displayed when you need them. Now you may wonder why I'm not suggesting sprite sheets for animation. Well, there's nothing wrong with doing so but read the next paragraph.

Sprite strips are great for animation. Animations are usually a few frames and you don't have to worry about going beyond the texture width that a graphics card can handle. Unlike putting a bunch of static images into a strip and easily going beyond 4096 in width. Which some gpu's can not handle. Also, sprite strips are smaller in file size then sprite sheets. Testing was done by using the same images for both. At least on OS X and I'm sure the same holds for Windows but don't hold me to it. The animation routine/procedure is smaller and quicker without having to calculate Y coordinates.

So I created a new procedure for animated sprite strips. This procedure does not calculate anything it doesn't need to and only clips static frames once. You can easily change the values of the procedure/new function within your loop. Such as going from an animation, such as running, to a still frame. There's also a return so you can easily find out what frame the current animation or still is on. This return is great if you want different events to happen depending on the frame that the event happens on. You can alter the fps of an animation too. If you want a still frame, just make "asStartFrame" and "asEndFrame" the same value and 0 for "asFpsDelay".

Anyway, here's the new code. ;)

Code: Select all

Structure AnimatedSprites
  asNextFrame.u
  asClipOnce.u
  asNextStill.u
  asFps.u
  asData.u
EndStructure

Procedure AnimateSprite(asSprite, asClipSquare, asStartFrame, asEndFrame, asFpsDelay) ;<--- For a still frame, use 0 for "asFpsDelay".

  Static Dim asSprites.AnimatedSprites(100) ;<--- Set to the maximum #Sprite number you plan to use with this procedure.
  
    If asSprites(asSprite)\asData <> 1 ;<--- Only need this data to start off with.
        asSprites(asSprite)\asNextFrame = 0
        asSprites(asSprite)\asClipOnce = 0
        asSprites(asSprite)\asNextStill = asStartFrame
        asSprites(asSprite)\asFps = 1
        asSprites(asSprite)\asData = 1
    EndIf
    
    If asStartFrame + asSprites(asSprite)\asNextFrame > asEndFrame ;<--- Already clipped the last frame and need to start from the begining.
        asSprites(asSprite)\asNextFrame = 0
    EndIf
    
    If asSprites(asSprite)\asNextStill <> asStartFrame Or asSprites(asSprite)\asNextStill <> asEndFrame;<--- If you go from one still frame to the next still frame or animation, we reset "asClipOnce".
        asSprites(asSprite)\asClipOnce = 0
        asSprites(asSprite)\asNextStill = asStartFrame ;<--- If we go to an animation, we don't want this "If" statement to keep calculating "asClipOnce = 0".
    EndIf
    
    If asStartFrame = asEndFrame And asSprites(asSprite)\asClipOnce = 0 ;<--- No need to keep clipping a still frame.
        ClipSprite(asSprite, ((asStartFrame + asSprites(asSprite)\asNextFrame) * asClipSquare) - asClipSquare, 0, asClipSquare, asClipSquare)
        asSprites(asSprite)\asClipOnce = 1
        asSprites(asSprite)\asNextStill = asStartFrame
    ElseIf asStartFrame < asEndFrame ;<--- Clipping for animation.
        ClipSprite(asSprite, ((asStartFrame + asSprites(asSprite)\asNextFrame) * asClipSquare) - asClipSquare, 0, asClipSquare, asClipSquare)
    EndIf
       
    If asSprites(asSprite)\asFps = asFpsDelay ;<--- Checking for "frames per second" before going to the next frame.
        asSprites(asSprite)\asNextFrame + 1
        asSprites(asSprite)\asFps = 1
    ElseIf asFpsDelay <> 0 ;<--- Not equal to a still frame or full blast 60fps animation or higher.
        asSprites(asSprite)\asFps + 1
    EndIf
    
  ProcedureReturn asStartFrame + asSprites(asSprite)\asNextFrame ;<--- If you want an event to react different depending on the current frame number.
           
EndProcedure
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Sprite sheet and strips info + sprite strip procedure

Post by Zach »

Interesting.. don't really understand it, but I haven't look at it closely enough either :p

I'm planning on doing some 2D stuff in the future, and have been contemplating making my own rendering routines and stuff, so this may prove to be educational.

Just wish I could draw / do sprites :( :oops:
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Sprite sheet and strips info + sprite strip procedure

Post by J. Baker »

Anyone can draw and graphics don't always have to be the best. Gameplay in my mind is #1. It doesn't matter how well the graphics are if the gameplay bites. The only tip I can give is, if you're not too good at graphics, try doing old school pixel style graphics and animation. There's no need for a lot of detail there. That's what I'm currently doing for my game. I enjoy the old school style games. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Sprite sheet and strips info + sprite strip procedure

Post by Zach »

Well yeah, I really would like to.

The problem is, I would hope that some day I could make a console/JRPG style RPG. But no one wants to play something designed at 320x240, even 640x480 would be a stretch..
I'd like to do something in 1280x720 minimum so it actually looks nice on the screen, but still has that old style.

I've tried editing sprites before, and I could probably get a handle on it if I dedicated myself to trying, but then comes the question of textures... I totally suck at that kind of thing.
I'd love to be able to just save up some money and hire someone to do the assets, but Pixel art is time consuming, and expensive.. Doing an HD resolution would probably make it even more time consuming and expensive too :(

But maybe there is hope.. I'd love to come up with a game that I could sell through Steam or something one day.. They do have some retro-style games on there, stuff that looks pretty old school.
But who knows when I would have time to even start this project..

I've got to get my Text RPG made first! (wonder if anyone would buy that on Steam for $0.99c LOL)
Post Reply