Texture.cpp 1.5 KB

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