diff --git a/CMakeLists.txt b/CMakeLists.txt index d79aeb8..aa054ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,4 +19,4 @@ endif () include(add_shader.cmake) add_subdirectory("aster") -add_subdirectory("triangle") +add_subdirectory("samples") diff --git a/aster/context.cpp b/aster/context.cpp index 2ce28a6..d78bd78 100644 --- a/aster/context.cpp +++ b/aster/context.cpp @@ -42,6 +42,16 @@ Context::Context(const cstr appName, const Version version, bool enableValidatio { INFO_IF(enableValidation, "Validation Layers enabled"); + if (!glfwInit()) + { + const char *error = nullptr; + const auto code = glfwGetError(&error); + ERROR("GLFW Init failed. Cause: ({}) {}", code, error) + THEN_ABORT(code); + } + + // TODO Get/Check API Version + // Creating Instance const vk::ApplicationInfo appInfo = { .pApplicationName = appName, @@ -109,4 +119,6 @@ Context::~Context() } m_Instance.destroy(nullptr); DEBUG("Instance destroyed"); + + glfwTerminate(); } diff --git a/aster/glfw_context.h b/aster/glfw_context.h deleted file mode 100644 index 7f70138..0000000 --- a/aster/glfw_context.h +++ /dev/null @@ -1,42 +0,0 @@ -// ============================================= -// Aster: glfw_context.h -// Copyright (c) 2020-2024 Anish Bhobe -// ============================================= - -#pragma once - -#include "global.h" - -struct GlfwContext -{ - - static i32 - PostError() noexcept - { - static const char *error = nullptr; - const auto code = glfwGetError(&error); - ERROR("GLFW {}", error); - return code; - } - - inline static u32 m_Count = 0; - - GlfwContext() - { - if (m_Count++ > 0) - return; - if (glfwInit() == GLFW_FALSE) - { - ABORT(PostError()); - } - } - - ~GlfwContext() - { - if (--m_Count == 0) - { - glfwTerminate(); - } - m_Count = 0; - } -}; diff --git a/aster/global.h b/aster/global.h index 218ef27..656d7a8 100644 --- a/aster/global.h +++ b/aster/global.h @@ -121,4 +121,4 @@ struct fmt::formatter> : nested_fo { return write_padded(ctx, [this, str](auto out) { return v10::format_to(out, "{}", nested(str.c_str())); }); } -}; \ No newline at end of file +}; diff --git a/aster/window.cpp b/aster/window.cpp index 2468cbd..104e988 100644 --- a/aster/window.cpp +++ b/aster/window.cpp @@ -6,7 +6,6 @@ #include "window.h" #include "context.h" -#include "glfw_context.h" #include "logger.h" void @@ -50,9 +49,10 @@ Window::Window(const cstr title, Context *context, vk::Extent2D extent, const b8 ELSE_DEBUG("Window '{}' created with resolution '{}x{}'", m_Name, extent.width, extent.height); if (m_Window == nullptr) { - auto code = GlfwContext::PostError(); - glfwTerminate(); - ABORT(code); + const char *error = nullptr; + const auto code = glfwGetError(&error); + ERROR("GLFW Window Creation failed. Cause: ({}) {}", code, error) + THEN_ABORT(code); } if (isFullScreen == false) diff --git a/triangle/CMakeLists.txt b/samples/01_triangle/CMakeLists.txt similarity index 84% rename from triangle/CMakeLists.txt rename to samples/01_triangle/CMakeLists.txt index 17d44cf..30f6efb 100644 --- a/triangle/CMakeLists.txt +++ b/samples/01_triangle/CMakeLists.txt @@ -1,4 +1,4 @@ -# CMakeList.txt ; CMake project for Aster Core +# CMakeList.txt ; CMake project for Triangle cmake_minimum_required(VERSION 3.13) diff --git a/triangle/shader/triangle.vs.hlsl b/samples/01_triangle/shader/triangle.vs.hlsl similarity index 100% rename from triangle/shader/triangle.vs.hlsl rename to samples/01_triangle/shader/triangle.vs.hlsl diff --git a/triangle/shader/white.frag.glsl b/samples/01_triangle/shader/white.frag.glsl similarity index 100% rename from triangle/shader/white.frag.glsl rename to samples/01_triangle/shader/white.frag.glsl diff --git a/triangle/triangle.cpp b/samples/01_triangle/triangle.cpp similarity index 97% rename from triangle/triangle.cpp rename to samples/01_triangle/triangle.cpp index 0aa7068..aa82da8 100644 --- a/triangle/triangle.cpp +++ b/samples/01_triangle/triangle.cpp @@ -1,7 +1,11 @@ +// ============================================= +// Aster: triangle.cpp +// Copyright (c) 2020-2024 Anish Bhobe +// ============================================= + #include "aster/constants.h" #include "aster/context.h" #include "aster/device.h" -#include "aster/glfw_context.h" #include "aster/physical_device.h" #include "aster/window.h" @@ -13,6 +17,8 @@ #include constexpr u32 MAX_FRAMES_IN_FLIGHT = 3; +constexpr auto VERTEX_SHADER_FILE = "shader/triangle.vs.hlsl.spv"; +constexpr auto FRAGMENT_SHADER_FILE = "shader/white.frag.glsl.spv"; bool IsSuitableDevice(const PhysicalDevice *physicalDevice); PhysicalDevice FindSuitableDevice(const PhysicalDevices &physicalDevices); @@ -48,9 +54,8 @@ main(int, char **) { MIN_LOG_LEVEL(Logger::LogType::eInfo); - GlfwContext glfwContext = {}; - Context context = {"Aster", VERSION}; - Window window = {"Aster1", &context, {640, 480}}; + Context context = {"Triangle", VERSION}; + Window window = {"Triangle (Aster)", &context, {640, 480}}; PhysicalDevices physicalDevices = {&window, &context}; PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices); @@ -65,10 +70,7 @@ main(int, char **) Swapchain swapchain = {&window, &device, "Primary Chain"}; - auto vertexShaderFile = "shader/triangle.vs.hlsl.spv"; - auto fragmentShaderFile = "shader/white.frag.glsl.spv"; - - Pipeline pipeline = CreatePipeline(&device, &swapchain, vertexShaderFile, fragmentShaderFile); + Pipeline pipeline = CreatePipeline(&device, &swapchain, VERTEX_SHADER_FILE, FRAGMENT_SHADER_FILE); // Persistent variables vk::Viewport viewport = { diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt new file mode 100644 index 0000000..7e2c004 --- /dev/null +++ b/samples/CMakeLists.txt @@ -0,0 +1,5 @@ +# CMakeList.txt ; CMake project for Samples + +cmake_minimum_required(VERSION 3.13) + +add_subdirectory("01_triangle")