drawing_context.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.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 "video/drawing_context.hpp"
  17. #include <algorithm>
  18. #include "supertux/globals.hpp"
  19. #include "util/obstackpp.hpp"
  20. #include "video/drawing_request.hpp"
  21. #include "video/renderer.hpp"
  22. #include "video/surface.hpp"
  23. #include "video/video_system.hpp"
  24. #include "video/viewport.hpp"
  25. DrawingContext::DrawingContext(VideoSystem& video_system_, obstack& obst, bool overlay) :
  26. m_video_system(video_system_),
  27. m_obst(obst),
  28. m_overlay(overlay),
  29. m_ambient_color(Color::WHITE),
  30. m_transform_stack({ DrawingTransform(m_video_system.get_viewport()) }),
  31. m_colormap_canvas(*this, m_obst),
  32. m_lightmap_canvas(*this, m_obst)
  33. {
  34. }
  35. DrawingContext::~DrawingContext()
  36. {
  37. clear();
  38. }
  39. void
  40. DrawingContext::set_ambient_color(Color ambient_color)
  41. {
  42. m_ambient_color = ambient_color;
  43. }
  44. Rectf
  45. DrawingContext::get_cliprect() const
  46. {
  47. return Rectf(get_translation().x,
  48. get_translation().y,
  49. get_translation().x + static_cast<float>(transform().viewport.get_width()) / transform().scale,
  50. get_translation().y + static_cast<float>(transform().viewport.get_height()) / transform().scale);
  51. }
  52. void
  53. DrawingContext::set_flip(Flip flip)
  54. {
  55. transform().flip = flip;
  56. }
  57. Flip
  58. DrawingContext::get_flip() const
  59. {
  60. return transform().flip;
  61. }
  62. void
  63. DrawingContext::set_alpha(float alpha)
  64. {
  65. transform().alpha = alpha;
  66. }
  67. float
  68. DrawingContext::get_alpha() const
  69. {
  70. return transform().alpha;
  71. }
  72. DrawingTransform&
  73. DrawingContext::transform()
  74. {
  75. assert(!m_transform_stack.empty());
  76. return m_transform_stack.back();
  77. }
  78. const DrawingTransform&
  79. DrawingContext::transform() const
  80. {
  81. assert(!m_transform_stack.empty());
  82. return m_transform_stack.back();
  83. }
  84. void
  85. DrawingContext::push_transform()
  86. {
  87. m_transform_stack.push_back(transform());
  88. }
  89. void
  90. DrawingContext::pop_transform()
  91. {
  92. m_transform_stack.pop_back();
  93. assert(!m_transform_stack.empty());
  94. }
  95. const Rect&
  96. DrawingContext::get_viewport() const
  97. {
  98. return transform().viewport;
  99. }
  100. float
  101. DrawingContext::get_width() const
  102. {
  103. return static_cast<float>(transform().viewport.get_width()) / transform().scale;
  104. }
  105. float
  106. DrawingContext::get_height() const
  107. {
  108. return static_cast<float>(transform().viewport.get_height()) / transform().scale;
  109. }
  110. Vector
  111. DrawingContext::get_size() const
  112. {
  113. return Vector(get_width(), get_height()) * transform().scale;
  114. }
  115. /* EOF */