Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
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  
  Horrendous Performance in a Simple Java2D App  (Read 4470 times)
0 Members and 1 Guest are viewing this topic.
Offline ido

Junior Devvie





« Posted 2009-04-20 09:38:20 »

Hi all,

I've just finished my entry for the 14th Ludum Dare 48-hours game making competition yesterday, and decided to do it in java using java2d for the graphics.

I'm not that familiar with the API and haven't done a lot of graphics programming, but my game is quite small (only a dozen or so very small moving object) so I assumed I could program it naively and still encounter no performance problems.

Needless to say, I was wrong.  The game performs alright most of the time but once there are a bit too many 'enemies' moving around on the screen or the resolution is cranked up too high it start getting visibly slower.

I've determined the performance bottleneck to be the screen drawing functions, when those are commented out the game is very fast.

Could someone give me a heads up on what I might be doing wrong here?  The (very short) source code is located here with most of it the Main class, with the usual suspects being the draw() function that is called in the inner game loop.

Thanks in advance,
Ido.

Offline moogie

JGO Ninja


Medals: 16
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2009-04-20 12:33:10 »

it looks like you are trying to redraw the Jframe each game tick... you probably want to use a bufferstrategy and draw to the back buffer and then flip it to the front.

Java4k RIP 2014
Offline ido

Junior Devvie





« Reply #2 - Posted 2009-04-20 14:08:45 »

it looks like you are trying to redraw the Jframe each game tick... you probably want to use a bufferstrategy and draw to the back buffer and then flip it to the front.

But if you look at line 337 you see that I already use the strategy to update the screen. 

Did I understand you wrong?

Could it also be because I reuse the same graphics instead of disposing it and getting a new graphics context every time?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #3 - Posted 2009-04-20 16:23:51 »

In general, there's no point in using swing if you use your own buffer strategy.

In drawPolygonBody you seem to be creating new polygons on each frame. Is that necessary? Did you check the rate of garbage being generated (-verbose:gc or use VisualVM/VisualGC)?

1  
2  
3  
4  
    graphics.drawLine((int) v1.x, (int) v1.y, (int) v2.x, (int) v2.y);
    graphics.drawLine((int) v2.x, (int) v2.y, (int) v3.x, (int) v3.y);
    graphics.drawLine((int) v3.x, (int) v3.y, (int) v4.x, (int) v4.y);
    graphics.drawLine((int) v4.x, (int) v4.y, (int) v1.x, (int) v1.y);


So you transform the points yourself and then draw the rectangle? Have you tried just setting a transform and doing a drawRect? With latest java versions that should be much faster.

Dmitri

Offline ido

Junior Devvie





« Reply #4 - Posted 2009-04-20 18:54:50 »

Thanks for the answers!

In general, there's no point in using swing if you use your own buffer strategy.

I'm sorry if that's a dumb question, but where did I use swing?  Do you mean defining it as extending JFrame? What should I use instead?

Quote
In drawPolygonBody you seem to be creating new polygons on each frame. Is that necessary? Did you check the rate of garbage being generated (-verbose:gc or use VisualVM/VisualGC)?

OK, I'll wrap the bodies in a class that also saves the polygons created upon instantiation. 

Quote
1  
2  
3  
4  
    graphics.drawLine((int) v1.x, (int) v1.y, (int) v2.x, (int) v2.y);
    graphics.drawLine((int) v2.x, (int) v2.y, (int) v3.x, (int) v3.y);
    graphics.drawLine((int) v3.x, (int) v3.y, (int) v4.x, (int) v4.y);
    graphics.drawLine((int) v4.x, (int) v4.y, (int) v1.x, (int) v1.y);


So you transform the points yourself and then draw the rectangle? Have you tried just setting a transform and doing a drawRect? With latest java versions that should be much faster.


I actually did but I ran into some problems & just reverted back to doing it this way.  I'll check it out again.

Thanks again Dimitri.

Offline ido

Junior Devvie





« Reply #5 - Posted 2009-04-20 20:39:16 »

2 more questions:

1. do you have any idea why it would sometimes be sluggish on key input?
I.e. it would wait a noticeable fraction of a second between key press and the action.

2. sound playing is also sometimes delayed by a small but evident amount of time.

-Ido.

Offline CommanderKeith
« Reply #6 - Posted 2009-04-21 09:12:13 »

Hi,

What about this line which is in your game loop:

Thread.sleep(25);

With such a big sleep you can only get 40fps maximum without drawing anything. Make it sleep for just 1ms and everything will probably be fine.

Once you do that, if there's still performance problems you should profile using this VM option: -Xprof

Good luck,
Keith


Offline ctrl-alt-dev

Senior Newbie




Today, my bedroom. Tomorrow the World !


« Reply #7 - Posted 2009-04-22 05:44:13 »

If you're running on linux with intel graphics and don't have 'UseFBDev' set to 'true' in xorg.conf this may help:

-Dsun.java2d.pmoffscreen=false

(add it as an addition argument on the commandline, or as VM argument in your IDE).

Also have a look at this : http://java.sun.com/products/java-media/2D/perf_graphics.html.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
Ecumene (51 views)
2017-09-30 02:57:34

theagentd (76 views)
2017-09-26 18:23:31

cybrmynd (183 views)
2017-08-02 12:28:51

cybrmynd (181 views)
2017-08-02 12:19:43

cybrmynd (188 views)
2017-08-02 12:18:09

Sralse (196 views)
2017-07-25 17:13:48

Archive (747 views)
2017-04-27 17:45:51

buddyBro (880 views)
2017-04-05 03:38:00

CopyableCougar4 (1429 views)
2017-03-24 15:39:42

theagentd (1319 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org 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‑gaming.org
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!