CPass.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "CPass.h"
  2. using namespace WallpaperEngine::Core::Objects::Effects::Constants;
  3. using namespace WallpaperEngine::Core::Objects::Images::Materials;
  4. CPass::CPass (std::string blending, std::string cullmode, std::string depthtest, std::string depthwrite, std::string shader) :
  5. m_blending (std::move(blending)),
  6. m_cullmode (std::move(cullmode)),
  7. m_depthtest (std::move(depthtest)),
  8. m_depthwrite (std::move(depthwrite)),
  9. m_shader (std::move(shader))
  10. {
  11. }
  12. CPass* CPass::fromJSON (json data)
  13. {
  14. // TODO: FIGURE OUT DEFAULT BLENDING MODE
  15. auto blending = jsonFindDefault <std::string> (data, "blending", "normal");
  16. auto cullmode = jsonFindDefault <std::string> (data, "cullmode", "nocull");
  17. auto depthtest_it = jsonFindRequired (data, "depthtest", "Material pass must have depthtest specified");
  18. auto depthwrite_it = jsonFindRequired (data, "depthwrite", "Material pass must have depthwrite specified");
  19. auto shader_it = jsonFindRequired (data, "shader", "Material pass must have shader specified");
  20. auto textures_it = data.find ("textures");
  21. auto combos_it = data.find ("combos");
  22. if (textures_it != data.end ())
  23. {
  24. // TODO: FETCH THIS FROM CImage TO MAKE IT COMPATIBLE WITH OLDER WALLPAPERS
  25. if ((*textures_it).is_array () == false)
  26. {
  27. throw std::runtime_error ("Textures for material must be a list");
  28. }
  29. }
  30. CPass* pass = new CPass (
  31. blending,
  32. cullmode,
  33. *depthtest_it,
  34. *depthwrite_it,
  35. *shader_it
  36. );
  37. if (textures_it != data.end ())
  38. {
  39. auto cur = (*textures_it).begin ();
  40. auto end = (*textures_it).end ();
  41. for (;cur != end; cur ++)
  42. {
  43. if ((*cur).is_null () == true)
  44. {
  45. pass->insertTexture ("");
  46. }
  47. else
  48. {
  49. pass->insertTexture (*cur);
  50. }
  51. }
  52. }
  53. if (combos_it != data.end ())
  54. {
  55. auto cur = (*combos_it).begin ();
  56. auto end = (*combos_it).end ();
  57. for (; cur != end; cur ++)
  58. {
  59. std::string name = cur.key ();
  60. if ((*cur).is_number_integer () == true)
  61. {
  62. pass->insertCombo (name, *cur);
  63. }
  64. else
  65. {
  66. throw std::runtime_error ("unexpected non-integer combo");
  67. }
  68. }
  69. }
  70. return pass;
  71. }
  72. void CPass::insertTexture (const std::string& texture)
  73. {
  74. this->m_textures.push_back (texture);
  75. }
  76. void CPass::setTexture (int index, const std::string& texture)
  77. {
  78. this->m_textures.at (index) = texture;
  79. }
  80. void CPass::insertCombo (const std::string& name, int value)
  81. {
  82. std::string uppercase = std::string (name);
  83. std::transform (uppercase.begin (), uppercase.end (), uppercase.begin (), ::toupper);
  84. this->m_combos.insert (std::pair <std::string, int> (uppercase, value));
  85. }
  86. const std::vector<std::string>& CPass::getTextures () const
  87. {
  88. return this->m_textures;
  89. }
  90. const std::map<std::string, CShaderConstant*>& CPass::getConstants () const
  91. {
  92. return this->m_constants;
  93. }
  94. std::map<std::string, int>* CPass::getCombos ()
  95. {
  96. return &this->m_combos;
  97. }
  98. const std::string& CPass::getShader () const
  99. {
  100. return this->m_shader;
  101. }
  102. const std::string& CPass::getBlendingMode () const
  103. {
  104. return this->m_blending;
  105. }
  106. const std::string& CPass::getCullingMode () const
  107. {
  108. return this->m_cullmode;
  109. }
  110. const std::string& CPass::getDepthTest () const
  111. {
  112. return this->m_depthtest;
  113. }
  114. const std::string& CPass::getDepthWrite ()const
  115. {
  116. return this->m_depthwrite;
  117. }
  118. void CPass::setBlendingMode (std::string mode)
  119. {
  120. this->m_blending = mode;
  121. }
  122. void CPass::insertConstant (const std::string& name, CShaderConstant* constant)
  123. {
  124. this->m_constants.insert (std::pair <std::string, CShaderConstant*> (name, constant));
  125. }