Skip to content

Commit 9380e40

Browse files
authored
Fixed OvGame window size & position (#391)
1 parent 012c349 commit 9380e40

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

Diff for: Sources/Overload/OvGame/src/OvGame/Core/Context.cpp

+46-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,35 @@
1414
using namespace OvCore::Global;
1515
using namespace OvCore::ResourceManagement;
1616

17+
std::array<int, 4> CalculateOptimalWindowSizeAndPosition(
18+
const std::array<int, 4>& p_workAreaSize,
19+
std::array<int, 2> p_resolution
20+
)
21+
{
22+
const auto [workAreaX, workAreaY, workAreaWidth, workAreaHeight] = p_workAreaSize;
23+
24+
std::array<int, 2> finalResolution = p_resolution;
25+
26+
// Check if the given resolution fits in the workArea. If not, keep reducing it until it fits
27+
while (finalResolution[0] > workAreaWidth || finalResolution[1] > workAreaHeight)
28+
{
29+
finalResolution[0] /= 2;
30+
finalResolution[1] /= 2;
31+
}
32+
33+
if (finalResolution != p_resolution)
34+
{
35+
OVLOG_WARNING("The target resolution didn't fit in the work area. It has been reduced to " + std::to_string(p_resolution[0]) + "x" + std::to_string(p_resolution[1]));
36+
}
37+
38+
return {
39+
workAreaX + workAreaWidth / 2 - p_resolution[0] / 2,
40+
workAreaY + workAreaHeight / 2 - p_resolution[1] / 2,
41+
p_resolution[0],
42+
p_resolution[1]
43+
};
44+
}
45+
1746
OvGame::Core::Context::Context() :
1847
engineAssetsPath("Data\\Engine\\"),
1948
projectAssetsPath("Data\\User\\Assets\\"),
@@ -35,15 +64,30 @@ OvGame::Core::Context::Context() :
3564

3665
OvWindowing::Settings::WindowSettings windowSettings;
3766
projectSettings.TryGet("executable_name", windowSettings.title);
38-
projectSettings.TryGet("x_resolution", windowSettings.width);
39-
projectSettings.TryGet("y_resolution", windowSettings.height);
67+
std::array<int, 2> targetWindowResolution = { 1280, 720 };
68+
projectSettings.TryGet("x_resolution", targetWindowResolution[0]);
69+
projectSettings.TryGet("y_resolution", targetWindowResolution[1]);
4070
windowSettings.maximized = false;
4171
windowSettings.resizable = false;
4272
projectSettings.TryGet("fullscreen", windowSettings.fullscreen);
4373
projectSettings.TryGet("samples", windowSettings.samples);
4474

4575
/* Window creation */
4676
device = std::make_unique<OvWindowing::Context::Device>(deviceSettings);
77+
if (windowSettings.fullscreen)
78+
{
79+
windowSettings.width = targetWindowResolution[0];
80+
windowSettings.height = targetWindowResolution[1];
81+
}
82+
else
83+
{
84+
const auto workAreaSize = device->GetWorkAreaSize();
85+
const auto bestFitWindowSizeAndPosition = CalculateOptimalWindowSizeAndPosition(workAreaSize, targetWindowResolution);
86+
windowSettings.x = bestFitWindowSizeAndPosition[0];
87+
windowSettings.y = bestFitWindowSizeAndPosition[1];
88+
windowSettings.width = bestFitWindowSizeAndPosition[2];
89+
windowSettings.height = bestFitWindowSizeAndPosition[3];
90+
}
4791
window = std::make_unique<OvWindowing::Window>(*device, windowSettings);
4892
auto iconRaw = std::to_array<uint64_t>({ 0,0,144115188614240000,7500771567664627712,7860776967494637312,0,0,0,0,7212820467466371072,11247766461832697600,14274185407633888512,12905091124788992000,5626708973701824512,514575842263176960,0,0,6564302121125019648,18381468271671515136,18381468271654737920,18237353083595659264,18165295488836311040,6708138037527189504,0,4186681893338480640,7932834557741046016,17876782538917681152,11319824055216379904,15210934132358518784,18381468271520454400,1085667680982603520,0,18093237891929479168,18309410677600032768,11391881649237530624,7932834561381570304,17300321784231761408,15210934132375296000,8293405106311272448,2961143145139082752,16507969723533236736,17516777143216379904,10671305705855129600,7356091234422036224,16580027318695106560,2240567205413984000,18381468271470188544,10959253511276599296,4330520004484136960,10815138323200743424,11607771853338181632,8364614976649238272,17444719546862998784,2669156352,18381468269893064448,6419342512197474304,11103650170688640000,6492244531366860800,14346241902646925312,13841557270159628032,7428148827772098304,3464698581331941120,18381468268953606144,1645680384,18381468271554008832,7140201027266418688,5987558797656659712,17588834734687262208,7284033640602212096,14273902834169157632,18381468269087692288,6852253225049397248,17732667349600245504,16291515470083266560,10022503688432981760,11968059825861367552,9733991836700645376,14850363587428816640,18381468271168132864,16147400282007410688,656430432014827520,18381468270950094848,15715054717226194944,72057596690306560,11823944635485519872,15859169905251653376,17084149004500473856,8581352906816952064,2527949855582584832,18381468271419856896,8581352907253225472,252776704,1376441223417430016,14994761349590357760,10527190521537370112,0,9806614576878321664,18381468271671515136,17156206598538401792,6059619689256392448,10166619973990488064,18381468271403079424,17444719549178451968,420746240,870625192710242304,4906133035823863552,18381468269289150464,18381468271671515136,18381468271671515136,9950729769032620032,14778305994951169792,269422336,0,0,18381468268785833984,8941923452686178304,18381468270950094848,3440842496,1233456333565402880,0,0,0,11823944636091210240,2383877888,16724143605745719296,2316834816,0,0 });
4993
window->SetIconFromMemory(reinterpret_cast<uint8_t*>(iconRaw.data()), 16, 16);

0 commit comments

Comments
 (0)