is there a good way to load a ton of images without crashing the program? Help!
in
Programming Questions
•
5 months ago
I'm working on a program for a lit arts class at school, and I'm encountering MAD STRUGGLES. Can you help me??(we've been instructed to ask you wonderful internet folks for guidance if necessary
)
Can anyone show me how to make this not crash? I'm absolutely willing to believe that I've done this in the most roundabout way possible. There must be a better way!
Basically, I'm trying to write a program that displays a new image (from a bank of around 100) on mouse-click. (I mean, the program does other stuff, but this is the issue I'm having trouble with specifically). Right now, I have a program that will run for about three trials, and then it gives me an OutOfMemory error (I tried increasing the memory available, which helped somewhat, but it still crashes). All of this leads me to believe there must be a better way.
Right now, I have all the images in my data folder, and all the file names in a text document. My program loads the text file (called names), sets n at 0, and then loads the image names[n]. Then, on mouseclick, it adds 1 to n (and I put in a part where n resets to zero when it runs out of images).
I'm not sure I even explained that well, so let me just drop in the relevant parts of the code here:
- PImage img;
- int n = 0;
- void setup()
- {
- size(800,800);
- }
- void draw()
- {
- background(255);
- String names[] = loadStrings("imgfiles.txt");
- img = loadImage(names[n]);
- image(img, 0, 0);
- }
- void mouseClicked()
- {
- n = n+1;
- if (n > 133) {
- n = 0;
- }
- String message = "N is now:" + n;
- println(message);
- }
(also, if it matters, imgfiles.txt is literally just filenames separated by line breaks.)
Thank you so much!
Clara
UPDATE: I solved it! Never mind!
I put noloop() in the setup part, and then told the program to redraw() on mouse clicked. If anyone else is having similar struggles, this worked for me!!
1