diff options
author | vimene <vincent.menegaux@gmail.com> | 2025-01-16 14:08:40 +0100 |
---|---|---|
committer | vimene <vincent.menegaux@gmail.com> | 2025-01-16 14:08:40 +0100 |
commit | fc8e5fc191822c244f51335e49d44f5a047b128e (patch) | |
tree | cad2f09ef78d9b61de244ff014e8db800cb70d1f | |
parent | 0485661fd05af686acf886cb2c96dd4a5e38bb4d (diff) | |
download | engine-fc8e5fc191822c244f51335e49d44f5a047b128e.tar.gz |
started working on vulkan support
-rw-r--r-- | configure.ac | 14 | ||||
-rw-r--r-- | src/engine.cpp | 147 |
2 files changed, 64 insertions, 97 deletions
diff --git a/configure.ac b/configure.ac index fe47a8e..5d252bc 100644 --- a/configure.ac +++ b/configure.ac @@ -10,9 +10,17 @@ AC_CONFIG_FILES([Makefile]) DEPS_CPPFLAGS="" DEPS_LIBS="" -PKG_CHECK_MODULES([SDL], [sdl2 >= 2.0]) -DEPS_CPPFLAGS="$SDL_CFLAGS $DEPS_CPPFLAGS" -DEPS_LIBS="$SDL_LIBS $DEPS_LIBS" +PKG_CHECK_MODULES([GLFW3], [glfw3 >= 3.3.10]) +DEPS_CPPFLAGS="$GLFW3_CFLAGS $DEPS_CPPFLAGS" +DEPS_LIBS="$GLFW3_LIBS $DEPS_LIBS" + +PKG_CHECK_MODULES([VULKAN], [vulkan >= 1.3.275]) +DEPS_CPPFLAGS="$VULKAN_CFLAGS $DEPS_CPPFLAGS" +# Not sure why they are needed +DEPS_LIBS="-ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi $DEPS_LIBS" +DEPS_LIBS="$VULKAN_LIBS $DEPS_LIBS" + +// TODO: add check for Vulkan Utility Library PKG_CHECK_MODULES([NCURSES], [ncurses >= 6.4.20230625], [ DEPS_CPPFLAGS="$NCURSES_CFLAGS $DEPS_CPPFLAGS" diff --git a/src/engine.cpp b/src/engine.cpp index 453f8c5..d276d70 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -12,10 +12,13 @@ #include <iterator> #include <memory> #include <utility> -#include <SDL.h> #include <numbers> #include <optional> +#define GLFW_INCLUDE_VULKAN +#include <GLFW/glfw3.h> +#include <vulkan/vk_enum_string_helper.h> + #ifdef ENABLE_NCURSES #include <ncurses.h> #endif @@ -290,102 +293,58 @@ static int main_term() { #define SCREEN_HEIGHT 480 static int main_graphical() { - SDL_Window* window = nullptr; - SDL_Renderer* renderer = nullptr; - SDL_Texture* texture = nullptr; - - // init - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - std::cerr << "Error: SDL_Init error: " << SDL_GetError() << std::endl; - return EXIT_FAILURE; - } - window = SDL_CreateWindow("Engine", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); - if (window == nullptr) { - SDL_Quit(); - std::cerr << "Error: SDL_CreateWindow error: " << SDL_GetError() << std::endl; - return EXIT_FAILURE; + // init window + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Engine", nullptr, nullptr); + + // init Vulkan + // init Vulkan - create instance + VkApplicationInfo app_info{}; + app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + app_info.pApplicationName = "engine - test"; + app_info.applicationVersion = VK_MAKE_VERSION(1, 0, 0); + app_info.pEngineName = "engine"; + app_info.engineVersion = VK_MAKE_VERSION(1, 0, 0); + app_info.apiVersion = VK_API_VERSION_1_0; + + uint32_t glfw_extension_count; + const char** glfw_extensions; + glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count); + + uint32_t extension_count; + vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, nullptr); + std::vector<VkExtensionProperties> extensions(extension_count); + vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, extensions.data()); + + std::cout << "vulkan extensions:\n"; + for (const auto& extension : extensions) + std::cout << " " << extension.extensionName << "\n"; + + VkInstanceCreateInfo create_info{}; + create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + create_info.pApplicationInfo = &app_info; + + create_info.enabledExtensionCount = glfw_extension_count; + create_info.ppEnabledExtensionNames = glfw_extensions; + create_info.enabledLayerCount = 0; + + VkInstance instance; + if (VkResult res = vkCreateInstance(&create_info, nullptr, &instance); res != VK_SUCCESS) { + std::cerr << "failed to create instance, error code: " << string_VkResult(res) << std::endl; + std::exit(EXIT_FAILURE); } - renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); - texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT); - Renderer<PixelFrameBuffer> engine_renderer{PixelFrameBuffer{SCREEN_WIDTH, SCREEN_HEIGHT}}; - - SDL_Event e; - - scene_main(engine_renderer, Matrix4::idty(), - [&](Scene& scene, auto& kb, auto& mouse) { - (void) scene; - SDL_UpdateTexture(texture, nullptr, engine_renderer.fb.pixels(), SCREEN_WIDTH * 4); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, texture, nullptr, nullptr); - SDL_RenderPresent(renderer); - SDL_UpdateWindowSurface(window); - SDL_SetRelativeMouseMode(SDL_TRUE); - bool cont = true; - if (SDL_WaitEventTimeout(&e, 10)) { - do { - switch (e.type) { - case SDL_QUIT: - cont = false; - break; - case SDL_KEYDOWN: - switch (e.key.keysym.sym) { - case SDLK_z: - kb.key_down_event(KeyboardKey::fw); - break; - case SDLK_q: - kb.key_down_event(KeyboardKey::key_left); - break; - case SDLK_s: - kb.key_down_event(KeyboardKey::bw); - break; - case SDLK_d: - kb.key_down_event(KeyboardKey::key_right); - break; - case SDLK_LCTRL: - kb.key_down_event(KeyboardKey::zoom); - break; - } - break; - case SDL_KEYUP: - switch (e.key.keysym.sym) { - case SDLK_z: - kb.key_up_event(KeyboardKey::fw); - break; - case SDLK_q: - kb.key_up_event(KeyboardKey::key_left); - break; - case SDLK_s: - kb.key_up_event(KeyboardKey::bw); - break; - case SDLK_d: - kb.key_up_event(KeyboardKey::key_right); - break; - case SDLK_LCTRL: - kb.key_up_event(KeyboardKey::zoom); - break; - case SDLK_ESCAPE: - cont = false; - break; - } - break; - case SDL_MOUSEMOTION: - mouse.mouse_motion_event(Vector2( - static_cast<float>(e.motion.xrel) * .01f, - static_cast<float>(e.motion.yrel) * .01f)); - break; - } - } while (SDL_PollEvent(&e)); - } - return cont; - } - ); + // main loop + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + } - // terminate - SDL_DestroyTexture(texture); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); + // cleanup + vkDestroyInstance(instance, nullptr); + glfwDestroyWindow(window); + glfwTerminate(); return EXIT_SUCCESS; } |