many changes: Makefile, util, fixed indent, fixes in swapchain
[vkrt] / src / main.c
index 07af1ef..b0a5ec4 100644 (file)
@@ -6,6 +6,7 @@
 #include <string.h>
 
 #include "vk.h"
+#include "util.h"
 
 
 /* static glfw callbacks */
@@ -36,7 +37,15 @@ static int win_w = 800;
 static int win_h = 600;
 
 static struct vk_ctx vk_core;
-static struct vk_swapchain vk_swap;
+static VkSurfaceKHR vk_surf;
+static int vsz, fsz;
+static struct vk_renderer vk_rnd;
+static struct vk_swapchain vk_chain;
+
+static struct vk_semaphores vk_sema;
+
+/* empty for as long as we hardcode the vertices in the vertex shader */
+static struct vk_vertex_info vk_vert_info;
 
 int main(int argc, char** argv)
 {
@@ -66,6 +75,8 @@ int main(int argc, char** argv)
 static bool
 init()
 {
+    char *vsdr, *fsdr;
+
     /* initialize GLFW */
 
     if (!glfwInit()) {
@@ -78,15 +89,6 @@ init()
         return false;
     }
 
-    /* initialize Vulkan context */
-
-    if (!vk_init_ctx_for_rendering(&vk_core)) {
-        fprintf(stderr, "Failed to initialize Vulkan context.\n");
-        return false;
-    }
-
-    memset(&vk_swap, 0, sizeof vk_swap);
-
     /* create window */
 
     glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
@@ -97,24 +99,41 @@ init()
         return false;
     }
 
+    /* initialize Vulkan context (instance) */
+
+    if (!vk_init_ctx_for_rendering(&vk_core)) {
+        fprintf(stderr, "Failed to initialize Vulkan context.\n");
+        return false;
+    }
+
     /* create (Xcb) surface */
 
     glfwGetFramebufferSize(win, &win_h, &win_h);
-    if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_swap.surface)
+    if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
             != VK_SUCCESS) {
         fprintf(stderr, "Failed to create XCB surface.\n");
+        glfwTerminate();
+
         return false;
     }
 
-    /* create Vulkan swapchain */
+    /* create semaphores */
+    vk_create_semaphores(&vk_core, false, &vk_sema);
 
-    /* aquire images? */
+    /* create swapchain */
+    vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, VK_NULL_HANDLE, &vk_chain);
 
     /* create shaders */
-
-    /* create semaphores? */
+    vsdr = sdr_load("data/main.vert.spv", &vsz);
+    fsdr = sdr_load("data/main.frag.spv", &fsz);
 
     /* create renderer */
+    if (!vk_create_renderer(&vk_core, vsdr, vsz, fsdr, fsz,
+                            false, false, 0, 0,
+                            &vk_vert_info, &vk_rnd)) {
+        fprintf(stderr, "Failed to create renderer.\n");
+        return false;
+    }
 
     /* set GLFW callbacks */
 
@@ -137,7 +156,10 @@ display()
 static void
 cleanup()
 {
+    vk_destroy_renderer(&vk_core, &vk_rnd);
+
     vk_cleanup_ctx(&vk_core);
+
     glfwTerminate();
 }