Hi,
I’m currently coding a basic test for a multiplayer game and I was having trouble with variable rendering speeds. Sometimes it’d run very smoothly on my desktop, sometimes it would be very slow with the same thing happening on my laptop. Another desktop seemed pretty consistent, low framerate but not really slow. While I was looking for a solution I happened upon the coke and code space invaders tutorial (again) and using that as a base, rewrote my rendering code.
I basically went from this:
JFrame containing JPanel > custom repaint > calls the game rendering function after setting scale etc
to this:
JFrame > Custom rendering class > calls the game rendering function after setting scale etc.
However, I’ve developed a slight problem that I can’t quite work out.
In the game I have a camera which I use to translate the entire Graphics2D object like so:
g.translate( -camera.x, -camera.y );
// Level and objects etc get rendered here
g.translate( camera.x, camera.y );
//UI/Text here
The 2nd version is MUCH smoother but any on screen text that I draw after I reposition the camera doesn’t appear. I did a test where I didn’t move the camera back before the text drawing and it appears, but it stays stuck in place to the top left of the level instead of the top left of the screen.
I did a second test where I quickly reimplemented the JPanel code (uncommenting it all) and suddenly the text draws where it’s supposed to. The only thing I’ve actually changed is what calls the main rendering method.
Here’s the relevant code:
http://pastebin.java-gaming.org/c39a74f5d99
The camera object is just a Rectangle that’s updated each update loop to match the player’s position.
At the end of my update loop which runs in its own thread I call either pnl.repaint(); or renderer.render();
e.g.
public void update() {
// player update
// camera update
// update everything else
//pnl.repaint();
renderer.render();
}
As far as I can see I haven’t done anything massively different between the rendering class and the custom paintComponent method. Any help would be greatly appreciated as I just find this odd given that the only code that doesn’t seem to be working happens in the method that hasn’t changed. Interestingly the g.drawString() calls in the renderer.render() and GamePanel paintComponent() methods both work so it seems to be related to the 2nd translation of the camera.
If you need more code let me know and I’ll add more but I think this is all the code that’s actually relevant to the problem.
Thanks in advance.