Sprite Manager?

Started by
14 comments, last by thekid 24 years ago
Hi I am working on my sprite manager and I have a few questions. I want to be able to request an image with a filename. Then I want the sprite manager to return a pointer to the image and If the sprite is not in memory then it loads it from file. How do I do this I know some others have done it but I dont how to do it. Any source? Thanx
Advertisement
Hi,
just use a little struct :

typedef struct TSprite{    bool Loaded;    int SurfaceIndex;    char FileName[25];}; 


And if you request the filename and "Loaded" is true, then you can blit it from the surface, else you gotta load it into a surface again.

CU

Graphix Coding @
Skullpture Entertainment
http://www.skullpture.de
Graphix Coding @Skullpture Entertainmenthttp://www.skullpture.de
Yea but How do you find the image you are trying to find with the filename.

For Example:

I have a function like this

GetImage(char *filename);

If I use your method I would have to index through all the images and check if the filname is equal to the filename in the struct.

Is their some more direct way like an index or something?

Anything is helpfull
You could just set up a binary search tree of all the filenames loaded, or of the actual surfaces themselves. I haven''t implemented this yet, but I''m planning on it. First, I''d create a class that has the method GetImage(). This would return a pointer to a DX surface (if you''re using DX). The class itself would have a binary search tree of structs:

struct NODE
{
struct NODE *left, *right;
char *filename;
LPDIRECTDRAWSURFACE7 lpddsSurface;
};

When an filename is requested, the class looks through the BST to see if it has been loaded yet, and if not, it loads it, puts it into the BST, then returns the actual pointer to the image. You may want something slightly faster if you plan on calling GetImage() from the class a million times while executing your program, but I think this implementation would be just fine if that''s all you''re looking for.

Good luck!


ColdfireV
[email=jperegrine@customcall.com]ColdfireV[/email]
Alright For restoreing my images and directx surfaces I use a system like this.

typedef struct IMAGE {
int h,w;
LPCTSTR name;
LPDIRECTDRAWSURFACE4 buffer;
BOOL V_BITMAP;
int flag;
BOOL mask;
} IMAGE;
typedef struct V_BITMAP {
IMAGE *Bitmap;
V_BITMAP *next;
} V_BITMAP;
static V_BITMAP *bitmap_list;

Then In my load bitmap function I take The parameters passed to it and store them in the list. (But only if the image is being loaded into video memory.) Then if I loss focus and my surfaces are destoryed I just loop through the list and restore the surfaces by calling the load bitmap function with the parameters saved in the list. This works pretty well and I havent had any speed problems.
I am still a little lost. By all of your methods I will have to loop through all my surfaces with alot of if elses to find if the filename matches.

Maybe I am just not understanding

Any source code?
I'm planning on doing something like this for my game developement library soon.

What I would do is set up a system where you register your images with a function like this:

int MakeImage( const char* szFileName ); 


That function would return a resource ID of sorts. Then, when you needed access to the image, you would use a function like this:

LPDIRECTDRAWSURFACE GetImage( int iResourceID ); 


What I would do internally is to use an array of pointers and use the ID as the array index. Then you could implement GetImage like so:

LPDIRECTDRAWSURFACE GetImage( int iResourceID ){  if( ! ImageInfoStructs[ iResourceID ]->Loaded ) {    LoadBitmap( &ImageInfoStructs[ iResourceID ] );  }  return ImageInfoStructs[ iResourceID ]->Surface;} 


You might also want to implement some sort of flushing where you can release surfaces not being used.

This should give you a general idea of how to do it. Hope it helps!

Josh
http://www.jh-software.com

Edited by - Josh on 4/4/00 7:10:20 AM
Joshhttp://www.jh-software.com
If you're in C++, then look into std::map<char*, Sprite*> for one quick and easy solution. It works wonders for me and took under half an hour to implement since I already was familiar with STL code.

Edited by - Kylotan on 4/4/00 8:35:46 AM
Kylotan: Your method works, but is it fast enough?

I wouldn''t think that using a map would be as fast as an array index. Of course I haven''t tried it, so I wouldn''t actually know. Your method would definately be easier


Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com
i''ve used STL map before. of course it wasn''t as fast as Josh''s method (which is great, btw), but i was storing a surface pointer in the game object and just got the pointer when object was created, not every time it was drawn. i didn''t have any "rapid-fire machine gun" that would search the map for bullet gfx 200x/min, but that seemed to be my impression of the speed concept that was brought up.
a map could be useful for a quick, stable (with little testing) way of doing this.

crazy166

some people think i'm crazy, some people know it

This topic is closed to new replies.

Advertisement