Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (496)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 848 times)
0 Members and 1 Guest are viewing this topic.
Offline Wolfner

Senior Newbie





« Posted 2010-06-29 22: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.

Click to Play
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #1 - Posted 2010-06-29 22: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: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2010-06-29 22: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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Wolfner

Senior Newbie





« Reply #3 - Posted 2010-06-29 23: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!

Click to Play
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.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (33 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (80 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (46 views)
2014-09-07 01:13:42

Longarmx (33 views)
2014-09-07 01:12:14

Longarmx (37 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!