Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions attachments/00_base_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,31 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every member
// below. Later chapters add Vulkan objects here that still reference the
// window system connection while they are destroyed, so glfwTerminate() has
// to outlive them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand All @@ -51,9 +70,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every other
// member. Terminating GLFW here would free the window system connection
// while later chapters' Vulkan objects are still alive.
}
};

Expand Down
29 changes: 24 additions & 5 deletions attachments/01_instance_creation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,34 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every vk::raii
// member below. The swapchain and surface still reference the window system
// connection when their destructors run, so glfwTerminate() has to outlive
// them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;

vk::raii::Context context;
vk::raii::Instance instance = nullptr;

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand All @@ -58,9 +77,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every vk::raii
// member. Destroying the window or terminating GLFW here would free the
// window system connection while the swapchain and surface are still alive.
}

void createInstance()
Expand Down
29 changes: 24 additions & 5 deletions attachments/02_validation_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every vk::raii
// member below. The swapchain and surface still reference the window system
// connection when their destructors run, so glfwTerminate() has to outlive
// them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;

vk::raii::Context context;
Expand All @@ -47,8 +68,6 @@ class HelloTriangleApplication

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand All @@ -71,9 +90,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every vk::raii
// member. Destroying the window or terminating GLFW here would free the
// window system connection while the swapchain and surface are still alive.
}

void createInstance()
Expand Down
29 changes: 24 additions & 5 deletions attachments/03_physical_device_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every vk::raii
// member below. The swapchain and surface still reference the window system
// connection when their destructors run, so glfwTerminate() has to outlive
// them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;

vk::raii::Context context;
Expand All @@ -52,8 +73,6 @@ class HelloTriangleApplication

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand All @@ -77,9 +96,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every vk::raii
// member. Destroying the window or terminating GLFW here would free the
// window system connection while the swapchain and surface are still alive.
}

void createInstance()
Expand Down
29 changes: 24 additions & 5 deletions attachments/04_logical_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every vk::raii
// member below. The swapchain and surface still reference the window system
// connection when their destructors run, so glfwTerminate() has to outlive
// them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;

vk::raii::Context context;
Expand All @@ -56,8 +77,6 @@ class HelloTriangleApplication

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand All @@ -82,9 +101,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every vk::raii
// member. Destroying the window or terminating GLFW here would free the
// window system connection while the swapchain and surface are still alive.
}

void createInstance()
Expand Down
29 changes: 24 additions & 5 deletions attachments/05_window_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every vk::raii
// member below. The swapchain and surface still reference the window system
// connection when their destructors run, so glfwTerminate() has to outlive
// them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;
vk::raii::Context context;
vk::raii::Instance instance = nullptr;
Expand All @@ -53,8 +74,6 @@ class HelloTriangleApplication

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand All @@ -80,9 +99,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every vk::raii
// member. Destroying the window or terminating GLFW here would free the
// window system connection while the swapchain and surface are still alive.
}

void createInstance()
Expand Down
29 changes: 24 additions & 5 deletions attachments/06_swap_chain_creation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every vk::raii
// member below. The swapchain and surface still reference the window system
// connection when their destructors run, so glfwTerminate() has to outlive
// them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;
vk::raii::Context context;
vk::raii::Instance instance = nullptr;
Expand All @@ -60,8 +81,6 @@ class HelloTriangleApplication

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand All @@ -88,9 +107,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every vk::raii
// member. Destroying the window or terminating GLFW here would free the
// window system connection while the swapchain and surface are still alive.
}

void createInstance()
Expand Down
29 changes: 24 additions & 5 deletions attachments/07_image_views.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ class HelloTriangleApplication
}

private:
// Owns the GLFW lifetime: initialises it here and terminates it in the
// destructor. Declared first, so it is destroyed last - after every vk::raii
// member below. The swapchain and surface still reference the window system
// connection when their destructors run, so glfwTerminate() has to outlive
// them. It also destroys any windows that are still open.
struct GlfwGuard
{
GlfwGuard()
{
if (!glfwInit())
{
throw std::runtime_error("failed to initialize GLFW!");
}
}

~GlfwGuard()
{
glfwTerminate();
}
} glfwGuard;

GLFWwindow *window = nullptr;
vk::raii::Context context;
vk::raii::Instance instance = nullptr;
Expand All @@ -60,8 +81,6 @@ class HelloTriangleApplication

void initWindow()
{
glfwInit();

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

Expand Down Expand Up @@ -89,9 +108,9 @@ class HelloTriangleApplication

void cleanup()
{
glfwDestroyWindow(window);

glfwTerminate();
// GLFW is torn down by glfwGuard, which is destroyed after every vk::raii
// member. Destroying the window or terminating GLFW here would free the
// window system connection while the swapchain and surface are still alive.
}

void createInstance()
Expand Down
Loading