tile.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "tile.hpp"
  17. #include <ClanLib/Core/System/error.h>
  18. #include <ClanLib/Display/Providers/provider_factory.h>
  19. #include <ClanLib/Display/palette.h>
  20. #include <ClanLib/Display/pixel_format.h>
  21. #include <ClanLib/Display/sprite.h>
  22. #include <ClanLib/Display/sprite_description.h>
  23. #include <assert.h>
  24. #include <iostream>
  25. #include "string_converter.hpp"
  26. #include "tile_provider.hpp"
  27. class TileImpl
  28. {
  29. public:
  30. TileProvider provider;
  31. CL_Sprite sprite;
  32. CL_PixelBuffer pixelbuffer;
  33. bool transparent;
  34. bool has_color;
  35. /** Color used for the minimap to represent this tile */
  36. CL_Color color;
  37. /** Color used on 'Show Attributes', ie. to represent walkable areas
  38. and such */
  39. CL_Color attribute_color;
  40. // FIXME: old windstille stuff
  41. unsigned char colmap[8];
  42. std::string filename;
  43. };
  44. Tile::Tile(const TileProvider& provider)
  45. : impl(new TileImpl())
  46. {
  47. impl->provider = provider;
  48. impl->has_color = false;
  49. }
  50. Tile::Tile(const CL_PixelBuffer& pixelbuffer,
  51. const CL_Sprite& sprite)
  52. : impl(new TileImpl())
  53. {
  54. impl->pixelbuffer = pixelbuffer;
  55. impl->sprite = sprite;
  56. impl->has_color = false;
  57. }
  58. Tile::Tile(const CL_PixelBuffer& pixelbuffer)
  59. : impl(new TileImpl())
  60. {
  61. impl->pixelbuffer = pixelbuffer;
  62. impl->has_color = false;
  63. }
  64. Tile::Tile(const std::string& filename_,
  65. const CL_Color& attribute_color_)
  66. : impl(new TileImpl())
  67. {
  68. impl->has_color = false;
  69. impl->attribute_color = attribute_color_;
  70. impl->filename = filename_;
  71. }
  72. Tile::~Tile()
  73. {
  74. }
  75. CL_Color
  76. Tile::get_color()
  77. {
  78. if (impl->has_color)
  79. {
  80. return impl->color;
  81. }
  82. else
  83. {
  84. impl->color = calc_color();
  85. impl->has_color = true;
  86. return impl->color;
  87. }
  88. }
  89. CL_Color
  90. Tile::get_attribute_color()
  91. {
  92. return impl->attribute_color;
  93. }
  94. CL_Sprite&
  95. Tile::get_sprite()
  96. {
  97. if (impl->sprite)
  98. {
  99. return impl->sprite;
  100. }
  101. else
  102. {
  103. if (impl->provider)
  104. {
  105. impl->sprite = impl->provider.get_sprite();
  106. }
  107. else
  108. {
  109. CL_SpriteDescription desc;
  110. desc.add_frame(CL_PixelBuffer(get_pixelbuffer()));
  111. impl->sprite = CL_Sprite(desc);
  112. }
  113. return impl->sprite;
  114. }
  115. }
  116. CL_PixelBuffer
  117. Tile::get_pixelbuffer()
  118. {
  119. if (impl->pixelbuffer)
  120. {
  121. return impl->pixelbuffer;
  122. }
  123. else
  124. {
  125. if (impl->provider)
  126. {
  127. impl->pixelbuffer = impl->provider.get_pixelbuffer();
  128. return impl->pixelbuffer;
  129. }
  130. else
  131. {
  132. // FIXME: Move all this into a special provider
  133. try {
  134. if (has_suffix(impl->filename, ".png") || has_suffix(impl->filename, ".jpg"))
  135. {
  136. impl->pixelbuffer = CL_PixelBuffer(CL_ProviderFactory::load(impl->filename));
  137. }
  138. else
  139. {
  140. //CL_SpriteDescription descr(impl->filename, resources);
  141. //impl->pixelbuffer = CL_PixelBuffer(*(descr.get_frames().begin()->first));
  142. std::cout << "Error: not a png or jpg file: " << impl->filename << std::endl;
  143. assert(0);
  144. }
  145. return impl->pixelbuffer;
  146. } catch(CL_Error& err) {
  147. std::cout << "CL_Error: " << err.message << std::endl;
  148. std::cout << " filename = " << impl->filename << std::endl;
  149. return CL_PixelBuffer();
  150. }
  151. }
  152. }
  153. }
  154. CL_Color
  155. Tile::calc_color()
  156. {
  157. CL_PixelBuffer buffer = get_pixelbuffer();
  158. buffer.lock();
  159. unsigned char* buf = static_cast<unsigned char*>(buffer.get_data());
  160. int len = buffer.get_height() * buffer.get_width();
  161. int red = 0;
  162. int green = 0;
  163. int blue = 0;
  164. int alpha = 0;
  165. switch (buffer.get_format().get_depth())
  166. {
  167. case 8:
  168. {
  169. CL_Palette palette = buffer.get_palette();
  170. for(int i = 0; i < len; ++i)
  171. {
  172. red += palette.colors[buf[i]].get_red();
  173. green += palette.colors[buf[i]].get_green();
  174. blue += palette.colors[buf[i]].get_blue();
  175. alpha += 255;
  176. }
  177. }
  178. break;
  179. case 24:
  180. for(int i = 0; i < len; ++i)
  181. {
  182. red += buf[3*i + 0];
  183. green += buf[3*i + 1];
  184. blue += buf[3*i + 2];
  185. alpha += 255;
  186. }
  187. break;
  188. case 32:
  189. for(int i = 0; i < len; ++i)
  190. {
  191. int a = buf[4*i + 0];
  192. alpha += a;
  193. red += buf[4*i + 3]*a/255;;
  194. green += buf[4*i + 2]*a/255;;
  195. blue += buf[4*i + 1]*a/255;;
  196. }
  197. break;
  198. }
  199. buffer.unlock();
  200. return CL_Color(static_cast<int>(red / len),
  201. static_cast<int>(green / len),
  202. static_cast<int>(blue / len),
  203. static_cast<int>(alpha / len));
  204. }
  205. bool
  206. Tile::get_col(unsigned char x, unsigned char y)
  207. {
  208. assert(x < 8);
  209. assert(y < 8);
  210. return (impl->colmap[y] & (1 << (7-x)));
  211. }
  212. void
  213. Tile::set_col(unsigned char x, unsigned char y, bool val)
  214. {
  215. assert(x < 8);
  216. assert(y < 8);
  217. if (val)
  218. impl->colmap[y] |= (1 << (7-x));
  219. else
  220. impl->colmap[y] &= ~(1 << (7-x));
  221. }
  222. std::string
  223. Tile::get_filename() const
  224. {
  225. return impl->filename;
  226. }
  227. /* EOF */