Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
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  
  Avoidance of Garbage Collection  (Read 2111 times)
0 Members and 1 Guest are viewing this topic.
Offline Zanthra

Junior Newbie




Java games rock!


« Posted 2005-02-28 02:44:26 »

Is it a good idea to avoid creating a new BufferedImage every time an image is refreshed to avoid java garbage collection, and if so, is it a good idea to have a blank image or blank raster to reset the image to when you want to redraw it?  Is there a better way?  What I have is this.

[CODE]
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/**
* @author Scott
*
*/
public class ScreenManager
{
   private BufferedImage bi;
   private Raster blank;
   
   private BufferStrategy bs;
   
   private int width;
   private int height;
   
   public ScreenManager(int width, int height)
   {
       JFrame screenFrame = new JFrame(String.format("%dx%d", width, height));
       screenFrame.setSize(width+6, height+24);
       screenFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       screenFrame.setResizable(false);
       screenFrame.setVisible(true);
       
       this.width = width;
       this.height = height;
       
       screenFrame.createBufferStrategy(2);
       
       bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       blank = bi.getData();
       
       bs = screenFrame.getBufferStrategy();
       
   }
   
   public void drawPixel(int x, int y, Color c)
   {
       bi.setData(blank);
       //bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       bi.setRGB(x,y,c.getRGB());
   }
   
   public void refresh()
   {
       render(bs.getDrawGraphics(), bi);
       bs.show();
   }
   
   public void render(Graphics g, Image i)
   {
       
       g.drawImage(i, 3, 21, null);
   }
}
[/CODE]

and

[CODE]
import java.awt.*;

/**
* @author Scott
*
*/
public class Main
{
   private int pixelX;
   private int pixelY;
   
   public static void main(String[] args)
   {
       ScreenManager sm = new ScreenManager(320, 240);
       
       for(int i = 0;i < 320; i++)
       {
           if(i == 319) i = 0;
           for(int j = 0;j < 240;j++)
           {
               if(j == 240) j = 0;
               sm.drawPixel(i, j, Color.WHITE);
               sm.refresh();
           }
       }
       
       sm.refresh();
   }
}
[/CODE]
Offline Zanthra

Junior Newbie




Java games rock!


« Reply #1 - Posted 2005-02-28 02:58:44 »

Another thing I was wondering was weather the use of something like a blocking queue to manage FPS is a good idea.

[CODE]
int fps = 30;

LinkedBlockingQueue<Thread> bq = new LinkedBlockingQueue<Thread>(1);

while(!exit)
{
   bq.put(new Thread(updater));
   bq.peek().start();
   Thread.sleep(1000/fps);
}
[/CODE]

Then have the updater somehow remove it'self from the LinkedBlockingQueue.  It prevents more than one updater from running at the same time, but also makes the wait continue while the thread is running.
Offline princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2005-02-28 08:24:59 »

Always attempt to keep hold of images. They're expensive to construct and fill up the heap fast.

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Valodim

Junior Member




*sigh*


« Reply #3 - Posted 2005-02-28 19:09:05 »

clearing & redrawing -> faster than constructing a new image?
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #4 - Posted 2005-03-01 00:19:28 »

yes, much faster.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Valodim

Junior Member




*sigh*


« Reply #5 - Posted 2005-03-01 12:44:51 »

*tries*
Offline whome

Junior Member




Carte Noir Java


« Reply #6 - Posted 2005-03-01 13:15:07 »

1  
2  
3  
4  
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Raster blank = bi.getData();
...clip...
bi.setData(blank);


Hmm. . Ive never thought resetting a bufferedimage state could be done this way.

But then "bi.setData(raster)" method always creates a copy of the given raster, it it remains unchanged for the entire app lifetime. Maybe I should make a small benchmark and see how it goes.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #7 - Posted 2005-03-01 17:20:35 »

Copying a raster into a buffered image has never worked for me, so I just copy the pixels.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #8 - Posted 2005-03-01 17:32:26 »

Don't go mucking around with Rasters or poking pixels. Use a Graphics2D and draw into the image. That way it'll all be nice and hardware accelerated.

...or use LWJGL and learn OpenGL instead Smiley

Cas Smiley

Offline Zanthra

Junior Newbie




Java games rock!


« Reply #9 - Posted 2005-03-01 18:10:17 »

Yeah, I went to useing a Graphics gotten from the BufferStrategy to render the image, I was a little dissapointed that they did not have the option to draw a single pixel, I decided to just use a fillRect(x, y, 1, 1); Worked at a good speed even at high resolutions like 1600 x 1200.  Although now that I am useing sprites, I am having trouble becasue it seems to take a really long time to draw a buffered image to a Graphics.  Is there any way to move the image into the memory on the graphics card so that it can access it quickly?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline whome

Junior Member




Carte Noir Java


« Reply #10 - Posted 2005-03-02 06:11:52 »

BufferedImage.setRGB(int x, int y, int rgbcolor) method might be the one you are looking for.

But true, then you must use "Graphics2D g" for general purpose drawings and plotting a single pixel use a parent bufferedImage instance.

Anyone know why graphics canvas does not have setRGB method.
Offline princec

JGO Kernel


Medals: 379
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #11 - Posted 2005-03-02 08:29:40 »

Because under the hood such a method would probably have to create a Graphics, plot a single pixel, and dispose the Graphics. You can imagine that'd be tragically slow.

Cas Smiley

Offline whome

Junior Member




Carte Noir Java


« Reply #12 - Posted 2005-03-02 15:31:18 »

But graphics has all the drawXYZ methods, why not just an optimized drawPixel(x, y). It looks silly to draw 1 pixel line.
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #13 - Posted 2005-03-02 16:18:36 »

Quote

But graphics has all the drawXYZ methods, why not just an optimized drawPixel(x, y). It looks silly to draw 1 pixel line.


If it were there people would use it, and as a setPixel(x,y) method is the wrong way to approach this problem its a good thing its not there.

If you want per-pixel access, then you want an unmanaged image (i.e. not in vram), and direct access to the images databuffer. (i.e. getRaster().getDataBuffer())

If on the other hand you want HW accelerated blitting, you should never grab an images databuffer. (as this will prevent it being cached in vram)

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

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

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

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

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

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

Tekkerue (50 views)
2014-09-09 02:24:56
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!