What is going wrong with my bufferstrategy?

I have implemented bufferstrategy into my game now and I am having some minor troubles. Sometimes when I start the game, everything works fine, but other times I just get a black screen where I can move the mouse. I don't know why it does this. Here is my implementation, is there anything wrong with it? :

while(isRunning){

Graphics2D g = (Graphics2D)strategy.getDrawGraphics();

//get the elapsed time since the last update

long et = System.currentTimeMillis() - lastUpdate;

lastUpdate += et;

//update

r.update(et);

//render

r.render(g);

g.dispose();

strategy.show();

}

[904 byte] By [Ak586] at [2007-9-30 5:59:23]
# 1

OK, I looked into it a little more and I haven't totally figured it out, but I have a better idea now. I put a couple of println statements before and after frame.createBufferStrategy(2) and the program never gets past that point (when it does freeze). About half of the time it works fine and the other half it won't get past the createBufferStrategy(2) call. I tried it on another system of mine and it does the same thing so I imagine it is not a vid card driver problem or anything. Please try it for yourself: download the jar and run it a few times until you get the black screen.

http://andyknotts.maccarey.com/test.jar

Thanks for the help.

Ak586 at 2007-7-1 19:23:09 > top of Java-index,Other Topics,Java Game Development...
# 2
Most of the time I get a black screen. Tested 10 times, and only 3 times it showed a little graphics.P 1.6 - M512 MB Ram64 MB ATI Radion 9000
kohaar1 at 2007-7-1 19:23:09 > top of Java-index,Other Topics,Java Game Development...
# 3

OK, when I ran a debug on it and added a breakpoint at the troubling line, I was given this error message:

An unexpected exception has been detected in native code outside the VM.

Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x6DA7EE6

Function=Java_sun_print_Win32PrintJob_endPrintRawData+0x1D42

Library=E:\Java\j2re1.4.2_03\bin\awt.dll

Current Java thread:

at sun.awt.windows.Win32BackBufferSurfaceData.initSurface(Native Method)

at sun.awt.windows.Win32BackBufferSurfaceData.createData(Unknown Source)

at sun.awt.windows.Win32BackBuffer.createHWData(Unknown Source)

at sun.awt.windows.WVolatileImage.initAcceleratedBackground(Unknown Source)

at sun.awt.image.SunVolatileImage.<init>(Unknown Source)

at sun.awt.windows.WVolatileImage.<init>(Unknown Source)

at sun.awt.windows.Win32BackBuffer.<init>(Unknown Source)

at sun.awt.windows.WComponentPeer.createBackBuffer(Unknown Source)

- locked <0x1053e640> (a sun.awt.windows.WFramePeer)

at sun.awt.windows.WComponentPeer.replaceSurfaceData(Unknown Source)

- locked <0x1053e640> (a sun.awt.windows.WFramePeer)

- locked <0x10524fd8> (a java.awt.Component$AWTTreeLock)

at sun.awt.windows.WComponentPeer.createBuffers(Unknown Source)

- locked <0x1053e640> (a sun.awt.windows.WFramePeer)

at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)

at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)

at java.awt.Component.createBufferStrategy(Unknown Source)

at java.awt.Window.createBufferStrategy(Unknown Source)

at java.awt.Component.createBufferStrategy(Unknown Source)

at java.awt.Window.createBufferStrategy(Unknown Source)

at system.Racer.init(Racer.java:77)

at system.Racer.<init>(Racer.java:63)

at system.Racer.main(Racer.java:42)

Dynamic libraries:

0x00400000 - 0x00407000 E:\Java\j2re1.4.2_03\bin\javaw.exe

0x77F50000 - 0x77FF7000 C:\WINDOWS\System32\ntdll.dll

0x77E60000 - 0x77F46000 C:\WINDOWS\system32\kernel32.dll

0x77DD0000 - 0x77E5D000 C:\WINDOWS\system32\ADVAPI32.dll

0x78000000 - 0x78086000 C:\WINDOWS\system32\RPCRT4.dll

0x77D40000 - 0x77DCC000 C:\WINDOWS\system32\USER32.dll

0x77C70000 - 0x77CB0000 C:\WINDOWS\system32\GDI32.dll

0x77C10000 - 0x77C63000 C:\WINDOWS\system32\MSVCRT.dll

0x08000000 - 0x08138000 E:\Java\j2re1.4.2_03\bin\client\jvm.dll

0x76B40000 - 0x76B6C000 C:\WINDOWS\System32\WINMM.dll

0x10000000 - 0x10007000 E:\Java\j2re1.4.2_03\bin\hpi.dll

0x00820000 - 0x0082E000 E:\Java\j2re1.4.2_03\bin\verify.dll

0x00830000 - 0x00849000 E:\Java\j2re1.4.2_03\bin\java.dll

0x00850000 - 0x0085D000 E:\Java\j2re1.4.2_03\bin\zip.dll

0x02B40000 - 0x02B5C000 E:\Java\j2re1.4.2_03\bin\jdwp.dll

0x06B60000 - 0x06B65000 E:\Java\j2re1.4.2_03\bin\dt_socket.dll

0x71AB0000 - 0x71AC5000 C:\WINDOWS\System32\ws2_32.dll

0x71AA0000 - 0x71AA8000 C:\WINDOWS\System32\WS2HELP.dll

0x71A50000 - 0x71A8B000 C:\WINDOWS\System32\mswsock.dll

0x76F20000 - 0x76F45000 C:\WINDOWS\System32\DNSAPI.dll

0x76FB0000 - 0x76FB7000 C:\WINDOWS\System32\winrnr.dll

0x76F60000 - 0x76F8C000 C:\WINDOWS\system32\WLDAP32.dll

0x76FC0000 - 0x76FC5000 C:\WINDOWS\System32\rasadhlp.dll

0x71A90000 - 0x71A98000 C:\WINDOWS\System32\wshtcpip.dll

0x06D40000 - 0x06E4F000 E:\Java\j2re1.4.2_03\bin\awt.dll

0x73000000 - 0x73023000 C:\WINDOWS\System32\WINSPOOL.DRV

0x76390000 - 0x763AC000 C:\WINDOWS\System32\IMM32.dll

0x771B0000 - 0x772D1000 C:\WINDOWS\system32\ole32.dll

0x5AD70000 - 0x5ADA4000 C:\WINDOWS\System32\uxtheme.dll

0x06EC0000 - 0x06F10000 E:\Java\j2re1.4.2_03\bin\fontmanager.dll

0x51000000 - 0x5104D000 C:\WINDOWS\System32\ddraw.dll

0x73BC0000 - 0x73BC6000 C:\WINDOWS\System32\DCIMAN32.dll

0x5C000000 - 0x5C0C8000 C:\WINDOWS\System32\D3DIM700.DLL

0x06FA0000 - 0x06FA7000 E:\Logitech\MouseWare\System\LgWndHk.dll

0x06FB0000 - 0x06FBD000 E:\rage3dtweak\GameUtil.dll

0x071C0000 - 0x071C7000 E:\Logitech\iTouch\iTchHk.dll

0x07250000 - 0x0725B000 C:\Program Files\Common Files\Logitech\Scrolling\LgMsgHk.dll

0x55900000 - 0x55961000 C:\WINDOWS\System32\MSVCP60.dll

0x07260000 - 0x0727E000 E:\Java\j2re1.4.2_03\bin\jpeg.dll

0x07280000 - 0x072A3000 E:\Java\j2re1.4.2_03\bin\cmm.dll

0x76C90000 - 0x76CB2000 C:\WINDOWS\system32\imagehlp.dll

0x6D510000 - 0x6D58D000 C:\WINDOWS\system32\DBGHELP.dll

0x77C00000 - 0x77C07000 C:\WINDOWS\system32\VERSION.dll

0x76BF0000 - 0x76BFB000 C:\WINDOWS\System32\PSAPI.DLL

Heap at VM Abort:

Heap

def new generationtotal 768K, used 6K [0x10010000, 0x100e0000, 0x104f0000)

eden space 704K,0% used [0x10010000, 0x100118b8, 0x100c0000)

from space 64K,0% used [0x100c0000, 0x100c0000, 0x100d0000)

tospace 64K,0% used [0x100d0000, 0x100d0000, 0x100e0000)

tenured generationtotal 14380K, used 10673K [0x104f0000, 0x112fb000, 0x14010000)

the space 14380K, 74% used [0x104f0000, 0x10f5c5a8, 0x10f5c600, 0x112fb000)

compacting perm gen total 4096K, used 3789K [0x14010000, 0x14410000, 0x18010000)

the space 4096K, 92% used [0x14010000, 0x143c3458, 0x143c3600, 0x14410000)

Local Time = Wed Apr 14 17:45:20 2004

Elapsed Time = 10

#

# The exception above was detected in native code outside the VM

#

# Java VM: Java HotSpot(TM) Client VM (1.4.2_03-b02 mixed mode)

#

# An error report file has been saved as hs_err_pid3396.log.

# Please refer to the file for further information.

#

The error mentions native code, but I didn't think I was using any native code by just creating a bufferStrategy. If you can make some sort of sense out of that I would appreciate it. If you need any more info I will gladly provide it. Thanks.

Ak586 at 2007-7-1 19:23:09 > top of Java-index,Other Topics,Java Game Development...
# 4
Actually I got this error once, but I can't remember what I did wrong. Can you post your game code?
kohaar1 at 2007-7-1 19:23:09 > top of Java-index,Other Topics,Java Game Development...
# 5
Or where you create your bufferstrategy and init fullscreen
kohaar1 at 2007-7-1 19:23:09 > top of Java-index,Other Topics,Java Game Development...
# 6

OK, here is my main class:

public class Racer {

public static final int SCREEN_WIDTH = 800;

public static final int SCREEN_HEIGHT = 600;

private static final boolean FULLSCREEN = true;

private static final int BIT_DEPTH = 16;

private static final int REFRESH_RATE = 60;

private static final int FPS = 60;

//Possible game states to be in

private static final int STATE_RACING = 0;

private static final int STATE_PAUSED = 1;

private static final int STATE_MAINMENU = 2;

private JFrame frame; //the main window

private int state; //the current state of the game

private boolean isRunning; //false if game is not running

private ImageLoader resMan;

//TODO remove:

public static Image road, roadLine, car;

public static void main(String[] args) {

new Racer();

}

public Racer() {

//game window to draw to

frame = new JFrame() {

protected void processKeyEvent(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_ESCAPE) System.exit(0);

}

} ;

frame.addKeyListener(null);

//set it to fullscreen

GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

frame.setUndecorated(true);

frame.setResizable(false);

device.setFullScreenWindow(frame);

device.setDisplayMode(new DisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, BIT_DEPTH, REFRESH_RATE));

resMan = new ImageLoader(device.getFullScreenWindow().getGraphicsConfiguration());

init();

gameLoop();

}

private void init() {

state = STATE_RACING;

isRunning = true;

road = resMan.loadImage("road.png");

roadLine = resMan.loadImage("road_stripe.png");

car = resMan.loadImage("car.png");

frame.createBufferStrategy(2);

}

private void gameLoop() {

BufferStrategy strategy = frame.getBufferStrategy();

//TODO remove:

Race r = new Race(new Track("fdak;"), 4);

Sprite player = new Sprite(car);

player.setX(400-player.getImage().getWidth(null));

player.setY(10000);

player.setDY(-.5f);

r.setPlayer(player);

//long lastUpdate = startTime;

AdvancedTimer timer = new AdvancedTimer();

long sleepTime = AdvancedTimer.getTicksPerSecond()/FPS;

long ticks = 0;

timer.start();

while(isRunning) {

Graphics2D g = (Graphics2D)strategy.getDrawGraphics();

//get the elapsed time since the last update

long et = (timer.getClockTicks() - ticks) / AdvancedTimer.getTicksPerSecond()/1000;

System.out.println((timer.getClockTicks()-ticks)+" / "+AdvancedTimer.getTicksPerSecond());

//update

r.update(et);

//render

r.render(g);

g.dispose();

strategy.show();

timer.sleepUntil(ticks + sleepTime);

ticks += sleepTime;

}

}

}

OK, I hope that helps. Sorry there is a lot of test code (so you will see some strange things in strange places), but I don't think that is causing any problems. Thanks again.

Ak586 at 2007-7-1 19:23:10 > top of Java-index,Other Topics,Java Game Development...
# 7
The code looks ok to me. I read something about a bug with bufferstrategy and fullscreen. Something about the fullscreen mode not beeing excecuted before the bufferstrategy kicks in. This is a long shoot, but try to add a Thread.sleep(500) after switching to fullscreen?
kohaar1 at 2007-7-1 19:23:10 > top of Java-index,Other Topics,Java Game Development...
# 8

Aha! I found that there really is a bug with this. You were right when you said that a delay was needed. I searched the bug database and found this bug:

http://developer.java.sun.com/developer/bugParade/bugs/4500063.html

I added a delay of 1000 and it still did it, but after I put in 5000 it always works now. It takes a little longer to start drawing, but it works. Thanks a lot.

BTW, anyone else who uses bufferstrategy in their games, how did you implement it? Did you have to use this workaround?

Ak586 at 2007-7-1 19:23:10 > top of Java-index,Other Topics,Java Game Development...
# 9
Acutally I use bufferstrategy and fullscreen in my game, and it works without the delay but I can't see much difference between the way you use it, and the way I use it, so I can't help you there. Sorry.
kohaar1 at 2007-7-1 19:23:10 > top of Java-index,Other Topics,Java Game Development...
# 10
BufferStrategy behaves itself far better in 1.5.You should give it a try.
Abuse at 2007-7-1 19:23:10 > top of Java-index,Other Topics,Java Game Development...
# 11
> BufferStrategy behaves itself far better in 1.5.> You should give it a try.Is there any downsides to 1.5 (as it is still in beta)?
Ak586 at 2007-7-1 19:23:10 > top of Java-index,Other Topics,Java Game Development...
# 12

You shouldn't commercially release against it.

However, for development & testing its an absolute must (simply beccause it helps Sun iron out any bugs before release :P)

1.5 Beta2 is about to be released (if it isn't already out)

So I'd guestimate the 1st public release will be in a month or 2 (May/June ish probably)

Abuse at 2007-7-1 19:23:10 > top of Java-index,Other Topics,Java Game Development...