Gorgon Game Engine
Layer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Layer.h"
4 #include "TextureTargets.h"
5 #include "../Utils/Assert.h"
6 #include "../Geometry/Point3D.h"
7 #include "../Geometry/Transform3D.h"
8 #include "../Geometry/Rectangle.h"
9 #include "../GL/Simple.h"
10 #include "../GL/FrameBuffer.h"
11 
12 namespace Gorgon { namespace Graphics {
13 
15  namespace internal {
16 
18  class Surface {
19  public:
20  Surface(const Surface &) = delete;
21 
22  Surface(Surface &&s) {
23  vertices = s.vertices;
24  texture = s.texture;
25  source = s.source;
26  color = s.color;
27  drawmode = s.drawmode;
28 
29  s.texture = nullptr;
30  s.source = nullptr;
31  }
32 
33  Surface &operator =(const Surface &) = delete;
34 
35  Surface &operator =(Surface &&s) {
36  vertices = s.vertices;
37  texture = s.texture;
38  source = s.source;
39  color = s.color;
40  drawmode = s.drawmode;
41 
42  s.texture = nullptr;
43  s.source = nullptr;
44 
45  return *this;
46  }
47 
49  Surface(const TextureSource &source, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
50  const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color, TextureTarget::DrawMode drawmode) :
51  source(&source), color(color), drawmode(drawmode) {
52 
53  vertices[0] = p1;
54  vertices[1] = p2;
55  vertices[2] = p3;
56  vertices[3] = p4;
57  }
58 
60  Surface(const Geometry::Pointf &p1, const Geometry::Pointf &p2,
61  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
62  RGBAf color, TextureTarget::DrawMode drawmode) : color(color), drawmode(drawmode) {
63  vertices[0] = p1;
64  vertices[1] = p2;
65  vertices[2] = p3;
66  vertices[3] = p4;
67  }
68 
70  Surface(const TextureSource &source, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
71  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
72  const Geometry::Pointf &t1, const Geometry::Pointf &t2,
73  const Geometry::Pointf &t3, const Geometry::Pointf &t4,
74  RGBAf color, TextureTarget::DrawMode drawmode) : source(&source), color(color), drawmode(drawmode) {
75 
76  texture = new Geometry::Pointf[4];
77 
78  texture [0] = t1;
79  texture [1] = t2;
80  texture [2] = t3;
81  texture [3] = t4;
82 
83  vertices[0] = p1;
84  vertices[1] = p2;
85  vertices[2] = p3;
86  vertices[3] = p4;
87  }
88 
90  bool IsSet() {
91  return source != nullptr;
92  }
93 
95  GL::Texture TextureID() const {
96  return source->GetID();
97  }
98 
100  GL::QuadVertices GetVertices(const Geometry::Transform3D &transform) {
101  return transform*vertices;
102  }
103 
105  GL::QuadTextureCoords GetTextureCoords() {
106  if(texture)
107  return {texture[0], texture[1], texture[2], texture[3]};
108  else {
109  auto texture=source->GetCoordinates();
110  return {texture[0], texture[1], texture[2], texture[3]};
111  }
112  }
113 
114  bool IsPartial() {
115  return source->IsPartial();
116  }
117 
119  ColorMode GetMode() const {
120  return source->GetMode();
121  }
122 
124  RGBAf GetColor() const {
125  return color;
126  }
127 
128  TextureTarget::DrawMode GetDrawMode() const {
129  return drawmode;
130  }
131 
132  void SetDrawMode(TextureTarget::DrawMode value) {
133  drawmode = value;
134  }
135 
136  ~Surface() {
137  if(texture) delete[] texture;
138  texture = nullptr;
139  }
140 
141  private:
143 
144  GL::QuadVertices vertices;
145  Geometry::Pointf *texture = nullptr;
146 
147  const TextureSource *source = nullptr;
148  RGBAf color;
149  };
150  }
152 
169  class Layer : public Gorgon::Layer, public Graphics::TextureTarget {
170  struct Operation {
171  enum {
172  NewMask
173  } type;
174 
175  std::size_t index;
176  };
177  friend void Initialize();
178  public:
179 
182  { }
183 
186  Layer() : Gorgon::Layer() { }
187 
189  Layer(const Geometry::Point &location) :
190  Gorgon::Layer(location)
191  { }
192 
194  Layer(const Layer&) = delete;
195 
197  Layer(Layer &&other) {
198  Swap(other);
199  }
200 
201  void Swap(Layer &other) {
202  using std::swap;
203 
204  swap(bounds, other.bounds);
205  swap(isvisible, other.isvisible);
206  swap(children, other.children);
207  swap(color, other.color);
208  swap(tint, other.tint);
209  swap(mode, other.mode);
210  swap(surfaces, other.surfaces);
211 
212  if(parent==other.parent) return;
213 
214  auto myparent = parent;
215  auto otherparent = other.parent;
216 
217  int myind = -1;
218  int otherind = -1;
219 
220  if(myparent) {
221  myind = myparent->Children.FindLocation(this);
222  }
223 
224  if(otherparent) {
225  otherind = otherparent->Children.FindLocation(other);
226  }
227 
228  if(myparent) {
229  myparent->Remove(this);
230  myparent->Insert(other, myind);
231  }
232 
233  if(otherparent) {
234  otherparent->Remove(other);
235  otherparent->Insert(this, otherind);
236  }
237  }
238 
239  using TextureTarget::Draw;
240 
242  virtual void Draw(const TextureSource &image, const Geometry::Pointf &p1, const Geometry::Pointf &p2,
243  const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color = RGBAf(1.f)) override {
244 
245  surfaces.emplace_back(image, p1, p2, p3, p4, color*this->color, mode);
246  }
247 
249  virtual void Draw(const Geometry::Pointf &p1, const Geometry::Pointf &p2,
250  const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color = RGBAf(1.f)) override {
251 
252  surfaces.emplace_back(p1, p2, p3, p4, color*this->color, mode);
253  }
254 
256  virtual void Draw(
257  const TextureSource &image,
258  const Geometry::Pointf &p1, const Geometry::Pointf &p2,
259  const Geometry::Pointf &p3, const Geometry::Pointf &p4,
260  const Geometry::Pointf &tex1, const Geometry::Pointf &tex2,
261  const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color = RGBAf(1.f)) override {
262 
263  surfaces.emplace_back(image, p1, p2, p3, p4, tex1, tex2, tex3, tex4, color*this->color, mode);
264  }
265 
267  virtual void Draw(const TextureSource &image, Tiling tiling, const Geometry::Rectanglef &location, RGBAf color = RGBAf(1.f)) override;
268 
269  virtual void Clear() override {
270  surfaces.clear();
271  }
272 
274  virtual void Render() override;
275 
277  virtual DrawMode GetDrawMode() const override { return mode; }
278 
280  virtual void SetDrawMode(DrawMode mode) override { this->mode=mode; }
281 
283  virtual void NewMask() override {
284  Operation op = {Operation::NewMask, surfaces.size()+operations.size()};
285  operations.push_back(op);
286  }
287 
289  virtual void SetTintColor(RGBAf value) { tint = value; }
290 
292  virtual RGBAf GetTintColor() const { return tint; }
293 
296  virtual void SetColor(RGBAf value) { color = value; }
297 
299  virtual RGBAf GetColor() const { return color; }
300 
301  virtual Geometry::Size GetTargetSize() const override {
302  if(bounds.Width() != 0 && bounds.Height() != 0)
303  return bounds.GetSize();
304  else
306  }
307 
309  void EnableClipping() {
310  clippingenabled = true;
311  }
312 
315  clippingenabled = false;
316  }
317 
319  bool IsClippingEnabled() const {
320  return clippingenabled;
321  }
322 
324 
325  private:
326  std::vector<internal::Surface> surfaces;
327  std::vector<Operation> operations;
328 
329  bool clippingenabled = false;
330  static Geometry::Rectangle cliprectangle;
331 
333  RGBAf tint = RGBAf(1.f);
334  RGBAf color = RGBAf(1.f);
335 
336  static GL::FrameBuffer mask;
337  };
338 
339 
340 } }
Gorgon::Graphics::TextureTarget::DrawMode
DrawMode
Definition: TextureTargets.h:14
Gorgon::Geometry::basic_Bounds
This class represents boundaries of 2D objects.
Definition: Bounds.h:27
Gorgon::swap
void swap(Event< Source_, Args_... > &l, Event< Source_, Args_... > &r)
Swaps two events.
Definition: Event.h:351
Gorgon::Graphics::Layer::Clear
virtual void Clear() override
Clears drawing buffer, in layer architecture this request only affects the layer itself,...
Definition: Layer.h:269
Gorgon::Graphics::Layer::IsClippingEnabled
bool IsClippingEnabled() const
Returns if the clipping is enabled.
Definition: Layer.h:319
Gorgon::Graphics::Layer::Layer
Layer(const Geometry::Point &location)
Constructor that places the layer to the given location.
Definition: Layer.h:189
Gorgon::Graphics::ColorMode
ColorMode
Color modes for images.
Definition: Color.h:16
Gorgon::Layer::Children
const Containers::Collection< Layer > & Children
Sub-layers that this layer holds, all the sub-layers are considered to be above current layer.
Definition: Layer.h:475
TextureTargets.h
Gorgon::Graphics::Layer::SetColor
virtual void SetColor(RGBAf value)
Changes the tint color of the layer, every image pixel will be multiplied by this color.
Definition: Layer.h:296
Gorgon::Graphics::Layer::Render
virtual void Render() override
Render this layer to the GL. This function is used internally and not necessary to be called.
Definition: Layer.cpp:61
Gorgon::Geometry::basic_Bounds::Width
T_ Width() const
Calculates and returns the width of the bounds.
Definition: Bounds.h:130
Gorgon::Layer::parent
Layer * parent
Parent layer, could be nullptr.
Definition: Layer.h:504
Gorgon::Graphics::Layer::Swap
void Swap(Layer &other)
Definition: Layer.h:201
Gorgon::Graphics::RGBAf
Represents a four channel 32 bit float per channel color information.
Definition: Color.h:373
Gorgon::Geometry::basic_Bounds::Height
T_ Height() const
Calculates and returns the height of the bounds.
Definition: Bounds.h:135
Gorgon::Graphics::Layer::Layer
Layer(const Layer &)=delete
Copy constructor is disabled.
Gorgon::Graphics::Layer::GetTargetSize
virtual Geometry::Size GetTargetSize() const override
Get size of the target.
Definition: Layer.h:301
Gorgon::Graphics::Layer::Draw
virtual void Draw(const TextureSource &image, const Geometry::Pointf &p1, const Geometry::Pointf &p2, const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color=RGBAf(1.f)) override
Prefer using Draw functions of image or animations.
Definition: Layer.h:242
Gorgon::Graphics::Layer
This layer allows drawing texture images on.
Definition: Layer.h:169
Gorgon::Graphics::Tiling
Tiling
Details which directions a texture should tile.
Definition: Graphics.h:31
Gorgon::Graphics::Layer::GetColor
virtual RGBAf GetColor() const
Changes the tint color of the layer, every image pixel will be multiplied by this color.
Definition: Layer.h:299
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Geometry::basic_Size
This class represents a 2D geometric size.
Definition: Size.h:23
Gorgon::Graphics::Layer::DisableClipping
void DisableClipping()
Disables graphics clipping.
Definition: Layer.h:314
Gorgon::Graphics::Layer::Layer
Layer()
Constructor that sets the layer to cover entire parent, no matter how big it is.
Definition: Layer.h:186
Gorgon::Layer::bounds
Geometry::Bounds bounds
Bounds of this layer.
Definition: Layer.h:507
Gorgon::Graphics::Layer::Draw
virtual void Draw(const TextureSource &image, const Geometry::Pointf &p1, const Geometry::Pointf &p2, const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color=RGBAf(1.f))=0
Draws a simple texture to the screen.
Gorgon::Graphics::Layer::SetTintColor
virtual void SetTintColor(RGBAf value)
Changes the tint color of the layer, every image pixel will be multiplied by this color.
Definition: Layer.h:289
Gorgon::Graphics::TextureTarget::Normal
@ Normal
Definition: TextureTargets.h:15
Gorgon::Graphics::Layer::Initialize
friend void Initialize()
Initializes the filesystem module.
Definition: Filesystem.cpp:14
Gorgon::Geometry::Pointf
basic_Point< Float > Pointf
Definition: Point.h:601
Gorgon::Graphics::TextureTarget
This interface defines a class that can be used as a common target for texture based drawing.
Definition: TextureTargets.h:12
Gorgon::Graphics::Layer::Layer
Layer(Layer &&other)
Move constructor.
Definition: Layer.h:197
Gorgon::GL::FrameBuffer
This is a frame buffer object that can be used for render to texture tasks.
Definition: FrameBuffer.h:13
Gorgon::Layer
This class is the base class for all layer types.
Definition: Layer.h:79
Gorgon::Geometry::basic_Point
This class represents a 2D point.
Definition: Point.h:32
Gorgon::Layer::GetEffectiveBounds
Geometry::Bounds GetEffectiveBounds() const
Returns the effective boundaries of the layer.
Definition: Layer.h:423
Gorgon::Graphics::TextureTarget::Draw
virtual void Draw(const TextureSource &image, const Geometry::Pointf &p1, const Geometry::Pointf &p2, const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color=RGBAf(1.f))=0
Draws a simple texture to the screen.
Gorgon::Graphics::Layer::EnableClipping
void EnableClipping()
Enables graphics clipping from the visible borders of the layer.
Definition: Layer.h:309
Gorgon::Graphics::Layer::Draw
virtual void Draw(const TextureSource &image, const Geometry::Pointf &p1, const Geometry::Pointf &p2, const Geometry::Pointf &p3, const Geometry::Pointf &p4, const Geometry::Pointf &tex1, const Geometry::Pointf &tex2, const Geometry::Pointf &tex3, const Geometry::Pointf &tex4, RGBAf color=RGBAf(1.f)) override
Prefer using Draw functions of image or animations.
Definition: Layer.h:256
Gorgon::UI::Graphics
@ Graphics
Definition: Template.h:164
Gorgon::Graphics::Layer::NewMask
virtual void NewMask() override
Queues the start of a new mask. Only one mask buffer exists and it will be cleared and reused.
Definition: Layer.h:283
Gorgon::Graphics::TextureSource
This interface represents a GL texture source.
Definition: Graphics.h:480
Gorgon::Graphics::Layer::GetDrawMode
virtual DrawMode GetDrawMode() const override
Get current drawing mode. See Layer page to see available drawing modes.
Definition: Layer.h:277
Gorgon::Geometry::basic_Bounds::GetSize
basic_Size< T_ > GetSize() const
Returns the size of the bounds object.
Definition: Bounds.h:141
Gorgon::Graphics::Layer::Layer
Layer(const Geometry::Bounds &bounds)
Initializing constructor.
Definition: Layer.h:181
Gorgon::Graphics::Layer::Draw
virtual void Draw(const Geometry::Pointf &p1, const Geometry::Pointf &p2, const Geometry::Pointf &p3, const Geometry::Pointf &p4, RGBAf color=RGBAf(1.f)) override
Prefer using Draw functions of image or animations.
Definition: Layer.h:249
Gorgon::Graphics::Layer::GetTintColor
virtual RGBAf GetTintColor() const
Returns the tint color of the layer, every image pixel will be multiplied by this color.
Definition: Layer.h:292
Gorgon::Graphics::Layer::SetDrawMode
virtual void SetDrawMode(DrawMode mode) override
Change current drawing mode. See Layer page to see available drawing modes.
Definition: Layer.h:280
Gorgon::Geometry::basic_Rectangle
Represents a rectangle in a 2D space.
Definition: Rectangle.h:19
Gorgon::Layer::children
Containers::Collection< Layer > children
Child layers that this layer holds, all child layers are considered to be above current layer.
Definition: Layer.h:501
Gorgon::Geometry::Transform3D
basic_Transform3D< Float > Transform3D
Definition: Transform3D.h:176
Gorgon::Layer::isvisible
bool isvisible
Whether this layer is visible, invisible layers will not be drawn or receive any events.
Definition: Layer.h:511
Gorgon::Layer::GetSize
Geometry::Size GetSize() const
Returns the size of the layer.
Definition: Layer.h:362