【发布时间】:2010-06-07 00:38:59
【问题描述】:
我正在尝试编写一个程序来玩全屏 PC 游戏以获得乐趣(作为计算机视觉和人工智能的实验)。
对于这个实验,我假设游戏没有用于 AI 玩家的底层 API(也没有可用的源),所以我打算处理游戏在屏幕上呈现的视觉信息。
游戏在win32系统上以全屏模式运行(我假设是direct-X)。
目前我使用的是win32函数
#include <windows.h>
#include <cvaux.h>
class Screen {
public:
HWND windowHandle;
HDC windowContext;
HBITMAP buffer;
HDC bufferContext;
CvSize size;
uchar* bytes;
int channels;
Screen () {
windowHandle = GetDesktopWindow();
windowContext = GetWindowDC (windowHandle);
size = cvSize (GetDeviceCaps (windowContext, HORZRES), GetDeviceCaps (windowContext, VERTRES));
buffer = CreateCompatibleBitmap (windowContext, size.width, size.height);
bufferContext = CreateCompatibleDC (windowContext);
SelectObject (bufferContext, buffer);
channels = 4;
bytes = new uchar[size.width * size.height * channels];
}
~Screen () {
ReleaseDC(windowHandle, windowContext);
DeleteDC(bufferContext);
DeleteObject(buffer);
delete[] bytes;
}
void CaptureScreen (IplImage* img) {
BitBlt(bufferContext, 0, 0, size.width, size.height, windowContext, 0, 0, SRCCOPY);
int n = size.width * size.height;
int imgChannels = img->nChannels;
GetBitmapBits (buffer, n * channels, bytes);
uchar* src = bytes;
uchar* dest = (uchar*) img->imageData;
uchar* end = dest + n * imgChannels;
while (dest < end) {
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest += imgChannels;
src += channels;
}
}
使用这种方法处理帧的速度太慢了。有没有更好的获取屏幕帧的方法?
【问题讨论】:
-
可能想要将 3d 或游戏添加到您的元标记中
-
我认为根本问题比我的特定应用程序更普遍(恰好是等距 RTS 游戏)
标签: winapi screen-scraping real-time