From 6a6dad4cdb1e869f59a87352c0f3ad719ad7ba40 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Wed, 17 Oct 2018 10:32:31 -0400 Subject: [PATCH] Keep window position and size in config --- src/main.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 15b96cd..150b820 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include #include @@ -27,8 +29,70 @@ 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*[]) { - window = SDL_CreateWindow("Graph", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE); + readConfig(); + + window = SDL_CreateWindow("Graph", + windowX, + windowY, + windowWidth, + windowHeight, + SDL_WINDOW_VULKAN | + SDL_WINDOW_RESIZABLE); if(!window) return -1; @@ -49,7 +113,7 @@ int main(int, char*[]) { 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); } @@ -88,6 +152,8 @@ int main(int, char*[]) { delete renderer; + writeConfig(); + SDL_DestroyWindow(window); return 0;