Hi !
Featured games (87)
games approved by the League of Dukes
Games in Showcase (670)
Games in Android Showcase (194)
games submitted by our members
Games in WIP (727)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D performance problems  (Read 2890 times)
0 Members and 1 Guest are viewing this topic.
Offline bison

Senior Newbie

« Posted 2009-03-29 02:27:21 »


Im having troubles getting decent performance out of a simple 2d tilescroller. Java alphablending with  translucent transparency is god awful slow, as expected.
What I want is simple colorkeying, eg defining a transparent color in the source. Couldn't find any direct way of doing that. Instead I read up on bitmask transparency.

Im loading image data from RGBA png files. So to get them ready for blitting I converted them like this:

            BufferedImage original =;
            java.awt.GraphicsEnvironment ge = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
            java.awt.GraphicsDevice gd = ge.getDefaultScreenDevice();
            java.awt.GraphicsConfiguration gc = gd.getDefaultConfiguration();
            BufferedImage bim = gc.createCompatibleImage(original.getWidth(), original.getHeight(), Transparency.BITMASK);
            Graphics2D g2d = (Graphics2D) bim.getGraphics();
            g2d.drawImage(original, 0, 0, null);

To my horror this is for some reason just as slow as Transparency.TRANSLUCENT Sad
I tried reading img data from GIF instead in case Transparency.BITMASK only works for indexed colormodel, with no noticeable speed difference.
Besides i'd still like to able to use 24 color instead of 8 bit, and I don't think thats whats slowing it down either. I get flawless performance on 24 RGB with Transparency.OPAQUE.

What am I doing wrong? seems like java2d is doing per pixel RGBA blitting when I want it to use  BITMASK (colorkeying).

oh and is there anyway of using colorkeying specifying your own color key as a 24 bit RGB value? And can I even assume that the above code auto converts RGBA data from the png to
RGB + transparency bitmask?
Offline CommanderKeith
« Reply #1 - Posted 2009-03-29 03:14:04 »

Try replacing this:
BufferedImage bim = gc.createCompatibleImage(original.getWidth(), original.getHeight(), Transparency.BITMASK);

with this

VolatileImage bim = gc.createCompatibleVolatileImage Image(original.getWidth(), original.getHeight(), Transparency.TRANSLUCENT);

Also, you don't want to create a new image every frame, do it once, then just draw on top of the image every frame.

When using VolatileImages there are some other checks you need to do when displaying them, just google it.

Hope that helps!

Offline bison

Senior Newbie

« Reply #2 - Posted 2009-03-29 03:52:36 »

Thank you for your reply

I've read about volatile images, but I thought they didn't make difference without acceleration, especially under linux where you don't have the directdraw render path.
I don't want to rely on hardware acceleration to archieve a playable framerate, then i'd gone opengl from the beginning.
Just thought java2d was fast enough to blit a tilemap with colorkey transparency to a 800x600 window in software. Anyway i've begun blitting the first layer of tiles as OPAQUE and the rest
as BITMASK. Helps tremendously.
I still can't understand why simple on/off colorkey transparency isn't faster than per pixel alphablending.
An underlying RLE implementation should result in even faster colorkeyed blits than a straight copy.

Also, you don't want to create a new image every frame, do it once, then just draw on top of the image every frame.
Don't insult my intelligence good sir Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder

Medals: 4

« Reply #3 - Posted 2009-03-29 04:08:31 »

What's the size of the tiles? you using bufferstrategy?

In my game I'm not having any trouble, my bottom tiles are all bitmask then I have a overlay over the whole game window for night which is translucent. I thought I would get slow downs, but I tested in Java 5 and 6 without and slow downs running about 6% cpu 30fps with intel graphics. I guess the game window is smaller, but still...

Current project - Rename and Sort
Offline bison

Senior Newbie

« Reply #4 - Posted 2009-03-29 04:31:39 »

yes im using bufferstrategy(2). On a canvas inside a JFrame under linux. Framesize is 800x600 and I render 27x20 = 540 32x32 tiles minimum each frame.
I've got a simple 3 layer setup: draw layer 1 opaque, 2 bitmask, entities (players and bullet) translucent, layer 3 bitmask. This gives me an acceptable framerate (60-80).
When I first posted this I didn't render layer 1 opaque but with bitmask (colorkey) transparency.

I know per pixel alpha is very slow in software but colorkeying should be just as fast or faster even (since you don't have to draw as many pixel as with opaque), thus I cannot understand
why there is such a dramatic difference between OPAQUE and BITMASK.. and such a small one between BITMASK and TRANSLUCENT.
I blit my tiles from one sheet image (an opaque for layer 1 and a bitmask'ed for layers 2-3) using drawImage(dst, 8 ints etc) (and im not unintentionally scaling, since rendering all tiles as opaque nets a 166+ FPS)
Offline DzzD
« Reply #5 - Posted 2009-03-29 10:56:53 »

yes im using bufferstrategy(2). On a canvas inside a JFrame under linux. Framesize is 800x600 and I render 27x20 = 540 32x32 tiles minimum each frame.

ps: you should really think about CommanderKeith advice, you will have then more free CPU for doing others stuff

...Also, you don't want to create a new image every frame, do it once, then just draw on top of the image every frame....

Offline kappa
« League of Dukes »

JGO Kernel

Medals: 103
Projects: 15


« Reply #6 - Posted 2009-03-29 11:22:23 »

if you want performance its recommended you use a java opengl binding, it'll be much faster than java2d. A good library to try is Slick found at it has similar api to java2d so porting your game should be pretty quick.
Offline bison

Senior Newbie

« Reply #7 - Posted 2009-03-29 14:40:31 »

thanks Ill look further into Volatile images. The reason why I was a bit dismissive of the idea was after reading stuff like this:

Eg. Graphics2D will only be accelerated on linux using the OpenGL pipeline.
I went with java2d on purpose, because I want my game to be playable without a 3d card. I have experience using OpenGL for 2d (and 3d for that matter)
using lwjgl as well as using the native library directly from C.
I don't want to rely on acceleration but of course I won't actively hinder it either. Bitmask transparency not being noticeable quicker than translucent transparency just
had me a bit flabbergasted. Guess its because the underlying implementation supports X bit alpha channels and have no optimization for the 1bit alpha channel case.
Figured I might be doing it the wrong way hence this topic.

I apologize if my remark about creating the image every frame came off as arrogant, what I meant was that of course I don't convert it every frame Smiley

Kapta: I've been checking out Slick as well as the rest of Kevin's site. Great stuff and ill love to try it out for a future project but in this case it was a design goal
to stay with software rendering.

I'm used to doing 2d with libSDL and was expecting bitmask transparency to be as fast as opaque blitting (since there is no technical reason it couldn't be).

As of now I'm getting acceptable performance blitting the ground layer opaque and the other layers with bitmask transparency, but as you can see there aren't
that many transparent tiles in the screenshot.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

IanParcs (36 views)
2016-04-18 14:18:53

KaiHH (36 views)
2016-04-18 08:35:41

KaiHH (67 views)
2016-04-15 12:43:58

theagentd (67 views)
2016-04-14 02:16:17

theagentd (74 views)
2016-04-14 02:15:43

IanParcs (67 views)
2016-04-12 03:51:16

IanParcs (37 views)
2016-04-12 03:50:03

IanParcs (33 views)
2016-04-12 03:49:54

IanParcs (30 views)
2016-04-12 03:49:52

IanParcs (39 views)
2016-04-12 03:49:52
Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17

List of Learning Resources
by SilverTiger
2016-02-05 09:39:47

List of Learning Resources
by SilverTiger
2016-02-05 09:38:38

List of Learning Resources
by SilverTiger
2016-02-05 09:35:50

Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!