1

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?

4

1 回答 1

3

美东时间。为什么问题总是在我不看的地方。是的,问题是我的游戏循环是基于 swing.timer 的。这是一个菜鸟的错误,事实上,我花了将近一周的时间才弄明白,这让我的自尊心沉重。

显然,许多摇摆操作都在 EDT 上进行,使用它进行更新和绘图不仅堵塞了吸盘,而且导致了这个问题的问题。

现在我将在主线程中使用 while() 进行可变时间步长循环。(即使使用只有 update() 和 draw() 的基本 while 循环,问题也消失了)

在开始我的程序之前,应该至少阅读一篇关于游戏循环的文章。生活和学习(浪费一周的时间)。

于 2013-04-28T01:26:53.450 回答