Skip to content

Commit

Permalink
Added auto-save/load for config
Browse files Browse the repository at this point in the history
  • Loading branch information
ferris committed May 18, 2013
1 parent 8ac0ad6 commit 87f9719
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 14 deletions.
51 changes: 51 additions & 0 deletions Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "Config.h"

Config::Config(const String& fileName)
{
this->fileName = fileName;
AudioEnabled = true;
AudioLatencyIndex = 2;
VideoZoomIndex = 1;
SystemSpeedIndex = 4;

try
{
auto input = File::ReadAllLines(fileName);

int value;
if (tryGetValue(input, "AudioEnabled", value)) AudioEnabled = value != 0;
if (tryGetValue(input, "AudioLatencyIndex", value)) AudioLatencyIndex = value;
if (tryGetValue(input, "VideoZoomIndex", value)) VideoZoomIndex = value;
if (tryGetValue(input, "SystemSpeedIndex", value)) SystemSpeedIndex = value;
}
catch (...) { }
}

void Config::Save()
{
String output;

output += String("AudioEnabled:") + AudioEnabled + "\n";
output += String("AudioLatencyIndex:") + AudioLatencyIndex + "\n";
output += String("VideoZoomIndex:") + VideoZoomIndex + "\n";
output += String("SystemSpeedIndex:") + SystemSpeedIndex + "\n";

try
{
File::WriteAllText(fileName, output);
}
catch (...) { }
}

bool Config::tryGetValue(const List<String>& input, const String& name, int& value)
{
for (int i = 0; i < input.Count(); i++)
{
auto parts = input[i].Split(':');
if (parts.Count() == 2 && parts[0] == name)
{
if (parts[1].TryParseInt(value)) return true;
}
}
return false;
}
24 changes: 24 additions & 0 deletions Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __CONFIG_H__
#define __CONFIG_H__

#include "Common.h"

class Config
{
public:
Config(const String& fileName);

void Save();

bool AudioEnabled;
int AudioLatencyIndex;
int VideoZoomIndex;
int SystemSpeedIndex;

private:
static bool tryGetValue(const List<String>& input, const String& name, int& value);

String fileName;
};

#endif
36 changes: 22 additions & 14 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Common.h"
#include "Chip8/Chip8.h"
#include "Config.h"
#include "GLVideoDriver.h"
#include "DirectSoundAudioDriver.h"

Expand All @@ -11,6 +12,8 @@ int Main(const List<String>& arguments)

auto running = true;

Config config(Directory::GetApplicationDirectory() + "\\config.cfg");

auto window = Window::Create("Vip8");
Key keys[] =
{
Expand Down Expand Up @@ -89,28 +92,33 @@ int Main(const List<String>& arguments)

auto systemAudioMenu = Menu::Create("Audio");
auto systemAudioEnabled = MenuItem::Create("Enabled");
systemAudioEnabled->SetChecked(audioDriver->GetEnabled());
systemAudioEnabled->SetChecked(config.AudioEnabled);
systemAudioEnabled->SetToggleEnabled(true);
systemAudioEnabled->CheckedChanged += [&] { audioDriver->SetEnabled(systemAudioEnabled->GetChecked()); };
auto reflectAudioEnabled = [&]
{
config.AudioEnabled = systemAudioEnabled->GetChecked();
audioDriver->SetEnabled(config.AudioEnabled);
};
systemAudioEnabled->CheckedChanged += reflectAudioEnabled;
reflectAudioEnabled();
systemAudioMenu->AddChild(systemAudioEnabled);
auto systemAudioLatencyMenu = Menu::Create("Latency");
const int numLatencies = 8;
const int latencies[] = { 20, 40, 60, 80, 100, 200, 500, 1000 };
int latencyIndex = 2;
List<MenuItem *> systemAudioLatencyItems;
auto reflectLatencyIndex = [&]
{
auto latency = latencies[latencyIndex];
auto latency = latencies[config.AudioLatencyIndex];
audioDriver->SetLatencyMs(latency);
for (int i = 0; i < systemAudioLatencyItems.Count(); i++) systemAudioLatencyItems[i]->SetChecked(i == latencyIndex);
for (int i = 0; i < systemAudioLatencyItems.Count(); i++) systemAudioLatencyItems[i]->SetChecked(i == config.AudioLatencyIndex);
};
for (int i = 0; i < numLatencies; i++)
{
auto latency = latencies[i];
auto item = MenuItem::Create(String(latency) + " ms");
item->Click += [&, i]
{
latencyIndex = i;
config.AudioLatencyIndex = i;
reflectLatencyIndex();
};
systemAudioLatencyMenu->AddChild(item);
Expand All @@ -124,21 +132,20 @@ int Main(const List<String>& arguments)
auto systemVideoZoomMenu = Menu::Create("Zoom");
const int numZooms = 4;
const int zooms[] = { 4, 8, 16, 32 };
int zoomIndex = 1;
List<MenuItem *> systemVideoZoomItems;
auto reflectZoomIndex = [&]
{
auto zoom = zooms[zoomIndex];
auto zoom = zooms[config.VideoZoomIndex];
window->SetDesiredSize(chip8.GetOutputWidth() * zoom, chip8.GetOutputHeight() * zoom);
for (int i = 0; i < systemVideoZoomItems.Count(); i++) systemVideoZoomItems[i]->SetChecked(i == zoomIndex);
for (int i = 0; i < systemVideoZoomItems.Count(); i++) systemVideoZoomItems[i]->SetChecked(i == config.VideoZoomIndex);
};
for (int i = 0; i < numZooms; i++)
{
auto zoom = zooms[i];
auto item = MenuItem::Create(String(zoom) + "x" + zoom);
item->Click += [&, i]
{
zoomIndex = i;
config.VideoZoomIndex = i;
reflectZoomIndex();
};
systemVideoZoomMenu->AddChild(item);
Expand All @@ -151,21 +158,20 @@ int Main(const List<String>& arguments)
auto systemSpeedMenu = Menu::Create("Speed");
const int numSpeeds = 10;
const int speeds[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 };
int speedIndex = 4;
List<MenuItem *> systemSpeedItems;
auto reflectSpeedIndex = [&]
{
auto speed = speeds[speedIndex];
auto speed = speeds[config.SystemSpeedIndex];
chip8.SetSpeed(speed);
for (int i = 0; i < systemSpeedItems.Count(); i++) systemSpeedItems[i]->SetChecked(i == speedIndex);
for (int i = 0; i < systemSpeedItems.Count(); i++) systemSpeedItems[i]->SetChecked(i == config.SystemSpeedIndex);
};
for (int i = 0; i < numSpeeds; i++)
{
auto speed = speeds[i];
auto item = MenuItem::Create(String(speed) + " instruction" + (speed > 1 ? "s" : "") + "/frame");
item->Click += [&, i]
{
speedIndex = i;
config.SystemSpeedIndex = i;
reflectSpeedIndex();
};
systemSpeedMenu->AddChild(item);
Expand Down Expand Up @@ -197,6 +203,8 @@ int Main(const List<String>& arguments)
window->Update();
}

config.Save();

delete window;
delete menu;
delete fileMenu;
Expand Down
2 changes: 2 additions & 0 deletions Vip8.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<ClCompile Include="Chip8\Apu.cpp" />
<ClCompile Include="Chip8\Chip8.cpp" />
<ClCompile Include="Chip8\Gpu.cpp" />
<ClCompile Include="Config.cpp" />
<ClCompile Include="DirectSoundAudioDriver.cpp" />
<ClCompile Include="GLVideoDriver.cpp" />
<ClCompile Include="Main.cpp" />
Expand All @@ -90,6 +91,7 @@
<ClInclude Include="Chip8\Chip8.h" />
<ClInclude Include="Chip8\Gpu.h" />
<ClInclude Include="Common.h" />
<ClInclude Include="Config.h" />
<ClInclude Include="DirectSoundAudioDriver.h" />
<ClInclude Include="GLVideoDriver.h" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Vip8.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ClCompile Include="Chip8\Apu.cpp">
<Filter>Chip8</Filter>
</ClCompile>
<ClCompile Include="Config.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Chip8\Chip8.h">
Expand All @@ -32,5 +33,6 @@
<ClInclude Include="Chip8\Apu.h">
<Filter>Chip8</Filter>
</ClInclude>
<ClInclude Include="Config.h" />
</ItemGroup>
</Project>

0 comments on commit 87f9719

Please sign in to comment.