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

Skip to content

Commit 893a0e8

Browse files
committed
Fix crash when minimizing window
1 parent 53bed94 commit 893a0e8

13 files changed

Lines changed: 64 additions & 26 deletions

03_Drawing_a_triangle/04_Swap_chain_recreation.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ void initWindow() {
137137
...
138138

139139
static void onWindowResized(GLFWwindow* window, int width, int height) {
140-
if (width == 0 || height == 0) return;
141-
142140
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
143141
app->recreateSwapChain();
144142
}
@@ -165,6 +163,22 @@ glfwGetWindowSize(window, &width, &height);
165163
VkExtent2D actualExtent = {width, height};
166164
```
167165

166+
We have to be careful with this, because the width and height may be 0 if the
167+
window is minimized, for example. Therefore we should check if it makes sense
168+
to recreate the swap chain:
169+
170+
```c++
171+
void recreateSwapChain() {
172+
int width, height;
173+
glfwGetWindowSize(window, &width, &height);
174+
if (width == 0 || height == 0) return;
175+
176+
vkDeviceWaitIdle(device);
177+
178+
...
179+
}
180+
```
181+
168182
## Suboptimal or out-of-date swap chain
169183

170184
It is also possible for Vulkan to tell us that the swap chain is no longer

code/depth_buffering.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,15 @@ class HelloTriangleApplication {
294294
}
295295

296296
static void onWindowResized(GLFWwindow* window, int width, int height) {
297-
if (width == 0 || height == 0) return;
298-
299297
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
300298
app->recreateSwapChain();
301299
}
302300

303301
void recreateSwapChain() {
302+
int width, height;
303+
glfwGetWindowSize(window, &width, &height);
304+
if (width == 0 || height == 0) return;
305+
304306
vkDeviceWaitIdle(device);
305307

306308
cleanupSwapChain();

code/descriptor_layout.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,15 @@ class HelloTriangleApplication {
248248
}
249249

250250
static void onWindowResized(GLFWwindow* window, int width, int height) {
251-
if (width == 0 || height == 0) return;
252-
253251
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
254252
app->recreateSwapChain();
255253
}
256254

257255
void recreateSwapChain() {
256+
int width, height;
257+
glfwGetWindowSize(window, &width, &height);
258+
if (width == 0 || height == 0) return;
259+
258260
vkDeviceWaitIdle(device);
259261

260262
cleanupSwapChain();

code/descriptor_set.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,15 @@ class HelloTriangleApplication {
255255
}
256256

257257
static void onWindowResized(GLFWwindow* window, int width, int height) {
258-
if (width == 0 || height == 0) return;
259-
260258
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
261259
app->recreateSwapChain();
262260
}
263261

264262
void recreateSwapChain() {
263+
int width, height;
264+
glfwGetWindowSize(window, &width, &height);
265+
if (width == 0 || height == 0) return;
266+
265267
vkDeviceWaitIdle(device);
266268

267269
cleanupSwapChain();

code/index_buffer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,15 @@ class HelloTriangleApplication {
227227
}
228228

229229
static void onWindowResized(GLFWwindow* window, int width, int height) {
230-
if (width == 0 || height == 0) return;
231-
232230
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
233231
app->recreateSwapChain();
234232
}
235233

236234
void recreateSwapChain() {
235+
int width, height;
236+
glfwGetWindowSize(window, &width, &height);
237+
if (width == 0 || height == 0) return;
238+
237239
vkDeviceWaitIdle(device);
238240

239241
cleanupSwapChain();

code/model_loading.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,15 @@ class HelloTriangleApplication {
301301
}
302302

303303
static void onWindowResized(GLFWwindow* window, int width, int height) {
304-
if (width == 0 || height == 0) return;
305-
306304
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
307305
app->recreateSwapChain();
308306
}
309307

310308
void recreateSwapChain() {
309+
int width, height;
310+
glfwGetWindowSize(window, &width, &height);
311+
if (width == 0 || height == 0) return;
312+
311313
vkDeviceWaitIdle(device);
312314

313315
cleanupSwapChain();

code/sampler.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,15 @@ class HelloTriangleApplication {
272272
}
273273

274274
static void onWindowResized(GLFWwindow* window, int width, int height) {
275-
if (width == 0 || height == 0) return;
276-
277275
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
278276
app->recreateSwapChain();
279277
}
280278

281279
void recreateSwapChain() {
280+
int width, height;
281+
glfwGetWindowSize(window, &width, &height);
282+
if (width == 0 || height == 0) return;
283+
282284
vkDeviceWaitIdle(device);
283285

284286
cleanupSwapChain();

code/staging_buffer.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ class HelloTriangleApplication {
216216
}
217217

218218
static void onWindowResized(GLFWwindow* window, int width, int height) {
219-
if (width == 0 || height == 0) return;
220-
221219
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
222220
app->recreateSwapChain();
223221
}
224222

225223
void recreateSwapChain() {
224+
int width, height;
225+
glfwGetWindowSize(window, &width, &height);
226+
if (width == 0 || height == 0) return;
227+
226228
vkDeviceWaitIdle(device);
227229

228230
cleanupSwapChain();

code/swap_chain_recreation.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,15 @@ class HelloTriangleApplication {
169169
}
170170

171171
static void onWindowResized(GLFWwindow* window, int width, int height) {
172-
if (width == 0 || height == 0) return;
173-
174172
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
175173
app->recreateSwapChain();
176174
}
177175

178176
void recreateSwapChain() {
177+
int width, height;
178+
glfwGetWindowSize(window, &width, &height);
179+
if (width == 0 || height == 0) return;
180+
179181
vkDeviceWaitIdle(device);
180182

181183
cleanupSwapChain();

code/texture_image.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,15 @@ class HelloTriangleApplication {
265265
}
266266

267267
static void onWindowResized(GLFWwindow* window, int width, int height) {
268-
if (width == 0 || height == 0) return;
269-
270268
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
271269
app->recreateSwapChain();
272270
}
273271

274272
void recreateSwapChain() {
273+
int width, height;
274+
glfwGetWindowSize(window, &width, &height);
275+
if (width == 0 || height == 0) return;
276+
275277
vkDeviceWaitIdle(device);
276278

277279
cleanupSwapChain();

0 commit comments

Comments
 (0)