aboutsummaryrefslogtreecommitdiff
path: root/src/engine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine.cpp')
-rw-r--r--src/engine.cpp147
1 files changed, 53 insertions, 94 deletions
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;
}