Texture.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "Texture.h"
  2. #include <SFML/Graphics/Texture.hpp>
  3. #include <SFML/OpenGL.hpp>
  4. #include <SFML/Graphics/Rect.hpp>
  5. #include <cstdint>
  6. #include <cstdlib>
  7. #include "utility/misc.h"
  8. #include "render/Pixel.h"
  9. using namespace WalrusRPG::Graphics; /*Texture*/
  10. using WalrusRPG::Graphics::Pixel;
  11. using WalrusRPG::PIAF::File;
  12. using WalrusRPG::Utils::Rect;
  13. Texture::Texture(char *data) : data()
  14. {
  15. uint16_t *data_16 = (uint16_t *) data;
  16. this->data.create(data_16[0], data_16[1]);
  17. sf::Uint8 *pixels = new sf::Uint8[data_16[0] * data_16[1] * 4];
  18. for (unsigned y = 0; y < data_16[1]; y++)
  19. {
  20. for (unsigned x = 0; x < data_16[0]; x++)
  21. {
  22. Pixel pix(data_16[3 + y * data_16[0] + x]);
  23. unsigned pixel_offset = data_16[0] * y + x;
  24. pixels[4 * pixel_offset] = pix.r << 3;
  25. pixels[4 * pixel_offset + 1] = pix.g << 2;
  26. pixels[4 * pixel_offset + 2] = pix.b << 3;
  27. pixels[4 * pixel_offset + 3] = pix.value == data_16[2] ? 0 : 255;
  28. }
  29. }
  30. this->data.update(pixels);
  31. free(pixels);
  32. }
  33. Texture::Texture(WalrusRPG::PIAF::File entry) : data()
  34. {
  35. // UNUSED(data);
  36. // TOOD : load from PIAF
  37. // this->data.loadFromFile("art/overworld.png");
  38. this->data.loadFromMemory(entry.get(), entry.file_size);
  39. }
  40. Texture::~Texture()
  41. {
  42. }
  43. const Rect Texture::get_dimensions()
  44. {
  45. sf::Vector2u size = data.getSize();
  46. return {0, 0, size.x, size.y};
  47. }
  48. const Pixel Texture::get_pixel(unsigned x, unsigned y)
  49. {
  50. UNUSED(x);
  51. UNUSED(y);
  52. // TODO : return the actual value
  53. return {0};
  54. }