You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

203 rivejä
4.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <cstdio>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #if defined _WIN32
  17. # define WIN32_LEAN_AND_MEAN
  18. # include <windows.h>
  19. # undef WIN32_LEAN_AND_MEAN
  20. #endif
  21. #include "textureimage-private.h"
  22. namespace lol
  23. {
  24. /*
  25. * TileSet implementation class
  26. */
  27. TextureImageData* TextureImage::GetNewData()
  28. {
  29. return new TextureImageData();
  30. }
  31. /*
  32. * Public TextureImage class
  33. */
  34. TextureImage::TextureImage(std::string const &path)
  35. : m_data(GetNewData())
  36. {
  37. Init(path);
  38. }
  39. TextureImage::TextureImage(std::string const &path, image* img)
  40. : m_data(GetNewData())
  41. {
  42. Init(path, img);
  43. }
  44. TextureImage::~TextureImage()
  45. {
  46. delete m_data;
  47. }
  48. void TextureImage::Init(std::string const &path)
  49. {
  50. Init(path, ResourceLoader::Load(path));
  51. }
  52. void TextureImage::Init(std::string const &path, ResourceCodecData* loaded_data)
  53. {
  54. //Load image if available
  55. auto image_data = dynamic_cast<ResourceImageData*>(loaded_data);
  56. if (image_data != nullptr)
  57. {
  58. Init(path, new image(*image_data->m_image));
  59. }
  60. delete image_data;
  61. }
  62. void TextureImage::Init(std::string const &path, image* img)
  63. {
  64. m_data->m_name = "<textureimage> " + path;
  65. m_data->m_texture = nullptr;
  66. m_data->m_image = img;
  67. m_data->m_image_size = m_data->m_image->size();
  68. m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x),
  69. PotUp(m_data->m_image_size.y));
  70. m_drawgroup = tickable::group::draw::texture;
  71. }
  72. void TextureImage::tick_draw(float seconds, Scene &scene)
  73. {
  74. super::tick_draw(seconds, scene);
  75. if (has_flags(entity::flags::destroying))
  76. {
  77. if (m_data->m_image)
  78. {
  79. delete m_data->m_image;
  80. m_data->m_image = nullptr;
  81. }
  82. else
  83. {
  84. delete m_data->m_texture;
  85. m_data->m_texture = nullptr;
  86. }
  87. }
  88. else if (m_data->m_image)
  89. {
  90. //Update texture is needed
  91. if (m_data->m_texture)
  92. {
  93. delete m_data->m_texture;
  94. m_data->m_texture = nullptr;
  95. }
  96. PixelFormat format = m_data->m_image->format();
  97. int planes = BytesPerPixel(format);
  98. int w = m_data->m_texture_size.x;
  99. int h = m_data->m_texture_size.y;
  100. uint8_t *pixels = (uint8_t *)m_data->m_image->lock();
  101. bool resized = false;
  102. if (w != m_data->m_image_size.x || h != m_data->m_image_size.y)
  103. {
  104. uint8_t *tmp = new uint8_t[planes * w * h];
  105. for (int line = 0; line < m_data->m_image_size.y; line++)
  106. memcpy(tmp + planes * w * line,
  107. pixels + planes * m_data->m_image_size.x * line,
  108. planes * m_data->m_image_size.x);
  109. pixels = tmp;
  110. resized = false;
  111. }
  112. /* FIXME: no unlock? */
  113. m_data->m_texture = new Texture(ivec2(w, h), format);
  114. m_data->m_texture->SetData(pixels);
  115. if (resized)
  116. delete[] pixels;
  117. delete m_data->m_image;
  118. m_data->m_image = nullptr;
  119. }
  120. }
  121. //-----------------------------------------------------------------------------
  122. std::string TextureImage::GetName() const
  123. {
  124. return m_data->m_name;
  125. }
  126. void TextureImage::UpdateTexture(image* img)
  127. {
  128. m_data->m_image = img;
  129. m_data->m_image_size = m_data->m_image->size();
  130. m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x),
  131. PotUp(m_data->m_image_size.y));
  132. }
  133. Texture * TextureImage::GetTexture()
  134. {
  135. return m_data->m_texture;
  136. }
  137. Texture const * TextureImage::GetTexture() const
  138. {
  139. return m_data->m_texture;
  140. }
  141. image * TextureImage::GetImage()
  142. {
  143. return m_data->m_image;
  144. }
  145. image const * TextureImage::GetImage() const
  146. {
  147. return m_data->m_image;
  148. }
  149. ivec2 TextureImage::GetImageSize() const
  150. {
  151. return m_data->m_image_size;
  152. }
  153. ivec2 TextureImage::GetTextureSize() const
  154. {
  155. return m_data->m_texture_size;
  156. }
  157. void TextureImage::Bind()
  158. {
  159. if (!m_data->m_image && m_data->m_texture)
  160. m_data->m_texture->Bind();
  161. }
  162. void TextureImage::Unbind()
  163. {
  164. ;
  165. }
  166. } /* namespace lol */