💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.4 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resource-loader.hpp"
  20. #include "resource-manager.hpp"
  21. #include "game/biome.hpp"
  22. #include "math/angles.hpp"
  23. #include <nlohmann/json.hpp>
  24. #include <physfs.h>
  25. template <typename T>
  26. static bool load_value(T* value, const nlohmann::json& json, const std::string& name)
  27. {
  28. if (auto element = json.find(name); element != json.end())
  29. {
  30. *value = element.value().get<T>();
  31. return true;
  32. }
  33. return false;
  34. }
  35. template <typename T>
  36. static bool load_array(T* value, std::size_t size, const nlohmann::json& json, const std::string& name)
  37. {
  38. if (auto element = json.find(name); element != json.end())
  39. {
  40. std::size_t i = 0;
  41. for (auto it = element.value().cbegin(); (it != element.value().cend()) && (i < size); ++it)
  42. {
  43. *(value++) = it.value().get<float>();
  44. ++i;
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. template <>
  51. biome* resource_loader<biome>::load(resource_manager* resource_manager, PHYSFS_File* file)
  52. {
  53. // Read file into buffer
  54. std::size_t size = static_cast<int>(PHYSFS_fileLength(file));
  55. std::string buffer;
  56. buffer.resize(size);
  57. PHYSFS_readBytes(file, &buffer[0], size);
  58. // Parse json from file buffer
  59. nlohmann::json json = nlohmann::json::parse(buffer);
  60. biome* biome = new ::biome();
  61. load_value(&biome->name, json, "name");
  62. if (auto terrain = json.find("terrain"); terrain != json.end())
  63. {
  64. std::string material_filename;
  65. if (load_value(&material_filename, terrain.value(), "material"))
  66. {
  67. biome->terrain_material = resource_manager->load<::material>(material_filename);
  68. }
  69. }
  70. if (auto weather = json.find("weather"); weather != json.end())
  71. {
  72. load_array(&biome->ambient_color.x, 3, weather.value(), "ambient_color");
  73. load_value(&biome->ambient_intensity, weather.value(), "ambient_intensity");
  74. load_value(&biome->sun_azimuth, weather.value(), "sun_azimuth");
  75. biome->sun_azimuth = math::radians(biome->sun_azimuth);
  76. load_value(&biome->sun_elevation, weather.value(), "sun_elevation");
  77. biome->sun_elevation = math::radians(biome->sun_elevation);
  78. load_array(&biome->sun_color.x, 3, weather.value(), "sun_color");
  79. load_value(&biome->sun_intensity, weather.value(), "sun_intensity");
  80. load_value(&biome->sun_angular_radius, weather.value(), "sun_angular_radius");
  81. biome->sun_angular_radius = math::radians(biome->sun_angular_radius);
  82. load_array(&biome->horizon_color.x, 3, weather.value(), "horizon_color");
  83. load_array(&biome->zenith_color.x, 3, weather.value(), "zenith_color");
  84. load_value(&biome->wind_speed, weather.value(), "wind_speed");
  85. load_value(&biome->wind_direction, weather.value(), "wind_direction");
  86. biome->wind_direction = math::radians(biome->wind_direction);
  87. }
  88. return biome;
  89. }