From f57582b96081a17b8bd5e4b353697faac050ef7b Mon Sep 17 00:00:00 2001 From: Warwick Date: Tue, 18 Jun 2024 16:16:40 +0100 Subject: [PATCH] Moving over to glfw and gonna try to go through vulkan tutorial again --- CMakeLists.txt | 18 ++++----- src/main.c | 103 ++++++++++++++-------------------------------- src/vec.c | 108 ------------------------------------------------- src/vec.h | 25 ------------ 4 files changed, 39 insertions(+), 215 deletions(-) delete mode 100644 src/vec.c delete mode 100644 src/vec.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 15e1d22..f657fc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,17 +10,16 @@ set(CMAKE_C_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Dependancies -find_package(SDL2 REQUIRED) -find_package(SDL2_IMAGE REQUIRED) find_package(Vulkan REQUIRED) +find_package(glfw3 REQUIRED) +set(GLFW_LIBRARIES glfw) find_package(glm REQUIRED) # Include dirs include_directories( - ${SDL2_INCLUDE_DIRS} - ${SDL2_IMAGE_INCLUDE_DIRS} - ${Vulkan_INCLUDE_DIRS} - ${GLM_INCLUDE_DIRS} + ${Vulkan_INCLUDE_DIRS} + ${GLFW_INCLUDE_DIRS} + ${GLM_INCLUDE_DIRS} ) # Executables @@ -32,10 +31,9 @@ add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES}) # Link Libraries target_link_libraries(${PROJECT_NAME} - ${SDL2_IMAGE_LIBRARIES} - ${SDL2_LIBRARIES} - ${Vulkan_LIBRARIES} - ${GLM_LIBRARY_DIRS} + ${GLFW_LIBRARIES} + ${Vulkan_LIBRARIES} + ${GLM_LIBRARY_DIRS} ) # Compile Shaders diff --git a/src/main.c b/src/main.c index c8fc353..8749ec7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,84 +1,43 @@ -#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; +#define GLFW_INCLUDE_VULKAN +#include - 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); +typedef struct Application { + GLFWwindow *window; +} Application; + +void initWindow(Application *app); +void initVulkan(); +void mainLoop(Application *app); +void cleanup(Application *app); + +void initWindow(Application *app) { + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + app->window = glfwCreateWindow(800, 600, "Vulkan", NULL, NULL); +} +void initVulkan() {} +void mainLoop(Application *app) { + while (!glfwWindowShouldClose(app->window)) { + glfwPollEvents(); } - - 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 cleanup(Application *app) { + glfwDestroyWindow(app->window); + glfwTerminate(); } -void initVulkan(SDL_Window *window) { create_instance(window); } +int main(void) { + Application app; -int main() { - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - printf("Failed to initialize the SDL2 library\n"); - return EXIT_FAILURE; - } - 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; - } + initWindow(&app); + initVulkan(); + mainLoop(&app); + cleanup(&app); - initVulkan(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 deleted file mode 100644 index a9376c5..0000000 --- a/src/vec.c +++ /dev/null @@ -1,108 +0,0 @@ -#include "vec.h" -#include -#include -#include - -#define DEFAULT_VECTOR_CAPACITY 4 - -typedef struct vector { - size_t capacity; - size_t size; - size_t element_size; - void *elements; -} vector; - -vector *vec_create(size_t element_size) { - vector *vec = malloc(sizeof(vector)); - vec->element_size = element_size; - vec->capacity = DEFAULT_VECTOR_CAPACITY; - vec->elements = malloc(vec->capacity * vec->element_size); - - return vec; -}; -void vec_destroy(vector *vec) { - assert(vec != NULL); - free(vec->elements); - free(vec); -} - -void vec_grow(vector *vec) { - assert(vec != NULL); - if (vec->size < vec->capacity) - return; - - // Grow the vector by half - vec->capacity += vec->capacity / 2; - vec->elements = reallocarray(vec->elements, vec->capacity, vec->element_size); - - assert(vec->elements != NULL); -} - -void vec_shrink_to_fit(vector *vec) { - assert(vec != NULL); - vec->capacity = vec->size; - vec->elements = reallocarray(vec->elements, vec->size, vec->element_size); -} - -void vec_push_back(vector *vec, void *data) { - assert(vec != NULL); - vec_grow(vec); - - memcpy(vec->elements + (vec->size * vec->element_size), data, - vec->element_size); - vec->size++; -} - -void vec_push_front(vector *vec, void *data) { vec_insert(vec, data, 0); } - -void vec_insert(vector *vec, void *data, uint index) { - assert(vec != NULL); - assert(index <= vec->size); - // TODO: handle wraparound - vec_grow(vec); - - memcpy(vec->elements + (index + 1) * vec->element_size, - vec->elements + index * vec->element_size, - (vec->size - index) * vec->element_size); - - memcpy(vec->elements + (index * vec->element_size), data, vec->element_size); - vec->size++; -} -void vec_erase(vector *vec, uint index) { - assert(vec != NULL); - assert(index < vec->size); - // TODO: handle wraparound - - memcpy(vec->elements + index * vec->element_size, - vec->elements + (index + 1) * vec->element_size, - (vec->size - index) * vec->element_size); - vec->size--; -} -void vec_pop_front(vector *vec) { vec_erase(vec, 0); } -void vec_pop_back(vector *vec) { - assert(vec != NULL); - assert(vec->size < 0); - vec->size--; -} - -void *vec_get(vector *vec, uint index) { - // TODO: handle wraparound - assert(vec != NULL); - assert(index <= vec->size); - return vec->elements + index * vec->element_size; -} - -size_t vec_size(vector *vec) { - assert(vec != NULL); - return vec->size; -} - -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 deleted file mode 100644 index 96073e5..0000000 --- a/src/vec.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include - -typedef struct vector vector; - -vector *vec_create(size_t element_size); -void vec_destroy(vector *vec); - -// Checks and grows vector only if necessary -void vec_grow(vector *vec); -void vec_shrink_to_fit(vector *vec); - -// Assumes data is the same size as the element size -// Which means you can only push one element at a time -void vec_push_back(vector *vec, void *data); -void vec_push_front(vector *vec, void *data); -void vec_insert(vector *vec, void *data, uint index); -void vec_erase(vector *vec, uint index); -void vec_pop_front(vector *vec); -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);