Android 编译通过,但肯定运行不了

This commit is contained in:
xsl
2025-10-16 16:40:41 +08:00
parent fc5d6e5cd8
commit 64a22ca513
4 changed files with 43 additions and 7 deletions
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
+9
View File
@@ -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()
+17 -3
View File
@@ -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<uint32_t>(extensions.size());
createInfo.ppEnabledExtensionNames = extensions.data();
#endif
if (enableValidationLayers) {
createInfo.enabledLayerCount = static_cast<uint32_t>(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
}
+11 -4
View File
@@ -1,8 +1,13 @@
#pragma once
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#ifdef _WIN32
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#else
#include <vulkan/vulkan.h>
#endif
#include <fstream>
#include <iostream>
#include <iostream>
@@ -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<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation"
};
const int kWidth = 480;
const int kHeight = 480;
const uint32_t kWidth = 480;
const uint32_t kHeight = 480;
};