marker_stroke_drawer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <ClanLib/gl.h>
  17. #include <ClanLib/Display/display.h>
  18. #include "stroke_drawer_impl.hpp"
  19. #include "stroke.hpp"
  20. #include "drawer_properties.hpp"
  21. #include "marker_stroke_drawer.hpp"
  22. class MarkerStrokeDrawerImpl : public StrokeDrawerImpl
  23. {
  24. public:
  25. MarkerStrokeDrawerImpl() {}
  26. void draw(const Stroke& stroke, CL_GraphicContext* gc)
  27. {
  28. CL_OpenGLState state(CL_Display::get_current_window()->get_gc());
  29. state.set_active();
  30. state.setup_2d();
  31. CL_Color color = DrawerProperties::current()->get_color();
  32. const Stroke::Dabs& dabs = stroke.get_interpolated_dabs(DrawerProperties::current()->get_spacing(),
  33. DrawerProperties::current()->get_spacing());
  34. if (dabs.size() >= 2)
  35. {
  36. std::vector<CL_Pointf> normals;
  37. if (stroke.get_dab_count() == 2)
  38. {
  39. normals.push_back(CL_Pointf(1.0f, 1.0f));
  40. normals.push_back(CL_Pointf(1.0f, 1.0f));
  41. }
  42. else if (stroke.get_dab_count() >= 3)
  43. {
  44. for(Stroke::Dabs::size_type i = 0; i < dabs.size()-1; ++i)
  45. {
  46. CL_Pointf normal((dabs[i].pos.y - dabs[i+1].pos.y),
  47. -(dabs[i].pos.x - dabs[i+1].pos.x));
  48. float length = sqrt(normal.x * normal.x + normal.y * normal.y);
  49. normal.x /= length;
  50. normal.y /= length;
  51. normals.push_back(normal);
  52. }
  53. normals.push_back(CL_Pointf(1.0f, 1.0f));
  54. }
  55. float len = DrawerProperties::current()->get_size() * 8.0f;
  56. float len2 = DrawerProperties::current()->get_size() * 16.0f;
  57. glEnable(GL_BLEND);
  58. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  59. glBegin(GL_QUAD_STRIP);
  60. for(unsigned int j = 0; j < dabs.size()-2; ++j)
  61. {
  62. glColor4ub(color.get_red(), color.get_green(), color.get_blue(), color.get_alpha());
  63. glVertex2f(dabs[j].pos.x + normals[j].x * len,
  64. dabs[j].pos.y + normals[j].y * len);
  65. glColor4ub(color.get_red(), color.get_green(), color.get_blue(), 0);
  66. glVertex2f(dabs[j].pos.x + normals[j].x * len2,
  67. dabs[j].pos.y + normals[j].y * len2);
  68. }
  69. glEnd();
  70. glBegin(GL_QUAD_STRIP);
  71. for(unsigned int j = 0; j < dabs.size()-2; ++j)
  72. {
  73. glColor4ub(color.get_red(), color.get_green(), color.get_blue(), 0);
  74. glVertex2f(dabs[j].pos.x - normals[j].x * len2,
  75. dabs[j].pos.y - normals[j].y * len2);
  76. glColor4ub(color.get_red(), color.get_green(), color.get_blue(), color.get_alpha());
  77. glVertex2f(dabs[j].pos.x - normals[j].x * len,
  78. dabs[j].pos.y - normals[j].y * len);
  79. }
  80. glEnd();
  81. glBegin(GL_QUAD_STRIP);
  82. glColor4ub(color.get_red(), color.get_green(), color.get_blue(), color.get_alpha());
  83. for(unsigned int j = 0; j < dabs.size()-2; ++j)
  84. {
  85. glVertex2f(dabs[j].pos.x + normals[j].x * len,
  86. dabs[j].pos.y + normals[j].y * len);
  87. glVertex2f(dabs[j].pos.x - normals[j].x * len,
  88. dabs[j].pos.y - normals[j].y * len);
  89. }
  90. glEnd();
  91. }
  92. }
  93. StrokeDrawerImpl* clone() const
  94. {
  95. MarkerStrokeDrawerImpl* drawer = new MarkerStrokeDrawerImpl();
  96. *drawer = *this;
  97. return drawer;
  98. }
  99. };
  100. MarkerStrokeDrawer::MarkerStrokeDrawer()
  101. : impl(new MarkerStrokeDrawerImpl())
  102. {
  103. }
  104. StrokeDrawer
  105. MarkerStrokeDrawer::to_drawer()
  106. {
  107. return StrokeDrawer(impl);
  108. }
  109. /* EOF */