CContainer.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "CContainer.h"
  2. #include "CTexture.h"
  3. #include <cstring>
  4. #include <utility>
  5. using namespace WallpaperEngine::Assets;
  6. const ITexture* CContainer::readTexture (std::string filename) const
  7. {
  8. // get the texture's filename (usually .tex)
  9. filename = "materials/" + filename + ".tex";
  10. const void* textureContents = this->readFile (filename, nullptr);
  11. ITexture* result = new CTexture (textureContents);
  12. #if DEBUG
  13. glObjectLabel (GL_TEXTURE, result->getTextureID (), -1, filename.c_str ());
  14. #endif /* DEBUG */
  15. return result;
  16. }
  17. std::string CContainer::readVertexShader (const std::string& filename) const
  18. {
  19. return this->readFileAsString ("shaders/" + filename + ".vert");
  20. }
  21. std::string CContainer::readFragmentShader (const std::string& filename) const
  22. {
  23. return this->readFileAsString ("shaders/" + filename + ".frag");
  24. }
  25. std::string CContainer::readIncludeShader (const std::string& filename) const
  26. {
  27. return this->readFileAsString ("shaders/" + filename);
  28. }
  29. std::string CContainer::readFileAsString (std::string filename) const
  30. {
  31. uint32_t length = 0;
  32. // read file contents and allocate a buffer for a string
  33. const void* contents = this->readFile (std::move (filename), &length);
  34. char* buffer = new char [length + 1];
  35. // ensure there's a 0 at the end
  36. memset (buffer, 0, length + 1);
  37. // copy over the data
  38. memcpy (buffer, contents, length);
  39. // now build the std::string to use
  40. std::string result = buffer;
  41. // free the intermediate buffer used to generate the std::string
  42. delete[] buffer;
  43. return result;
  44. }