Re: Requesting tips, comments for an EDT thread-safe game architecture



Oliver Wong wrote:
"Oliver Wong" <owong@xxxxxxxxxxxxxx> wrote in message news:soxMh.490$NM.6958@xxxxxxxxxxxxxxxxxxxxxxx
[most of the code snipped]
/*
* This while loop is the main game loop. It basically iterates through
* 3 stages forever: getting the player input, reacting to it, and
* drawing the results on screen.
*/
while (!timeToQuit) {
getPlayerInput();
processGameLogic();
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
if (mainWindow != null) {
BufferStrategy strategy = mainWindow
.getBufferStrategy();
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
Insets insets = mainWindow.getInsets();
g.translate(insets.left, insets.top);
g.setTransform(AffineTransform
.getScaleInstance(
(double) (mainWindow.getWidth() - insets.right)
/ (double) DEFAULT_RENDERING_WIDTH,
(double) (mainWindow
.getHeight() - insets.bottom)
/ (double) DEFAULT_RENDERING_HEIGHT));
updateScreen(g);

Oops, I should probably call g.dispose() here.

strategy.show();
}
}
});
Thread.sleep(1);
}

- Oliver



Oliver:

For active rendering I would take your rendering out of the EDT altogether. The problem that you are going to run into is that eventually too much will be happening on the EDT and you won't be able to do any user input processing or that it will delay your rendering. You only need to create the BufferStrategy once not every time in the rendering loop. I like to use java.util.Timer to drive the loop but you can use Thread.sleep(). As Daniel said I would do my user input event driven. You can use a Frame or JFrame but you have to deal with the insets and title bar your self. If you use an undecorated frame you might as well use a window.

Below is how I would do it. See if you like any of it.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

public class test4 extends JWindow {
java.util.Timer timer;
BufferStrategy bs;
int x,y,deltaX=3,deltaY=3;

public test4() {
// you want to do all drawing
setIgnoreRepaint(true);

addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent we) {
// don't create the buffer strategy
// until window is visible
test4.this.createBufferStrategy(2);
bs = getBufferStrategy();
// schedule animation
timer.schedule(new AnimationTask(),0,30);
}
});

timer = new java.util.Timer();

setSize(400,300);
setVisible(true);
}

class AnimationTask extends TimerTask {
public void run() {
x += deltaX;
y += deltaY;
if (x > getWidth() - 30 || x < 0)
deltaX = -deltaX;
if (y > getHeight() - 30 || y < 0)
deltaY = -deltaY;
render();
}
}

void render() {
do {
do {
// get new draw graphics every time
Graphics g = bs.getDrawGraphics();
// draw
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLUE);
g.fillOval(x,y,30,30);
// dispose of the draw graphics
g.dispose();
} while (bs.contentsRestored()) ;
// blit/flip buffers
bs.show();
} while (bs.contentsLost()) ;
}

public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
test4 t4 = new test4();
t4.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.exit(0);
}
});
}
};
EventQueue.invokeLater(r);
}
}

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: AWT event model problem
    ... I followed your suggestions and initialized the Graphics g inside each event ... However the FIRST TIME i left click and hold the mouse down, ... public void paint ... public void mouseEntered(MouseEvent e) { ...
    (comp.lang.java.gui)
  • How to convert GUI components graphics to int[]or byte[]?
    ... Has anyone ran into the problem of trying to get a GUI component's graphics converted into an array of ints? ... private TestGraphicPanel testPanel; ... public void windowDeactivated ...
    (comp.lang.java.gui)
  • Re: Graphics2D question.
    ... All the examples I have read so far for Image stuff in Java have everything stuffed into one monolithic class, and the crux of the rendering seems to be done in paintor paintComponentusing a g2d object cast againg the parameter g. ... public void paintComponent{ ... The frame to hold your display component need be no more complicated than the one I show here. ... add those to the frame but have the display component implement ActionListener. ...
    (comp.lang.java.gui)
  • Re: AffineTransform.getScaleInstance question
    ... and the way i defined my polygon. ... I want to scale the shape so that it seems ... It is if you create a Graphics from an Image ... public void mousePressed{ ...
    (comp.lang.java.gui)
  • Re: Code efficiency - declare variable sinside or outdie a loop.
    ... > vraible inside a loop or outside a loop in Java. ... > public void outside{ ... > inside the loop is 2 opcodes shorter The first two opcodes in outside ... Always declare variables in as ...
    (comp.lang.java.programmer)