FileTexture.h 806 B

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