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

Skip to content

Commit 5962e1f

Browse files
committed
Add window minimization handling to swap chain recreation chapter
1 parent c941fc4 commit 5962e1f

14 files changed

Lines changed: 84 additions & 40 deletions

03_Drawing_a_triangle/04_Swap_chain_recreation.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ allocate the new command buffers.
112112

113113
To handle window resizes properly, we also need to query the current size of the framebuffer to make sure that the swap chain images have the (new) right size. To do that change the `chooseSwapExtent` function to take the actual size into account:
114114

115-
```
115+
```c++
116116
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
117117
if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
118118
return capabilities.currentExtent;
@@ -185,6 +185,24 @@ In this case we will also recreate the swap chain if it is suboptimal, because
185185
we want the best possible result. Try to run it and resize the window to see if
186186
the framebuffer is indeed resized properly with the window.
187187

188+
## Handling minimization
189+
190+
There is another case where a swap chain may become out of data and that is a special kind of window resizing: window minimization. This case is special because it will result in a frame buffer size of `0`. In this tutorial we will handle that by pausing until the window is in the foreground again by extending the `recreateSwapChain` function:
191+
192+
```c++
193+
void recreateSwapChain() {
194+
int width = 0, height = 0;
195+
while (width == 0 || height == 0) {
196+
glfwGetFramebufferSize(window, &width, &height);
197+
glfwWaitEvents();
198+
}
199+
200+
vkDeviceWaitIdle(device);
201+
202+
...
203+
}
204+
```
205+
188206
Congratulations, you've now finished your very first well-behaved Vulkan
189207
program! In the next chapter we're going to get rid of the hardcoded vertices in
190208
the vertex shader and actually use a vertex buffer.

code/16_swap_chain_recreation.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,11 @@ class HelloTriangleApplication {
178178
}
179179

180180
void recreateSwapChain() {
181-
int width, height;
182-
glfwGetWindowSize(window, &width, &height);
183-
if (width == 0 || height == 0) return;
181+
int width = 0, height = 0;
182+
while (width == 0 || height == 0) {
183+
glfwGetFramebufferSize(window, &width, &height);
184+
glfwWaitEvents();
185+
}
184186

185187
vkDeviceWaitIdle(device);
186188

code/17_vertex_input.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,11 @@ class HelloTriangleApplication {
217217
}
218218

219219
void recreateSwapChain() {
220-
int width, height;
221-
glfwGetWindowSize(window, &width, &height);
222-
if (width == 0 || height == 0) return;
220+
int width = 0, height = 0;
221+
while (width == 0 || height == 0) {
222+
glfwGetFramebufferSize(window, &width, &height);
223+
glfwWaitEvents();
224+
}
223225

224226
vkDeviceWaitIdle(device);
225227

code/18_vertex_buffer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ class HelloTriangleApplication {
225225
}
226226

227227
void recreateSwapChain() {
228-
int width, height;
229-
glfwGetWindowSize(window, &width, &height);
230-
if (width == 0 || height == 0) return;
228+
int width = 0, height = 0;
229+
while (width == 0 || height == 0) {
230+
glfwGetFramebufferSize(window, &width, &height);
231+
glfwWaitEvents();
232+
}
231233

232234
vkDeviceWaitIdle(device);
233235

code/19_staging_buffer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ class HelloTriangleApplication {
225225
}
226226

227227
void recreateSwapChain() {
228-
int width, height;
229-
glfwGetWindowSize(window, &width, &height);
230-
if (width == 0 || height == 0) return;
228+
int width = 0, height = 0;
229+
while (width == 0 || height == 0) {
230+
glfwGetFramebufferSize(window, &width, &height);
231+
glfwWaitEvents();
232+
}
231233

232234
vkDeviceWaitIdle(device);
233235

code/20_index_buffer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,11 @@ class HelloTriangleApplication {
236236
}
237237

238238
void recreateSwapChain() {
239-
int width, height;
240-
glfwGetWindowSize(window, &width, &height);
241-
if (width == 0 || height == 0) return;
239+
int width = 0, height = 0;
240+
while (width == 0 || height == 0) {
241+
glfwGetFramebufferSize(window, &width, &height);
242+
glfwWaitEvents();
243+
}
242244

243245
vkDeviceWaitIdle(device);
244246

code/21_descriptor_layout.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,11 @@ class HelloTriangleApplication {
257257
}
258258

259259
void recreateSwapChain() {
260-
int width, height;
261-
glfwGetWindowSize(window, &width, &height);
262-
if (width == 0 || height == 0) return;
260+
int width = 0, height = 0;
261+
while (width == 0 || height == 0) {
262+
glfwGetFramebufferSize(window, &width, &height);
263+
glfwWaitEvents();
264+
}
263265

264266
vkDeviceWaitIdle(device);
265267

code/22_descriptor_set.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,11 @@ class HelloTriangleApplication {
264264
}
265265

266266
void recreateSwapChain() {
267-
int width, height;
268-
glfwGetWindowSize(window, &width, &height);
269-
if (width == 0 || height == 0) return;
267+
int width = 0, height = 0;
268+
while (width == 0 || height == 0) {
269+
glfwGetFramebufferSize(window, &width, &height);
270+
glfwWaitEvents();
271+
}
270272

271273
vkDeviceWaitIdle(device);
272274

code/23_texture_image.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,11 @@ class HelloTriangleApplication {
274274
}
275275

276276
void recreateSwapChain() {
277-
int width, height;
278-
glfwGetWindowSize(window, &width, &height);
279-
if (width == 0 || height == 0) return;
277+
int width = 0, height = 0;
278+
while (width == 0 || height == 0) {
279+
glfwGetFramebufferSize(window, &width, &height);
280+
glfwWaitEvents();
281+
}
280282

281283
vkDeviceWaitIdle(device);
282284

code/24_sampler.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,11 @@ class HelloTriangleApplication {
281281
}
282282

283283
void recreateSwapChain() {
284-
int width, height;
285-
glfwGetWindowSize(window, &width, &height);
286-
if (width == 0 || height == 0) return;
284+
int width = 0, height = 0;
285+
while (width == 0 || height == 0) {
286+
glfwGetFramebufferSize(window, &width, &height);
287+
glfwWaitEvents();
288+
}
287289

288290
vkDeviceWaitIdle(device);
289291

0 commit comments

Comments
 (0)