X-Git-Url: https://eleni.mutantstargoat.com/git/?p=vkrt;a=blobdiff_plain;f=src%2Fmain.c;h=cd2f1400253c367fca66c55c62ce6ce1d163bab2;hp=d12433eded9d3636f27a78fc1d103b7f57ee5028;hb=9ba38e2fb3b09c2cebfa780ed548c2a6c5a9ff79;hpb=6d2c68e0e885a93a4b94ca56c49240728d4a6e35 diff --git a/src/main.c b/src/main.c index d12433e..cd2f140 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,166 @@ #include -#include "vk.h" +#define GLFW_INCLUDE_VULKAN +#include +#include +#include + +#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; }