Just can't seem to get the framerate up...
I'm writing a space shooter that for now can be thought of as an Asteroids clone. I would like to run it fullscreen and the highest resolution possible but I'm having some major problems.
My problem is I'm getting crappy framerates when in fullscreen. When I do anything (in either windowed or fullscreen) with transparency my FPS drops by half, without exception. If you look back to a previous thread I had (http://forum.java.sun.com/thread.jspa?threadID=635783&messageID=3702674#3702674) I was having the same problem then. Even when I implemented the examples they gave, like using a tiled background/sprites instead, it still doesn't do any good. I tried using GIFs instead of PNGs and that didn't do any good either - little or no performance increase.
Let me say that I have the ground work set - I'm loading my images as managed images. I load them with:
BufferedImage src = ImageIO.read(<IMAGE>);
BufferedImage dst = gc.createCompatibleImage(src.getWidth(), src.getHeight(), Transparency.BITMASK);
Graphics2D g2d = dst.createGraphics();
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(src, 0, 0, null);
g2d.dispose();
I'm starting to think that Java2D isn't a API for games. First off, if you are forced to use GIFs you are going to have jaggies all over the place since the transparency is so simple. Second, on a very fast computer with hardware acceleration I can't do much better than 35 FPS, which is with only a few graphics! Nevermind the fact that my game will need to display many, many more sprites than what I display. I have no idea where to go from here. Without a better framerate there's no reason to continue to try and use Java2D.
Any help or ideas on this would be great.
[1789 byte] By [
gjbeyla] at [2007-10-2 4:20:50]

Dang! That's too bad. Are you using OpenGL? Probably not...if i were you i would switch to LightWave Java Game Library (LWJGL). 3d is the future. The thing with game programming is that it's so hard to debug...i mean some bugs are easy, but then again some are like yours here.
A friend of mine told me that if you put "-Xprof " as a program argument you will get all kinds of statistics like which part of the code takes up most of your memory, time to execute, and things like that. I never got around to try it out but if you google it you might get some leeds.
Loops are where bad things usually happen. You might be drawing 100 ships instead in the same place or off the screen.
Try a different method of buffering the image. comment some lines out and try again. Play around with it.
I am also working on a game sort of like Asteroids, but with a little kick (also i am using Java/OpenGL/LWJGL). The screenshots are up on javagamesfactory.org, the name is CroAsteroids. I know this hasn't been much of help but at least you got a reply! :)
Are you using BufferStrategy for your backbuffer?
Yes, BufferStrategy. I get the graphics from it and paint directly to them.I think I'm just going to take a look at LWJGL and see what I can do with no projection 3D (essentially 2D). That way I can do things that would otherwise be more sprites, like particles.
> I'm writing a space shooter that for now can be
> thought of as an Asteroids clone. I would like to
> run it fullscreen and the highest resolution possible
> but I'm having some major problems.
>
> My problem is I'm getting crappy framerates when in
> fullscreen. When I do anything (in either windowed
> or fullscreen) with transparency my FPS drops by
> half, without exception. If you look back to a
> previous thread I had
> (http://forum.java.sun.com/thread.jspa?threadID=635783
> &messageID=3702674#3702674) I was having the same
> problem then. Even when I implemented the examples
> they gave, like using a tiled background/sprites
> instead, it still doesn't do any good. I tried using
> GIFs instead of PNGs and that didn't do any good
> either - little or no performance increase.
>
> Let me say that I have the ground work set - I'm
> loading my images as managed images. I load them
> with:
>
> BufferedImage src = ImageIO.read(<IMAGE>);
> BufferedImage dst =
> gc.createCompatibleImage(src.getWidth(),
> src.getHeight(), Transparency.BITMASK);
> Graphics2D g2d = dst.createGraphics();
> g2d.setComposite(AlphaComposite.Src);
> g2d.drawImage(src, 0, 0, null);
> g2d.dispose();
>
> I'm starting to think that Java2D isn't a API for
> games. First off, if you are forced to use GIFs you
> are going to have jaggies all over the place since
> the transparency is so simple.
I don't know how you came to this conclusion.
Aliasing is going to be a direct product of the exact image
unless you are jumping resolution. ie: if it is jaggied. to begin with
it will stay jaggied. If it is not jaggied it will not suddenly turn jaggied.
unless there is a difference between the image and display resolution
in the first place.
> Second, on a very
> fast computer with hardware acceleration I can't do
> much better than 35 FPS, which is with only a few
> graphics!
As soon as you use Aplha (work with composites)
values manually you loose acceleration.
For this reason I only use gif images which retain their Alpha
values/acceleration.
> Nevermind the fact that my game will need
> to display many, many more sprites than what I
> display. I have no idea where to go from here.
> Without a better framerate there's no reason to
> o continue to try and use Java2D.
> Any help or ideas on this would be great.
Don't use composites if you want to retain acceleration.
They likely have to be blitted together by the CPU.
(T)
> As soon as you use Aplha (work with composites)
> values manually you loose acceleration.
> For this reason I only use gif images which retain
> their Alpha
> values/acceleration.
Do you know if you lose acceleration if you use pngs that only have bitmask transparency?
> > I'm starting to think that Java2D isn't a API for
> > games. First off, if you are forced to use GIFs
> you
> > are going to have jaggies all over the place since
> > the transparency is so simple.
>
>I don't know how you came to this conclusion.
> Aliasing is going to be a direct product of the exact
> image
> unless you are jumping resolution. ie: if it is
> jaggied. to begin with
> it will stay jaggied. If it is not jaggied it will
> not suddenly turn jaggied.
> unless there is a difference between the image and
> display resolution
> in the first place.
I agree with gibeyl that we are constrained to using only opaque or full transparency if we want hardware acceleration. This means that there can be no translucent pixels in an image on the border between the opaque and transparent pixels > jaggies.
A work around could be to colour the border pixels half the image colour and half a colour that is the average colour of the background, ie anti-alias on a solid background then replace the solid background with transparent.
The full-screen mode is slower than normal mode because of the synchronization with the screen refresh, see Onyx's posts at http://forum.java.sun.com/thread.jspa?threadID=567851&tstart=30
By the way, are your sprites VolatileImages?
Sorry if this is a late response. My sprites are BufferedImages but it really doesn't matter, I've tried them as Volatile and I didn't see any significant benefit or hindrance.
CommanderKeith you made it clear that I can't use multi-level translucency in my images and that's defintely proven by what occurred to me in my code. I would do the background similarity thing but the images I am using need the translucency to look good. It's unfortunate. I had high hopes for Java since I do gui development in it.
Looks like LWJGL is where I'm going with my project next. It'll still be in Java but I was hoping to stay away from libraries since they can hurt cross-platform functionality.
So what is LWJGL? Is it just using the command-line VM option -Dsun.java2d.opengl=Trueor is it a library of Java classes or is it something different?How can it do anti-aliasing faster than normal Java? I'd really like to know because I'm having exactly your
LWJGL is a Java wrapper for OpenGL. So far from what I've seen it will dramatically increase performance although I will still have to jump through some hoops to accomplish what I want.
LWJGL will not result in perfect cross-platform distribution and requires the user to have an OpenGL-compatible 3D video card, but is webstartable. You are drawing in 3D but without a projection and it is relatively easy to do. Everything is on primatives like rectangles and the transparency is super fast.
I've found a good game demo that, although written in C++, should show you what OpenGL can do - http://www.blackskygame.com/. The graphics in this game are top notch and the devs have forums that you can ask how they accomplished certain effects.
The link to LWJGL is http://ww.lwjgl.org/ and there are tutorials and documentation there.
Thanks a lot, I'll look into that.