Texure.cpp 1.8 KB

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