Sprite Manager?

Started by
14 comments, last by thekid 24 years ago
when i created a surface manager for my engine, i first thought a STL map<> would be ideal.
well, it certainly was concerning programming comfort, but i requested surfaces every frame before they are drawn.
when i profiled my engine, i saw that those plain map lookups were taking up near half (!) of the process time.
so now i am using a c array which is of course helluva fast
Advertisement
Still got a problem. With Josh''s method you must store a bunch of intigers somewhere and remember them all to access a bitmap. And also with his BitmapLoad function he has no information to load a bitmap. Like where do you get the filename?

Maybe I still dont get it?
quote:
Still got a problem. With Josh''s method you must store a bunch of intigers somewhere and remember them all to access a bitmap. And also with his BitmapLoad function he has no information to load a bitmap. Like where do you get the filename?


Everything needed would be kept in a struct:

struct ImageInfo {  bool   bLoaded;  string sFileName;  LPDIRECTDRAWSURFACE Surface;  // whatever else you need here...}; 


Now, make a few globals:
const int  MAX_IMAGES = 100;ImageInfo* ImageInfoArray[ MAX_IMAGES ];int        iCurImage = -1; 


Now, you could implement MakeImage and LoadBitmap like so:
int MakeImage( const char* szFileName ){  if( ++iCurImage >= MAX_IMAGES ){ --iCurImage; return -1; }  ImageInfoArray[ iCurImage ] = new ImageInfo;  ImageInfoArray[ iCurImage ]->bLoaded   = false;  ImageInfoArray[ iCurImage ]->sFileName = szFileName;  return iCurImage;}LPDIRECTDRAWSURFACE LoadBitmap( ImageInfo* pImageInfo ){  Some_Bitmap_Function( pImageInfo->Surface, pImageInfo->sFileName );  return pImageInfo->Surface;} 


Of course, you would want to put in error handling, but I''m trying to keep this as simple as possible. Hope this clears things up.


Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com
quote:Original post by thekid

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 find it interesting these days that if a person doesn''t know the concept of a binary tree or a linked list, they require code explinations in order to implement them.

I remember back in HS when I took pascal (yes I am old) that things like linked list and binary trees were the first and foremost data structures to learn in any thing related to programming.

The trend nowadays seems to be "teach yourself in 21 days --- and try to grab as much of other peoples canned code as possible".... honestly, this makes me sad, that one day, the true hard-core programmer will go the way of the do-do.

O Please!!!

Whatever!!!

What I hate about you OLD accoplished coders is that you expect everyone else to be as good as you. This is a comunity and Sharing code is one of the strong points here. If you dont want to then FINE forget I ever asked but dont flame me for doing so.

I have been programing for over a year now and NO I dont know how to do binary search tree. I have read alot of books and I come here every day so I guess I will learn about thems sometime.
I've never had a problem with STL speed. It often uses a lot of memory, but is quite fast. The array method mentioned earlier requires that you get an ID number back. That is, of course, quicker, but defeats the original idea of being able to get a sprite based on the name.To get one based on the name, you'd have to have a lookup to get the ID based on the name, and then look in the array, so you'd probably be best off with the map anyway. For the array method, you will have to declare a global variable to hold each ID that gets returned, which may be great for many uses but a hassle for others. Sometimes it's nice to be able to just do:
Draw(GetGraphic("sidebar.bmp");
rather than
int SidebarID = MakeImage("sidebar.bmp"); // in Initialisation section
extern int SidebarID; // in some other file
Draw GetImage(SidebarID);


As it doesn't require adding in extra variables just to draw more graphics, etc. And you can guarantee that "sidebar.bmp" is an accurate description of what you're drawing, whereas SidebarID could be anything you felt like loading into that integer.

Not that I'm knocking Josh's method - I use it myself for certain things (my pathfinding returns PathIDs, for instance.) Just that it's always worth considering other methods

Edited by - Kylotan on 4/5/00 5:30:24 AM

This topic is closed to new replies.

Advertisement