Browse Source

Cleaned up the config code in main slightly.

* Introduced a template/conversion GetValue implementation in Config.
    * Used tables to store the default config values and the supported
      argument->config mappings.
master
Ian Burgmyer 4 years ago
parent
commit
cdfc62d925
  1. 1
      plip-sdl/Config.cpp
  2. 15
      plip-sdl/Config.h
  3. 35
      plip-sdl/main.cpp

1
plip-sdl/Config.cpp

@ -5,6 +5,7 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include "Config.h"

15
plip-sdl/Config.h

@ -17,6 +17,21 @@ namespace PlipSdl {
void SetValue(const std::string &key, const std::string &value);
void SetValue(const std::string &section, const std::string &key, const std::string &value);
template<typename T>
T GetValue(const std::string &key) {
return GetValue<T>(global, key);
}
template<typename T>
T GetValue(const std::string &section, const std::string &key) {
auto val = GetValue(section, key);
T output;
std::stringstream conversion(val);
conversion >> output;
return output;
}
const std::string global = "\x01";
const std::string empty = "\xff";

35
plip-sdl/main.cpp

@ -5,6 +5,7 @@
#include <iostream>
#include <string>
#include <vector>
#include "cxxopts.hpp"
#include "Plip.h"
@ -19,17 +20,25 @@
#include "Timer/TimerSdl.h"
#endif
std::vector<std::vector<std::string>> defaultConfig = {
{ "video", "scale" , "1" },
{ "video", "targetFps", "60" }
};
std::vector<std::vector<std::string>> intParamMapping = {
{ "scale", "video", "scale" },
{ "fps" , "video", "targetFps" }
};
void gameLoop(Plip::Plip *plip, PlipSdl::Config *config, PlipSdl::Timer *timer) {
auto input = plip->GetInput();
auto video = plip->GetVideo();
auto event = new PlipSdl::SdlEvent(input);
auto targetFps = std::stoi(config->GetValue("video", "targetFps"));
auto targetFps = config->GetValue<int>("video", "targetFps");
auto frameTime = 1000000000 / targetFps;
std::cout << targetFps << "hz (" << frameTime << "ns)" << std::endl;
auto running = true;
while(running) {
timer->StopwatchStart();
@ -110,10 +119,10 @@ int main(int argc, char **argv) {
SDL_Init(0);
// TODO: Make the config handler less of a mess. :|
auto config = new PlipSdl::Config();
config->SetValue("video", "scale", "1");
config->SetValue("video", "targetFps", "60");
for(auto opt : defaultConfig)
config->SetValue(opt[0], opt[1], opt[2]);
if(opts["config"].count()) {
auto configFile = opts["config"].as<std::string>();
@ -121,12 +130,16 @@ int main(int argc, char **argv) {
std::cerr << "Error opening config file: " << configFile << std::endl;
}
if(opts["scale"].count())
config->SetValue("video", "scale", std::to_string(opts["scale"].as<int>()));
if(opts["fps"].count())
config->SetValue("video", "targetFps", std::to_string(opts["fps"].as<int>()));
for(auto param : intParamMapping) {
if(opts[param[0]].count()) {
config->SetValue(
param[1], param[2],
std::to_string(opts[param[0]].as<int>())
);
}
}
auto videoScale = std::stoi(config->GetValue("video", "scale"));
auto videoScale = config->GetValue<int>("video", "scale");
auto wnd = new PlipSdl::SdlWindow(videoScale, version);
auto plip = new Plip::Plip(wnd);

Loading…
Cancel
Save