Many images cause program to exit without warning

Hi all,

I am trying to write a 2d shooter in java. I want to have the background rotate rather than the ship rotate because I’m difficult like that. I tried using affine transforms but found them to be very slow.

Instead I decided to try loading in a large number of images (64 at 800*600 in 256 colour gifs) and display the appropriate image to show the background rotating.

This works fine when I only load 50 or so images but when I load more than that number the program suddenly exits without throwing any exceptions.

I loaded each of the images by popping the following code into a method and calling it in a loop assigning each of the images to an array element.

Toolkit tk = Toolkit.getDefaultToolkit();
Image im1=tk.createImage(imageName);
MediaTracker tracker = new MediaTracker(mainFrame);
tracker.addImage(im1,0);
try
{
tracker.waitForID(0);
}
catch ( Exception e )
{
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(“We’ve messed up somewhere in the image loading”);
System.exit(0);
}

If it makes any difference I am using full screen exclusive mode. The version of the jdk I am using is 1.4.2_03

What I find interesting is that it always fails on the second frame. i.e. it draws the first frame fine along with all the calls to drawImage then fails when I call drawImage on the ship image for the second time. I tried using a smaller ship graphic and this worked but I want to use the original one!

Given that using a smaller image for the ship allows it to work (The larger one contains multiple animation frames the smaller only one) I assume this must have something to do with memory on the graphics card. This seems a little odd to me as when I check the memory usage of the java process in windows it shows I am using 74Mb of memory in total and I have a 128Mb graphics card.

I would have expected that if the graphics card was full java should just hold the image in main memory instead and the performance would degrade. If it is an error throwing situation I would have expected an exception of some kind to be thrown or for the java process to quit with some kind of error.

Am I doing something daft or is it java?

Thanks for reading.

64 800*600 images, which are likely to be stored in 32bit color when in vram…

thats 64800600*4… thats 117meg!

Add in the backbuffer and any other sprites you have, and you are very close (if not exceeding) the capacity of your gfx card.

That 74meg you stated is likely wrong, I doubt very much task manager reports vram memory usage.

I don’t know what behaviour you should expect when you run out of vram, but, knowing the buggy nature of the current implementation, I would not be suprised if it caused a crash.

Crashing on the 2nd frame is consistent with running out of vram. Managed images arn’t copied to vram until the 2nd draw operation

Requiring 100+ megs of ram & vram for a simple 2d shooter is IMHO ludicrous, I think your design needs some rethinking.

[quote][…]
Requiring 100+ megs of ram & vram for a simple 2d shooter is IMHO ludicrous, I think your design needs some rethinking.
[/quote]
Requiring a 128mb card is also abit… you know… Q3 already works with 4mb vram (32mb are optimal).

The easiest approach is using tiles. Just build your background out of 32x32 or 64x64 pixel tiles. Use a 2d array to manage their placement… it’s really that simple :slight_smile:

:-[

When you put it like that it does sound a bit mental. My mistake was assuming that because they were 256 colour images they would only use 1 byte per pixel. Doh.

Tiled 64 by 64 images you say. Hmmn. I could even draw it over the top of itself at an offset to get a multi layered starfield and move them at different speeds to get a parralax effect. All from the same image. Lots of calls to drawImage though.

I did some tests with small images where I had an image consisting of a single star and found that the overhead to calling drawImage made it much quicker to draw an image the size of the entire screen. I guess having bounced between the two extremes it makes sense to actually try something in the middle.

Thanks for the responses guys that was a great help.

I’m now going to try something vaguely sensible. I did have the sneaking suspicion my username might be more apt than was really comfortable.

Monkey

Heh… I’m sorry. I’m kinda mean by default… I can’t help it.

Oh and excuse my manners… welcome to the board :slight_smile:

Well, in any case, the app shouldn’t be quitting silently. Likely an OutOfMemoryError is being thrown somewhere, and then it gets swallowed.

Try increasing the min heap size (that is, if you’re interested in tracing the problem), or follow the advices given above if not =)

[quote]Try increasing the min heap size
[/quote]
I’m curious why you say increase the min heap size as opposed to the max heap size. (Though I realize it may still make more memory available if increased to more than the previous max.) Typo?

No problem. I’d much rather be told why what I’m doing is wrong than not. I’m pleased to have found somewhere with people who know what they’re talking about.

Today I tried one image an eigth of the size of the screen and rotated that onto a volatile image and tiled that across the screen. Good performance and only one small image used.

O.K. so at the moment I’ve now got eight small images rotating independently but I should be able to make it look like the whole screen is rotating by positioning them appropriately.

I don’t think I’m going to bother trying to work out why I’m not getting an error when the graphics card fills up but it does sound like a bug. Maybe I should check the bug list on suns site to see if something similar has been submitted.

This one. Cheers. :slight_smile:

[quote]Typo?
[/quote]
Yep, thanks for catching.