Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4be0406

Browse files
committed
Fix application crashing when minimizing the window
1 parent a38fa1d commit 4be0406

13 files changed

Lines changed: 29 additions & 1 deletion

03_Drawing_a_triangle/04_Swap_chain_recreation.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ void initWindow() {
112112
...
113113

114114
static void onWindowResized(GLFWwindow* window, int width, int height) {
115+
if (width == 0 || height == 0) return;
116+
115117
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
116118
app->recreateSwapChain();
117119
}
@@ -123,7 +125,9 @@ argument, so we can't directly use a member function. Luckily GLFW allows us to
123125
store an arbitrary pointer in the window object with `glfwSetWindowUserPointer`,
124126
so we can specify a static class member and get the original class instance back
125127
with `glfwGetWindowUserPointer`. We can then proceed to call
126-
`recreateSwapChain`.
128+
`recreateSwapChain`, but only if the size of the window is non-zero. This case
129+
occurs when the window is minimized and it will cause swap chain creation to
130+
fail.
127131
128132
## Suboptimal or out-of-date swap chain
129133

code/depth_buffering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ class HelloTriangleApplication {
283283
}
284284

285285
static void onWindowResized(GLFWwindow* window, int width, int height) {
286+
if (width == 0 || height == 0) return;
287+
286288
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
287289
app->recreateSwapChain();
288290
}

code/descriptor_layout.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ class HelloTriangleApplication {
249249
}
250250

251251
static void onWindowResized(GLFWwindow* window, int width, int height) {
252+
if (width == 0 || height == 0) return;
253+
252254
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
253255
app->recreateSwapChain();
254256
}

code/descriptor_set.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ class HelloTriangleApplication {
254254
}
255255

256256
static void onWindowResized(GLFWwindow* window, int width, int height) {
257+
if (width == 0 || height == 0) return;
258+
257259
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
258260
app->recreateSwapChain();
259261
}

code/index_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ class HelloTriangleApplication {
230230
}
231231

232232
static void onWindowResized(GLFWwindow* window, int width, int height) {
233+
if (width == 0 || height == 0) return;
234+
233235
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
234236
app->recreateSwapChain();
235237
}

code/model_loading.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ class HelloTriangleApplication {
289289
}
290290

291291
static void onWindowResized(GLFWwindow* window, int width, int height) {
292+
if (width == 0 || height == 0) return;
293+
292294
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
293295
app->recreateSwapChain();
294296
}

code/sampler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ class HelloTriangleApplication {
265265
}
266266

267267
static void onWindowResized(GLFWwindow* window, int width, int height) {
268+
if (width == 0 || height == 0) return;
269+
268270
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
269271
app->recreateSwapChain();
270272
}

code/staging_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ class HelloTriangleApplication {
222222
}
223223

224224
static void onWindowResized(GLFWwindow* window, int width, int height) {
225+
if (width == 0 || height == 0) return;
226+
225227
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
226228
app->recreateSwapChain();
227229
}

code/swap_chain_recreation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ class HelloTriangleApplication {
178178
}
179179

180180
static void onWindowResized(GLFWwindow* window, int width, int height) {
181+
if (width == 0 || height == 0) return;
182+
181183
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
182184
app->recreateSwapChain();
183185
}

code/texture_image.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class HelloTriangleApplication {
261261
}
262262

263263
static void onWindowResized(GLFWwindow* window, int width, int height) {
264+
if (width == 0 || height == 0) return;
265+
264266
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
265267
app->recreateSwapChain();
266268
}

0 commit comments

Comments
 (0)