Stretched pixels in windowed mode

This topic is 1321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all,

I've created a simple program using DX9 in windowed mode. It uses a simple diffuse shader to render a mesh in a 640x480 window. The program seems to work fine but there's a strange array of stretched pixels which distort the image. Here's a shot:

[attachment=21955:pix1.png]

If you can't see them, here's the same image after edge-detect:

[attachment=21956:pix2.png]

Is there anything I'm overlooking that needs to be done to render in windowed mode (I've been doing fullscreen stuff until now). Thanks!

Share on other sites

The pixels aren’t being stretched, they are being squashed.

When the backbuffer and the window are not the same size the image will be rescaled upon presenting, which is what is happening here.

Most likely you set the window to 640-480 and you set the back buffer to the same value without taking into account the borders (hence it is squashed more vertically than horizontally, as vertically you have extra window border spent on the title bar).

The client area of a window will be smaller than the actual window size.  Your back buffer must be the same size as the client rectangle to avoid scaling.

If you want the client area to be 640-480, adjust the window size using AdjustWindowRect().

L. Spiro

Edited by L. Spiro

Share on other sites

You should also keep in mind that the ratio of buffer size to window size is important. If your backbuffer is set to say 640 x 480 but your window rectangle is 600 x 480, then the result will look "squashed" as you put it. If the window rectangle was, say, 700 x 480, then it would look "stretched."

As a rule I like to not optimize unless I must, and also try to keep things simple until it needs to be changed...thus I would suggest that your backbuffer and window rect keep the width and height in constant ratio of each other. I like to use multiples of the back buffer. Again, this isn't "optimal" as much as I just follow the rule: "get it to look right before getting to be fast."

Share on other sites

Thanks for the help, yes, I resized the client area to 640x480 and the problem's solved. That bit of code will be staying in my future projects :)

Share on other sites

As it appears that you're coding a windowed app, a better approach might be to size the backbuffer to the client area, rather than the other way 'round. Using a window means the user may move or resize the window for his/her own convenience, and you should respect that. So, when the window is resized:

RECT rect;
GetClientRect(hwnd, &rect);
params.BackBufferWidth = rect.right - rect.left;
params.BackBufferHeight = rect.bottom - rect.top;
OnDeviceLost(); // prepare objects for device reset
device->Reset( &params );
OnDeviceRestored(); // restore objects after the reset
ResetProjectionAspectRatio(); // account for change in the aspect ratio


Perhaps even better than setting the width/height yourself, unless you have some specific reason to size the backbuffer to a particular size, set the BackBufferWidth and BackBufferHeight to 0, and let D3DX set the buffer to the client area dimensions itself. You can do that when creating the device or when the window is resized (device reset). Just remember to check the aspect ratio of your projection matrix whenever the client/backbuffer is resized.

Edited by Buckeye