WIP: started the code to use a swapchain and aquire swapchain images
[vkrt] / src / main.c
index d12433e..cd2f140 100644 (file)
@@ -1,8 +1,166 @@
 #include <stdio.h>
 
-#include "vk.h"
+#define GLFW_INCLUDE_VULKAN
+#include <GLFW/glfw3.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ui.h"
+
+
+/* static glfw callbacks */
+
+static void
+clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
+
+static void
+clb_reshape(GLFWwindow *win, int width, int height);
+
+/* static functions */
+
+static bool
+init();
+
+static void
+cleanup();
+
+static void
+display();
+
+
+/* static variables */
+
+static GLFWwindow *win;
+
+static int win_w = 800;
+static int win_h = 600;
+
+static struct vk_ctx vk_core;
+static struct vk_swapchain vk_swap;
 
 int main(int argc, char** argv)
 {
-       printf("hi\n");
+    atexit(cleanup);
+
+    if (!init()) {
+        return 1;
+    }
+
+    /* reshape window once just in case */
+
+    glfwGetWindowSize(win, &win_w, &win_h);
+    clb_reshape(win, win_w, win_h);
+
+    /* event loop */
+
+    while(!glfwWindowShouldClose(win)) {
+        display();
+        glfwPollEvents();
+    }
+
+    return 0;
+}
+
+/* static functions */
+
+static bool
+init()
+{
+    /* initialize GLFW */
+
+    if (!glfwInit()) {
+        fprintf(stderr, "Failed to initialize GLFW.\n");
+        return false;
+    }
+
+    if (glfwVulkanSupported() != GLFW_TRUE) {
+        fprintf(stderr, "Vulkan is not supported on this device.\n");
+        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);
+    win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0);
+
+    if (!win) {
+        fprintf(stderr, "Failed to create GLFW window\n");
+        return false;
+    }
+
+    /* create (Xcb) surface */
+
+    glfwGetFramebufferSize(win, &win_h, &win_h);
+    if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_swap.surface)
+            != VK_SUCCESS) {
+        fprintf(stderr, "Failed to create XCB surface.\n");
+        return false;
+    }
+
+    /* create Vulkan swapchain */
+
+    /* aquire images? */
+
+    /* create shaders */
+
+    /* create semaphores? */
+
+    /* create renderer */
+
+    /* set GLFW callbacks */
+
+    glfwSetKeyCallback(win, clb_key);
+    glfwSetWindowSizeCallback(win, clb_reshape);
+
+    /*
+    glfwSetCursorPosCallback(win, clb_motion);
+    glfwSetMouseButtonCallback(win, clb_mouse);
+    */
+
+    return true;
+}
+
+static void
+display()
+{
+}
+
+static void
+cleanup()
+{
+    vk_cleanup_ctx(&vk_core);
+    glfwTerminate();
+}
+
+/* glfw callbacks */
+
+static void
+clb_key(GLFWwindow *win, int key, int scancode,
+        int action, int mods)
+{
+    if (action == GLFW_PRESS) {
+        switch(key) {
+        case GLFW_KEY_ESCAPE:
+            glfwSetWindowShouldClose(win, GLFW_TRUE);
+            return;
+        }
+    }
+}
+
+static void
+clb_reshape(GLFWwindow *win, int width, int height)
+{
+    /* set viewport */
+
+    win_w = width;
+    win_h = height;
 }