Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 1313 times)
0 Members and 1 Guest are viewing this topic.
Offline Frost

Junior Newbie




Java games rock!


« Posted 2003-03-28 09: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();
    bitImg.flush();
    gr.drawImage(bitImg,0,0,null);
    //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) {
    addConsumer(ic);        
     if( !visiblePixelsValid )
      validatePixels();
  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?

Regards,
Anders


Offline jbanes

JGO Coder


Projects: 1


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


« Reply #1 - Posted 2003-03-29 02: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 Coder


Medals: 11


falling into the abyss of reality


« Reply #2 - Posted 2003-03-29 14: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 :/

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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 06:53:46 »

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

Regards,
Anders
Offline afarmand

Innocent Bystander




Java games rock!


« Reply #4 - Posted 2003-04-03 05: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();
        repaint();
        elapsed_Time = System.currentTimeMillis() - frame_Start;
        try{
             if(elapsed_Time < frame_Rate)
                 paint_Timer.sleep(frame_Rate - elapsed_Time);
             else
                 paint_Timer.sleep(3);
         }catch(InterruptedException e) {break;}
     }
}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (37 views)
2014-07-18 06:55:21

Zero Volt (35 views)
2014-07-17 23:47:54

danieldean (28 views)
2014-07-17 23:41:23

MustardPeter (31 views)
2014-07-16 23:30:00

Cero (46 views)
2014-07-16 00:42:17

Riven (47 views)
2014-07-14 18:02:53

OpenGLShaders (36 views)
2014-07-14 16:23:47

Riven (36 views)
2014-07-14 11:51:35

quew8 (32 views)
2014-07-13 13:57:52

SHC (69 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!