Jump to content
  • Advertisement
Sign in to follow this  
SirGorthon

Storing Images/sprites in an array, linked list, or just making it one BIG image?!

This topic is 4846 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

Howdy guys! I was wondering how I should store sprites/images for my games! Originally, I stored the same image in multiple places - i.e. one copy of the same image per instance of a monster! Obviously, that's not a very memory efficient way to do things (although it WAS pretty easy on the programmer's part ;) Anywho, right now I have a linked list of images, and I pull an image from the list with a function (in Java) along the lines of:
     public BufferedImage get(int n)
     {
	  DerImageNode current = first;
	  for(int i = 0; i < n; i++)
	       current = current.next;
	  return current.src;
     }

(src is a BufferedImage in DerImageNode, and first is the beginning of the linked list) This way seems flexible - I can add as many images as I want to the list and not worry too much about memory allocation, but I'm afraid it's slower than using an index in an array! Running through the list like that every time I want to display a sprite seems like it'd suck up a lot of processing power and time! If I have 50 images, and have to run through them each frame like that, the speed would definitely take a hit! I'm afraid to use arrays, because if I suddenly need to load another image, creating a new array and copying the old one over seems like that would be awfully time consuming too! It appears I'm stuck between a rock and a hard place! Any advice or pointers would be appreciated! I'm pretty much stuck with Java because I'm on linux, and my friends all use Windows (making cross platform stuff necessary :/)! Anywho, if there's some Java-language specific deal (a library or class thingy) that handles this sort of thing, I can that too! Thanks again! SirGorthon

Share this post


Link to post
Share on other sites
Advertisement
Hashtable.

Just make the hashtable key the name of the sprite (or some way that you can ID the texture you need--possibly through an enum), and set the value to the sprite. No searching or index required.

Cyric

Share this post


Link to post
Share on other sites
There's several ways to solve your problem.

1. Sprite sheet, or "make it one BIG image". This solves your problem. If you need to be dynamic, you could create your one BIG image at run time.

Reiner's has a sprite sheet creator.

2. Hash with bins. Basically, you divide your images into certian hashes and place the images into arrays that is assigned to those hashes. If you have enough hashes, you don't need any arrays.

An example would be taking the first letter of the name of the image and using that to divide your images. Then you'd search through the a's instead of the entire array.

3. Tree. Instead of using a link list, use a Red-Black tree. A self balancing tree would be equal for your best case search(first element), but cut down the time on your worse case search.

4. Array of pointers. Make an array of your current.next and use it to reference your character graphics. An array of pointers would cost less to expand than an array of actually graphics.

5. Heap. A heap is basically a self-sorting link list. Everytime something is inserted, it places it where you 'expect' it to be. You could use this information to speed up searches.




Share this post


Link to post
Share on other sites
Be aware that linked lists, trees etc. are all implemented in the standard library, and by no means should you attempt to do them yourself - it is a waste of time.

You should also be aware of how the Java object model works. For example, if you have something like


class Monster {
public Image myAppearance;
// other stuff
}

class Game {
public static void main(String[] args) {
Monster x = new Monster();
x.myAppearance = getAnImageSomehow();
Monster y = new Monster();
y.myAppearance = x.myAppearance();
}
}


Here you do NOT actually have "one copy of the image per Monster instance"; rather, each Monster references the same Image - as if you had a Monster* as the data member in C++ - ok, more like a Monster& because it can only be a valid pointer or NULL, and a bit friendlier than that because there is automatic garbage collection. But anyway :) Java stores objects by reference, so beware of (and make good use of! A lot of bad Java code results from obsession with copying/"cloning" objects needlessly) the resulting aliasing.

Share this post


Link to post
Share on other sites
Wow!

Thanks for all the helps guys!

This hashtable business sounds crazy enough to work (although perhaps TOO easy :)! :D

Thanks again!
SirGorthon

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!