Skip to content

Commit 2ca2bae

Browse files
committed
improve window size handling in mock backend
1 parent 18f99ed commit 2ca2bae

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/render/mock_opengl/mock_gl_engine.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,22 +1733,31 @@ void MockGLEngine::hideWindow() {}
17331733

17341734
void MockGLEngine::updateWindowSize(bool force) {
17351735

1736-
// this silly code mimicks the gl backend version, but it is important that we preserve
1736+
// This silly code mimicks the gl backend version, but it is important that we preserve
17371737
// the view::bufferWidth, etc, otherwise it is impossible to manually set the window size
1738-
// in the mock backend (which appears in unit tests, etc)
1739-
int newBufferWidth = view::bufferWidth;
1740-
int newBufferHeight = view::bufferHeight;
1738+
// in the mock backend (which appears in unit tests, etc).
1739+
// In this case, we just let the buffer size follow the window size 1:1
1740+
int newBufferWidth = view::windowWidth;
1741+
int newBufferHeight = view::windowHeight;
17411742
int newWindowWidth = view::windowWidth;
17421743
int newWindowHeight = view::windowHeight;
17431744

17441745
if (force || newBufferWidth != view::bufferWidth || newBufferHeight != view::bufferHeight ||
17451746
newWindowHeight != view::windowHeight || newWindowWidth != view::windowWidth) {
17461747
// Basically a resize callback
17471748
requestRedraw();
1749+
1750+
// prevent any division by zero for e.g. aspect ratio calcs
1751+
if (newBufferHeight == 0) newBufferHeight = 1;
1752+
if (newWindowHeight == 0) newWindowHeight = 1;
1753+
17481754
view::bufferWidth = newBufferWidth;
17491755
view::bufferHeight = newBufferHeight;
17501756
view::windowWidth = newWindowWidth;
17511757
view::windowHeight = newWindowHeight;
1758+
1759+
render::engine->resizeScreenBuffers();
1760+
render::engine->setScreenBufferViewports();
17521761
}
17531762
}
17541763

test/src/basics_test.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,25 @@ TEST_F(PolyscopeTest, EmptyBuffer) {
114114
TEST_F(PolyscopeTest, WindowProperties) {
115115

116116
// set/get window size
117-
polyscope::view::setWindowSize(300, 400);
117+
int32_t target_w = 500;
118+
int32_t target_h = 300;
119+
polyscope::view::setWindowSize(target_w, target_h);
118120
int32_t w, h;
119121
std::tie(w, h) = polyscope::view::getWindowSize();
120-
EXPECT_EQ(w, 300);
121-
EXPECT_EQ(h, 400);
122+
EXPECT_EQ(w, target_w);
123+
EXPECT_EQ(h, target_h);
124+
125+
polyscope::show(3);
122126

123127
// get buffer size
124128
// (hard to say what this should be, given hi-dpi etc)
125-
std::tie(w, h) = polyscope::view::getBufferSize();
129+
int32_t buffer_w, buffer_h;
130+
std::tie(buffer_w, buffer_h) = polyscope::view::getBufferSize();
131+
132+
float target_aspect = target_w / static_cast<float>(target_h);
133+
float buffer_aspect = buffer_w / static_cast<float>(buffer_h);
134+
EXPECT_NEAR(target_aspect, buffer_aspect, 0.01);
135+
126136

127137
// resizable
128138
polyscope::view::setWindowResizable(false);

0 commit comments

Comments
 (0)