Oxygine  1
2g game engine
Renderer.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include <vector>
4 
5 #include "math/Color.h"
6 #include "math/Rect.h"
7 #include "math/AffineTransform.h"
8 #include "NativeTexture.h"
9 #include "VideoDriver.h"
10 
11 #if OXYGINE_NO_SUBPIXEL_RENDERING
12 #define SNAP_PIXEL(arg) floor(arg)
13 #else
14 #define SNAP_PIXEL(arg) (arg)
15 #endif
16 
17 namespace oxygine
18 {
19  typedef AffineTransform Transform;
20 
21 #define MAKE_BLEND_MODE(source, dest) ((IVideoDriver::BT_##source << 16) | (IVideoDriver::BT_##dest))
22 
23 
24  enum blend_mode
25  {
26  blend_disabled = 0,
27  blend_premultiplied_alpha = MAKE_BLEND_MODE(ONE, ONE_MINUS_SRC_ALPHA),
28  blend_alpha = MAKE_BLEND_MODE(SRC_ALPHA, ONE_MINUS_SRC_ALPHA),
29  blend_add = MAKE_BLEND_MODE(ONE, ONE),
30  blend_screen = MAKE_BLEND_MODE(ONE, ONE_MINUS_SRC_COLOR),
31  blend_multiply = MAKE_BLEND_MODE(DST_COLOR, ONE_MINUS_SRC_ALPHA),
32  blend_inverse = MAKE_BLEND_MODE(ONE_MINUS_DST_COLOR, ZERO),
33  };
34 
35 
36  template<class V>
37  void fillQuadT(V* pv, const RectF& srcRect, const RectF& destRect, const AffineTransform& transform, unsigned int rgba)
38  {
39  float u = srcRect.pos.x;
40  float v = srcRect.pos.y;
41 
42  float du = srcRect.size.x;
43  float dv = srcRect.size.y;
44 
45  V vt;
46  vt.color = rgba;
47 
48  const Vector2& pos = destRect.pos;
49  const Vector2& size = destRect.size;
50 
51  Vector2 p1(pos.x, pos.y);
52  Vector2 p2(pos.x, pos.y + size.y);
53  Vector2 p3(pos.x + size.x, pos.y);
54  Vector2 p4(pos.x + size.x, pos.y + size.y);
55 
56 
57  p1 = transform.transform(p1);
58  p2 = transform.transform(p2);
59  p3 = transform.transform(p3);
60  p4 = transform.transform(p4);
61 
62  vt.z = 0;
63 
64  vt.x = SNAP_PIXEL(p1.x);
65  vt.y = SNAP_PIXEL(p1.y);
66  vt.u = u;
67  vt.v = v;
68  *pv = vt;
69  ++pv;
70 
71  vt.x = SNAP_PIXEL(p2.x);
72  vt.y = SNAP_PIXEL(p2.y);
73  vt.u = u;
74  vt.v = v + dv;
75  *pv = vt;
76  ++pv;
77 
78  vt.x = SNAP_PIXEL(p3.x);
79  vt.y = SNAP_PIXEL(p3.y);
80  vt.u = u + du;
81  vt.v = v;
82  *pv = vt;
83  ++pv;
84 
85  vt.x = SNAP_PIXEL(p4.x);
86  vt.y = SNAP_PIXEL(p4.y);
87  vt.u = u + du;
88  vt.v = v + dv;
89  *pv = vt;
90  ++pv;
91  }
92 
93  template<class V>
94  void fillQuadZT(V* pv, const RectF& srcRect, const RectF& destRect, float Z, unsigned int rgba)
95  {
96  float u = srcRect.pos.x;
97  float v = srcRect.pos.y;
98 
99  float du = srcRect.size.x;
100  float dv = srcRect.size.y;
101 
102  V vt;
103  vt.color = rgba;
104 
105  const Vector2& pos = destRect.pos;
106  const Vector2& size = destRect.size;
107 
108  Vector2 p1(pos.x, pos.y);
109  Vector2 p2(pos.x, pos.y + size.y);
110  Vector2 p3(pos.x + size.x, pos.y);
111  Vector2 p4(pos.x + size.x, pos.y + size.y);
112 
113 
114  vt.z = Z;
115 
116  vt.x = SNAP_PIXEL(p1.x);
117  vt.y = SNAP_PIXEL(p1.y);
118  vt.u = u;
119  vt.v = v;
120  *pv = vt;
121  ++pv;
122 
123  vt.x = SNAP_PIXEL(p2.x);
124  vt.y = SNAP_PIXEL(p2.y);
125  vt.u = u;
126  vt.v = v + dv;
127  *pv = vt;
128  ++pv;
129 
130  vt.x = SNAP_PIXEL(p3.x);
131  vt.y = SNAP_PIXEL(p3.y);
132  vt.u = u + du;
133  vt.v = v;
134  *pv = vt;
135  ++pv;
136 
137  vt.x = SNAP_PIXEL(p4.x);
138  vt.y = SNAP_PIXEL(p4.y);
139  vt.u = u + du;
140  vt.v = v + dv;
141  *pv = vt;
142  ++pv;
143  }
144 
145 
146  template<class V>
147  void fillQuadT2(V* pv, const RectF& srcRect, const RectF& srcRect2, const RectF& destRect, const AffineTransform& transform, unsigned int rgba)
148  {
149  float u = srcRect.pos.x;
150  float v = srcRect.pos.y;
151 
152  float du = srcRect.size.x;
153  float dv = srcRect.size.y;
154 
155  float u2 = srcRect2.pos.x;
156  float v2 = srcRect2.pos.y;
157 
158  float du2 = srcRect2.size.x;
159  float dv2 = srcRect2.size.y;
160 
161  V vt;
162  vt.color = rgba;
163 
164  const Vector2& pos = destRect.pos;
165  const Vector2& size = destRect.size;
166 
167  Vector2 p1(pos.x, pos.y);
168  Vector2 p2(pos.x, pos.y + size.y);
169  Vector2 p3(pos.x + size.x, pos.y);
170  Vector2 p4(pos.x + size.x, pos.y + size.y);
171 
172 
173  p1 = transform.transform(p1);
174  p2 = transform.transform(p2);
175  p3 = transform.transform(p3);
176  p4 = transform.transform(p4);
177 
178  vt.z = 0;
179 
180  vt.x = SNAP_PIXEL(p1.x);
181  vt.y = SNAP_PIXEL(p1.y);
182  vt.u = u;
183  vt.v = v;
184  vt.u2 = u2;
185  vt.v2 = v2;
186  *pv = vt;
187  ++pv;
188 
189  vt.x = SNAP_PIXEL(p2.x);
190  vt.y = SNAP_PIXEL(p2.y);
191  vt.u = u;
192  vt.v = v + dv;
193  vt.u2 = u2;
194  vt.v2 = v2 + dv2;
195  *pv = vt;
196  ++pv;
197 
198  vt.x = SNAP_PIXEL(p3.x);
199  vt.y = SNAP_PIXEL(p3.y);
200  vt.u = u + du;
201  vt.v = v;
202  vt.u2 = u2 + du2;
203  vt.v2 = v2;
204  *pv = vt;
205  ++pv;
206 
207  vt.x = SNAP_PIXEL(p4.x);
208  vt.y = SNAP_PIXEL(p4.y);
209  vt.u = u + du;
210  vt.v = v + dv;
211  vt.u2 = u2 + du2;
212  vt.v2 = v2 + dv2;
213  *pv = vt;
214  ++pv;
215  }
216 
218  {
219  public:
220  virtual ~IElementRenderer() {}
221  virtual void drawElement(const spNativeTexture& texture, unsigned int color, const RectF& src, const RectF& dest) = 0;
222  };
223 
225  Matrix makeViewMatrix(int w, int h, bool flipU = false);
226 }
Matrix makeViewMatrix(int w, int h, bool flipU=false)
–oxgl-end–!
Definition: Actor.h:14
Definition: Renderer.h:217