We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › check if image already loaded in an array
Page Index Toggle Pages: 1
check if image already loaded in an array (Read 726 times)
check if image already loaded in an array
Oct 19th, 2006, 12:19am
 
Hey,
Excuse my ignorance, but I just dont get my head around this, should be very easy. Say I have an array of Pimages:

textures = new PImage[texture_paths.length];

now i want to load images in when needed, and have to check if there is already one I loaded before:

if ( textures[texpointer] ) {

tex = textures[ texpointer ];
} else {

textures[texpointer] = loadImage( texture_paths[texpointer] );

tex = textures[ texpointer ];
}

the if ( textures[texpointer]  ) {... bit doesn't work (not a boolean). How can i find out properly if the image was already loaded. I tried java functions like instanceof but didnt get them to work.

Related to that how can i unload an image, so it frees up memory?



Re: check if image already loaded in an array
Reply #1 - Oct 19th, 2006, 3:14am
 
You'd have to loop through the array checking for a match.

For example:

Code:
boolean inArray = false;

for(int i=0; i<textures.length; i++) {
if(textures[i]==newTexture) {
inArray = true;
break;
}
}

You might want to have a look at the ArrayList class too:

http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html

Managing arrays can be kind of a pain sometimes, and ArrayList has most of that built in.


As far as checking if an image is loaded or not, you can't really loop through the array because in order to check the image against those in the array it would have to be loaded.

You could try something like checking if your texture==null and then load if needed..
Re: check if image already loaded in an array
Reply #2 - Oct 19th, 2006, 8:27am
 
to free the memory for a particular image, just equate it to null
Re: check if image already loaded in an array
Reply #3 - Oct 19th, 2006, 11:34pm
 
Hey, thanks guys.

checking if null does the trick!

Another thing I was wondering about is whats the best way to have associative arrays in Processing? they are dead easy to do in AS or PHP, but I had to digg deep ( for me at least ) into Java docs to find what I am looking for. In the end I used something called HashMaps then to sort it i had to transfer it into a TreeMap. I am just wondering if thats the best and fastest way?

I guess i could have also build a custom class with two arrays, one for the keys and one for the values.

What do you guys use?
Re: check if image already loaded in an array
Reply #4 - Oct 19th, 2006, 11:38pm
 
you might want to encapsulate the code and data you need into a class of its own and operate on an array, arraylist, hashmap or whatever containing said class. something like:

Quote:
Texture [] myTextures;

(...)

class Texture {
   PImage img;
   boolean isLoaded;
   String filePath;
   public Texture(String fp) {
           filePath = fp;
           isLoaded = false;
   }

   public void load() {
           img.loadImage(filePath);
           isLoaded = true; // actually, you should *check*, if loadImage() really succeeded ;-)
   }
   (... and so on)
}

Re: check if image already loaded in an array
Reply #5 - Oct 19th, 2006, 11:53pm
 
Thanks brsma,

I think thats what i was trying to do, i have a class called Tile which loads different texture resolutions deending how close it its to the camera. Its just that the textures come out of an XML file  as attributes with something like this:

512="path/to/tex" 256="path/to/tex" ....

that  seemed to me perfect for an associative array hence the question, especially I dont want to predefine the texture sizes as properties of the class.
Re: check if image already loaded in an array
Reply #6 - Oct 21st, 2006, 1:21am
 
eberclouds wrote on Oct 19th, 2006, 11:34pm:
Another thing I was wondering about is whats the best way to have associative arrays in Processing


Yeah HashMap would be the one to use. Honestly though, I've only ever needed to use an associative array once in Processing/Java so depending on what you need it for it might be a little overkill. But it can't hurt to just go for it!
Page Index Toggle Pages: 1