Skip to content

Commit

Permalink
added Settings class with window pos, size, etc and xml load, save
Browse files Browse the repository at this point in the history
  • Loading branch information
cryham committed Apr 8, 2017
1 parent 0b37da3 commit 7392932
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 2.6)
set(PROJECT ccc)
set(PROJECT ckeys)
project(${PROJECT})


Expand Down
6 changes: 2 additions & 4 deletions source/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
37 changes: 22 additions & 15 deletions source/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();


Expand All @@ -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
Expand All @@ -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();
Expand All @@ -111,6 +116,8 @@ bool AppMain::Run()

// dtor
//------------------
set.Save();

ImGui::Shutdown();
delete window;
delete app;
Expand Down
8 changes: 8 additions & 0 deletions source/App_Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
101 changes: 101 additions & 0 deletions source/Settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <SFML/Window.hpp>
#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;
}
36 changes: 36 additions & 0 deletions source/Settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <SFML/Config.hpp>

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];
};

0 comments on commit 7392932

Please sign in to comment.