Advertisement

[java] MemoryImageSource vs. Image

Started by March 05, 2000 07:39 PM
3 comments, last by sckime 24 years, 7 months ago
I have been working on a pure Java game for some time now. However, I have some advanced questions about AWT graphics processing that I was going to throw out to everyone here. 1) I have read about MemoryImageSource, but when I have implemented basic trials of it, I see a halving of my frame rate. I use the setAnimated function to have it redisplay itself whenever a call to newPixels is made. Does anyone have any suggestions or comments on the performance of MemoryImageSource in their games. 2)I recently reread the Javadocs on graphics.. It says that drawImage returns immediately. If this is the case, how does one accurately get a frame rate? The method that I''ve been using is to get the time via system calls and subtract the time before the call from after the call to drawImage. 3) Other than using JNI to augment AWT, does anyone know of any ways of increasing performance in a Java game application? I know this is a broad question, but I''m not terribly sure of what tricks people have come up with.
1. I don''t print a memoryimagesource to the screen, I always make an Image from it first.

2. I don''t know.

3. Only draw what you absolutely have to. Use clipping regions. Optimize your code.
Advertisement
1) Jim_Ross: He is drawing an Image that has been created from a MemoryImageSource with the animated flag set. The resulting Image gets automatically updated when you call newPixels method in the MemoryImageSource. This allows doing some of those fine effects seen in Java demos that require heavy pixel tweaking.

As for the original question, sorry can''t help. Have you looked into some Java demo''s source codes? Maybe you could find out some tricks from there? What are you doing with the MemoryImageSource anyway, are you implementing alpha blitting of bitmaps or what? Maybe you could optimize those needed calculations somehow...

2) This is a good question, I noticed this when I benchmarked my library and got some insane numbers (around 150 FPS). I don''t know any complete cure to this.

The first step (that you propably are using allready) is to not use the normal repaint()-updated()-paint() cycle, but to getGraphics() and paint directly to the graphics. At least this way your game logic and your graphics are more in sync with eachother.

One caveat though: when you call the getGraphics method you are creating a new graphics object, if you do this for a while (around 10 seconds of 50 FPS game) you will get some serious garbage collector trashing that will ruin your game.
The key here is to use the once gotten Graphics object until it turns bad (at least it does this under Win32-JDK 1.1, for some reason the Graphics context changes during the lifetime of a component irregularly) and then get a new one. This involves a simple try-catch setup.

3) I''d advice against using clip regions heavily. I noticed a major slowdown when I started using clip regions in my library code, unfortunately I''m stuck with them for now. The reason for this slowdown is that when you change the clip region, drawing colour, used font etc. you are changing the state of the graphics object and as in OpenGL these state changes are expensive when compared to the basic drawing operations. So the way to get the most speed out of pure Java based game is to minimize these state changes. E.g. if you have to draw several blue, red and white lines first set the color to blue and draw the blues lines, then change the color to red and draw the red lines and finally change the color to white and draw the white lines.
-Pasi Keranen
Thoughts on Question 2:

as long as you aren''t using null as your last argument on drawImage, you could override

imageUpdate(Image img, int infoflags, int x, int y,
int width, int height)

and probably check certain flags (maybe ALLBITS) to see when your current frame is actually drawn...maybe get the current time before your call to drawImage and then subtract from the current time in imageUpdate when the whole image is available.
Wouldn''t this approach (using the imageUpdated method) measure the loading speed of the image, not the speed of the game loop (that is what sckime is wanting to measure, I think)?

I mean the imageUpdated is invoked until all image bits have arrived and after that (even if you keep drawing that image) it doesn''t get invoked anymore.

But using this approach with the MemoryImageSource (where the actual Image object gets updated and thus the image updated method gets invoked upon redraw) might have some use...
-Pasi Keranen

This topic is closed to new replies.

Advertisement