Handmade Hero»Forums»Code
Scott Hunt
41 posts
Father, Thinker, Mechanical Engineer @NASA, and C/C++ Hobby Enthusiast / @TexxStudio
Monitor Aspect Ratios
Edited by Scott Hunt on
Good afternoon everyone, hopefully this hasn't been asked previously, couldn't search the forums.

I'm following along in development on 1920x1200 monitors and the StrechDIBits in episode 40 did some scaling when going to full screen that distorts the image pretty badly.

In the mean time I've adjusted the StretchDIBits to not scale out side 16:9 and just center the image (hardcoded for now), but is there a more proper explanation or implementation in a later episode that I can fast forward to for an interim fix? Th

1
2
3
4
5
6
StretchDIBits(DeviceContext,
                  0, 60, WindowWidth, WindowHeight - 120,
		  0, 0, Buffer->Width, Buffer->Height,
		  Buffer->Memory,
		  &Buffer->Info,
		  DIB_RGB_COLORS, SRCCOPY);


Thanks in advance,

-Mebourne
Mārtiņš Možeiko
2562 posts / 2 projects
Monitor Aspect Ratios
Currently there is no adjustment for aspect ratio. Casey is hardcoding output to 16:9.
You could do something like this (not tested):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int ExpectedWidth = 1920;
int ExpectedHeight = 1080;
int Width;
int Height;

if (WindowWidth * ExpectedHeight < WindowHeight * ExpectedWidth)
{
    Width = Width * WindowWidth / ExpectedWidth;
    Height = Height * WindowWidth / ExpectedWidth;
}
else if (WindowWidth * ExpectedHeight > WindowHeight * ExpectedWidth)
{
    Width = Width * WindowHeight / ExpectedHeight;
    Height = Height * WindowHeight / ExpectedHeight;
}
else
{
    Width = WindowWidth;
    Height = WindowHeight;
}

int OffsetX = (ScreenWidth - Width) / 2;
int OffsetY = (ScreenHeight - Height) / 2;

StretchDIBits(DeviceContext,
                  OffsetX, OffsetY, Width, Height,
		  0, 0, Buffer->Width, Buffer->Height,
		  Buffer->Memory,
		  &Buffer->Info,
		  DIB_RGB_COLORS, SRCCOPY);

This will either shrink or expand image to fit in window area and will preserve aspect ratio.

I'm not sure how GetSystemMetrics deals with DPI setting for Windows.