Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  Background Loop causes performance drop  (Read 636 times)
0 Members and 1 Guest are viewing this topic.
Offline Wolfner

Senior Newbie





« Posted 2010-06-30 00:40:41 »

Hi,

I'm working on an auto-sidescroller like R-Type at the moment.
I implemented a simple background picture which scrolls automatically in order to "simulate" the movement.
But then I wanted to add a second layer to the background (stars which are "closer" to the player and scroll faster).
I took a transparent .png pic which had the same size as my background and added it to the paintComponent()-method of the panel on which everything is drawn (i'm using Swing at the moment).
Unfortunately that pretty much "killed" the performance.
So I decided to take a smaller picture of the layer and just let it loop.
I thought: Once the end of the current layer-picture reaches the right edge of the frame (while scrolling to the left) the same picture has to be created at just that position (and once the current picture is completely out of sight it can be deleted in order to avoid trash objects).
So I added the pictures to a Linked List and everytime the gameview draws the frame (respectively has to get the linked list) my algorithm checks the above mentioned conditions and adds/deletes pictures from the list.
This worked much better than the big picture, but now I have the following problem:
Everytime a new picture is created/added to the list the game has a performance drop for a split second.

This is the algorithm where i add the pictures to the linked list.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   
   public LinkedList<GraphicBackground> getLayers(){
     
      if(layers.getLast().getX()+layers.getLast().getWidth()<this.width){
         this.layers.add(new GraphicLayer(1));
         System.out.println("New Layer drawn!");
      }
      if(layers.getFirst().getX()+layers.getFirst().getWidth()<0){
         layers.removeFirst();
         System.out.println("Old Layer removed!");
      }
      return layers;
   }


After this the linked list is given to the draw-method of the gameview, which goes through all objects and draws them.

"layers.getLast().getX()+layers.getLast().getWidth()<this.width" is the moment where the end of the layer-picture reaches the right edge of the frame.
When this line is executed:
this.layers.add(new GraphicLayer(1));
the performance drop occurs.

The GraphicLayer Class (you can ignore the modifier next to the 1245 (which is the frame size)):
1  
2  
3  
4  
5  
6  
7  
8  
public class GraphicLayer extends GraphicBackground {

   public GraphicLayer(int mod) {
      super(1245*mod, 0, "images/layer.png", 10);
     
   }

}


And these are the two super classes:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class GraphicBackground extends GraphicObject {
   File imagepath;
   BufferedImage image;
   int speed;
   int width;
     
   public int getX(){
      return this.x;
   }

   public int getStartingPos(){
      return this.startingpos;
   }
   
   public int getWidth(){
      return this.width;
   }
   
   public GraphicBackground(int x, int y, String imgepth, int speed){
      this.x=x;
      this.y=y;
      this.speed=speed;
     
      imagepath = new File(imgepth);
      try{
         this.image = ImageIO.read(imagepath);
      }catch(IOException e){
         System.out.println("Can't find file!");
      }
      this.width=image.getWidth();
   }
   
   public void draw(Graphics g){
      if(this.x>-10000+1245){
         this.x-=speed;
      }
      g.drawImage(this.image, this.x, this.y, null);
   }
   
}


1  
2  
3  
4  
5  
6  
7  
8  
import java.awt.Graphics;

public abstract class GraphicObject {
   int x;
   int y;
   
   public abstract void draw(Graphics g);
}



Any ideas how i can get rid of this problem?

Cheers
Wolfner

Edit:
Btw the .png-pic only has 18,5kb.

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 50
Projects: 15


★★★★★


« Reply #1 - Posted 2010-06-30 00:47:56 »

if you really want fast performance of sounds and graphics (especially the type expected in bullet hell shmups) then its better you use a proper java games library which has hardware acceleration instead of Swing. Swing isn't really designed for high performance games.

I'd recommend Slick2D, it has very similar api to Java2D so should be very easy to move your code over, you get fast graphics (accelerated by opengl) and fast sound (accelerated by openal).
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #2 - Posted 2010-06-30 00:57:55 »

Regardless of the rendering backend, you should not load compressed images from disk on the rendering thread. Even OpenGL acceleration will have hickups when you use ImageIO.read during rendering.

Create some thread(s) that do the loading of the images in the background, give the threads a low priority.

Make some kind of queueing mechanism, so that you can request an image way ahead of time, and can be reasonably sure the image will be there when you need it. Using the classes in java.util.concurrent will greatly simplify the job.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Wolfner

Senior Newbie





« Reply #3 - Posted 2010-06-30 01:55:33 »

Loading the picture in advance solved the "hickup" problem thanks!
Nevertheless i guess i will also take a look at Slick2D. Thanks for the tip!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (79 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.157 seconds with 20 queries.