Texure.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "Texture.h"
  2. #include "piaf/Archive.h"
  3. #include "utility/Rect.h"
  4. #include "render/Pixel.h"
  5. #include "utility/misc.h"
  6. #include "lodepng.h"
  7. #include "Input.h"
  8. #include "Graphics.h"
  9. using WalrusRPG::Graphics::Black;
  10. using WalrusRPG::Graphics::Pixel;
  11. using WalrusRPG::Graphics::Texture;
  12. using WalrusRPG::Utils::Rect;
  13. using WalrusRPG::PIAF::File;
  14. namespace
  15. {
  16. /*
  17. texture_data_t loadPNG(char *data)
  18. {
  19. // TODO : stuff
  20. UNUSED(data);
  21. return nullptr;
  22. }
  23. */
  24. }
  25. Texture::Texture(File entry)
  26. {
  27. unsigned char* pic;
  28. unsigned width, height;
  29. signed result = lodepng_decode_memory(&pic, &width, &height, (unsigned char*)entry.get(), entry.file_size, LCT_RGBA, 8);
  30. UNUSED(result);
  31. data = new uint16_t[width * height + 3];
  32. data[0] = width;
  33. data[1] = height;
  34. for(unsigned y = 0; y < height; y++)
  35. {
  36. for (unsigned x = 0; x < width; x++) {
  37. uint16_t color = (pic[(y*width + x)*4]>>3)<<11;
  38. color |= (pic[(y*width + x)*4 + 1]>>2)<<5;
  39. color |= (pic[(y*width + x)*4 + 2]>>3);
  40. if(pic[(y*width + x)*4+3] == 0)
  41. {
  42. data[2] = color;
  43. }
  44. data[y*width + x+3] = color;
  45. }
  46. }
  47. delete[] pic;
  48. }
  49. Texture::Texture(char *data) : data((texture_data_t) data)
  50. {
  51. }
  52. Texture::~Texture()
  53. {
  54. // Don't deallocate for now since we still hardcode the data
  55. // delete (data);
  56. }
  57. Rect Texture::get_dimensions()
  58. {
  59. return Rect(0, 0, data[0], data[1]);
  60. }
  61. const Pixel Texture::get_pixel(unsigned x, unsigned y)
  62. {
  63. return Pixel(data[2 + data[0] * y + x]);
  64. }