AWT 快速图形处理线程安全

发布于 2024-11-09 21:26:03 字数 532 浏览 0 评论 0原文

我正在将一个非常旧的 AWT 游戏移植到一个非常旧的新设备上。

该游戏存在很多问题,包括非常宽松的线程安全方法:游戏引擎试图使用从 UI 线程获得的图形上下文在其引擎线程中直接绘制到屏幕上。这在设备上不起作用。

我已经设法让引擎线程绘制到离屏缓冲区上,然后让 UI 线程定期调用显示组件上的 repaint() 并让显示组件将缓冲区传输到屏幕上,从而使其正常工作,但性能糟糕——考虑到所有的上下文切换和双缓冲,这并不奇怪。

我实际上并不是一个知识渊博的 AWT 程序员;这实在是太可恨了,以至于我至今都在回避它。但是这个问题——有一个引擎线程想要在屏幕上绘制——一定是一个常见的问题。有谁知道如何以安全的方式执行此操作以尽可能地提高系统性能的任何体面策略(最好是示例代码!)?

(我特别喜欢的是一个安全的快捷方式,允许引擎线程在感觉准备好时直接渲染到屏幕图形上下文上,从而避免必须告诉 UI 线程请求重绘。这会让我拿出一整层双缓冲。但我不知道这样的事情是否可能......)

这都是在PBP 1.1.2上——是的,它既不是完整的Java也不是诚实的MidP......

I'm porting a really old AWT game to a really naff new device.

The game has a whole bunch of things wrong with it, including a very lax approach to thread safety: the game engine is trying to draw directly onto the screen in its engine thread using a graphics context it got from the UI thread. This doesn't work on the device.

I've managed to hack it into working, by having the engine thread draw onto an off-screen buffer and then have the UI thread periodically call repaint() on the display component and the display component blitting the buffer onto the screen, but performance sucks --- not surprising given all the context switches and double buffering.

I'm not actually a particularly knowlegable AWT programmer; it's sufficiently hateful that I've avoided it up to now. But this problem --- having an engine thread want to draw onto the screen --- must be a common one. Does anyone know of any decent strategies (and preferably example code!) of how to do this in a safe manner that squeezes as much performance out of the system as possible?

(What I'd particularly like is a safe shortcut to allow the engine thread to directly render onto the screen graphics context when it feels ready to do so, and so avoid having to tell the UI thread to request a redraw. That will let me take out a whole layer of double-buffering. But I don't know whether such a thing is possible...)

This is all on PBP 1.1.2 --- yes, it's neither full Java nor honest MidP...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-11-16 21:26:04

使用游戏画布可能会有所帮助。它允许在游戏循环中完成绘画,因此您不需要双缓冲。另一种更粗暴的方法是使用 立即绘制()。它将强制 GUI 重新绘制。

//Gui
public void update(/*may want to pass the shapes to paint*/)
{
    paintImmediately(this.getGrphics());    // assuming 'this' is a jpanel
}


//Game loop
public void gameLoop()
{
    // collision detection etc
    gui.update();
}

Using a game canvas might help. It allows the painting to be done in the game loop, so you wont need double buffering. Another more crude approach is to use paintImmediately(). It will force the gui to repaint.

//Gui
public void update(/*may want to pass the shapes to paint*/)
{
    paintImmediately(this.getGrphics());    // assuming 'this' is a jpanel
}


//Game loop
public void gameLoop()
{
    // collision detection etc
    gui.update();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文