Archived

This topic is now archived and is closed to further replies.

Hungry Joe

My first double buffer / game loop. In Java

Recommended Posts

OK i''m just trying to create a gameloop with double buffering, which will load a 640x480 image as background. which i have done. however, it flashes periodically and when you move the mouse about. i think paint is drawing to the screen without copying from the buffer but i dont know how to fix it. any help anyone? ps when i have sorted this out i will switch it to use proper buffers and that Game.java ________________________________________________________ package game; import java.awt.*; import java.awt.event.*; import java.awt.Toolkit; public class Game implements Runnable { private Image backBuffer, background; private Graphics offscr; private int width, height = 0; private long time, lastFrameTime = 0; private Thread runThread; private Frame drawFrame; /**Game state */ boolean gameRunning = true; public Game() { // this is where it looks for the 640x480 gif file... background = Toolkit.getDefaultToolkit().getImage("c:/game/back.gif"); if (background == null) {System.out.println("isNull");} drawFrame = new Frame(); drawFrame.setSize(640,480); width = drawFrame.getSize().width; height = drawFrame.getSize().height; drawFrame.setVisible(true); /**Set backbuffer*/ backBuffer = drawFrame.createImage(width, height); offscr = backBuffer.getGraphics(); runThread = new Thread(this); runThread.start(); } public void paint(Graphics g) { g.drawImage(backBuffer, 0, 0, this.drawFrame); } public void run() { System.out.println("entering main loop..."); while (runThread != null) { /**Check time and update*/ lastFrameTime = System.currentTimeMillis() - time; time = System.currentTimeMillis(); //get Input //update game world //draw to buffer drawObjects(); drawFrame.getGraphics().drawImage(backBuffer, 0, 0, this.drawFrame); //draw to screen drawFrame.repaint(); //limit fps?? try { runThread.sleep(5); } catch (InterruptedException e) { ; } } } public void drawObjects(){ //load background offscr.drawRect(10,10,10,10); offscr.drawImage(background, 0,0,Color.black,this.drawFrame); offscr.drawRect(30,10,10,10); } public void update(Graphics g) { } public static void main(String[] args){ Game game = new Game(); } }

Share this post


Link to post
Share on other sites
I would guess you need to overload update(); then that calls paint(); you don''t need to call repaint(); If I recall calling repaint does lead to some ugly tearing and junk.

Share this post


Link to post
Share on other sites
1) Your drawing logic is a bit of a mess, you shouldn''t be calling repaint for a start and you''re not double buffered efficiently either. Grab this file for some sample code. It''s a test program i did for double buffering/active rendering amongst other things and should give you a few pointers.

2) Check out javagaming.org forums as well, theres tons of info there...

Share this post


Link to post
Share on other sites
update() is called by the system when the frame needs to be repainted, so if you "intercept" that it stops all the weird effects. Its kinda wierd but its like update calls repaint calls paint, Kapeesh?

Share this post


Link to post
Share on other sites
thanks everyone you have been a great help. OrangyTang your code is exactly what i was trying to make. Think i may still chop your ups though so i see how it all works. ANd cheers for the clarification on whats going on with the paint/repaint

______________
Live forever or die in the attempt.

Share this post


Link to post
Share on other sites