onion_skin_layer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "onion_skin_layer.hpp"
  17. #include <iostream>
  18. #include <vector>
  19. #include <ClanLib/Core/System/error.h>
  20. #include <ClanLib/Display/pixel_buffer.h>
  21. #include <ClanLib/Display/pixel_format.h>
  22. #include <ClanLib/Display/surface.h>
  23. #include <ClanLib/Display/canvas.h>
  24. #include "gui/editor_map_component.hpp"
  25. #include "layer_impl.hpp"
  26. #define SCALE 4
  27. class OnionSkinLayerImpl : public LayerImpl
  28. {
  29. public:
  30. CL_Surface surface;
  31. CL_Canvas* canvas;
  32. CL_Surface surface2;
  33. CL_Canvas* canvas2;
  34. std::vector<EditorMap> editormaps;
  35. std::vector<CL_Color> color;
  36. void draw(const GraphicContextState& state, CL_GraphicContext* gc)
  37. {
  38. // FIXME: We need to stop onion layer to draw onto itself
  39. surface.set_blend_func(blend_one, blend_one_minus_src_alpha);
  40. surface.set_scale(SCALE, SCALE);
  41. surface.draw(0, 0);
  42. }
  43. bool has_bounding_rect() const
  44. {
  45. return false;
  46. }
  47. };
  48. OnionSkinLayer::OnionSkinLayer(Layer layer) :
  49. impl(std::dynamic_pointer_cast<OnionSkinLayerImpl>(layer.impl))
  50. {
  51. }
  52. OnionSkinLayer::OnionSkinLayer(int width, int height) :
  53. impl(new OnionSkinLayerImpl())
  54. {
  55. impl->surface = CL_Surface(CL_PixelBuffer(width/SCALE, height/SCALE, width*4/SCALE, CL_PixelFormat::rgba8888));
  56. impl->surface2 = CL_Surface(CL_PixelBuffer(width/SCALE, height/SCALE, width*4/SCALE, CL_PixelFormat::rgba8888));
  57. try
  58. {
  59. impl->canvas = new CL_Canvas(impl->surface);
  60. impl->canvas->get_gc()->clear(CL_Color(0, 0, 0, 0));
  61. impl->canvas->get_gc()->flush();
  62. impl->canvas->sync_surface();
  63. impl->canvas2 = new CL_Canvas(impl->surface2);
  64. impl->canvas2->get_gc()->clear(CL_Color(0, 0, 0, 0));
  65. impl->canvas2->get_gc()->flush();
  66. impl->canvas2->sync_surface();
  67. }
  68. catch(CL_Error& err)
  69. {
  70. std::cout << "CL_Error: " << err.message << std::endl;
  71. throw err;
  72. }
  73. }
  74. void
  75. OnionSkinLayer::clear()
  76. {
  77. impl->canvas->get_gc()->clear(CL_Color(0, 0, 0, 0));
  78. impl->canvas->sync_surface();
  79. }
  80. void
  81. OnionSkinLayer::add_map(EditorMap editor_map, const CL_Color& color)
  82. {
  83. impl->editormaps.push_back(editor_map);
  84. impl->color.push_back(color);
  85. }
  86. void
  87. OnionSkinLayer::update()
  88. {
  89. impl->canvas->get_gc()->clear(CL_Color(0, 0, 0, 0));
  90. for (std::vector<EditorMap>::size_type i = 0; i < impl->editormaps.size(); ++i)
  91. {
  92. impl->canvas2->get_gc()->clear(CL_Color(0, 0, 0, 0));
  93. impl->canvas2->get_gc()->push_modelview();
  94. impl->canvas2->get_gc()->add_scale(1.0f/SCALE, 1.0f/SCALE);
  95. impl->editormaps[i].draw(EditorMapComponent::current()->get_gc_state(), impl->canvas2->get_gc());
  96. impl->canvas2->get_gc()->pop_modelview();
  97. impl->canvas2->sync_surface();
  98. impl->surface2.set_color(impl->color[i]);
  99. impl->surface2.draw(0, 0, impl->canvas->get_gc());
  100. impl->canvas->sync_surface();
  101. }
  102. }
  103. Layer
  104. OnionSkinLayer::to_layer()
  105. {
  106. return Layer(impl);
  107. }
  108. /* EOF */