bitmap_layer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmail.com>
  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 "bitmap_layer.hpp"
  17. #include <assert.h>
  18. #include "color.hpp"
  19. #include "graphic_context.hpp"
  20. #include "objmap_object_impl.hpp"
  21. #include "pixel_buffer.hpp"
  22. #include "pixel_buffer.hpp"
  23. #include "surface.hpp"
  24. class CL_Canvas;
  25. BitmapLayer* BitmapLayer::current_ = 0;
  26. class BitmapLayerImpl : public ObjMapObjectImpl
  27. {
  28. public:
  29. typedef std::vector<Stroke> Strokes;
  30. /** All strokes done on this image are recorded for possible later
  31. playback on a larger size canvas */
  32. Strokes strokes;
  33. /** Used to cache drawings */
  34. Surface surface;
  35. CL_Canvas* canvas;
  36. Pointf last_pos;
  37. BitmapLayerImpl(Surface surface_) :
  38. surface(surface_),
  39. canvas(0)
  40. {
  41. #ifdef GRUMBEL
  42. try {
  43. canvas = new CL_Canvas(surface.to_cl());
  44. canvas->sync_surface();
  45. } catch(const CL_Error& err) {
  46. std::cout << "CL_Error: " << err.message << std::endl;
  47. throw err;
  48. }
  49. #endif
  50. }
  51. BitmapLayerImpl(PixelBuffer buffer)
  52. : surface(buffer),
  53. canvas(0)
  54. {
  55. #ifdef GRUMBEL
  56. try {
  57. canvas = new CL_Canvas(surface.to_cl());
  58. canvas->sync_surface();
  59. } catch(const CL_Error& err) {
  60. std::cout << "CL_Error: " << err.message << std::endl;
  61. throw err;
  62. }
  63. #endif
  64. }
  65. BitmapLayerImpl(int width, int height)
  66. : surface(PixelBuffer(width, height)),
  67. canvas(0)
  68. {
  69. #ifdef GRUMBEL
  70. try {
  71. canvas = new CL_Canvas(surface.to_cl());
  72. canvas->get_gc()->clear(Color(0, 0, 0, 0).to_cl());
  73. canvas->get_gc()->flush();
  74. canvas->sync_surface();
  75. } catch(const CL_Error& err) {
  76. std::cout << "CL_Error: " << err.message << std::endl;
  77. throw err;
  78. }
  79. #endif
  80. }
  81. ~BitmapLayerImpl() {
  82. #ifdef GRUMBEL
  83. delete canvas;
  84. #endif
  85. }
  86. void draw(GraphicContext& gc)
  87. {
  88. assert(canvas);
  89. // Nothing to draw, so we go byebye
  90. if (strokes.empty())
  91. return;
  92. surface.set_blend_func(BlendFunc::one, BlendFunc::one_minus_src_alpha);
  93. surface.draw(pos.x, pos.y);
  94. gc.draw_rect(get_bounding_rect(), Color(155, 155, 155, 100));
  95. }
  96. Rectf get_bound_rect() const
  97. {
  98. return Rectf(Pointf(ObjMapObjectImpl::pos), Sizef(surface.get_width(), surface.get_height()));
  99. }
  100. Rect get_bounding_rect() {
  101. // FIXME: Do we need to handle its position here or does the Layer keep care of that?
  102. return Rect(Point(0, 0),
  103. Size(surface.get_width(), surface.get_height()));
  104. }
  105. bool has_bounding_rect() const {
  106. return true;
  107. }
  108. };
  109. BitmapLayer::BitmapLayer(Surface surface)
  110. : impl(new BitmapLayerImpl(surface))
  111. {
  112. current_ = this;
  113. }
  114. BitmapLayer::BitmapLayer(int width, int height)
  115. : impl(new BitmapLayerImpl(width, height))
  116. {
  117. current_ = this;
  118. }
  119. BitmapLayer::BitmapLayer(PixelBuffer buffer)
  120. : impl(new BitmapLayerImpl(buffer))
  121. {
  122. current_ = this;
  123. }
  124. void
  125. BitmapLayer::add_stroke(const Stroke& stroke)
  126. {
  127. #ifdef GRUMBEL
  128. if (stroke.get_dab_count() > 0)
  129. {
  130. impl->strokes.push_back(stroke);
  131. stroke.draw(impl->canvas->get_gc());
  132. // FIXME: doesn't sync when manually manipulating the canvas
  133. impl->canvas->get_gc()->flush();
  134. impl->canvas->sync_surface();
  135. }
  136. #endif
  137. }
  138. std::vector<Stroke>
  139. BitmapLayer::get_strokes()
  140. {
  141. return impl->strokes;
  142. }
  143. Surface
  144. BitmapLayer::get_background_surface()
  145. {
  146. return impl->surface;
  147. }
  148. CL_Canvas*
  149. BitmapLayer::get_canvas() const
  150. {
  151. return impl->canvas;
  152. }
  153. void
  154. BitmapLayer::set_pixeldata(PixelBuffer buffer)
  155. {
  156. #ifdef GRUMBEL
  157. //impl->canvas->set_pixeldata(buffer);
  158. Surface(buffer).draw(0, 0, impl->canvas->get_gc());
  159. impl->canvas->get_gc()->flush();
  160. impl->canvas->sync_surface();
  161. #endif
  162. }
  163. PixelBuffer
  164. BitmapLayer::get_pixeldata() const
  165. {
  166. #ifdef GRUMBEL
  167. return impl->canvas->get_pixeldata();
  168. #endif
  169. return PixelBuffer();
  170. }
  171. ObjMapObject
  172. BitmapLayer::to_object()
  173. {
  174. return ObjMapObject(impl);
  175. }
  176. /* EOF */