# Clipping a windows draw area - Direct x 9 and c++ - 2D using D3D

DirectX 9, C++... Let me set the scene:

• I have a 16:9 image.
• I have a 16:10 screen.
• I want the 16:9 image to be drawn so that it is vertically centred within my 16:10 screen.
• My 16:9 image is 1920 x 1080.
• My screen is 1920 x 1200.

Easy:

float topOffset = ( 1200 - 1080 / 2 )


But what if my screen was 1400 x 900

I have to resize the image and get its new height, then do the above again:

float newHeightForScaledImage = ( 1080 * 1400 ) / 1920


Okay so now I have a topOffset for both widescreen cases. I draw all of my images and interactive objects at their ( y position + the topOffset ).

New problem is that the menu is also needed to take into consideration, so I use that systemmetricssomething call which returns 20 which I then subtract from the topOffset... Okay my drawing is vertically centred... perfect!

Question is, regardless of my entire game now being vertically centred (in the most crazy long hauled way possibly known to man), I can still draw outside of the game area into the black zone that now exists.

I know I am approaching this wrong, but I am too new to this to know any better.

How do you guys vertically centre your games if they are a different ratio to the artwork you have created for the backgrounds etc?

• Apr 19 '13 at 21:45

We use letterboxing. This allows us to keep a consistent ratio; you can implement it similarly here. The sample is done in XNA but the general gist is pretty much the same. I'll outline the steps here, anyway for consistency sake since this link has already gone dead once.

2. When drawing, first set the viewport to the full backbuffer and clear black.
3. Compute the largest viewport of the desired aspect ratio that fits on screen and set that to the device.
4. Clear the screen again (it will only clear the viewport area) and draw your game.

All credit to Mr. Nick Gravelyn.

• Really appreciate this... This may be of help, im knackered at the moment from work, will get on this and respond asap. Apr 20 '13 at 0:00
• I see, I'm actually using standard CreateDevice for directX, i'm not sure if I have the width and height of the back buffer.. or a viewport Apr 23 '13 at 0:11
• I assume you are talking about this: msdn.microsoft.com/en-us/library/windows/desktop/… I HAVENT USED THIS YET!!! :D Apr 23 '13 at 0:16
• Oh so I have the viewport.. and I see that clips it well. I still have to draw within the new coordinates? does that sound right? Apr 23 '13 at 0:35
• @JamesT That's correct; so make sure you keep track of what your "clipped" resolution is so you can draw accordingly. Apr 23 '13 at 0:36

Make your render target 19x10 and draw everything based on that. Then you can copy it to the window surface while scaling it down to fit.

• hm, okay im going to take a look at my code when I get back from work. Apr 19 '13 at 16:13
• My render target seems to be CreateDevice average function: hr = d3dObject->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &d3dDevice ); Apr 23 '13 at 0:12