From 5076e4609aa5bc674255e7165bf1e834c875bfc8 Mon Sep 17 00:00:00 2001 From: Warwick Date: Thu, 8 Feb 2024 12:56:47 +0000 Subject: [PATCH] Starting to move into setting up vulkan --- src/main.c | 125 ++++++++++++++++++++++++++++++----------------------- src/vec.c | 5 +++ src/vec.h | 1 + 3 files changed, 78 insertions(+), 53 deletions(-) diff --git a/src/main.c b/src/main.c index 9438e8c..c8fc353 100644 --- a/src/main.c +++ b/src/main.c @@ -1,65 +1,84 @@ #include "vec.h" #include +#include +#include #include #include #include +#include +#include +#include + +VkInstance create_instance(SDL_Window *window) { + VkApplicationInfo app_info; + app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + app_info.pApplicationName = "Hello Triangle"; + app_info.applicationVersion = VK_MAKE_VERSION(1, 0, 0); + app_info.pEngineName = "No Engine"; + app_info.engineVersion = VK_MAKE_VERSION(1, 0, 0); + app_info.apiVersion = VK_API_VERSION_1_0; + + uint32_t sdl_extension_count = 0; + const char **sdl_extensions; + if (!SDL_Vulkan_GetInstanceExtensions(window, &sdl_extension_count, + sdl_extensions)) { + fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError()); + printf("Couldn't get required Vulkan Extentions for window\n"); + exit(EXIT_FAILURE); + } + + VkInstanceCreateInfo create_info; + create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + create_info.pApplicationInfo = &app_info; + create_info.enabledExtensionCount = sdl_extension_count; + create_info.ppEnabledExtensionNames = sdl_extensions; + create_info.enabledLayerCount = 0; + + VkInstance instance; + if (vkCreateInstance(&create_info, NULL, &instance)) { + printf("failed to create vulkan instance!"); + exit(EXIT_FAILURE); + } + return instance; +} + +void initVulkan(SDL_Window *window) { create_instance(window); } int main() { - vector *vec = vec_create(sizeof(int)); - for (int x = 0; x < 110; x++) { - vec_push_back(vec, &x); + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("Failed to initialize the SDL2 library\n"); + return EXIT_FAILURE; } - int i = 99999999; - vec_insert(vec, &i, 5); - vec_push_front(vec, &i); - printf("Capacity before shrink: %zu | ", vec_capacity(vec)); - vec_shrink_to_fit(vec); - printf("Capacity after: %zu\n", vec_capacity(vec)); - vec_erase(vec, 0); - vec_erase(vec, 101); - for (int x = 0; x < 110; x++) { - printf("%d ", *(int *)vec_get(vec, x)); + SDL_Vulkan_LoadLibrary(NULL); + SDL_Window *window = SDL_CreateWindow( + "SDL2 Vulkan Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 680, + 480, SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN); + if (!window) { + printf("Failed to create window\n"); + return EXIT_FAILURE; } - printf("\n"); - vec_destroy(vec); - // if (SDL_Init(SDL_INIT_VIDEO) < 0) { - // printf("Failed to initialize the SDL2 library\n"); - // return -1; - // } + initVulkan(window); - // SDL_Window *window = - // SDL_CreateWindow("SDL2 Window", SDL_WINDOWPOS_CENTERED, - // SDL_WINDOWPOS_CENTERED, 680, 480, SDL_WINDOW_VULKAN); - // if (!window) { - // printf("Failed to create window\n"); - // return -1; - // } - - // SDL_Surface *window_surface = SDL_GetWindowSurface(window); - // if (!window_surface) { - // printf("Failed to get the surface from the window\n"); - // return -1; - // } - - // bool running = true; - // while (running) { - // SDL_Event input; - // while (SDL_PollEvent(&input) > 0) { - // if (input.type == SDL_QUIT) { - // running = false; - // } - // switch (input.type) { - // case SDL_QUIT: - // running = false; - // case SDL_KEYDOWN: { - // const Uint8 *keys = SDL_GetKeyboardState(NULL); - // if (keys[SDL_SCANCODE_ESCAPE]) - // running = false; - // } - // } - // } - // } - - // SDL_UpdateWindowSurface(window); + bool running = true; + while (running) { + SDL_Event input; + while (SDL_PollEvent(&input) > 0) { + if (input.type == SDL_QUIT) { + running = false; + } + switch (input.type) { + case SDL_QUIT: + running = false; + case SDL_KEYDOWN: { + const Uint8 *keys = SDL_GetKeyboardState(NULL); + if (keys[SDL_SCANCODE_ESCAPE]) + running = false; + } + } + } + } + SDL_DestroyWindow(window); + SDL_Quit(); + return EXIT_SUCCESS; } diff --git a/src/vec.c b/src/vec.c index e51db7d..a9376c5 100644 --- a/src/vec.c +++ b/src/vec.c @@ -101,3 +101,8 @@ size_t vec_capacity(vector *vec) { assert(vec != NULL); return vec->capacity; } + +void* vec_data(vector *vec) { + assert(vec != NULL); + return vec->elements; +} diff --git a/src/vec.h b/src/vec.h index 72c324f..96073e5 100644 --- a/src/vec.h +++ b/src/vec.h @@ -22,3 +22,4 @@ void vec_pop_back(vector *vec); void *vec_get(vector *vec, uint index); size_t vec_size(vector *vec); size_t vec_capacity(vector *vec); +void* vec_data(vector *vec);