Skip to content

Commit

Permalink
Added a preferences system
Browse files Browse the repository at this point in the history
  • Loading branch information
prophile committed Jan 19, 2009
1 parent 08e188d commit a793239
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions Engine/Apollo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Scripting/Scripting.h"
#include "Scripting/Compile.h"
#include "Sound/Sound.h"
#include "Preferences.h"

extern "C"
{
Expand Down
86 changes: 86 additions & 0 deletions Engine/Preferences.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "Preferences.h"
#include "Utilities/ResourceManager.h"
#include <map>

namespace Preferences
{

typedef std::map<std::string, std::string> PreferenceMap;
PreferenceMap preferences;

std::string Get ( const std::string& key, const std::string& defaultValue )
{
PreferenceMap::iterator iter = preferences.find(key);
if (iter == preferences.end())
{
return defaultValue;
}
else
{
return iter->second;
}
}

void Set ( const std::string& key, const std::string& defaultValue )
{
preferences[key] = defaultValue;
}

void Clear ( const std::string& key )
{
PreferenceMap::iterator iter = preferences.find(key);
if (iter != preferences.end())
{
preferences.erase(iter);
}
}

static void ParseLine ( const std::string& line )
{
if (line != "")
{
std::string::size_type n = line.find_first_of(':');
std::string k = line.substr(0, n);
std::string v = line.substr(n + 1, std::string::npos);
preferences[k] = v;
}
}

void Load ()
{
preferences.clear();
SDL_RWops* ops = ResourceManager::OpenFile("Config/Preferences.cfg");
if (!ops)
return;
size_t len;
char* buffer = (char*)ResourceManager::ReadFull(&len, ops, 1);
std::string preferenceLine;
char* tempPointer = buffer;
while (*tempPointer)
{
char ch = *tempPointer;
if (ch == '\n')
{
ParseLine(preferenceLine);
preferenceLine.clear();
}
else
{
preferenceLine.push_back(ch);
}
tempPointer++;
}
free((void*)buffer);
}

void Save ()
{
std::string buffer;
for ( PreferenceMap::iterator iter = preferences.begin(); iter != preferences.end(); iter++ )
{
buffer.append(iter->first + ":" + iter->second + "\n");
}
ResourceManager::WriteFile("Config/Preferences.cfg", buffer.data(), buffer.length());
}

}
18 changes: 18 additions & 0 deletions Engine/Preferences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __xsera_preferences_h
#define __xsera_preferences_h

#include <string>

namespace Preferences
{

std::string Get ( const std::string& key, const std::string& defaultValue = "" );
void Set ( const std::string& key, const std::string& newValue );
void Clear ( const std::string& key );

void Load ();
void Save ();

}

#endif
6 changes: 6 additions & 0 deletions Resources/Config/Preferences.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Screen/Width:800
Screen/Height:600
Screen/Fullscreen:false
Audio/SamplingRate:48000
Audio/Resolution:24
Audio/Channels:64
15 changes: 13 additions & 2 deletions Source/Xsera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ void RunLoop ()
UpdateModeManager();
}

unsigned ToInt ( const std::string& value )
{
return atoi(value.c_str());
}

bool ToBool ( const std::string& value )
{
return value == "true";
}

void Startup ()
{
// do init stuff
InitModeManager();
ResourceManager::Init("Xsera");
Graphics::Init(960, 720, false);
Sound::Init(48000, 24, 128); // init with 48 kHz sampling rate, 24-bit resolution, 128 channels
Preferences::Load();
Graphics::Init(ToInt(Preferences::Get("Screen/Width")), ToInt(Preferences::Get("Screen/Height")), ToBool(Preferences::Get("Screen/Fullscreen")));
Sound::Init(ToInt(Preferences::Get("Audio/SamplingRate")), ToInt(Preferences::Get("Audio/Resolution")), ToInt(Preferences::Get("Audio/Channels"))); // init with 48 kHz sampling rate, 24-bit resolution, 128 channels
// compile class script
CompileScript("System/Class");
LuaScript bootScript ("System/Boot");
Expand Down
8 changes: 8 additions & 0 deletions Xcode/Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
49118E6B0F251D2B004710D9 /* Preferences.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 49118E690F251D2B004710D9 /* Preferences.cpp */; };
49118E6C0F251D2B004710D9 /* Preferences.h in Headers */ = {isa = PBXBuildFile; fileRef = 49118E6A0F251D2B004710D9 /* Preferences.h */; };
493223950F23B5C40048495A /* Entry.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 493223940F23B5C40048495A /* Entry.cpp */; };
493223BB0F23B9D60048495A /* Apollo.h in Headers */ = {isa = PBXBuildFile; fileRef = 493223BA0F23B9D60048495A /* Apollo.h */; };
493223CF0F23BB360048495A /* SDLMain.m in Sources */ = {isa = PBXBuildFile; fileRef = 493223CE0F23BB360048495A /* SDLMain.m */; };
Expand Down Expand Up @@ -139,6 +141,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
49118E690F251D2B004710D9 /* Preferences.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Preferences.cpp; path = ../Engine/Preferences.cpp; sourceTree = SOURCE_ROOT; };
49118E6A0F251D2B004710D9 /* Preferences.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Preferences.h; path = ../Engine/Preferences.h; sourceTree = SOURCE_ROOT; };
493223940F23B5C40048495A /* Entry.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Entry.cpp; path = ../Engine/Entry.cpp; sourceTree = SOURCE_ROOT; };
493223BA0F23B9D60048495A /* Apollo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Apollo.h; path = ../Engine/Apollo.h; sourceTree = SOURCE_ROOT; };
493223CE0F23BB360048495A /* SDLMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDLMain.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -307,6 +311,8 @@
isa = PBXGroup;
children = (
493223BA0F23B9D60048495A /* Apollo.h */,
49118E690F251D2B004710D9 /* Preferences.cpp */,
49118E6A0F251D2B004710D9 /* Preferences.h */,
493223940F23B5C40048495A /* Entry.cpp */,
493A3C290F0EDB4F00F44530 /* Logging.cpp */,
493A3C270F0EDB3900F44530 /* Logging.h */,
Expand Down Expand Up @@ -616,6 +622,7 @@
493A3C280F0EDB3900F44530 /* Logging.h in Headers */,
493223BB0F23B9D60048495A /* Apollo.h in Headers */,
493224620F23DE030048495A /* Shaders.h in Headers */,
49118E6C0F251D2B004710D9 /* Preferences.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -728,6 +735,7 @@
493223950F23B5C40048495A /* Entry.cpp in Sources */,
493223CF0F23BB360048495A /* SDLMain.m in Sources */,
493224610F23DE030048495A /* Shaders.cpp in Sources */,
49118E6B0F251D2B004710D9 /* Preferences.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit a793239

Please sign in to comment.