FileTexture.h 827 B

123456789101112131415161718192021222324252627282930313233343536
  1. #ifndef FILETEXTURE_H
  2. #define FILETEXTURE_H
  3. #include <iostream>
  4. #include "wrapper/Texture.h"
  5. #include "images/PNGReader.h"
  6. template<int N>
  7. class FileTexture final {
  8. Texture texture;
  9. public:
  10. FileTexture(const char* path) : texture(TextureFormat::color8(N)) {
  11. PNGReader png(path);
  12. if(png.hasError()) {
  13. return;
  14. }
  15. if(png.getChannels() != N) {
  16. std::cout << "expected " << N << " from '" << path << "' but got " << png.getChannels() << "\n";
  17. return;
  18. }
  19. Color<N>* buffer = new Color<N>[png.getBufferSize()];
  20. if(!png.readData(buffer)) {
  21. texture.setData(png.getWidth(), png.getHeight(), buffer);
  22. }
  23. delete[] buffer;
  24. }
  25. void bindTo(int index) const {
  26. texture.bindTo(index);
  27. }
  28. };
  29. #endif