Texure.cpp 2.0 KB

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