Texure.cpp 1.9 KB

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