diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c8397c9 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 711184f..811c413 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,10 +8,16 @@ set(CMAKE_CXX_EXTENSIONS OFF) if(CMAKE_SYSTEM_NAME STREQUAL "Android") message(STATUS "Building for Android") + + include_directories( + + ) add_library(sample SHARED app/src/main/cpp/main.cpp app/src/main/cpp/AndroidOut.cpp + vulkan/Application.h + vulkan/Application.cpp ) find_package(game-activity REQUIRED CONFIG) @@ -20,11 +26,14 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Android") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u Java_com_google_androidgamesdk_GameActivity_initializeNativeCode") + find_package(Vulkan REQUIRED) target_link_libraries(sample game-activity::game-activity_static + Vulkan::Vulkan android log) + endif() diff --git a/vulkan/Application.cpp b/vulkan/Application.cpp index d20dc52..33b2981 100644 --- a/vulkan/Application.cpp +++ b/vulkan/Application.cpp @@ -10,7 +10,7 @@ void Application::createInstance() VkApplicationInfo appInfo{}; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; - appInfo.pApplicationName = "Hello Triangle"; + appInfo.pApplicationName = "Sample"; appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); appInfo.pEngineName = "No Engine"; appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); @@ -20,6 +20,7 @@ void Application::createInstance() createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; createInfo.pApplicationInfo = &appInfo; +#ifdef _WIN32 // 获取 GLFW 所需的扩展 uint32_t glfwExtensionCount = 0; const char** glfwExtensions; @@ -33,6 +34,7 @@ void Application::createInstance() createInfo.enabledExtensionCount = static_cast(extensions.size()); createInfo.ppEnabledExtensionNames = extensions.data(); +#endif if (enableValidationLayers) { createInfo.enabledLayerCount = static_cast(validationLayers.size()); @@ -49,12 +51,16 @@ void Application::createInstance() void Application::initWindow() { +#ifdef _WIN32 glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); window = glfwCreateWindow(kWidth, kHeight, "Vulkan", nullptr, nullptr); +#else + +#endif } void Application::run() @@ -66,9 +72,11 @@ void Application::run() } void Application::createSurface() { +#ifdef _WIN32 if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) { throw std::runtime_error("failed to create window surface!"); } +#endif } static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( @@ -202,11 +210,15 @@ void Application::createCommandBuffer() { void Application::mainLoop() { +#ifdef _WIN32 while (!glfwWindowShouldClose(window)) { glfwPollEvents(); drawFrame(); // 在这里调用 } +#else + +#endif vkDeviceWaitIdle(device); // 等待设备空闲 } @@ -417,8 +429,8 @@ void Application::createPipelineLayout() { void Application::createGraphicsPipeline() { // 顶点着色器和片段着色器 - auto vertShaderCode = readFile("D:/Graphics/vulkan_demo/shaders/simple_shader.vert.spv"); - auto fragShaderCode = readFile("D:/Graphics/vulkan_demo/shaders/simple_shader.frag.spv"); + auto vertShaderCode = readFile("shaders/simple_shader.vert.spv"); + auto fragShaderCode = readFile("shaders/simple_shader.frag.spv"); // 创建着色器模块 VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); @@ -727,9 +739,11 @@ void Application::cleanup() { // 销毁 Vulkan 实例 vkDestroyInstance(instance, nullptr); +#ifdef _WIN32 // 销毁 GLFW 窗口 glfwDestroyWindow(window); // 终止 GLFW glfwTerminate(); +#endif } \ No newline at end of file diff --git a/vulkan/Application.h b/vulkan/Application.h index 2103c4d..1993c9a 100644 --- a/vulkan/Application.h +++ b/vulkan/Application.h @@ -1,8 +1,13 @@  #pragma once -#define GLFW_INCLUDE_VULKAN -#include +#ifdef _WIN32 + #define GLFW_INCLUDE_VULKAN + #include +#else + #include +#endif + #include #include #include @@ -61,7 +66,9 @@ public: void run(); private: +#ifdef _WIN32 GLFWwindow *window; +#endif VkInstance instance; void initWindow(); @@ -113,6 +120,6 @@ private: const std::vector validationLayers = { "VK_LAYER_KHRONOS_validation" }; - const int kWidth = 480; - const int kHeight = 480; + const uint32_t kWidth = 480; + const uint32_t kHeight = 480; }; \ No newline at end of file