1
votes

I'm working on a game framework inspired by XNA. Almost every day I find a new problem with this or that. Today, it's going from window mode to full-screen and back. The gist of it is:

If I start the game in windowed mode it works fine. When I go full-screen in works fine too. After switching back from full-screen something goes wrong and nothing is drawn on the screen. Going to full-screen again works fine. I just get this problem in windowed mode unless the game started in it.

I set the window this way:

public void setW(){
    jFrame.setVisible(false);
    if(graphicDevice.getFullScreenWindow() != null && jFrame.isDisplayable())graphicDevice.getFullScreenWindow().dispose();
    graphicDevice.setFullScreenWindow(null);
    jFrame.setResizable(false);
    jFrame.setUndecorated(false);               
    jFrame.getContentPane().setPreferredSize(new Dimension(width, height)); 
    jFrame.setVisible(true);        
    jFrame.pack();          
    graphics = (Graphics2D)jFrame.getContentPane().getGraphics();       
    fullScreen = false;
}   

Full-screen is set like this

public void setFS(){
    jFrame.setVisible(false);
    if(jFrame.isDisplayable())jFrame.dispose();     
    jFrame.setResizable(false);
    jFrame.setUndecorated(true);
    graphicDevice.setFullScreenWindow(jFrame);      
    graphicDevice.setDisplayMode(new DisplayMode(width, height, 32, 60));
    graphics = (Graphics2D)jFrame.getContentPane().getGraphics(); // graphicDevice.getFullScreenWindow().getGraphics(); does the same thing
    fullScreen = true;          
}

Then I use this method to draw... deviceManager.getGraphics().draw...(actually i use an intermediary bufferImage) I use a game loop so this happens continuously.

public Graphics2D getGraphics(){
return graphics;
}

Now if I use this method:

public Graphics2D getGraphics(){
if(fullScreen)
return (Graphics2D)graphics;
else
return (Graphics2D)jFrame.getContentPane().getGraphics();
}

I'm sure I'm doing something wrong. Thew windowed mode works, this I know. Why does it go pear-shaped when I return from full-screen. The window just stays gray with nothing being painted on it.

However if I create a method like this:

public void assignGraphics(){
graphics = (Graphics2D)jFrame.getContentPane().getGraphics()
} 

And call it later(one game cycle passed) it fixes the problem. That's why the second mode-switching method works, since it gets Graphics from the JFrame every cycle.

Worked on the problem quite a bit since starting this question and I thing here is the real crux of it: Why can't I get the Graphics for a Window in the same cycle I leave full-screen?

1

1 Answers

3
votes

EDT. How come the problem is always where I'm not looking. Yes, the problem was that my game-loop is swing.timer-based. It's a rookie mistake and the fact, that it took me almost a week to figure it out, came down heavy on my ego.

Apparently many of swing operations go of on the EDT and using it for updating and drawing not only clogged the sucker up, but resulted in this questions' problem.

Now I'm going for variable time-step loop using while() in the main thread. (Even with a basic while loop with only update() and draw() the problem is gone)

Should have at least read one article on game loops before starting on my program. Live and learn(and waste a week being stumped).