Simplify findCVar

This commit is contained in:
Phillip Stephens 2015-11-22 00:01:51 -08:00
parent 9c053b53e8
commit b7fda5a620
3 changed files with 28 additions and 17 deletions

View File

@ -3,33 +3,43 @@
#include <Specter/Specter.hpp>
#include <Runtime/CVarManager.hpp>
#include <Runtime/CGameAllocator.hpp>
#include <functional>
namespace RUDE
{
struct Application : boo::IApplicationCallback
{
Retro::CGameAllocator m_allocator;
HECL::Runtime::FileStoreManager m_fileMgr;
Specter::FontCache m_fontCache;
Specter::RootView m_rootView;
Retro::CVarManager m_cvarManager;
boo::IWindow* m_mainWindow;
Zeus::CColor m_clearColor;
bool m_running = true;
Application() : m_fileMgr(_S("rude")), m_fontCache(m_fileMgr), m_rootView(m_fontCache), m_cvarManager(m_fileMgr, true){}
void onCVarModified(Retro::CVar* cvar)
{
if (cvar == m_cvarManager.findCVar("r_clearColor"))
m_clearColor = cvar->toColor();
}
Application() : m_fileMgr(_S("rude")), m_fontCache(m_fileMgr), m_rootView(m_fontCache), m_cvarManager(m_fileMgr){}
int appMain(boo::IApplication* app)
{
m_allocator.Initialize();
m_mainWindow = app->newWindow(_S("RUDE"));
m_rootView.setWindow(m_mainWindow, 1.0f);
m_cvarManager.serialize();
Retro::CVar* tmp = m_cvarManager.findCVar("r_clearcolor");
Retro::CVar::ListenerFunc listen = std::bind(&Application::onCVarModified, this, std::placeholders::_1);
if (tmp)
tmp->addListener(listen);
while (m_running)
{
m_cvarManager.update();
m_mainWindow->waitForRetrace();
}

View File

@ -30,7 +30,7 @@ CVarManager::~CVarManager()
void CVarManager::update()
{
for (const std::pair<std::string, CVar*> pair : m_cvars)
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isModified())
{
pair.second->dispatch();
@ -49,20 +49,19 @@ bool CVarManager::registerCVar(CVar* cvar)
return true;
}
CVar* CVarManager::findCVar(const std::string &name)
CVar* CVarManager::findCVar(std::string name)
{
std::string tmp = std::string(name);
Athena::utility::tolower(tmp);
if (m_cvars.find(tmp) == m_cvars.end())
Athena::utility::tolower(name);
if (m_cvars.find(name) == m_cvars.end())
return nullptr;
return m_cvars[tmp];
return m_cvars[name];
}
std::vector<CVar*> CVarManager::archivedCVars() const
{
std::vector<CVar*> ret;
for (std::pair<std::string, CVar*> pair : m_cvars)
for (const std::pair<std::string, CVar*>& pair : m_cvars)
if (pair.second->isArchive())
ret.push_back(pair.second);
@ -72,7 +71,7 @@ std::vector<CVar*> CVarManager::archivedCVars() const
std::vector<CVar*> CVarManager::cvars() const
{
std::vector<CVar*> ret;
for (std::pair<std::string, CVar*> pair : m_cvars)
for (const std::pair<std::string, CVar*>& pair : m_cvars)
ret.push_back(pair.second);
return ret;
@ -90,12 +89,12 @@ void CVarManager::deserialize(CVar* cvar)
HECL::SystemString filename = m_store.getStoreRoot() + _S('/') + com_configfile->toLiteral();
#endif
HECL::Sstat st;
if (HECL::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
return;
if (m_useBinary)
{
filename += _S(".bin");
if (HECL::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
return;
Athena::io::FileReader reader(filename);
if (reader.isOpen())
container.read(reader);
@ -103,6 +102,8 @@ void CVarManager::deserialize(CVar* cvar)
else
{
filename += _S(".yaml");
if (HECL::Stat(filename.c_str(), &st) || !S_ISREG(st.st_mode))
return;
FILE* f = HECL::Fopen(filename.c_str(), _S("rb"));
if (f)
container.fromYAMLFile(f);
@ -117,7 +118,7 @@ void CVarManager::deserialize(CVar* cvar)
if (serialized != container.cvars.end())
{
DNACVAR::CVar tmp = *serialized;
DNACVAR::CVar& tmp = *serialized;
if (tmp.m_type != cvar->type())
{
CVarLog.report(LogVisor::Error, _S("Stored type for %s does not match actual type!"), tmp.m_name.c_str());

View File

@ -52,7 +52,7 @@ public:
bool registerCVar(CVar* cvar);
CVar* findCVar(const std::string& name);
CVar* findCVar(std::string name);
std::vector<CVar*> archivedCVars() const;
std::vector<CVar*> cvars() const;