如何在 Windows 游戏上叠加图形?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3266346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to overlay graphics on Windows games?
提问by rup
I want my program to be able to launch any Windows game, and while the user is playing it, intermittently display some text or pictures in some part of the game window. The game may be in windowed or full-screen mode. From what I have been able to figure out from online resources, this could be done using a graphics library that supports overlays and using Windows Hooks to keep track of the target application's window. In this context I have some questions.
我希望我的程序能够启动任何 Windows 游戏,并且在用户玩游戏时,在游戏窗口的某些部分间歇性地显示一些文本或图片。游戏可能处于窗口或全屏模式。从我从在线资源中了解到的情况来看,这可以使用支持覆盖的图形库并使用 Windows Hooks 来跟踪目标应用程序的窗口来完成。在这种情况下,我有一些问题。
- Will the overlays affect the game's performance?
- How will hooking the application affect performance?
- Is there any other way one could achieve this? For example, how do you think PIX, the DirectX debugging and analysis tool, work?
- 叠加层会影响游戏的性能吗?
- 挂钩应用程序将如何影响性能?
- 有没有其他方法可以实现这一目标?例如,您认为DirectX 调试和分析工具PIX 是如何工作的?
回答by Alan
Frapsis the archetypal example of doing this sort of thing to a fullscreen DirectX application from a third-party app. It works by hooking some system calls and inserting itself into the call-chain between an app and DirectX. There is some performance hit, but in general its minimal.
Fraps是从第三方应用程序对全屏 DirectX 应用程序执行此类操作的典型示例。它通过挂钩一些系统调用并将自身插入应用程序和 DirectX 之间的调用链来工作。有一些性能损失,但总的来说它是最小的。
This pageseems to have some details and sample code on how to hook the app in this way.
这个页面似乎有一些关于如何以这种方式挂钩应用程序的详细信息和示例代码。
If I recall correctly, from other forum discussions (can't find the link at the moment. search for things like "how does fraps work", it's a popular question), Fraps hooks a few things to force the app to load its DLL, then hooks Present() calls and executes a device->Clear() call before calling the real Present(), with a list of small rectangles to set to a different color, which can spell out the FPS number that it displays. This has a minimal performance impact and is widely compatible with whatever rendering the app is doing. Overlaying a bitmap would be more complicated since it wouldn't be as easy to do at Present-time. Perhaps if you could hook EndScene, then you could do more, but you would have to be careful to not change the device state.
如果我没记错的话,从其他论坛讨论中(目前找不到链接。搜索诸如“fraps 是如何工作的”之类的内容,这是一个流行的问题),Fraps 会挂钩一些东西以强制应用程序加载其 DLL ,然后在调用真正的 Present() 之前钩住 Present() 调用并执行 device->Clear() 调用,并带有一个小矩形列表以设置为不同的颜色,这可以拼出它显示的 FPS 数字。这对性能的影响最小,并且与应用程序正在执行的任何渲染广泛兼容。叠加位图会更复杂,因为在现在做起来并不容易。也许如果您可以挂钩 EndScene,那么您可以做更多的事情,但是您必须小心不要更改设备状态。
PIX has privileged access to the DirectX driver, so I wouldn't expect to be able to use that as a model to emulate.
PIX 拥有对 DirectX 驱动程序的特权访问,因此我不希望能够将其用作模型进行模拟。
If an the target app is running in windowed mode, hooking DirectX still work, but you could also just use GDI instead.
如果目标应用程序在窗口模式下运行,挂钩 DirectX 仍然有效,但您也可以只使用 GDI。
Edit:I think thisis the link I was originally thinking of.
编辑:我认为这是我最初想到的链接。