supertux
drawing_context.hpp
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 
17 #ifndef HEADER_SUPERTUX_VIDEO_DRAWING_CONTEXT_HPP
18 #define HEADER_SUPERTUX_VIDEO_DRAWING_CONTEXT_HPP
19 
20 #include <string>
21 #include <vector>
22 #include <obstack.h>
23 #include <optional>
24 
25 #include "math/rect.hpp"
26 #include "math/rectf.hpp"
27 #include "math/vector.hpp"
28 #include "video/canvas.hpp"
29 #include "video/color.hpp"
30 #include "video/drawing_context.hpp"
31 #include "video/drawing_transform.hpp"
32 #include "video/font.hpp"
33 #include "video/font_ptr.hpp"
34 
35 class VideoSystem;
36 struct DrawingRequest;
37 struct obstack;
38 
42 class DrawingContext final
43 {
44 public:
45  DrawingContext(VideoSystem& video_system, obstack& obst, bool overlay, float time_offset);
46  ~DrawingContext();
47 
49  Rectf get_cliprect() const;
50 
51  Canvas& color() { return m_colormap_canvas; }
52  Canvas& light() { assert(!m_overlay); return m_lightmap_canvas; }
53  Canvas& get_canvas(DrawingTarget target) {
54  switch (target)
55  {
56  case DrawingTarget::LIGHTMAP:
57  return light();
58 
59  default:
60  return color();
61  }
62  }
63 
64  void set_ambient_color(Color ambient_color);
65  Color get_ambient_color() const { return m_ambient_color; }
66 
67  void push_transform();
68  void pop_transform();
69  DrawingTransform& transform();
70  const DrawingTransform& transform() const;
71 
72  const Vector& get_translation() const
73  { return transform().translation; }
74 
75  void set_translation(const Vector& newtranslation)
76  { transform().translation = newtranslation; }
77 
78  float get_scale() const { return transform().scale; }
79  void scale(float scale) { transform().scale *= scale; }
80 
82  bool perspective_scale(float speed_x, float speed_y);
83 
85  void set_flip(Flip flip);
86  Flip get_flip() const;
87 
89  void set_alpha(float alpha);
90  float get_alpha() const;
91 
93  void set_time_offset(float time_offset) { m_time_offset = time_offset; }
94  float get_time_offset() const { return m_time_offset; }
95 
96  void clear()
97  {
98  m_lightmap_canvas.clear();
99  m_colormap_canvas.clear();
100  }
101 
102  void set_viewport(const Rect& viewport)
103  {
104  transform().viewport = viewport;
105  }
106 
107  const Rect& get_viewport() const;
108 
109  float get_width() const;
110  float get_height() const;
111  Vector get_size() const;
112  Rectf get_rect() const { return Rectf(Vector(0, 0), get_size()); }
113 
114  bool use_lightmap() const
115  {
116  return !m_overlay && m_ambient_color != Color::WHITE;
117  }
118 
119  bool is_overlay() const { return m_overlay; }
120 
121 private:
122  VideoSystem& m_video_system;
123 
126  obstack& m_obst;
127 
130  bool m_overlay;
131 
132  Color m_ambient_color;
133  std::vector<DrawingTransform> m_transform_stack;
134 
135  Canvas m_colormap_canvas;
136  Canvas m_lightmap_canvas;
137 
138  float m_time_offset;
139 
140 private:
141  DrawingContext(const DrawingContext&) = delete;
142  DrawingContext& operator=(const DrawingContext&) = delete;
143 };
144 
145 #endif
146 
147 /* EOF */
Definition: obstack.h:151
Definition: drawing_transform.hpp:24
Definition: rect.hpp:30
Definition: drawing_request.hpp:39
bool perspective_scale(float speed_x, float speed_y)
Recalculates the scaling factor for parallax layers.
Definition: drawing_context.cpp:137
Definition: rectf.hpp:31
void set_time_offset(float time_offset)
For position extrapolation at high frame rates: real time since last game update step.
Definition: drawing_context.hpp:93
void set_alpha(float alpha)
apply that alpha in the next draws (1.0 means fully opaque)
Definition: drawing_context.cpp:74
Definition: video_system.hpp:37
void set_flip(Flip flip)
Apply that flip in the next draws (flips are listed on surface.h).
Definition: drawing_context.cpp:62
Rectf get_cliprect() const
Returns the visible area in world coordinates.
Definition: drawing_context.cpp:53
Definition: color.hpp:26
Definition: canvas.hpp:43
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42