PDA

View Full Version : Help with sprite please...



Crisp_N_Dry
03-02-2003, 06:25 PM
This may be classed as a DelphiX question but it does also pertain to general so I'll stick it here (plus people tend to take more notice of the general area).Anyway...

I am attempting to create a sprite object that allows me to create a sprite with frame count info and image data and compile it into one object. The only problem is that I am creating many of these objects (hundreds!) and because each has at least one or more TDirectDrawSurfaces (a Surface for each animation type,{stand, walk , idle, etc}, I am worried about memory problems. I don't even know how much a TDirectDrawSurface takes up and SizeOf returns a size of 4 bytes for any object it is given (I think it is the size of the address, 32 bits, is this right). Anyway can anyone give me any pointers on ensuring minimum memory overhead. I would prefer to use a surface fo every animation but I guess that would be a bit much. What say you? Please let me know if I have explained this poorly and I will try to write it more clearly. Here's is my TSprite definition

type
TAnimation = Class(TObject)
FrameWidth: Integer;
FrameHeight: Integer;
FrameCount: Integer;
Surface: TDirectDrawSurface;
constructor Create etc
destructor Destroy etc
end;

type
TSprite = Class(TObject)
Anims: Array Of TAnimation;
FootprintX: Byte;//How many tiles it will take up on X axis
FootprintY: Byte;//How many tiles it will take up on Y axis
constructor Create etc
destructor Destroy etc
end;

Alimonster
03-02-2003, 07:51 PM
Your guess about sizeof is correct - all classes are implicit pointers, so sizeof will get you the size of a pointer (4 bytes in other words).

About your question: consider having a manager class. If you have 20 different sprites that look identical, there's no need to store the surface in each class instance wanting that pic. Instead, you would store what pic it has as an index (usually just an integer, string or enum'd type), and when you needed it you'd go to the manager class and say "give me bitmap 'whatever'. The manager would be responsible for loading, creating, and freeing the objects on demand (possibly lazily or maybe at load time). As a result, only one of each surface would be required for the animations by the manager. You could apply this to the animation class itself instead of to the surfaces, depending on your needs.

Alternatively, you could simply chuck an implementation-level surface or array of surfaces (or anims again) and reference count it, although that depends on your chosen design a little. It'd be simpler and can be a quite handy solution. The finalization section can be used to get rid of any created-but-not-yet-killed anims.

Storing a TAnimation in your sprites won't be a major problem if it's a reference to a pre-created anim, since many references will use the same anim object (with only one surface). However, if you create many of those objects then you'll end up wasting a lot of memory, since each anim object will contain a surface. Again, this can be alleviated either by storing references to the animations in your sprites or references to the surfaces in your animations.

You can get the amount of memory taken up by a surface by locking it and checking the pitch (remembering to unlock it later!). The amount of mem is about surface_height * surface_pitch bytes. I couldn't tell you how to lock a surface with DelphiX since I've never used the thing, but somebody else will, I'm sure! :batman: There's probably a way to the IDirectDrawSurface[whatever] interface, in which case you'd use the lock and unlock functions in the usual fashion. DelphiX probably has a wrapper property or function for that task, mind you.

A surface will pretty much take up its expected size or a bit more per scanline, depending on whether the graphics card decides to pad the surface. The main thing to watch out for is how much video memory you want for your surfaces - could be a problem on older kits, I guess.

Crisp_N_Dry
03-02-2003, 09:28 PM
Awesome response there AliMonster. It seems you've bailed me out yet again. Cool website btw.

Alimonster
04-02-2003, 09:27 AM
Cool website btw.
Thanks! Always wanting to improve it but never enough time. I'll probably bung some nearly finished tutorials soon (DDraw, Timing). :|