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

  1. #include "biome.hpp"
  2. #include "../settings.hpp"
  3. #if defined(_WIN32) || defined(__CYGWIN__)
  4. #include "../windows-dirent.h"
  5. #else
  6. #include <dirent.h>
  7. #endif
  8. #include <iostream>
  9. Biome::Biome()
  10. {}
  11. Biome::~Biome()
  12. {}
  13. bool Biome::load()
  14. {
  15. ParameterDict parameters;
  16. if (!parameters.load(filename))
  17. {
  18. return false;
  19. }
  20. parameters.get("name", &name);
  21. parameters.get("soil-horizon-o", &soilHorizonOFilename);
  22. parameters.get("soil-horizon-a", &soilHorizonAFilename);
  23. parameters.get("soil-horizon-b", &soilHorizonBFilename);
  24. parameters.get("soil-horizon-c", &soilHorizonCFilename);
  25. parameters.get("cubemap", &cubemapName);
  26. TextureLoader textureLoader;
  27. textureLoader.setCubemap(false);
  28. textureLoader.setMipmapChain(false);
  29. // Load soil horizon textures
  30. soilHorizonO = textureLoader.load(std::string("data/textures/") + soilHorizonOFilename);
  31. soilHorizonA = textureLoader.load(std::string("data/textures/") + soilHorizonAFilename);
  32. soilHorizonB = textureLoader.load(std::string("data/textures/") + soilHorizonBFilename);
  33. soilHorizonC = textureLoader.load(std::string("data/textures/") + soilHorizonCFilename);
  34. // Load diffuse cubemap
  35. textureLoader.setCubemap(true);
  36. textureLoader.setMipmapChain(false);
  37. std::string diffuseCubemapFilename = std::string("data/textures/") + cubemapName + std::string("-diffuse.png");
  38. diffuseCubemap = textureLoader.load(diffuseCubemapFilename);
  39. if (!diffuseCubemap)
  40. {
  41. std::cerr << "Failed to load diffuse cubemap \"" << diffuseCubemapFilename << "\"" << std::endl;
  42. }
  43. // Load specular cubemap
  44. textureLoader.setCubemap(true);
  45. textureLoader.setMipmapChain(true);
  46. std::string specularCubemapFilename = std::string("data/textures/") + cubemapName + std::string("-specular_m%02d.png");
  47. specularCubemap = textureLoader.load(specularCubemapFilename);
  48. if (!specularCubemap)
  49. {
  50. std::cerr << "Failed to load specular cubemap \"" << specularCubemapFilename << "\"" << std::endl;
  51. }
  52. return true;
  53. }
  54. bool Biosphere::load(const std::string& directory)
  55. {
  56. // Open biomes directory
  57. DIR* dir = opendir(directory.c_str());
  58. if (dir == nullptr)
  59. {
  60. std::cout << "Failed to open biome directory \"" << directory << "\"" << std::endl;
  61. return false;
  62. }
  63. // Scan directory for .bio files
  64. for (struct dirent* entry = readdir(dir); entry != nullptr; entry = readdir(dir))
  65. {
  66. if (entry->d_type == DT_DIR || *entry->d_name == '.')
  67. {
  68. continue;
  69. }
  70. std::string filename = entry->d_name;
  71. std::string::size_type delimeter = filename.find_last_of('.');
  72. if (delimeter == std::string::npos)
  73. {
  74. continue;
  75. }
  76. std::string extension = filename.substr(delimeter + 1);
  77. if (extension != "bio")
  78. {
  79. continue;
  80. }
  81. // Add biome
  82. std::string name = filename.substr(0, delimeter);
  83. Biome* biome = &biomes[name];
  84. biome->filename = directory + filename;
  85. }
  86. // Close biomes directory
  87. closedir(dir);
  88. // Load biomes
  89. for (std::map<std::string, Biome>::iterator it = biomes.begin(); it != biomes.end(); ++it)
  90. {
  91. Biome* biome = &it->second;
  92. if (!biome->load())
  93. {
  94. std::cout << "Failed to load biome \"" << biome->filename << "\"" << std::endl;
  95. }
  96. else
  97. {
  98. std::cout << "Loaded biome \"" << biome->filename << "\"" << std::endl;
  99. }
  100. }
  101. return true;
  102. }