使用数组中的 RGB 数据重复填充窗口的最快方法是什么?

发布于 2024-09-30 16:56:37 字数 655 浏览 7 评论 0 原文

我目前正在编写一个简单的游戏。我的图形代码每帧运行一次(大约每秒 30 次),并将 RGB 数据写入具有 640 * 480 = 307200 个条目的数组。

我创建了一个 Win32 窗口,其客户区大小正好为 640 x 480 像素。在空窗口中显示 RGB 数据的最快方法是什么?

窗口需要每一帧更新,所以我需要避免闪烁。我应该为此使用 GDI 吗?或者是否有使用不同库(例如 DirectDraw)的更快方法?

编辑:

感谢您的所有回复。首先,让我排除 Direct2D 作为一个选项 - 我需要支持 Windows XP。其次,我有 3D 背景,因此 Yann Ramin 和 Ben Voigtto 建议的方法 - 使用 Direct3D/OpenGL 和单个纹理四边形 - 是我过去一直这样做的方法。

不过,我有兴趣了解 2D API 是否是更好的解决方案。由于没有明确的答案,我整理了一个测试程序。该代码只是尽可能快地在屏幕上重复显示帧缓冲区。

我已将结果作为 这个后续问题

I'm currently writing a simple game. My graphics code runs once per frame (approximately 30 times per second), and writes RGB data to an array with 640 * 480 = 307200 entries.

I have created a Win32 window with a client area exactly 640 x 480 pixels in size. What's the fastest way to get my RGB data displayed within the otherwise-empty window?

The window will need to be updated every frame, so I need to avoid flickering. Should I use GDI for this? Or is there a faster approach using a different library - say, DirectDraw?

Edit:

Thanks for all the replies. Firstly, let me rule out Direct2D as an option - I need to support Windows XP. Secondly, I have a background in 3D so the method suggested by Yann Ramin and Ben Voigtto - use Direct3D/OpenGL and a single textured quad - is how I've always done this in the past.

However, I was interested to find out whether a 2D API would be a better solution. Since there has been no definite answer, I threw together a test program. The code simply displays a framebuffer on the screen repeatedly, as fast as possible.

I've posted my results as part of this follow-up question.

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

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

发布评论

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

评论(4

只是在用心讲痛 2024-10-07 16:56:37

使用 GDI 最好的方法是使用 SetDIBitsToDevice 直接将 RGB 数据从数组复制到窗口。当然,如果您需要完全消除闪烁,DirectX 是您的最佳选择。

The best you can do using GDI is use SetDIBitsToDevice to directly copy the RGB data from your array to the window. Of course, DirectX is the way to go if you need to completely eliminate flickering.

时光无声 2024-10-07 16:56:37

GDI 函数 SetDIBitsToDevice 可以正常工作。

如剧院所建议的那样,纹理Quad也将起作用,除非它不一定是全屏,并且OpenGL是迄今为止最便携的选项,要实现这一目标,请参见glTexSubImage2D

更直接的方法是 glDrawPixels

不过,理想情况下,您的图形代码将直接使用 GPU 加速渲染到 GPU 内存中,那么您就不会'无需担心将位从系统内存移至 GPU。

The GDI function SetDIBitsToDevice will work fine.

Texturing a quad as suggested by theatrus will work as well, except it doesn't have to be full-screen and OpenGL is by far the most portable option, to make this happen, see glTexSubImage2D

A much more direct way is glDrawPixels

Ideally, though, your graphics code would use GPU-accelerated rendering directly into GPU memory, then you wouldn't need to worry about moving the bits from system memory into the GPU.

挽袖吟 2024-10-07 16:56:37

GDI绝对不是一条路。

最好的跨 Windows 版本方法是使用 Direct3D,并简单地更新单个全屏四边形上的纹理。

Direct2D 是一个新的 API(带有 Service pack 及更高版本的 Win Vista),如果您不需要支持 XP,这是首选机制。 Direct2d 以加速方式处理字体和其他系统级任务。

GDI is absolutely not the way to go.

The best cross-windows-version way is to use Direct3D, and simply update the texture on a single full screen quad.

Direct2D is a new API (Win Vista with Service pack and above) which is the preferred mechanism if you do not need to support XP. Direct2d handles fonts and other system level tasks in an accelerated way.

甜`诱少女 2024-10-07 16:56:37

使用直接绘图。我有一个全屏的《生命游戏》,当我使用 GDI 时,它在像素绘制方面遇到了瓶颈,在我切换到 DirectDraw 后,我开始轻松地输出 60 fps。

Use DirectDraw. I had a full screen Game of Life which was bottle-necked on pixel drawing when I used GDI and after I switched to DirectDraw I started to pump out 60 fps easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文