Texure.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. bool transparency_set(false);
  35. for(unsigned y = 0; y < height; y++)
  36. {
  37. for (unsigned x = 0; x < width; x++) {
  38. bool is_transparent = (pic[(y*width + x)*4+3] == 0);
  39. if(is_transparent && transparency_set)
  40. {
  41. data[y*width + x+3] = data[2];
  42. continue;
  43. }
  44. uint16_t color = (pic[(y*width + x)*4]>>3)<<11;
  45. color |= (pic[(y*width + x)*4 + 1]>>2)<<5;
  46. color |= (pic[(y*width + x)*4 + 2]>>3);
  47. if(is_transparent && !transparency_set)
  48. {
  49. data[2] = color;
  50. transparency_set = true;
  51. }
  52. data[y*width + x+3] = color;
  53. }
  54. }
  55. delete[] pic;
  56. }
  57. Texture::Texture(char *data) : data((texture_data_t) data)
  58. {
  59. }
  60. Texture::~Texture()
  61. {
  62. // Don't deallocate for now since we still hardcode the data
  63. // delete (data);
  64. }
  65. Rect Texture::get_dimensions()
  66. {
  67. return Rect(0, 0, data[0], data[1]);
  68. }
  69. const Pixel Texture::get_pixel(unsigned x, unsigned y)
  70. {
  71. return Pixel(data[2 + data[0] * y + x]);
  72. }