SettingsSystem.cpp 4.63 KB
Newer Older
1 2 3 4
#include <engine/core/SettingsSystem.hpp>
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Settings.hh>

5 6 7 8 9 10
#define PICOJSON_USE_INT64
#include <engine/utils/picojson.h>

#include <fstream>


11 12
using namespace ACGL::Base;

13 14 15 16 17 18
std::string getDirectory(std::string filename) {
  size_t found;
  found = filename.find_last_of("/\\");
  return filename.substr(0, found);
}

19 20 21 22 23 24
bool SettingsSystem::startup() {
  Settings::the()->setResourcePath(m_resourcePath);
  Settings::the()->setTexturePath(m_texturePath);
  Settings::the()->setGeometryPath(m_geometryPath);
  Settings::the()->setShaderPath(m_shaderPath);

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  
  std::ifstream configFile(m_resourcePath + m_configFilePath);

  if (!configFile.is_open()) {
    std::cerr << "Could not open config file " << m_configFilePath << "!" << std::endl;
    return false;
  }

  picojson::value config;
  configFile >> config;

  if (!config.is<picojson::object>()) {
    std::cerr << "Could not read config file " << m_configFilePath << "!" << std::endl;
    return false;
  }

  const picojson::object& o = config.get<picojson::object>();

  m_availablePlanets = { };
  m_resolution = { 1280, 720 };
  m_qualitySetting = QualitySetting::High;
  m_fullscreen = false;
Dario Seyb's avatar
Dario Seyb committed
47
  m_vsyncEnabled = true;
48 49
  m_targetFps = 60;
  m_defaultScene = "AtmosphereTest";
Dario Seyb's avatar
Dario Seyb committed
50
  m_defaultPlanetType = "earth";
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

#define VALIDATE_TYPE(p, type) if(!p.second.is<type>()) { std::cout << "Warning: Setting \"" << i.first << "\": " << i.second << " is not of expected type " #type "." << std::endl; continue; }

  for (auto& i : o) {
    if (i.first == "available_planet_prefabs") {
      VALIDATE_TYPE(i, picojson::array);
      const picojson::array& planets = i.second.get<picojson::array>();
      m_availablePlanets.clear();
      for (auto& planet : planets) {

        auto planetName = planet.get<std::string>();
        std::string fullPath = m_resourcePath + getDirectory(m_configFilePath) + "/planets/" + planetName + ".json";
        std::ifstream planetFile(fullPath);

        if (!planetFile.is_open()) {
          std::cerr << "Could not open planet file " << fullPath << "!" << std::endl;
          continue;
        }

        picojson::value planetVal;
        planetFile >> planetVal;

        if (!planetVal.is<picojson::object>()) {
          std::cerr << "Could not read planet file " << fullPath << "!" << std::endl;
          return false;
        }

        const picojson::object& planetObj = planetVal.get<picojson::object>();


Dario Seyb's avatar
Dario Seyb committed
81 82
        PlanetMetaData planetData;
        planetData.name = planetName;
83 84 85
        for (auto& prop : planetObj) {
          if (prop.first == "shaders") {
            VALIDATE_TYPE(prop, std::string);
Dario Seyb's avatar
Dario Seyb committed
86
            planetData.shaders = prop.second.get<std::string>();
Dario Seyb's avatar
Dario Seyb committed
87
          } else if (prop.first == "atmosphere") {
88
            VALIDATE_TYPE(prop, bool);
Dario Seyb's avatar
Dario Seyb committed
89
            planetData.hasAtmosphere = prop.second.get<bool>();
90 91
          } else if (prop.first == "water") {
            VALIDATE_TYPE(prop, bool);
Dario Seyb's avatar
Dario Seyb committed
92
            planetData.hasWater = prop.second.get<bool>();
93 94
          } else if (prop.first == "moons") {
            VALIDATE_TYPE(prop, int64_t);
Dario Seyb's avatar
Dario Seyb committed
95
            planetData.moonCount = prop.second.get<int64_t>();
96 97 98
          }
        }

Dario Seyb's avatar
Dario Seyb committed
99
        m_availablePlanets.push_back(planetData);
100 101 102 103 104 105 106 107 108
      }
    } else if(i.first == "resolution") {
      VALIDATE_TYPE(i, picojson::array);
      const picojson::array& res = i.second.get<picojson::array>();
      m_resolution.x = res[0].get<int64_t>();
      m_resolution.y = res[1].get<int64_t>();
    } else if (i.first == "fullscreen") {
      VALIDATE_TYPE(i, bool);
      m_fullscreen = i.second.get<bool>();
109 110 111
    } else if (i.first == "ssao") {
      VALIDATE_TYPE(i, bool);
      m_ssaoEnabled = i.second.get<bool>();
Dario Seyb's avatar
Dario Seyb committed
112 113 114
    } else if (i.first == "vsync") {
      VALIDATE_TYPE(i, bool);
      m_vsyncEnabled = i.second.get<bool>();
115 116 117 118 119 120 121 122 123 124 125 126 127 128
    } else if (i.first == "target_fps") {
      VALIDATE_TYPE(i, int64_t);
      m_targetFps = i.second.get<int64_t>();
    } else if (i.first == "quality") {
      VALIDATE_TYPE(i, std::string);
      auto setting = i.second.get<std::string>();

      if (setting == "low") m_qualitySetting = QualitySetting::Low;
      else if (setting == "medium") m_qualitySetting = QualitySetting::Medium;
      else if (setting == "high") m_qualitySetting = QualitySetting::High;

    } else if (i.first == "scene") {
      VALIDATE_TYPE(i, std::string);
      m_defaultScene = i.second.get<std::string>();
Dario Seyb's avatar
Dario Seyb committed
129 130 131
    } else if (i.first == "default_planet_type") {
      VALIDATE_TYPE(i, std::string);
      m_defaultPlanetType = i.second.get<std::string>();
132 133 134 135 136 137
    } else {
      std::cout << "Warning: Unknown setting \"" << i.first << "\"." << std::endl;
    }
  }
#undef VALIDATE_TYPE

138 139 140 141 142
  return true;
}

void SettingsSystem::shutdown() {

Dario Seyb's avatar
Dario Seyb committed
143
}