Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Is it my code or java2D thats being slow?  (Read 587 times)
0 Members and 2 Guests are viewing this topic.
Offline Embedded

JGO n00b
*

Posts: 34
Medals: 1



« on: 2012-02-07 09:47:53 »

I have a 2D platformer game which runs at set at 60 fps, i only update tiles that are one the screen around the player, so about 12x8 tiles which are 64x64 in size. It runs fine but when i animate the tile by giving them an image they grab from another class, every time the update they tick to see if its time to update their animation if so, they grab a different image but this makes the game lag, the water animation has 3 slides each one is just blue with some different noise.

I also draw the player pixel by pixel and depending on the tiles hes standing on change the RGBA of each pixel to make it cool, e.g. i add transparency when in water so it looks like it.
tl;dr it only really slows down when >60% of the screen are water tiles but i have no idea how else i would implement them :S

Offline appel

JGO Wizard
****

Posts: 1477
Medals: 23


I always win!


« Reply #1 on: 2012-02-07 09:49:10 »

I haven't done Java2D in a long time, but last time I did I remember everything having translucency being dead slow, and anti-aliased/smooth slowing things down as well.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Embedded

JGO n00b
*

Posts: 34
Medals: 1



« Reply #2 on: 2012-02-07 09:54:28 »

I havent done any anti-aliasing so that cant be it, also when i disable the water affecting the player sprite so the player is just drawn normal over the water it is still slow :/
Games published by our own members! Go get 'em!
Offline Cero

JGO Neuromancer
****

Posts: 1050
Medals: 18



« Reply #3 on: 2012-02-07 10:31:27 »

it's slow.
especially for tile maps and the sort, and
Quote
I also draw the player pixel by pixel
sounds bad too for java2d

Offline Embedded

JGO n00b
*

Posts: 34
Medals: 1



« Reply #4 on: 2012-02-07 10:34:46 »

aghh thanks :/ Sad this means im going to have to go lurk the Jogl forum now and buy a new book, gah. Thanks though  Grin
Offline Cero

JGO Neuromancer
****

Posts: 1050
Medals: 18



« Reply #5 on: 2012-02-07 10:38:00 »

aghh thanks :/ Sad this means im going to have to go lurk the Jogl forum now and buy a new book, gah. Thanks though  Grin

well try Slick2D which has a similar API to java2D, using openGL (lwjgl)
libGdx is also possible

Offline Embedded

JGO n00b
*

Posts: 34
Medals: 1



« Reply #6 on: 2012-02-07 10:40:42 »

Yeaa i could, thinking of using doing my A level computing project using JOGL though. Because i could see an examiner who doesn't know what hes talking about thinking "hes using a library! That means he practically didn't do anything himself!"
Offline BoBear2681

Full Member
**

Posts: 238
Medals: 8



« Reply #7 on: 2012-02-07 12:02:19 »

tl;dr it only really slows down when >60% of the screen are water tiles but i have no idea how else i would implement them :S

Just to be sure, when you cycle through water images you're just updating a BufferedImage to point to another, pre-loaded BufferedImage, and not creating a new BufferedImage each time, right?

I couldn't be 100% sure from reading your original post, but are your water tiles created by drawing a translucent water image on top of another image?  If so then I agree with appel that this is likely your problem, since translucent images most likely won't be accelerated.  But if they're fully opaque then your problem is likely elsewhere.
Offline Embedded

JGO n00b
*

Posts: 34
Medals: 1



« Reply #8 on: 2012-02-07 16:48:52 »

Yea all the water tiles access 3 static images in a loader class that are loaded from the start and each cycle through pretty randomly, with the picture changing around every 300ms.

Water tiles are drawn first then player is drawn translucent ontop of the tile, as play is smaller i thought be better to do this way round(less pixels to shade).
So the tiles are drawn fully opaque, but the images that are drawn are basicly 64x64 of randomly bluey coloured pixels, i think this might be whats slowing it down
Offline loom_weaver

Full Member
**

Posts: 224
Medals: 14



« Reply #9 on: 2012-02-07 21:49:29 »

Hmm, I was able to paint about 3000-4000 small translucent images ranging in size from 1x1 to 32x32 in under 20 msec per frame.

I would keep checking because from the numbers you give Java 2D shouldn't be that slow.

I just noticed that you are changing the value of a single pixel.  That definitely is a no-no as it causes an image to become un-managed.  http://www.jhlabs.com/ip/managed_images.html
Games published by our own members! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3155
Medals: 196


I'm the King!


« Reply #10 on: 2012-02-07 22:29:22 »

I just noticed that you are changing the value of a single pixel.  That definitely is a no-no as it causes an image to become un-managed.  http://www.jhlabs.com/ip/managed_images.html
That's an EXCELLENT article that everyone should read.

The last point is also the most important. The BufferedImage returned by ImageIO MUST be turned into a compatible image or else it will not be drawn efficiently. Line 87 shows how to create a BufferedImage and line 107 shows how to copy a BufferedImage into a compatible BufferedImage.

Offline BoBear2681

Full Member
**

Posts: 238
Medals: 8



« Reply #11 on: 2012-02-08 01:02:47 »

That's true; for some reason I was thinking the OP was only manipulating pixels for a single tile (64x64), which shouldn't be a problem.  If you're doing a larger image performance'll drop rapidly.

FWIW, if you do want to edit individual pixels, *and* you're doing it on an image small enough that you're OK with it being unmanaged, be sure to do it the "right" way (i.e., most performant):

http://www.java-gaming.org/topics/fastest-image-writing/24620/view.html

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.099 seconds with 19 queries.