Manual repaints.

I’m working on an isometric RPG with JOGL, and I want to be able to make updates to the game world independant of framerate. For instance, I’d want to refresh the screen at least 30 times a second, but I can get away with updating the world every 10th of a second.

The problem, of course, is that when I call GLCanvas.refresh(), I have to wait for an unspecified point in time when GLEventListener.refresh() is called, and as that’s called (I assume) from an external thread, I have no idea what state the world will be in by then.
My current workaround is to use a synchronised ‘gateway’ method to ensure that any world update is complete before I begin an attempt to refresh the screen based on that information. Are there any other solutions you’d reccomend?

To clarify, GLCanvas.repaint() obeys the AWT rules (the repaint happens asynchronously, at some future point in time), but GLCanvas.display() executes synchronously. It does not return until the render operation is complete. Maybe this hint will help in your multithreading design.

[quote]…but GLCanvas.display() executes synchronously. It does not return until the render operation is complete. Maybe this hint will help in your multithreading design.
[/quote]
Thank you. That’s what I needed to know.

Oh- on a related note. Is there any easy way I can guarantee that I can call display() in exact sync with the monitor’s refresh rate?

You can ensure that you don’t see tearing by calling GL.setSwapInterval(1) which will cap your framerate to your monitor’s refresh rate on platforms which support the appropriate extensions. We don’t have additional utility methods in this area.

Thanks for that. Is there no way of querying the interval elapsed since the last screen refresh?

Not in the core JOGL APIs. There are many “timer” classes in other libraries that can help with this, or you can use System.nanoTime() in JDK 5. For what it’s worth, I have never personally found the need for this kind of measurement. In my own apps I either repaint the screen only on demand, at a relatively fixed frame rate (using e.g. the FPSAnimator), or as quickly as possible.

Well, there’s a non-trivial CPU cost for rendering to the screen in my case, while world updates could be fairly CPU-intensive themselves, so I don’t want to update ‘as fast as possible’ if the refresh rate is only 60 hz or so.

Conversely, if I set the framerate to something like 25 fps on a 60 hz monitor, since the two won’t be in sync I’ll either see tearing, or miss frames on occasion since the buffer wasn’t ready for display. I think.

Ideally, what I want is a system where I can check if a screen refresh was just completed, then render to the frame buffer, and make no further attempt at rendering until the next screen refresh occurs. Or does the synchronous GLCanvas.display() method not return until buffers have been swapped?

It doesn’t return until the buffers have been swapped, assuming you’re using the default auto-swap-buffer mode.