如何使用Direct2D从窗口获取像素 [英] How to get pixels from window using Direct2D
问题描述
我的问题是获取窗口中的像素.我找不到执行此操作的方法.我使用标准的Windows函数和Direct2D(不是DirectDraw). 我正在使用新窗口的标准初始化:
My problem is getting the pixels in a window. I can't find a way to do this. I using standard windows functions and Direct2D (not DirectDraw). I am using standard initialization of new window:
WNDCLASS wc;
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(6);
wc.lpszMenuName = 0;
wc.lpszClassName = L"WINDOW"; RegisterClass(&wc);
hWnd = CreateWindow(L"WINDOW", L"Game", WS_OVERLAPPEDWINDOW,100,100,1024,768,NULL,NULL,hInstance,NULL);
然后我创建一个D2D1factory
对象并在窗口中绘制位图:
Then I create a D2D1factory
object and draw the bitmap in the window:
HWND hWnd = NULL;
srand((unsigned int)time((time_t)NULL));
ID2D1Factory* factory = NULL;
ID2D1HwndRenderTarget* rt = NULL;
CoInitializeEx(NULL,COINIT_MULTITHREADED);
My_CreateWindow(&hWnd, hInstance);
My_CreateFactory(&hWnd, factory, rt);
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,&factory);
factory->CreateHwndRenderTarget(RenderTargetProperties(), HwndRenderTargetProperties(hWnd,SizeU(800,600)), &rt);
// tons of code
rt->BeginDraw(); rt->DrawBitmap(background[0], RectF(0,0,800,600));
rt->DrawBitmap(GIFHorse[(int)iterator],
RectF(0+MyX-100,0+MyY-100,100+MyX,100+MyY));
rt->DrawBitmap(Star[(int)iterator], RectF(0 + starX, 0 + starY, 50 + starX, 50+starY)); rt->EndDraw();
我需要计算点击率(我正在尝试制作简单的游戏).这就是为什么我需要访问窗口中的所有像素的原因.
I need to calculate the hits (I am trying to make simple game). So that's why I need access to all pixels in window.
我正在考虑使用其他算法,但是它们很难实现,我想从更简单的方法入手.
I'm thinking about using other algorithms, but they're harder to make and I want to start with something easier.
我了解GetPixel()
函数,但是我不明白应该为HDC
使用什么功能.
I know about the GetPixel()
function, but I cant understand what I should use for HDC
.
推荐答案
没有简单易行的方法来获取D2D像素.
There is no simple and easy way to get to D2D pixels.
可以使用几何图形实现简单的碰撞检测.如果在ID2D1Geometry
对象中具有对象的轮廓,则可以使用
A simple collision detection can be implemented using geometries. If you have outlines of your objects in ID2D1Geometry
objects you can compare them using ID2D1Geometry::CompareWithGeometry
.
If you insist on the pixels, a way to access them is from the DC
obtainable via IDXGISurface1::GetDC
在这种情况下,您需要使用DXGI表面渲染目标和交换链,而不是hwnd渲染目标.
In this case you need to use the DXGI surface render target and the swap chain instead of the hwnd render target.
逐步:
-
D3D11CreateDeviceAndSwapChain
为您提供交换链. -
IDXGISwapChain::GetBuffer
为您提供DXGI表面. -
ID2D1Factory::CreateDxgiSurfaceRenderTarget
为您提供渲染目标. -
IDXGISurface1::GetDC
给您DC.阅读本页上的注释!
这篇关于如何使用Direct2D从窗口获取像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!