xoreos  0.0.5
borderquad.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 #include <cfloat>
27 
30 
31 namespace Graphics {
32 
33 namespace Aurora {
34 
36  float x, float y, float w, float h, int dimension) :
37  Renderable(kRenderableTypeGUIFront), _verticalCut(false),
38  _edgeWidth(0), _edgeHeight(0), _cornerWidth(0), _cornerHeight(0),
39  _r(1.0f), _g(1.0f), _b(1.0f), _a(1.0f), _x(x), _y(y), _w(w), _h(h) {
40 
41  _edge = TextureMan.get(edge);
42  _corner = TextureMan.get(corner);
43 
44  if (dimension == 0) {
49  } else {
50  /*
51  * because we do not have a proper factor for modifying the corners, we simply calculate the
52  * factor with the difference of the edge to the dimension
53  */
54  float widthFactor = static_cast<float>(dimension) / static_cast<float>(_edge.getTexture().getWidth());
55  float heightFactor = static_cast<float>(dimension) / static_cast<float>(_edge.getTexture().getHeight());
56  _cornerWidth = widthFactor * _corner.getTexture().getWidth();
57  _cornerHeight = heightFactor * _corner.getTexture().getHeight();
58  _edgeWidth = dimension;
59  _edgeHeight = dimension;
60  }
61 
62  if (_h < 2 * _edgeHeight) {
63  _verticalCut = true;
64  }
65 
66  _distance = -FLT_MAX;
67 
68  assert(!_corner.empty());
69  assert(!_edge.empty());
70 }
71 
72 void BorderQuad::setColor(float r, float g, float b, float a) {
73  _r = r;
74  _g = g;
75  _b = b;
76  _a = a;
77 }
78 
79 void BorderQuad::setPosition(float x, float y, float z) {
81 
82  // Because of some graphic glitches with non-integer floats, i have to floor the values
83  _x = std::floor(x);
84  _y = std::floor(y);
85  _distance = z;
86 
88 }
89 
90 void BorderQuad::getPosition(float &x, float &y, float &z) {
91  x = _x;
92  y = _y;
93  z = _distance;
94 }
95 
96 void BorderQuad::setSize(float w, float h) {
97  _w = std::floor(w);
98  _h = std::floor(h);
99 
100  if (_h < 2 * _edgeHeight) {
101  _verticalCut = true;
102  } else {
103  _verticalCut = false;
104  }
105 }
106 
107 void BorderQuad::getSize(float &w, float &h) const {
108  w = _w;
109  h = _h;
110 }
111 
113 }
114 
116  bool isTransparent = (!_corner.empty() && _corner.getTexture().hasAlpha()) ||
117  (!_edge.empty() && _edge.getTexture().hasAlpha());
118  if (((pass == kRenderPassOpaque) && isTransparent) ||
119  ((pass == kRenderPassTransparent) && !isTransparent))
120  return;
121 
122  TextureMan.set(_corner);
123 
124  glColor4f(_r, _g, _b, _a);
125 
126  // Upper left corner
127  glBegin(GL_QUADS);
128  glTexCoord2f(0, 0);
129  glVertex2f(_x, _y + _h - _cornerHeight);
130  glTexCoord2f(1, 0);
131  glVertex2f(_x + _cornerWidth, _y + _h - _cornerHeight);
132  glTexCoord2f(1, 1);
133  glVertex2f(_x + _cornerWidth, _y + _h);
134  glTexCoord2f(0, 1);
135  glVertex2f(_x, _y + _h);
136  glEnd();
137 
138  // Upper right corner
139  glBegin(GL_QUADS);
140  glTexCoord2f(1, 0);
141  glVertex2f(_x + _w - _cornerWidth, _y + _h - _cornerHeight);
142  glTexCoord2f(1, 1);
143  glVertex2f(_x + _w, _y + _h - _cornerHeight);
144  glTexCoord2f(0, 1);
145  glVertex2f(_x + _w, _y + _h);
146  glTexCoord2f(0, 0);
147  glVertex2f(_x + _w - _cornerWidth, _y + _h);
148  glEnd();
149 
150  if (_verticalCut) {
151  glEnable(GL_SCISSOR_TEST);
152  GLint viewport[4];
153  glGetIntegerv(GL_VIEWPORT, viewport);
154  glScissor(
155  viewport[2]/2 + _x, viewport[3]/2 + _y,
156  _w, _h - _edgeHeight
157  );
158  }
159 
160  // Lower left corner
161  glBegin(GL_QUADS);
162  glTexCoord2f(0, 1);
163  glVertex2f(_x, _y);
164  glTexCoord2f(1, 1);
165  glVertex2f(_x + _cornerWidth, _y);
166  glTexCoord2f(1, 0);
167  glVertex2f(_x + _cornerWidth, _y + _cornerHeight);
168  glTexCoord2f(0, 0);
169  glVertex2f(_x, _y + _cornerHeight);
170  glEnd();
171 
172  // Lower right corner
173  glBegin(GL_QUADS);
174  glTexCoord2f(0, 0);
175  glVertex2f(_x + _w - _cornerWidth, _y);
176  glTexCoord2f(0, 1);
177  glVertex2f(_x + _w, _y);
178  glTexCoord2f(1, 1);
179  glVertex2f(_x + _w, _y + _cornerHeight);
180  glTexCoord2f(1, 0);
181  glVertex2f(_x + _w - _cornerWidth, _y + _cornerHeight);
182  glEnd();
183 
184  TextureMan.set(_edge);
185 
186  // Lower edge
187  glBegin(GL_QUADS);
188  glTexCoord2f(0, 1);
189  glVertex2f(_x + _cornerWidth, _y);
190  glTexCoord2f(1, 1);
191  glVertex2f(_x + _w - _cornerWidth, _y);
192  glTexCoord2f(1, 0);
193  glVertex2f(_x + _w - _cornerWidth, _y + _edgeHeight);
194  glTexCoord2f(0, 0);
195  glVertex2f(_x + _cornerWidth, _y + _edgeHeight);
196  glEnd();
197 
198  if (_verticalCut) {
199  glDisable(GL_SCISSOR_TEST);
200  }
201 
202  // Left edge
203  glBegin(GL_QUADS);
204  glTexCoord2f(0, 0);
205  glVertex2f(_x + _w - _edgeWidth, _y + _cornerHeight);
206  glTexCoord2f(0, 1);
207  glVertex2f(_x + _w, _y + _cornerHeight);
208  glTexCoord2f(1, 1);
209  glVertex2f(_x + _w, _y + _h - _cornerHeight);
210  glTexCoord2f(1, 0);
211  glVertex2f(_x + _w - _edgeWidth, _y + _h - _cornerHeight);
212  glEnd();
213 
214  // Right edge
215  glBegin(GL_QUADS);
216  glTexCoord2f(0, 1);
217  glVertex2f(_x, _y + _cornerHeight);
218  glTexCoord2f(0, 0);
219  glVertex2f(_x + _cornerWidth, _y + _cornerHeight);
220  glTexCoord2f(1, 0);
221  glVertex2f(_x + _cornerWidth, _y + _h - _cornerHeight);
222  glTexCoord2f(1, 1);
223  glVertex2f(_x, _y + _h - _cornerHeight);
224  glEnd();
225 
226  // Upper Edge
227  glBegin(GL_QUADS);
228  glTexCoord2f(1, 0);
229  glVertex2f(_x + _cornerWidth, _y + _h - _edgeHeight);
230  glTexCoord2f(0, 0);
231  glVertex2f(_x + _w - _cornerWidth, _y + _h - _edgeHeight);
232  glTexCoord2f(0, 1);
233  glVertex2f(_x + _w - _cornerWidth, _y + _h);
234  glTexCoord2f(1, 1);
235  glVertex2f(_x + _cornerWidth, _y + _h);
236  glEnd();
237 
238  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
239 }
240 
241 } // End of namespace Aurora
242 
243 } // End of namespace Graphics
bool hasAlpha() const
Definition: texture.cpp:84
Only render transparent parts.
Definition: types.h:99
A class holding an UTF-8 string.
Definition: ustring.h:48
#define TextureMan
Shortcut for accessing the texture manager.
Definition: textureman.h:127
double _distance
The distance of the object from the viewer.
Definition: renderable.h:101
The Aurora texture manager.
uint32 getHeight() const
Definition: texture.cpp:80
BorderQuad(const Common::UString &edge, const Common::UString &corner, float x, float y, float w, float h, int dimension=0)
Definition: borderquad.cpp:35
virtual void calculateDistance()
Calculate the object&#39;s distance.
Definition: borderquad.cpp:112
RenderPass
Definition: types.h:97
void setPosition(float x, float y, float z)
Definition: borderquad.cpp:79
A quad for generating borders.
void render(RenderPass pass)
Render the object.
Definition: borderquad.cpp:115
uint32 getWidth() const
Definition: texture.cpp:76
An object that can be displayed by the graphics manager.
Definition: renderable.h:42
void setColor(float r, float g, float b, float a=1.0f)
Set the color of the border quad.
Definition: borderquad.cpp:72
void getPosition(float &x, float &y, float &z)
Definition: borderquad.cpp:90
#define pass
Definition: fft.cpp:257
void getSize(float &w, float &h) const
Definition: borderquad.cpp:107
#define FLT_MAX
Definition: maths.h:47
Only render opaque parts.
Definition: types.h:98
void setSize(float w, float h)
Definition: borderquad.cpp:96