Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
preview_main.cpp
Go to the documentation of this file.
1/**
2 * @file preview_main.cpp
3 * @brief Standalone smoke-test for the Vulkan preview scaffold.
4 *
5 * Builds when MONAD_VULKAN_PREVIEW=ON. Not linked into the main game; this
6 * executable exists so we can iterate on the Vulkan backend without touching
7 * the working OpenGL render path.
8 *
9 * Run: bin/debug/vulkan_preview
10 * Quit: ESC, or close the window.
11 */
12
13#include <cmath>
14#include <cstdio>
15#include <stdexcept>
16
17#define GLFW_INCLUDE_VULKAN
18#include <GLFW/glfw3.h>
19
23
24namespace
25{
26bool g_framebufferResized = false;
27
28void OnResize(GLFWwindow * /*win*/, int /*w*/, int /*h*/)
29{
30 g_framebufferResized = true;
31}
32} // namespace
33
34int main()
35{
36 if (!glfwInit())
37 {
38 fprintf(stderr, "glfwInit failed\n");
39 return 1;
40 }
41 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
42 GLFWwindow *win = glfwCreateWindow(1280, 720, "monad-engine vulkan preview", nullptr, nullptr);
43 if (!win)
44 {
45 fprintf(stderr, "glfwCreateWindow failed (does the system support Vulkan?)\n");
46 glfwTerminate();
47 return 1;
48 }
49 glfwSetFramebufferSizeCallback(win, OnResize);
50
51 try
52 {
53 // Validation on by default for the preview tool — it's the only way
54 // to catch the kind of mistakes a scaffold accumulates.
55 mnd::vk::Context ctx(win, /*enableValidation=*/true);
56 mnd::vk::Swapchain swap(ctx, win);
57 mnd::vk::Renderer renderer(ctx, swap);
58
59 printf("[vk preview] device + swapchain online (%u images, %ux%u)\n",
60 swap.ImageCount(), swap.GetExtent().width, swap.GetExtent().height);
61
62 double t0 = glfwGetTime();
63 while (!glfwWindowShouldClose(win))
64 {
65 glfwPollEvents();
66 if (glfwGetKey(win, GLFW_KEY_ESCAPE) == GLFW_PRESS)
67 {
68 glfwSetWindowShouldClose(win, GLFW_TRUE);
69 }
70
71 // Animated clear colour so a successful frame is visually obvious.
72 float t = static_cast<float>(glfwGetTime() - t0);
73 float r = 0.5f + 0.5f * std::sin(t * 0.7f);
74 float g = 0.5f + 0.5f * std::sin(t * 0.9f + 2.0f);
75 float b = 0.5f + 0.5f * std::sin(t * 1.1f + 4.0f);
76
77 bool ok = renderer.DrawFrame(r, g, b);
78 if (!ok || g_framebufferResized)
79 {
80 swap.Recreate();
81 g_framebufferResized = false;
82 }
83 }
84 renderer.WaitIdle();
85 }
86 catch (const std::exception &ex)
87 {
88 fprintf(stderr, "[vk preview] fatal: %s\n", ex.what());
89 glfwDestroyWindow(win);
90 glfwTerminate();
91 return 1;
92 }
93
94 glfwDestroyWindow(win);
95 glfwTerminate();
96 return 0;
97}
Vulkan instance + physical/logical device + queue selection.
Per-frame Vulkan command-buffer recording + presentation.
VkSwapchain + per-image views, render pass, framebuffers.
Owns the Vulkan instance, surface (tied to a GLFW window), physical + logical device,...
Definition Context.h:41
void WaitIdle() const
Definition Renderer.cpp:45
bool DrawFrame(float clearR, float clearG, float clearB)
Definition Renderer.cpp:118
uint32_t ImageCount() const
Definition Swapchain.h:40
VkExtent2D GetExtent() const
Definition Swapchain.h:37
int main()