From 73929324f25b32044d0478bf59bbcf25d4a18df5 Mon Sep 17 00:00:00 2001 From: CryHam Date: Sat, 8 Apr 2017 19:38:44 +0200 Subject: [PATCH] added Settings class with window pos, size, etc and xml load, save --- CMakeLists.txt | 2 +- source/App.h | 6 +-- source/AppMain.cpp | 37 +++++++++------- source/App_Init.cpp | 8 ++++ source/Settings.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++ source/Settings.h | 36 ++++++++++++++++ 6 files changed, 170 insertions(+), 20 deletions(-) create mode 100644 source/Settings.cpp create mode 100644 source/Settings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e43e179..eb4e484 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 2.6) -set(PROJECT ccc) +set(PROJECT ckeys) project(${PROJECT}) diff --git a/source/App.h b/source/App.h index b8f023e..5277d3b 100644 --- a/source/App.h +++ b/source/App.h @@ -10,19 +10,17 @@ class App : public AppDraw //-------------------------- App(); bool Init(); + void Resize(int x, int y); bool KeyDown(const sf::Event::KeyEvent& key); void Graph(); // draw graphics void Gui(); // draw Gui - // vars - //-------------------------- - int iFontH = 18; // font height // gui util ----- void SetupGuiClr(); void Sep(int y); // dummy separator void Line(bool dark = false); //-- - + Settings set; }; diff --git a/source/AppMain.cpp b/source/AppMain.cpp index 3085551..f84c13b 100644 --- a/source/AppMain.cpp +++ b/source/AppMain.cpp @@ -13,19 +13,23 @@ AppMain::AppMain() bool AppMain::Run() { + // laod Settings first + //------------------ + App* app = new App(); + app->set.Load(); + Settings& set = app->set; + // Create window //------------------------------------------------ - //VideoMode vm = VideoMode::getDesktopMode(); - VideoMode vm = VideoMode(800, 600); - RenderWindow* window = new RenderWindow( - vm, "SFML ImGui demo", // title + VideoMode(set.xwSize, set.ywSize), + "SFML ImGui", // title Style::Default, //Style::None, ContextSettings()); window->setVerticalSyncEnabled(true); - //window->setPosition(Vector2i(0,0)); + window->setPosition(Vector2i(set.xwPos, set.ywPos)); // ImGui @@ -36,14 +40,16 @@ bool AppMain::Run() io.Fonts->ClearFonts(); // font ImFont* fnt = io.Fonts->AddFontFromFileTTF( - "data/DejaVuLGCSans.ttf", 18); + "data/DejaVuLGCSans.ttf", app->set.iFontGui); Texture* fntTex = new Texture; createFontTexture(*fntTex); setFontTexture(*fntTex); // Init app - App* app = new App(); + //------------------ + Vector2u ws = window->getSize(); + app->Resize(ws.x, ws.y); app->Init(); @@ -59,12 +65,12 @@ bool AppMain::Run() Sprite back(tex); - // pass sfml vars + // pass to app app->pWindow = window; app->pBackgr = &back; app->pFont = &font; app->text.setFont(font); - app->text.setCharacterSize(app->iFontH); + app->text.setCharacterSize(app->set.iFontH); // Loop @@ -81,13 +87,12 @@ bool AppMain::Run() switch (e.type) { - case Event::KeyPressed: - app->KeyDown(e.key); - break; - case Event::Closed: - window->close(); - break; + case Event::KeyPressed: app->KeyDown(e.key); break; + //case Event::KeyReleased: app->KeyUp(e.key); break; + + case Event::Resized: app->Resize(e.size.width, e.size.height); break; + case Event::Closed: set.GetWndDim(window); window->close(); break; } } sf::Time time = timer.restart(); @@ -111,6 +116,8 @@ bool AppMain::Run() // dtor //------------------ + set.Save(); + ImGui::Shutdown(); delete window; delete app; diff --git a/source/App_Init.cpp b/source/App_Init.cpp index 80855df..5f554bc 100644 --- a/source/App_Init.cpp +++ b/source/App_Init.cpp @@ -12,5 +12,13 @@ bool App::Init() { SetupGuiClr(); + return true; } + +void App::Resize(int x, int y) +{ + set.xwSize = x; set.ywSize = y; + if (pWindow) + pWindow->setView(sf::View(sf::FloatRect(0.f, 0.f, set.xwSize, set.ywSize))); +} diff --git a/source/Settings.cpp b/source/Settings.cpp new file mode 100644 index 0000000..4bc4031 --- /dev/null +++ b/source/Settings.cpp @@ -0,0 +1,101 @@ +#include +#include "Settings.h" +#include "../libs/tinyxml2.h" +//#include "Util.h" +using namespace std; using namespace tinyxml2; + + +// ctor +Settings::Settings() +{ + Default(); +} + +void Settings::GetWndDim(sf::Window* wnd) +{ + xwPos = wnd->getPosition().x; + ywPos = wnd->getPosition().y; + xwSize = wnd->getSize().x; + ywSize = wnd->getSize().y; +} + + +// Defaults, init paths +//----------------------------------------------- +void Settings::Default() +{ + iFontH = 18; + iFontGui = 17; + + iCombo = 0; + escQuit = false; + + strcpy(pathSet, "ckeys.xml"); +} + + +/// Load +//------------------------------------------------------------------------------------------------ +bool Settings::Load() +{ + Default(); + + XMLDocument doc; + XMLError er = doc.LoadFile(pathSet); + if (er != XML_SUCCESS) + { /*Can't load: "+file);*/ return false; } + + XMLElement* root = doc.RootElement(); + if (!root) return false; + string rn = root->Name(); + if (rn != "ckeys") return false; + + Default(); + + XMLElement* e; const char* a; + + e = root->FirstChildElement("dim"); + if (e) + { a = e->Attribute("iFontH"); if (a) iFontH = atoi(a); + a = e->Attribute("iFontGui"); if (a) iFontGui = atoi(a); + + a = e->Attribute("combo"); if (a) iCombo = atoi(a); + } + e = root->FirstChildElement("window"); + if (e) + { a = e->Attribute("x"); if (a) xwPos = atoi(a); + a = e->Attribute("y"); if (a) ywPos = atoi(a); + a = e->Attribute("sx"); if (a) xwSize = atoi(a); + a = e->Attribute("sy"); if (a) ywSize = atoi(a); + a = e->Attribute("escQuit"); if (a) escQuit = atoi(a) >0? true: false; + } + return true; +} + +/// Save +//------------------------------------------------------------------------------------------------ +bool Settings::Save() +{ + XMLDocument xml; + XMLElement* root = xml.NewElement("ckeys"); + root->SetAttribute("ver", ver); + XMLElement* e; + + e = xml.NewElement("dim"); + e->SetAttribute("iFontH", iFontH); + e->SetAttribute("iFontGui", iFontGui); + + e->SetAttribute("combo", iCombo); + root->InsertEndChild(e); + + e = xml.NewElement("window"); + e->SetAttribute("x", xwPos); + e->SetAttribute("y", ywPos); + e->SetAttribute("sx", xwSize); + e->SetAttribute("sy", ywSize); + e->SetAttribute("escQuit", escQuit ? 1 : 0); + root->InsertEndChild(e); + + xml.InsertEndChild(root); + return xml.SaveFile(pathSet) == XML_SUCCESS; +} diff --git a/source/Settings.h b/source/Settings.h new file mode 100644 index 0000000..374a714 --- /dev/null +++ b/source/Settings.h @@ -0,0 +1,36 @@ +#pragma once +#include + +namespace sf { class Window; } + + +// App Settings +//------------------------------------------------ +class Settings +{ +public: + const static int ver = 50; // version + + // main ----- + Settings(); + void Default(); + + bool Load(), Save(); + + + // dimensions ----- + int iFontH = 18; // font height in list + int iFontGui = 17; // font height for Gui + + int iCombo = 0; // combo pick id + + + // window ----- + int xwPos = 0, ywPos = 0; + int xwSize = 1024, ywSize = 768; + void GetWndDim(sf::Window* wnd); + + bool escQuit = false; + + char pathSet[260]; +};