First off, I’m new - sorry if I missed an introduction thread or an FAQ or something.
I’m having a very strange issue with BufferStrategy and getDrawGraphics().
I’m working on a tile based game, and I wanted to add a background. While the background by itself drew fine, and the tiles by themselves drew fine, having tiles on a background caused a lot of flickering. I moved from Graphics to Grahpics2D, which shaved my frame redraw time from ~170MS to ~60MS, but there was still a noticeable flicker. After poking around the internet, I realized that even though I was using createBufferStrategy(2) (drawing on a Canvas), I need to use getDrawGraphics() and not getGraphics(). However, after adding that my game crashes immediately after trying to render anything, or if it doesn’t crash simply doesn’t draw anything (and Windows says that the display driver stopped responding and recovered).
This is the gist of the error (I omitted the whole file because it was just loaded dynamic libraries and such).
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d0b7521, pid=4000, tid=4784
#
# JRE version: 6.0_18-b07
# Java VM: Java HotSpot(TM) Client VM (16.0-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# C [awt.dll+0xb7521]
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Here’s my main game loop (with non-graphics code trimmed as I don’t think you’re interested in my awful world generation ;))
public void run(){
...
Graphics2D g = null;
try{
g = (Graphics2D)gui.bufferer.getDrawGraphics();
while(true){
gui.drawPlanet(p, g);
...
}
} finally{
g.dispose();
...
}
}
drawPlanet:
int bx = p.getPlayerPosition()[0] - 32;
int by = p.getPlayerPosition()[1] - 21;
g.setColor(Color.BLACK);
g.fill(new Rectangle(0, 0, getSize().width, getSize().height)); //I probably should prealloc this I know
for(int i = 42; i > 0; i--){
for (int j = 0; j < 64; j++){
int tile = p.worldData[bx + j][by + i];
Tile t = Tile.tiles[tile];
if (tile != 0){
int index = t.sIndex;
drawTile(j, i, index, g);
}
}
}
drawTile:
g.drawImage(sprites[index], x*16, y*16, 16, 16, this);
I’m new to Java2D, so this is probably something blindingly stupid, but thanks for looking at this.