From d3e00990b86277e490e0f68a1cd6f092b28d54aa Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 26 Jan 2025 22:50:01 +0100 Subject: Move game package --- hk21/game/BUILD.bazel | 12 ++++++ hk21/game/main.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 hk21/game/BUILD.bazel create mode 100644 hk21/game/main.cpp (limited to 'hk21') diff --git a/hk21/game/BUILD.bazel b/hk21/game/BUILD.bazel new file mode 100644 index 0000000..9aa2e2b --- /dev/null +++ b/hk21/game/BUILD.bazel @@ -0,0 +1,12 @@ +cc_binary( + name = "game", + srcs = [ + "main.cpp", + ], + deps = [ + "@asl//asl", + "@asl//asl/log", + "@sdl3_windows//:sdl3", + "//hk21/vulkan_loader", + ], +) diff --git a/hk21/game/main.cpp b/hk21/game/main.cpp new file mode 100644 index 0000000..03bc77b --- /dev/null +++ b/hk21/game/main.cpp @@ -0,0 +1,117 @@ +#include +#include +#include + +#include +#include +#include + +#include "hk21/vulkan_loader/api.hpp" + +#define VK_ALLOCATOR nullptr + +int SDL_main(int /* argc */, char* /* argv */[]) +{ + SDL_Init(SDL_INIT_VIDEO); + SDL_Window* window = SDL_CreateWindow("HK-21 - 460nm", 1280, 720, SDL_WINDOW_VULKAN); + + SDL_ShowWindow(window); + + asl::buffer instance_extensions; + asl::buffer device_extensions; + asl::buffer layers; + + { + uint32_t count = 0; + const char* const* extensions = SDL_Vulkan_GetInstanceExtensions(&count); + for (uint32_t i = 0; i < count; ++i) + { + instance_extensions.push(extensions[i]); // NOLINT(*-pointer-arithmetic) + } + } + + layers.push("VK_LAYER_KHRONOS_validation"); + layers.push("VK_LAYER_LUNARG_monitor"); + + instance_extensions.push(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); + + device_extensions.push(VK_KHR_SWAPCHAIN_EXTENSION_NAME); + + auto vkGetInstanceProcAddr = asl::bit_cast(SDL_Vulkan_GetVkGetInstanceProcAddr()); + auto status = vulkan_loader::load_global(vkGetInstanceProcAddr); + if (!status.ok()) + { + ASL_LOG_ERROR("Couldn't load global Vulkan functions: {}", status); + return 1; + } + + uint32_t version{}; + vkEnumerateInstanceVersion(&version); + + uint32_t vk_major = VK_API_VERSION_MAJOR(version); // NOLINT + uint32_t vk_minor = VK_API_VERSION_MINOR(version); // NOLINT + + if (vk_major != 1 || vk_minor < 3) + { + ASL_LOG_ERROR("Incompatible Vulkan version: {}.{}", vk_major, vk_minor); + return 1; + } + + VkApplicationInfo app_info{ + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .pNext = nullptr, + .pApplicationName = "HK-21", + .applicationVersion = 0, + .pEngineName = "Custom", + .engineVersion = 0, + .apiVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), // NOLINT + }; + + VkInstanceCreateInfo instance_create_info{ + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .pApplicationInfo = &app_info, + .enabledLayerCount = static_cast(layers.size()), + .ppEnabledLayerNames = layers.data(), + .enabledExtensionCount = static_cast(instance_extensions.size()), + .ppEnabledExtensionNames = instance_extensions.data(), + }; + + VkInstance instance{}; + VkResult res = vkCreateInstance(&instance_create_info, VK_ALLOCATOR, &instance); + if (res != VK_SUCCESS) + { + ASL_LOG_ERROR("Couldn't create Vulkan instance: {}", res); + return 1; + } + + status = vulkan_loader::load_instance(vkGetInstanceProcAddr, instance); + if (!status.ok()) + { + ASL_LOG_ERROR("Couldn't load instance Vulkan functions: {}", status); + return 1; + } + + bool running = true; + while (running) + { + SDL_Event e; + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_EVENT_QUIT || (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE)) + { + running = false; + } + } + + SDL_Delay(16); + } + + vkDestroyInstance(instance, VK_ALLOCATOR); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +} + -- cgit