Direct2D Gauges

From FSDeveloper Wiki
Jump to: navigation, search

Despite FSX being a DirectX 9 program and Direct2D being part of DirectX 10/11, it is possible to use it as a gauge drawing method in conjunction with GDI/GDI+(In other words, you can mix the 2) in FSX. Here is how it is done:

First of all, define the following pointers along with any other pens/brushes/bitmaps or other D2D objects you want to use. These 2 are vital though:

ID2D1Factory* pD2DFactory = NULL;
ID2D1DCRenderTarget* pRT = NULL;

In your PANEL_SERVICE_CONNECT_TO_WINDOW (or gauge class constructor if using the ESP example as a template), initialize your ID2D1Factory object and your ID2D1DCRenderTarget as well as initializing any other objects:

//Initialize the ID2D1Factory object
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
//initialize the ID2D1DCRenderTarget
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(
	DXGI_FORMAT_B8G8R8A8_UNORM,
	D2D1_ALPHA_MODE_PREMULTIPLIED),
	0,
	0,
	D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE,
	D2D1_FEATURE_LEVEL_DEFAULT
	);
pD2DFactory->CreateDCRenderTarget(&props, &pRT);

Then, in your PANEL_SERVICE_POST_INSTALL call, link the ID2D1DCRenderTarget with the hDC of the gauge(it will be relinked with any gauge resize)

RECT rct;
rct.top = 0;
rct.left = 0;
rct.right = tempX;//Width of the gauge
rct.bottom = tempY;//Height of the gauge
pRT->BindDC(element->hdc, &rct);

Then in order to draw, place all your D2D drawing code between these 2 lines

pRT->BeginDraw();

pRT->EndDraw();

Finally, to close down Direct2D, place this in your PANEL_SERVICE_DISCONNECT or class destructor (if using the ESP GDI+ example as a template)

if(pRT)
 pRT->Release();
//Release the other Direct2D objects you have created as well
if(pD2DFactory)
 pD2DFactory->Release();

Thats it. Enjoy!