Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
graph/src/main.cpp

205 lines
5.2 KiB
C++
Raw Normal View History

2018-09-26 17:51:22 -04:00
#include <SDL.h>
2018-09-29 21:03:06 -04:00
#include <SDL_vulkan.h>
2018-10-01 21:02:27 -04:00
#include <iostream>
#include <fstream>
#include <sstream>
2018-10-16 08:49:25 -04:00
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "renderer.h"
2018-09-29 21:03:06 -04:00
#include "platform.h"
2018-10-16 08:49:25 -04:00
#include "world.h"
#include "mesh.h"
2018-10-18 21:46:48 -04:00
#include "light.h"
2018-10-25 08:57:36 -04:00
#include "camera.h"
2018-09-29 21:03:06 -04:00
SDL_Window* window = nullptr;
std::vector<const char*> platform::getRequiredExtensions() {
uint32_t count = 0;
SDL_Vulkan_GetInstanceExtensions(window, &count, nullptr);
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
std::vector<const char*> names(count);
SDL_Vulkan_GetInstanceExtensions(window, &count, names.data());
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
return names;
}
2018-09-26 17:51:22 -04:00
uint32_t platform::getTime() {
return SDL_GetTicks();
}
int windowX = SDL_WINDOWPOS_CENTERED;
int windowY = SDL_WINDOWPOS_CENTERED;
int windowWidth = 640;
int windowHeight = 480;
2018-10-17 17:11:26 -04:00
int windowFullscreen = 0;
int toInt(const std::string &str) {
std::stringstream ss(str);
int num;
if((ss >> num).fail())
return -1;
2018-10-26 20:56:06 -04:00
return num;
}
void readConfig() {
std::ifstream file("config.txt");
if(!file)
return;
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);
2018-10-17 17:11:26 -04:00
} else if(key == "fullscreen") {
windowFullscreen = toInt(value);
}
}
}
}
void writeConfig() {
std::ofstream file("config.txt");
if(!file)
return;
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";
2018-10-17 17:11:26 -04:00
file << "fullscreen=" << windowFullscreen << "\n";
}
2018-10-26 20:56:06 -04:00
int main(int argc, char* argv[]) {
readConfig();
window = SDL_CreateWindow("Graph",
windowX,
windowY,
windowWidth,
windowHeight,
SDL_WINDOW_VULKAN |
SDL_WINDOW_RESIZABLE);
2018-10-26 20:56:06 -04:00
if(window == nullptr)
return -1;
2018-10-26 20:56:06 -04:00
SDL_SetWindowFullscreen(window, windowFullscreen == 1 ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
2018-10-16 08:49:25 -04:00
2018-10-26 20:56:06 -04:00
auto renderer = new Renderer();
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
VkSurfaceKHR surface = nullptr;
SDL_Vulkan_CreateSurface(window, renderer->getInstance(), &surface);
2018-10-16 08:49:25 -04:00
2018-09-29 21:03:06 -04:00
RenderTarget* target = renderer->createSurfaceRenderTarget(surface);
2018-10-16 08:49:25 -04:00
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("data/suzanne.obj", aiProcess_Triangulate);
2018-10-16 08:49:25 -04:00
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);
2018-10-16 13:03:26 -04:00
vertex.normal = glm::vec3(m->mNormals[i].x, m->mNormals[i].y, m->mNormals[i].z);
2018-10-16 08:49:25 -04:00
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);
2018-10-26 20:56:06 -04:00
auto light = new Light();
2018-10-18 21:46:48 -04:00
light->position.y = 5;
2018-10-18 21:46:48 -04:00
world.lights.push_back(light);
2018-10-16 08:49:25 -04:00
2018-10-25 08:57:36 -04:00
Camera camera;
camera.position.y = 1;
camera.position.z = 3;
2018-09-26 17:51:22 -04:00
bool running = true;
while(running) {
SDL_Event event = {};
2018-10-26 20:56:06 -04:00
while(SDL_PollEvent(&event) > 0) {
2018-09-26 17:51:22 -04:00
if(event.type == SDL_QUIT)
running = false;
2018-10-16 08:49:25 -04:00
2018-10-01 21:02:27 -04:00
if(event.type == SDL_WINDOWEVENT) {
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
target = renderer->createSurfaceRenderTarget(surface, target);
}
2018-10-17 17:11:26 -04:00
if(event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_F11) {
if(windowFullscreen == 1) {
SDL_SetWindowFullscreen(window, 0);
windowFullscreen = 0;
} else {
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
windowFullscreen = 1;
}
2018-10-17 17:11:26 -04:00
target = renderer->createSurfaceRenderTarget(surface, target);
}
2018-10-24 21:13:55 -04:00
if(event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_F12) {
renderer->takeScreenshot(target);
}
2018-09-26 17:51:22 -04:00
}
2018-10-16 08:49:25 -04:00
2018-10-25 08:57:36 -04:00
camera.position.x = sin(platform::getTime() / 500.0f);
renderer->render(world, camera, target);
2018-09-26 17:51:22 -04:00
}
2018-10-24 19:29:44 -04:00
delete light;
2018-10-16 08:49:25 -04:00
2018-10-16 09:01:14 -04:00
renderer->destroyMeshBuffers(mesh);
2018-10-24 19:29:44 -04:00
delete mesh;
2018-09-29 21:03:06 -04:00
renderer->destroyRenderTarget(target);
2018-10-16 08:49:25 -04:00
2018-10-01 21:02:27 -04:00
vkDestroySurfaceKHR(renderer->getInstance(), surface, nullptr);
2018-10-16 08:49:25 -04:00
delete renderer;
2018-10-16 08:49:25 -04:00
writeConfig();
SDL_DestroyWindow(window);
2018-10-16 08:49:25 -04:00
2018-09-26 17:51:22 -04:00
return 0;
}