Results 1 to 7 of 7
- 05-20-2012, 09:39 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Drawing Thousands of pixels from array
I am trying to make a simple game where you can draw sand that falls, however when I draw the pixels I am suffering a lot of lag.
Any help would be much appreciated!
(Its fine up until about 500 pixels)
Java Code://Paint draw method int i = 0; while(particles[i] != null){ if(particles[i].getType().equals("air") == false) particles[i].draw(g2d); i++; }Java Code://Particle draw method public void draw(Graphics g2d){ g2d.setColor(color); g2d.fillRect(x, y, 1, 1); }
- 05-20-2012, 10:25 AM #2
Re: Drawing Thousands of pixels from array
One optimization that's immediately evident is that you could set the color before the loop, not at every pass through the draw(...) method.
But the real bottleneck here is likely to be the String equality test, and possibly in the getType() method, if it's non-trivial. Why don't you subclass and give the "air" type particles a draw(...) method that does nothing?
Also note that it's never needed to compare a boolean expression with a boolean literal.dbJava Code:// if(particles[i].getType().equals("air") == false) if(!particles[i].getType().equals("air"))Why do they call it rush hour when nothing moves? - Robin Williams
- 05-20-2012, 12:37 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Drawing Thousands of pixels from array
Thanks, that did help with the lag a little but I found out the lag was mostly coming from my utterly shockingly bad method of moving the particles which involved going through several thousand, thousand iteration long while loops, I am now re-writing it.
- 05-21-2012, 04:53 PM #4
Re: Drawing Thousands of pixels from array
So another thing:
...is extremely slow. It's many many times faster to simply set the color of a BufferedImage directly using setRGB(x, y, rgb);Java Code:g2d.fillRect(x, y, 1, 1);
Once all the drawing is done for a given frame, then just draw the whole buffered image. You're exploring pixel simulations which are very taxing on software based rendering systems (like most of java2d). I have written many, and have found lots of small optimizations like the one above can make a huge difference.
In the end, the single biggest thing that improved performance was to switch to OpenGL. I played with both JOGL and LWJGL and prefer the latter. I had frame rates go from about 10fps (after heavy optimization) to around 190fps when working with around a half million particles.
Another thing that tends to be quite slow depending on what you're doing is collision detection. I was doing some bouncing ball simulations which had a few hundred balls, and the collision detection alone was causing something like 1.5 million checks per second. No matter how well I optimized the detection code, the number of checks was so great that it caused massive lag. The solution there was to implement a QuadTree data structure to hold the balls, and use it to minimize the number of checks (1,500,000 per second to about 235 per second). In other words, I reduced the complexity of the check from O(n^2) to O(log(n)).
Your case probably isn't as bad since you aren't bouncing every grain of sand off of every other grain, but you can reduce the complexity of collision with solid objects by doing targeted detections instead of brute force. Hope this helps!
- 05-21-2012, 11:29 PM #5
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
Re: Drawing Thousands of pixels from array
Im actually doing something quite like this, and I got a major speedup by not drawing cells that are the same as the last time they were drawn.
Last edited by lolmister; 05-21-2012 at 11:42 PM.
- 05-23-2012, 12:20 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Drawing Thousands of pixels from array
Thanks, using a BufferedImage sped it up considerably and I'm going to have a look at LWJGL, any good tutorial websites that you know of?
- 05-23-2012, 03:01 PM #7
Re: Drawing Thousands of pixels from array
The LWJGL website itself has some tutorials. I did the space invaders one back in the day. You really only need to know enough to get a 2D canvas and how to draw simple polygons on it. If you can get that far, you can mimic what java2d does, but, it will be hardware accelerated!
D
OpenGL is tricky to get started with, so be patient and give it a week of playing around with. Let us know how it works out!
Similar Threads
-
how to present thousands, hundreds, tens, and ones ...
By smasm in forum New To JavaReplies: 4Last Post: 09-19-2011, 06:15 AM -
What is the most efficient data structure for storing possibly upto thousands of ...
By oontvoo in forum Advanced JavaReplies: 4Last Post: 08-05-2011, 04:10 PM -
drawing scattered plot from 2D array
By yeahwa in forum Java 2DReplies: 16Last Post: 06-07-2010, 08:38 PM -
Drawing 3d graph from array table
By Mekonom in forum Java 2DReplies: 3Last Post: 12-26-2009, 09:42 AM -
get image file from array of pixels
By mester in forum Advanced JavaReplies: 1Last Post: 12-28-2008, 10:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks