💿🐜 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.

123 lines
3.5 KiB

  1. /*
  2. * Copyright (C) 2021 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<T>();
  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. float3 location;
  63. if (load_array(&location.x, 3, json, "location"))
  64. {
  65. biome->location = {math::radians(location.x), math::radians(location.y), location.z};
  66. }
  67. if (auto terrain = json.find("terrain"); terrain != json.end())
  68. {
  69. std::string material_filename;
  70. if (load_value(&material_filename, terrain.value(), "material"))
  71. {
  72. biome->terrain_material = resource_manager->load<::material>(material_filename);
  73. }
  74. }
  75. if (auto weather = json.find("weather"); weather != json.end())
  76. {
  77. std::string sky_palette_filename;
  78. if (load_value(&sky_palette_filename, weather.value(), "sky_palette"))
  79. {
  80. biome->sky_palette = resource_manager->load<image>(sky_palette_filename);
  81. }
  82. std::string sun_palette_filename;
  83. if (load_value(&sun_palette_filename, weather.value(), "sun_palette"))
  84. {
  85. biome->sun_palette = resource_manager->load<image>(sun_palette_filename);
  86. }
  87. std::string moon_palette_filename;
  88. if (load_value(&moon_palette_filename, weather.value(), "moon_palette"))
  89. {
  90. biome->moon_palette = resource_manager->load<image>(moon_palette_filename);
  91. }
  92. std::string ambient_palette_filename;
  93. if (load_value(&ambient_palette_filename, weather.value(), "ambient_palette"))
  94. {
  95. biome->ambient_palette = resource_manager->load<image>(ambient_palette_filename);
  96. }
  97. std::string shadow_palette_filename;
  98. if (load_value(&shadow_palette_filename, weather.value(), "shadow_palette"))
  99. {
  100. biome->shadow_palette = resource_manager->load<image>(shadow_palette_filename);
  101. }
  102. }
  103. return biome;
  104. }