#include #include #include #include #include #include #include #include #include "renderer.h" #include "platform.h" #include "world.h" #include "mesh.h" SDL_Window* window = nullptr; std::vector platform::getRequiredExtensions() { uint32_t count = 0; SDL_Vulkan_GetInstanceExtensions(window, &count, nullptr); std::vector names(count); SDL_Vulkan_GetInstanceExtensions(window, &count, names.data()); return names; } uint32_t platform::getTime() { return SDL_GetTicks(); } int windowX = SDL_WINDOWPOS_CENTERED; int windowY = SDL_WINDOWPOS_CENTERED; int windowWidth = 640; int windowHeight = 480; int toInt(const std::string &str) { std::stringstream ss(str); int num; if((ss >> num).fail()) return -1; else return num; } void readConfig() { std::ifstream file("config.txt"); std::string line; while(std::getline(file, line)) { if(line.find('=') != std::string::npos) { std::string key, value; key = line.substr(0, line.find('=')); value = line.substr(line.find('=') + 1, line.length()); if(key == "x") { windowX = toInt(value); } else if(key == "y") { windowY = toInt(value); } else if(key == "width") { windowWidth = toInt(value); } else if(key == "height") { windowHeight = toInt(value); } } } } void writeConfig() { std::ofstream file("config.txt"); int x, y; SDL_GetWindowPosition(window, &x, &y); file << "x=" << x << "\n"; file << "y=" << y << "\n"; int w, h; SDL_GetWindowSize(window, &w, &h); file << "width=" << w << "\n"; file << "height=" << h << "\n"; } int main(int, char*[]) { readConfig(); window = SDL_CreateWindow("Graph", windowX, windowY, windowWidth, windowHeight, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE); if(!window) return -1; Renderer* renderer = new Renderer(); VkSurfaceKHR surface = nullptr; SDL_Vulkan_CreateSurface(window, renderer->getInstance(), &surface); RenderTarget* target = renderer->createSurfaceRenderTarget(surface); Assimp::Importer importer; const aiScene* scene = importer.ReadFile("suzanne.obj", aiProcess_Triangulate); aiMesh* m = scene->mMeshes[0]; Mesh* mesh = new Mesh(); for(unsigned int i = 0; i < m->mNumVertices; i++) { Vertex vertex; vertex.position = glm::vec3(m->mVertices[i].x, m->mVertices[i].y, m->mVertices[i].z); vertex.normal = glm::vec3(m->mNormals[i].x, m->mNormals[i].y, m->mNormals[i].z); mesh->vertices.push_back(vertex); } for(unsigned int i = 0; i < m->mNumFaces; i++) { aiFace face = m->mFaces[i]; for(unsigned int j = 0; j < face.mNumIndices; j++) mesh->indices.push_back(face.mIndices[j]); } renderer->fillMeshBuffers(mesh); World world; world.meshes.push_back(mesh); bool running = true; while(running) { SDL_Event event = {}; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) running = false; if(event.type == SDL_WINDOWEVENT) { if(event.window.event == SDL_WINDOWEVENT_RESIZED) target = renderer->createSurfaceRenderTarget(surface, target); } } renderer->render(world, target); } renderer->destroyMeshBuffers(mesh); renderer->destroyRenderTarget(target); vkDestroySurfaceKHR(renderer->getInstance(), surface, nullptr); delete renderer; writeConfig(); SDL_DestroyWindow(window); return 0; }