Hi.
This is my first thread in this forum. First at all, sorry my bad english (read is easier than write, as can see).
Well, I’m developing a little fps 2d game (like CS2D but much simpler and very different themed).
For it, I use a MVC model and find to make a good object design.
I begin with the model development, and I finished this. Obviously this is the easiest part.
Now, I’m writing the JAVA2D presentation and my head begins to ache.
At this point is where a sync problem makes present.
I know that is one of the most common problems and there is a lot of threads in the forum about it. The alternatives I wrote are based in your help.
The only thing is there are three alternatives with similar performance but I think that where the game grow a lot, the difference will appear.
Then, I need you give me your opinions about what method y should use, and why:
- sync1
- sync2
- sync3
In all the cases, dt is in milliseconds and the main loop is something like:
while (isPlaying) {
boolean modelHasChanged = false;
// get time to eat
long currentTimestamp = Timer.getTimestamp();
timeAccumulator += currentTimestamp - lastLoopTimestamp;
// eat by dt bites
while (timeAccumulator >= dt) {
updateModel(dt / 1000);
timeAccumulator -= dt;
modelHasChanged = true;
}
// generate time...
sync1(modelHasChanged, dt);
// or: sync2(dt, currentTimestamp, ft);
// or: sync3(dt);
// update timers
lastLoopTimestamp = currentTimestamp;
}
And, the view.update is something like:
public void update(...) {
// prepare the off-screen graphic sandbox
Graphics2D g2d = offScreenGraphics.createGraphics();
g2d.setColor(Color.black);
g2d.fillRect(0, 0, width, height);
// draw rectangles, text and things in g2d...
// ...
// to the buffer
Graphics gr = bufferStrategy.getDrawGraphics();
gr.drawImage(offScreenGraphics, 0, 0, null);
// free resources
gr.dispose();
g2d.dispose();
}
public void show(...) {
// BLT
if (!bufferStrategy.contentsLost()) bufferStrategy.show();
}
The three sync methods are:
/**
* Omits update view if not was changed model.
* + Very smooth
* - View has the same update rate that model (model updates with fps frequency).
*/
private void sync1(boolean modelHasChanged, double dt) {
if (modelHasChanged) {
view.update(this);
Thread.yield();
view.show(this);
}
else try { Thread.sleep(1); }
catch (InterruptedException e1) {}
}
/**
* Independent frame rate and model update rate.
* + Independent rates (example: 60fps and dt=30ms)
* - Not very smooth
*/
private void sync2(double dt, long currentTimestamp, long ft) {
view.update(this);
// wait up to the end frame time
long desiredEnd = currentTimestamp + ft;
while (Timer.getTimestamp() <= desiredEnd)
try { Thread.sleep(1); }
catch (InterruptedException e) {}
view.show(this);
}
/**
* Independent frame rate and model update rate.
* + The more smooth, the frame rate is determined by "sync"
* - I don't know.
*/
private void sync3(double dt) {
view.update(this);
Toolkit.getDefaultToolkit().sync();
Thread.yield();
view.show(this);
}
Thanks!