Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  Drawing an Image is sloooow...  (Read 2109 times)
0 Members and 1 Guest are viewing this topic.
Offline Frost

Junior Newbie

Java games rock!

« Posted 2003-03-28 08:15:22 »

Hi, wonder if anyone got some advice for me.
We are writing a classic 2D top-down view car battle game, like Tank War or wathever but with cars (if you were there back in the days, perhaps you have played Battle Cars Smiley ).

To display the world, we use a tile-based map. Since the map can be much larger than the screen, only a part of the map is rendered at a time. The rendered part is stored in a class called DirectImage, which has a rectangular display area. When the screen scrolls, the display area is moved acordingly. The image is thus slightly larger than the display area.
The class DirectImage implements the ImageProducer interface.
So when I want the image to be drawn to the screen we call:

protected void paintComponent(Graphics gr)
    //long startTime = System.currentTimeMillis();
    //long stopTime = System.currentTimeMillis();
    //System.out.println("Time elapsed in piantComponent: " + (stopTime - startTime));

The startProduction method  in DirectImage looks like:

public void startProduction (ImageConsumer ic) {
     if( !visiblePixelsValid )
  if (consumer!=null)   {
      consumer.setPixels(0, 0, visibleRectangle.width, visibleRectangle.height, model, visiblePixels, 0, visibleRectangle.width); consumer.imageComplete(ImageConsumer.SINGLEFRAMEDONE);

The problem is that the drawImage method seems to take very long time to complete in one out of every two times its called. It alternates between taking like 10 ms to taking up to 80 ms.
Does anyone has any suggestions on what I can do, cause this makes the scrolling look very unsmooth.
My measures suggests that it is the call to consumer.setPixels(...) that takes up most of the time.

If you need to see more of the sourcecode, just let me know OK?


Offline jbanes

JGO Coder

Projects: 1

"Java Games? Incredible! Mr. Incredible, that is!"

« Reply #1 - Posted 2003-03-29 01:34:45 »

The answer is simple. Don't use an ImageProducer/Consumer. The Java API is very slow at direct pixel blitting. If you want it fast, break up your background into managable image tiles, load them using Toolkit.getImage(), and only paint the tiles that are visible. This will take advantage of "Automatic Images" (basically VolatileImages, but with transparency) and speed up your code by 100x or better.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Abuse

JGO Ninja

Medals: 66

falling into the abyss of reality

« Reply #2 - Posted 2003-03-29 13:19:02 »

and if you absolutely must have per-pixel access,
BufferedImage/DataBufferInt is faster than ImageProducer/Consumer.

The only reason you would use your current method, is if you are limited to java1.1.

And if you are limited to the 1.1 api, dont expect anything to be fast :/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Frost

Junior Newbie

Java games rock!

« Reply #3 - Posted 2003-03-30 04:53:46 »

Thanks a lot guys, I've ditched the ImageProducer/Consumer thing and the scrolling is really smooth now! Smiley

Offline afarmand

Innocent Bystander

Java games rock!

« Reply #4 - Posted 2003-04-03 03:51:51 »

Another few things you might wand to concider is 1) If you're using a frame as your window, use full screen exclusive mode with larger dimensions such as 640x480.  (for this you must be using 1.4.0 and up.) 2) If you're going with VolatileImage which is extremely powerful, use framerate sync so that the animation is constant (especially with VolatileImage). Here is the code in the run method of a thread:
public vod run(){
   frame_Start  = 0;
   elapsed_Time = 0;
   Thread t = Thread.currentThread();
   while(t == paint_Timer){
         frame_Start = System.currentTimeMillis();
        elapsed_Time = System.currentTimeMillis() - frame_Start;
             if(elapsed_Time < frame_Rate)
                 paint_Timer.sleep(frame_Rate - elapsed_Time);
         }catch(InterruptedException e) {break;}
Pages: [1]
  ignore  |  Print  

EgonOlsen (74 views)
2018-06-10 19:43:48

EgonOlsen (55 views)
2018-06-10 19:43:44

EgonOlsen (75 views)
2018-06-10 19:43:20

DesertCoockie (254 views)
2018-05-13 18:23:11

nelsongames (154 views)
2018-04-24 18:15:36

nelsongames (154 views)
2018-04-24 18:14:32

ivj94 (895 views)
2018-03-24 14:47:39

ivj94 (156 views)
2018-03-24 14:46:31

ivj94 (808 views)
2018-03-24 14:43:53

Solater (172 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

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 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!