Sign in to follow this  

Storing Images

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, so I've got this game, and some of the graphics are JPEGs that I load up at initialisation. Now, at the moment I'm just storing them as public, static members of a class totally unrelated to images. The reason is that, at the beginning, that class was the only one that used any JPEGs, so I put the loading code there, and then when other classes started to do so, I couldn't be bothered to move the code. Now, plainly this is not ideal design, even if it works for a project with only one man on it - after all, I know exactly where to find my images, and it's trivial to add another. Still, I'd like to do this a bit more elegantly; does anyone have any good suggestions? There are no real speed requirements; all I really need is a good way to refer to my images from other classes, which can be easily extended as new picture requirements come up.

Share this post


Link to post
Share on other sites
What you don't want is a code free-for-all where classes load their own resources however they want. That gets messy in case you ever change anything. What most people do is create a "resource manager" that fetches images off the HD and serves them up in some kind of cute little wrapper package.

One approach is to do what OpenGL does. When an image is loaded as a texture, OpenGL manages the actual texture and it gives you back an int that refers to the texture. It never gives you the actual texture itself once you feed it the texture data. So basically, load the image and store it in an array of some sort, then just pass back an index into the array. When you say:

image.BlitImage( 41 );

the image "wrapper" class does this:

GraphicBlittingFunction( big_image_array[41] );

This way, external classed do not directly touch the images themselves. Now all loading, dumping, and rendering is done by another central class. If you ever need to change anything, you only change it in one place.

Hope that helps. You are welcome to my resource manager code if you want it (it requires TinyXML and OpenGL).

Share this post


Link to post
Share on other sites
Doesn't that make for slightly unreadable code, since I would have to remember what image number 41 is? Or are you thinking of doing something like this in initialisation :

public void initClass () {
int imageForBlah = ImageManager.loadPiccy("imageForBlah.jpg");
}

and later I would refer to the integer by name, instead of the picture by name? On the one hand, it means that the manager class can be written once and for all, since I don't have to change it to add a new picture. On the other hand, the image (or integer) names are now stored lots of places in the code, instead of all in one place. Hmm, tradeoffs I guess. But on the whole I think I like your method, since why would I need to keep track of all the different image names? And I'd only need to modify the class that is actually using the new image, when I added something. OK, sounds good.

Share this post


Link to post
Share on other sites

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this