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 <boost/optional.hpp>
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);
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 
79  void set_flip(Flip flip);
80  Flip get_flip() const;
81 
83  void set_alpha(float alpha);
84  float get_alpha() const;
85 
86  void clear()
87  {
88  m_lightmap_canvas.clear();
89  m_colormap_canvas.clear();
90  }
91 
92  void set_viewport(const Rect& viewport)
93  {
94  m_viewport = viewport;
95  }
96 
97  const Rect& get_viewport() const { return m_viewport; }
98 
99  int get_width() const { return m_viewport.get_width(); }
100  int get_height() const { return m_viewport.get_height(); }
101 
102  bool use_lightmap() const
103  {
104  return !m_overlay && m_ambient_color != Color::WHITE;
105  }
106 
107  bool is_overlay() const { return m_overlay; }
108 
109 private:
110  VideoSystem& m_video_system;
111 
114  obstack& m_obst;
115 
118  bool m_overlay;
119 
120  Rect m_viewport;
121  Color m_ambient_color;
122  std::vector<DrawingTransform> m_transform_stack;
123 
124  Canvas m_colormap_canvas;
125  Canvas m_lightmap_canvas;
126 
127 private:
128  DrawingContext(const DrawingContext&) = delete;
129  DrawingContext& operator=(const DrawingContext&) = delete;
130 };
131 
132 #endif
133 
134 /* EOF */
Definition: obstack.h:151
Definition: drawing_transform.hpp:23
Simple two dimensional vector.
Definition: vector.hpp:24
Definition: rect.hpp:30
Definition: drawing_request.hpp:37
Definition: rectf.hpp:29
void set_alpha(float alpha)
apply that alpha in the next draws (1.0 means fully opaque)
Definition: drawing_context.cpp:76
Definition: video_system.hpp:36
void set_flip(Flip flip)
Apply that flip in the next draws (flips are listed on surface.h).
Definition: drawing_context.cpp:64
Rectf get_cliprect() const
Returns the visible area in world coordinates.
Definition: drawing_context.cpp:55
Definition: color.hpp:25
Definition: canvas.hpp:43
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42