1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2004-2009 The Mana World Development Team |
4 |
|
|
* Copyright (C) 2009-2010 The Mana Developers |
5 |
|
|
* Copyright (C) 2011-2019 The ManaPlus Developers |
6 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
7 |
|
|
* |
8 |
|
|
* This file is part of The ManaPlus Client. |
9 |
|
|
* |
10 |
|
|
* This program is free software; you can redistribute it and/or modify |
11 |
|
|
* it under the terms of the GNU General Public License as published by |
12 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
13 |
|
|
* any later version. |
14 |
|
|
* |
15 |
|
|
* This program is distributed in the hope that it will be useful, |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
* GNU General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU General Public License |
21 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#if defined USE_OPENGL && !defined ANDROID && !defined(__SWITCH__) |
25 |
|
|
#include "render/safeopenglgraphics.h" |
26 |
|
|
|
27 |
|
|
#ifdef DEBUG_OPENGL |
28 |
|
|
#include "render/opengl/mgl.h" |
29 |
|
|
#endif // DEBUG_OPENGL |
30 |
|
|
|
31 |
|
|
#include "resources/imagerect.h" |
32 |
|
|
#include "resources/safeopenglimagehelper.h" |
33 |
|
|
|
34 |
|
|
#include "resources/image/image.h" |
35 |
|
|
|
36 |
|
|
#include "utils/sdlcheckutils.h" |
37 |
|
|
|
38 |
|
|
#include "debug.h" |
39 |
|
|
|
40 |
|
|
GLuint SafeOpenGLGraphics::mTextureBinded = 0; |
41 |
|
|
|
42 |
|
|
SafeOpenGLGraphics::SafeOpenGLGraphics() : |
43 |
|
|
mTexture(false), |
44 |
|
|
mIsByteColor(false), |
45 |
|
|
mByteColor(), |
46 |
|
|
mFloatColor(1.0F), |
47 |
|
|
mColorAlpha(false), |
48 |
|
|
mFbo() |
49 |
|
|
{ |
50 |
|
|
mOpenGL = RENDER_SAFE_OPENGL; |
51 |
|
|
mName = "safe OpenGL"; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
SafeOpenGLGraphics::~SafeOpenGLGraphics() |
55 |
|
|
{ |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
void SafeOpenGLGraphics::deleteArrays() restrict2 |
59 |
|
|
{ |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
bool SafeOpenGLGraphics::setVideoMode(const int w, const int h, |
63 |
|
|
const int scale, |
64 |
|
|
const int bpp, |
65 |
|
|
const bool fs, |
66 |
|
|
const bool hwaccel, |
67 |
|
|
const bool resize, |
68 |
|
|
const bool noFrame, |
69 |
|
|
const bool allowHighDPI) restrict2 |
70 |
|
|
{ |
71 |
|
|
setMainFlags(w, h, |
72 |
|
|
scale, |
73 |
|
|
bpp, |
74 |
|
|
fs, |
75 |
|
|
hwaccel, |
76 |
|
|
resize, |
77 |
|
|
noFrame, |
78 |
|
|
allowHighDPI); |
79 |
|
|
|
80 |
|
|
return setOpenGLMode(); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
static inline void drawQuad(const Image *restrict image, |
84 |
|
|
const int srcX, |
85 |
|
|
const int srcY, |
86 |
|
|
const int dstX, |
87 |
|
|
const int dstY, |
88 |
|
|
const int width, |
89 |
|
|
const int height) A_NONNULL(1) A_INLINE; |
90 |
|
|
|
91 |
|
|
static inline void drawQuad(const Image *restrict image, |
92 |
|
|
const int srcX, |
93 |
|
|
const int srcY, |
94 |
|
|
const int dstX, |
95 |
|
|
const int dstY, |
96 |
|
|
const int width, |
97 |
|
|
const int height) |
98 |
|
|
{ |
99 |
|
|
if (SafeOpenGLImageHelper::mTextureType == GL_TEXTURE_2D) |
100 |
|
|
{ |
101 |
|
|
const float tw = static_cast<float>(image->mTexWidth); |
102 |
|
|
const float th = static_cast<float>(image->mTexHeight); |
103 |
|
|
// Find OpenGL normalized texture coordinates. |
104 |
|
|
const float texX1 = static_cast<float>(srcX) / tw; |
105 |
|
|
const float texY1 = static_cast<float>(srcY) / th; |
106 |
|
|
const float texX2 = static_cast<float>(srcX + width) / tw; |
107 |
|
|
const float texY2 = static_cast<float>(srcY + height) / th; |
108 |
|
|
|
109 |
|
|
glTexCoord2f(texX1, texY1); |
110 |
|
|
glVertex2i(dstX, dstY); |
111 |
|
|
glTexCoord2f(texX2, texY1); |
112 |
|
|
glVertex2i(dstX + width, dstY); |
113 |
|
|
glTexCoord2f(texX2, texY2); |
114 |
|
|
glVertex2i(dstX + width, dstY + height); |
115 |
|
|
glTexCoord2f(texX1, texY2); |
116 |
|
|
glVertex2i(dstX, dstY + height); |
117 |
|
|
} |
118 |
|
|
else |
119 |
|
|
{ |
120 |
|
|
glTexCoord2i(srcX, srcY); |
121 |
|
|
glVertex2i(dstX, dstY); |
122 |
|
|
glTexCoord2i(srcX + width, srcY); |
123 |
|
|
glVertex2i(dstX + width, dstY); |
124 |
|
|
glTexCoord2i(srcX + width, srcY + height); |
125 |
|
|
glVertex2i(dstX + width, dstY + height); |
126 |
|
|
glTexCoord2i(srcX, srcY + height); |
127 |
|
|
glVertex2i(dstX, dstY + height); |
128 |
|
|
} |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
static inline void drawRescaledQuad(const Image *restrict const image, |
132 |
|
|
const int srcX, const int srcY, |
133 |
|
|
const int dstX, const int dstY, |
134 |
|
|
const int width, const int height, |
135 |
|
|
const int desiredWidth, |
136 |
|
|
const int desiredHeight) |
137 |
|
|
A_NONNULL(1) A_INLINE; |
138 |
|
|
|
139 |
|
|
static inline void drawRescaledQuad(const Image *restrict const image, |
140 |
|
|
const int srcX, const int srcY, |
141 |
|
|
const int dstX, const int dstY, |
142 |
|
|
const int width, const int height, |
143 |
|
|
const int desiredWidth, |
144 |
|
|
const int desiredHeight) |
145 |
|
|
{ |
146 |
|
|
if (SafeOpenGLImageHelper::mTextureType == GL_TEXTURE_2D) |
147 |
|
|
{ |
148 |
|
|
const float tw = static_cast<float>(image->mTexWidth); |
149 |
|
|
const float th = static_cast<float>(image->mTexHeight); |
150 |
|
|
// Find OpenGL normalized texture coordinates. |
151 |
|
|
const float texX1 = static_cast<float>(srcX) / tw; |
152 |
|
|
const float texY1 = static_cast<float>(srcY) / th; |
153 |
|
|
const float texX2 = static_cast<float>(srcX + width) / tw; |
154 |
|
|
const float texY2 = static_cast<float>(srcY + height) / th; |
155 |
|
|
|
156 |
|
|
glTexCoord2f(texX1, texY1); |
157 |
|
|
glVertex2i(dstX, dstY); |
158 |
|
|
glTexCoord2f(texX2, texY1); |
159 |
|
|
glVertex2i(dstX + desiredWidth, dstY); |
160 |
|
|
glTexCoord2f(texX2, texY2); |
161 |
|
|
glVertex2i(dstX + desiredWidth, dstY + desiredHeight); |
162 |
|
|
glTexCoord2f(texX1, texY2); |
163 |
|
|
glVertex2i(dstX, dstY + desiredHeight); |
164 |
|
|
} |
165 |
|
|
else |
166 |
|
|
{ |
167 |
|
|
glTexCoord2i(srcX, srcY); |
168 |
|
|
glVertex2i(dstX, dstY); |
169 |
|
|
glTexCoord2i(srcX + width, srcY); |
170 |
|
|
glVertex2i(dstX + desiredWidth, dstY); |
171 |
|
|
glTexCoord2i(srcX + width, srcY + height); |
172 |
|
|
glVertex2i(dstX + desiredWidth, dstY + desiredHeight); |
173 |
|
|
glTexCoord2i(srcX, srcY + height); |
174 |
|
|
glVertex2i(dstX, dstY + desiredHeight); |
175 |
|
|
} |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
void SafeOpenGLGraphics::drawImage(const Image *restrict const image, |
179 |
|
|
int dstX, int dstY) restrict2 |
180 |
|
|
{ |
181 |
|
|
drawImageInline(image, dstX, dstY); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
void SafeOpenGLGraphics::drawImageInline(const Image *restrict const image, |
185 |
|
|
int dstX, int dstY) restrict2 |
186 |
|
|
{ |
187 |
|
|
FUNC_BLOCK("Graphics::drawImage", 1) |
188 |
|
|
if (image == nullptr) |
189 |
|
|
return; |
190 |
|
|
|
191 |
|
|
setColorAlpha(image->mAlpha); |
192 |
|
|
bindTexture(SafeOpenGLImageHelper::mTextureType, image->mGLImage); |
193 |
|
|
enableTexturingAndBlending(); |
194 |
|
|
|
195 |
|
|
const SDL_Rect &bounds = image->mBounds; |
196 |
|
|
// Draw a textured quad. |
197 |
|
|
glBegin(GL_QUADS); |
198 |
|
|
drawQuad(image, bounds.x, bounds.y, |
199 |
|
|
dstX, dstY, bounds.w, bounds.h); |
200 |
|
|
glEnd(); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
void SafeOpenGLGraphics::copyImage(const Image *restrict const image, |
204 |
|
|
int dstX, int dstY) restrict2 |
205 |
|
|
{ |
206 |
|
|
drawImageInline(image, dstX, dstY); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
void SafeOpenGLGraphics::testDraw() restrict2 |
210 |
|
|
{ |
211 |
|
|
if (SafeOpenGLImageHelper::mTextureType == GL_TEXTURE_2D) |
212 |
|
|
{ |
213 |
|
|
glBegin(GL_QUADS); |
214 |
|
|
glTexCoord2f(0.0F, 0.781250F); |
215 |
|
|
glVertex2i(0, 0); |
216 |
|
|
glTexCoord2f(0.0F, 0.781250F); |
217 |
|
|
glVertex2i(800, 0); |
218 |
|
|
glTexCoord2f(0.0F, 0.585938F); |
219 |
|
|
glVertex2i(800, 600); |
220 |
|
|
glTexCoord2f(0.0F, 0.585938F); |
221 |
|
|
glVertex2i(0, 600); |
222 |
|
|
glEnd(); |
223 |
|
|
} |
224 |
|
|
else |
225 |
|
|
{ |
226 |
|
|
glBegin(GL_QUADS); |
227 |
|
|
glTexCoord2i(0, 0); |
228 |
|
|
glVertex2i(0, 0); |
229 |
|
|
glTexCoord2i(800, 0); |
230 |
|
|
glVertex2i(800, 0); |
231 |
|
|
glTexCoord2i(800, 600); |
232 |
|
|
glVertex2i(800, 600); |
233 |
|
|
glTexCoord2i(0, 600); |
234 |
|
|
glVertex2i(0, 600); |
235 |
|
|
glEnd(); |
236 |
|
|
} |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
void SafeOpenGLGraphics::drawImageCached(const Image *restrict const image, |
240 |
|
|
int x, int y) restrict2 |
241 |
|
|
{ |
242 |
|
|
FUNC_BLOCK("Graphics::drawImageCached", 1) |
243 |
|
|
if (image == nullptr) |
244 |
|
|
return; |
245 |
|
|
|
246 |
|
|
setColorAlpha(image->mAlpha); |
247 |
|
|
bindTexture(SafeOpenGLImageHelper::mTextureType, image->mGLImage); |
248 |
|
|
enableTexturingAndBlending(); |
249 |
|
|
|
250 |
|
|
const SDL_Rect &bounds = image->mBounds; |
251 |
|
|
// Draw a textured quad. |
252 |
|
|
glBegin(GL_QUADS); |
253 |
|
|
drawQuad(image, bounds.x, bounds.y, x, y, bounds.w, bounds.h); |
254 |
|
|
glEnd(); |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
void SafeOpenGLGraphics::drawPatternCached(const Image *restrict const image, |
258 |
|
|
const int x, const int y, |
259 |
|
|
const int w, const int h) restrict2 |
260 |
|
|
{ |
261 |
|
|
FUNC_BLOCK("Graphics::drawPatternCached", 1) |
262 |
|
|
if (image == nullptr) |
263 |
|
|
return; |
264 |
|
|
|
265 |
|
|
const SDL_Rect &imageRect = image->mBounds; |
266 |
|
|
const int iw = imageRect.w; |
267 |
|
|
const int ih = imageRect.h; |
268 |
|
|
if (iw == 0 || ih == 0) |
269 |
|
|
return; |
270 |
|
|
|
271 |
|
|
const int srcX = imageRect.x; |
272 |
|
|
const int srcY = imageRect.y; |
273 |
|
|
|
274 |
|
|
setColorAlpha(image->mAlpha); |
275 |
|
|
bindTexture(SafeOpenGLImageHelper::mTextureType, image->mGLImage); |
276 |
|
|
enableTexturingAndBlending(); |
277 |
|
|
|
278 |
|
|
// Draw a set of textured rectangles |
279 |
|
|
glBegin(GL_QUADS); |
280 |
|
|
|
281 |
|
|
for (int py = 0; py < h; py += ih) |
282 |
|
|
{ |
283 |
|
|
const int height = (py + ih >= h) ? h - py : ih; |
284 |
|
|
const int dstY = y + py; |
285 |
|
|
for (int px = 0; px < w; px += iw) |
286 |
|
|
{ |
287 |
|
|
int width = (px + iw >= w) ? w - px : iw; |
288 |
|
|
int dstX = x + px; |
289 |
|
|
drawQuad(image, srcX, srcY, dstX, dstY, width, height); |
290 |
|
|
} |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
glEnd(); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
void SafeOpenGLGraphics::completeCache() restrict2 |
297 |
|
|
{ |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
void SafeOpenGLGraphics::drawRescaledImage(const Image *restrict const image, |
301 |
|
|
int dstX, int dstY, |
302 |
|
|
const int desiredWidth, |
303 |
|
|
const int desiredHeight) restrict2 |
304 |
|
|
{ |
305 |
|
|
FUNC_BLOCK("Graphics::drawRescaledImage", 1) |
306 |
|
|
if (image == nullptr) |
307 |
|
|
return; |
308 |
|
|
|
309 |
|
|
const SDL_Rect &imageRect = image->mBounds; |
310 |
|
|
|
311 |
|
|
// Just draw the image normally when no resizing is necessary, |
312 |
|
|
if (imageRect.w == desiredWidth && imageRect.h == desiredHeight) |
313 |
|
|
{ |
314 |
|
|
drawImageInline(image, dstX, dstY); |
315 |
|
|
return; |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
setColorAlpha(image->mAlpha); |
319 |
|
|
bindTexture(SafeOpenGLImageHelper::mTextureType, image->mGLImage); |
320 |
|
|
enableTexturingAndBlending(); |
321 |
|
|
|
322 |
|
|
// Draw a textured quad. |
323 |
|
|
glBegin(GL_QUADS); |
324 |
|
|
drawRescaledQuad(image, imageRect.x, imageRect.y, dstX, dstY, |
325 |
|
|
imageRect.w, imageRect.h, desiredWidth, desiredHeight); |
326 |
|
|
glEnd(); |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
void SafeOpenGLGraphics::drawPattern(const Image *restrict const image, |
330 |
|
|
const int x, const int y, |
331 |
|
|
const int w, const int h) restrict2 |
332 |
|
|
{ |
333 |
|
|
drawPatternInline(image, x, y, w, h); |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
void SafeOpenGLGraphics::drawPatternInline(const Image *restrict const image, |
337 |
|
|
const int x, const int y, |
338 |
|
|
const int w, const int h) restrict2 |
339 |
|
|
{ |
340 |
|
|
FUNC_BLOCK("Graphics::drawPattern", 1) |
341 |
|
|
if (image == nullptr) |
342 |
|
|
return; |
343 |
|
|
|
344 |
|
|
const SDL_Rect &imageRect = image->mBounds; |
345 |
|
|
const int iw = imageRect.w; |
346 |
|
|
const int ih = imageRect.h; |
347 |
|
|
if (iw == 0 || ih == 0) |
348 |
|
|
return; |
349 |
|
|
|
350 |
|
|
const int srcX = imageRect.x; |
351 |
|
|
const int srcY = imageRect.y; |
352 |
|
|
|
353 |
|
|
setColorAlpha(image->mAlpha); |
354 |
|
|
bindTexture(SafeOpenGLImageHelper::mTextureType, image->mGLImage); |
355 |
|
|
enableTexturingAndBlending(); |
356 |
|
|
|
357 |
|
|
// Draw a set of textured rectangles |
358 |
|
|
glBegin(GL_QUADS); |
359 |
|
|
|
360 |
|
|
for (int py = 0; py < h; py += ih) |
361 |
|
|
{ |
362 |
|
|
const int height = (py + ih >= h) ? h - py : ih; |
363 |
|
|
const int dstY = y + py; |
364 |
|
|
for (int px = 0; px < w; px += iw) |
365 |
|
|
{ |
366 |
|
|
int width = (px + iw >= w) ? w - px : iw; |
367 |
|
|
int dstX = x + px; |
368 |
|
|
drawQuad(image, srcX, srcY, dstX, dstY, width, height); |
369 |
|
|
} |
370 |
|
|
} |
371 |
|
|
|
372 |
|
|
glEnd(); |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
void SafeOpenGLGraphics::drawRescaledPattern(const Image *restrict const image, |
376 |
|
|
const int x, const int y, |
377 |
|
|
const int w, const int h, |
378 |
|
|
const int scaledWidth, |
379 |
|
|
const int scaledHeight) restrict2 |
380 |
|
|
{ |
381 |
|
|
if (image == nullptr) |
382 |
|
|
return; |
383 |
|
|
|
384 |
|
|
const int iw = scaledWidth; |
385 |
|
|
const int ih = scaledHeight; |
386 |
|
|
if (iw == 0 || ih == 0) |
387 |
|
|
return; |
388 |
|
|
|
389 |
|
|
const SDL_Rect &imageRect = image->mBounds; |
390 |
|
|
const int srcX = imageRect.x; |
391 |
|
|
const int srcY = imageRect.y; |
392 |
|
|
|
393 |
|
|
setColorAlpha(image->mAlpha); |
394 |
|
|
bindTexture(SafeOpenGLImageHelper::mTextureType, image->mGLImage); |
395 |
|
|
enableTexturingAndBlending(); |
396 |
|
|
|
397 |
|
|
// Draw a set of textured rectangles |
398 |
|
|
glBegin(GL_QUADS); |
399 |
|
|
|
400 |
|
|
const float scaleFactorW = static_cast<float>(scaledWidth) |
401 |
|
|
/ image->getWidth(); |
402 |
|
|
const float scaleFactorH = static_cast<float>(scaledHeight) |
403 |
|
|
/ image->getHeight(); |
404 |
|
|
|
405 |
|
|
for (int py = 0; py < h; py += ih) |
406 |
|
|
{ |
407 |
|
|
const int height = (py + ih >= h) ? h - py : ih; |
408 |
|
|
const int dstY = y + py; |
409 |
|
|
for (int px = 0; px < w; px += iw) |
410 |
|
|
{ |
411 |
|
|
int width = (px + iw >= w) ? w - px : iw; |
412 |
|
|
int dstX = x + px; |
413 |
|
|
|
414 |
|
|
drawRescaledQuad(image, srcX, srcY, dstX, dstY, |
415 |
|
|
width / scaleFactorW, height / scaleFactorH, |
416 |
|
|
scaledWidth, scaledHeight); |
417 |
|
|
} |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
glEnd(); |
421 |
|
|
} |
422 |
|
|
|
423 |
|
|
void SafeOpenGLGraphics::calcTileCollection(ImageCollection *restrict const |
424 |
|
|
vertCol A_UNUSED, |
425 |
|
|
const Image *restrict const image |
426 |
|
|
A_UNUSED, |
427 |
|
|
int x A_UNUSED, |
428 |
|
|
int y A_UNUSED) restrict2 |
429 |
|
|
{ |
430 |
|
|
} |
431 |
|
|
|
432 |
|
|
void SafeOpenGLGraphics::calcTileVertexes(ImageVertexes *restrict const vert |
433 |
|
|
A_UNUSED, |
434 |
|
|
const Image *restrict const image |
435 |
|
|
A_UNUSED, |
436 |
|
|
int x A_UNUSED, |
437 |
|
|
int y A_UNUSED) const restrict2 |
438 |
|
|
{ |
439 |
|
|
} |
440 |
|
|
|
441 |
|
|
void SafeOpenGLGraphics::calcTileVertexesInline(ImageVertexes *restrict const |
442 |
|
|
vert A_UNUSED, |
443 |
|
|
const Image *restrict const |
444 |
|
|
image A_UNUSED, |
445 |
|
|
int x A_UNUSED, |
446 |
|
|
int y A_UNUSED) const restrict2 |
447 |
|
|
{ |
448 |
|
|
} |
449 |
|
|
|
450 |
|
|
void SafeOpenGLGraphics::calcPattern(ImageVertexes *restrict const vert |
451 |
|
|
A_UNUSED, |
452 |
|
|
const Image *restrict const image |
453 |
|
|
A_UNUSED, |
454 |
|
|
const int x A_UNUSED, |
455 |
|
|
const int y A_UNUSED, |
456 |
|
|
const int w A_UNUSED, |
457 |
|
|
const int h A_UNUSED) const restrict2 |
458 |
|
|
{ |
459 |
|
|
} |
460 |
|
|
|
461 |
|
|
void SafeOpenGLGraphics::calcPatternInline(ImageVertexes *restrict const vert |
462 |
|
|
A_UNUSED, |
463 |
|
|
const Image *restrict const image |
464 |
|
|
A_UNUSED, |
465 |
|
|
const int x A_UNUSED, |
466 |
|
|
const int y A_UNUSED, |
467 |
|
|
const int w A_UNUSED, |
468 |
|
|
const int h A_UNUSED) |
469 |
|
|
const restrict2 |
470 |
|
|
{ |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
void SafeOpenGLGraphics::calcPattern(ImageCollection *restrict const vert |
474 |
|
|
A_UNUSED, |
475 |
|
|
const Image *restrict const image |
476 |
|
|
A_UNUSED, |
477 |
|
|
const int x A_UNUSED, |
478 |
|
|
const int y A_UNUSED, |
479 |
|
|
const int w A_UNUSED, |
480 |
|
|
const int h A_UNUSED) const restrict2 |
481 |
|
|
{ |
482 |
|
|
} |
483 |
|
|
|
484 |
|
|
void SafeOpenGLGraphics::drawTileVertexes(const ImageVertexes |
485 |
|
|
*restrict const vert A_UNUSED) |
486 |
|
|
restrict2 |
487 |
|
|
{ |
488 |
|
|
} |
489 |
|
|
|
490 |
|
|
void SafeOpenGLGraphics::drawTileCollection(const ImageCollection * |
491 |
|
|
restrict const vertCol A_UNUSED) |
492 |
|
|
restrict2 |
493 |
|
|
{ |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
void SafeOpenGLGraphics::updateScreen() restrict2 |
497 |
|
|
{ |
498 |
|
|
BLOCK_START("Graphics::updateScreen") |
499 |
|
|
glFlush(); |
500 |
|
|
glFinish(); |
501 |
|
|
#ifdef USE_SDL2 |
502 |
|
|
SDL_GL_SwapWindow(mWindow); |
503 |
|
|
#else |
504 |
|
|
SDL_GL_SwapBuffers(); |
505 |
|
|
#endif // USE_SDL2 |
506 |
|
|
#ifdef DEBUG_OPENGL |
507 |
|
|
if (isGLNotNull(mglFrameTerminator)) |
508 |
|
|
mglFrameTerminator(); |
509 |
|
|
#endif // DEBUG_OPENGL |
510 |
|
|
|
511 |
|
|
BLOCK_END("Graphics::updateScreen") |
512 |
|
|
} |
513 |
|
|
|
514 |
|
|
void SafeOpenGLGraphics::calcWindow(ImageCollection *restrict const vertCol |
515 |
|
|
A_UNUSED, |
516 |
|
|
const int x A_UNUSED, const int y A_UNUSED, |
517 |
|
|
const int w A_UNUSED, const int h A_UNUSED, |
518 |
|
|
const ImageRect &restrict imgRect A_UNUSED) |
519 |
|
|
restrict2 |
520 |
|
|
{ |
521 |
|
|
} |
522 |
|
|
|
523 |
|
|
void SafeOpenGLGraphics::beginDraw() restrict2 |
524 |
|
|
{ |
525 |
|
|
glMatrixMode(GL_TEXTURE); |
526 |
|
|
glLoadIdentity(); |
527 |
|
|
|
528 |
|
|
glMatrixMode(GL_PROJECTION); |
529 |
|
|
glLoadIdentity(); |
530 |
|
|
|
531 |
|
|
glOrtho(0.0, static_cast<double>(mRect.w), |
532 |
|
|
static_cast<double>(mRect.h), |
533 |
|
|
0.0, -1.0, 1.0); |
534 |
|
|
|
535 |
|
|
glMatrixMode(GL_MODELVIEW); |
536 |
|
|
glLoadIdentity(); |
537 |
|
|
|
538 |
|
|
setOpenGLFlags(); |
539 |
|
|
glDisable(GL_LIGHTING); |
540 |
|
|
glDisable(GL_FOG); |
541 |
|
|
glDisable(GL_COLOR_MATERIAL); |
542 |
|
|
|
543 |
|
|
glShadeModel(GL_FLAT); |
544 |
|
|
|
545 |
|
|
pushClipArea(Rect(0, 0, mRect.w, mRect.h)); |
546 |
|
|
} |
547 |
|
|
|
548 |
|
|
void SafeOpenGLGraphics::endDraw() restrict2 |
549 |
|
|
{ |
550 |
|
|
popClipArea(); |
551 |
|
|
} |
552 |
|
|
|
553 |
|
|
void SafeOpenGLGraphics::pushClipArea(const Rect &restrict area) restrict2 |
554 |
|
|
{ |
555 |
|
|
int transX = 0; |
556 |
|
|
int transY = 0; |
557 |
|
|
|
558 |
|
|
if (!mClipStack.empty()) |
559 |
|
|
{ |
560 |
|
|
const ClipRect &clipArea = mClipStack.top(); |
561 |
|
|
transX = -clipArea.xOffset; |
562 |
|
|
transY = -clipArea.yOffset; |
563 |
|
|
} |
564 |
|
|
|
565 |
|
|
Graphics::pushClipArea(area); |
566 |
|
|
|
567 |
|
|
const ClipRect &clipArea = mClipStack.top(); |
568 |
|
|
|
569 |
|
|
glPushMatrix(); |
570 |
|
|
glTranslatef(static_cast<GLfloat>(transX + clipArea.xOffset), |
571 |
|
|
static_cast<GLfloat>(transY + clipArea.yOffset), 0); |
572 |
|
|
glScissor(clipArea.x * mScale, |
573 |
|
|
(mRect.h - clipArea.y - clipArea.height) * mScale, |
574 |
|
|
clipArea.width * mScale, |
575 |
|
|
clipArea.height * mScale); |
576 |
|
|
} |
577 |
|
|
|
578 |
|
|
void SafeOpenGLGraphics::popClipArea() restrict2 |
579 |
|
|
{ |
580 |
|
|
Graphics::popClipArea(); |
581 |
|
|
|
582 |
|
|
if (mClipStack.empty()) |
583 |
|
|
return; |
584 |
|
|
|
585 |
|
|
glPopMatrix(); |
586 |
|
|
const ClipRect &clipArea = mClipStack.top(); |
587 |
|
|
glScissor(clipArea.x * mScale, |
588 |
|
|
(mRect.h - clipArea.y - clipArea.height) * mScale, |
589 |
|
|
clipArea.width * mScale, |
590 |
|
|
clipArea.height * mScale); |
591 |
|
|
} |
592 |
|
|
|
593 |
|
|
void SafeOpenGLGraphics::drawPoint(int x, int y) restrict2 |
594 |
|
|
{ |
595 |
|
|
disableTexturingAndBlending(); |
596 |
|
|
restoreColor(); |
597 |
|
|
|
598 |
|
|
glBegin(GL_POINTS); |
599 |
|
|
glVertex2i(x, y); |
600 |
|
|
glEnd(); |
601 |
|
|
} |
602 |
|
|
|
603 |
|
|
void SafeOpenGLGraphics::drawNet(const int x1, const int y1, |
604 |
|
|
const int x2, const int y2, |
605 |
|
|
const int width, const int height) restrict2 |
606 |
|
|
{ |
607 |
|
|
disableTexturingAndBlending(); |
608 |
|
|
restoreColor(); |
609 |
|
|
|
610 |
|
|
glBegin(GL_LINES); |
611 |
|
|
for (int y = y1; y < y2; y += height) |
612 |
|
|
{ |
613 |
|
|
glVertex2f(static_cast<float>(x1) + 0.5F, |
614 |
|
|
static_cast<float>(y) + 0.5F); |
615 |
|
|
glVertex2f(static_cast<float>(x2) + 0.5F, |
616 |
|
|
static_cast<float>(y) + 0.5F); |
617 |
|
|
} |
618 |
|
|
|
619 |
|
|
for (int x = x1; x < x2; x += width) |
620 |
|
|
{ |
621 |
|
|
glVertex2f(static_cast<float>(x) + 0.5F, |
622 |
|
|
static_cast<float>(y1) + 0.5F); |
623 |
|
|
glVertex2f(static_cast<float>(x) + 0.5F, |
624 |
|
|
static_cast<float>(y2) + 0.5F); |
625 |
|
|
} |
626 |
|
|
glEnd(); |
627 |
|
|
} |
628 |
|
|
|
629 |
|
|
void SafeOpenGLGraphics::drawLine(int x1, int y1, |
630 |
|
|
int x2, int y2) restrict2 |
631 |
|
|
{ |
632 |
|
|
disableTexturingAndBlending(); |
633 |
|
|
restoreColor(); |
634 |
|
|
|
635 |
|
|
glBegin(GL_LINES); |
636 |
|
|
glVertex2f(static_cast<float>(x1) + 0.5F, static_cast<float>(y1) + 0.5F); |
637 |
|
|
glVertex2f(static_cast<float>(x2) + 0.5F, static_cast<float>(y2) + 0.5F); |
638 |
|
|
glEnd(); |
639 |
|
|
} |
640 |
|
|
|
641 |
|
|
void SafeOpenGLGraphics::drawRectangle(const Rect &restrict rect) restrict2 |
642 |
|
|
{ |
643 |
|
|
drawRectangle(rect, false); |
644 |
|
|
} |
645 |
|
|
|
646 |
|
|
void SafeOpenGLGraphics::fillRectangle(const Rect &restrict rect) restrict2 |
647 |
|
|
{ |
648 |
|
|
drawRectangle(rect, true); |
649 |
|
|
} |
650 |
|
|
|
651 |
|
|
void SafeOpenGLGraphics::enableTexturingAndBlending() restrict2 |
652 |
|
|
{ |
653 |
|
|
if (!mTexture) |
654 |
|
|
{ |
655 |
|
|
glEnable(SafeOpenGLImageHelper::mTextureType); |
656 |
|
|
mTexture = true; |
657 |
|
|
} |
658 |
|
|
|
659 |
|
|
if (!mAlpha) |
660 |
|
|
{ |
661 |
|
|
glEnable(GL_BLEND); |
662 |
|
|
mAlpha = true; |
663 |
|
|
} |
664 |
|
|
} |
665 |
|
|
|
666 |
|
|
void SafeOpenGLGraphics::disableTexturingAndBlending() restrict2 |
667 |
|
|
{ |
668 |
|
|
mTextureBinded = 0; |
669 |
|
|
if (mAlpha && !mColorAlpha) |
670 |
|
|
{ |
671 |
|
|
glDisable(GL_BLEND); |
672 |
|
|
mAlpha = false; |
673 |
|
|
} |
674 |
|
|
else if (!mAlpha && mColorAlpha) |
675 |
|
|
{ |
676 |
|
|
glEnable(GL_BLEND); |
677 |
|
|
mAlpha = true; |
678 |
|
|
} |
679 |
|
|
|
680 |
|
|
if (mTexture) |
681 |
|
|
{ |
682 |
|
|
glDisable(SafeOpenGLImageHelper::mTextureType); |
683 |
|
|
mTexture = false; |
684 |
|
|
} |
685 |
|
|
} |
686 |
|
|
|
687 |
|
|
void SafeOpenGLGraphics::drawRectangle(const Rect &restrict rect, |
688 |
|
|
const bool filled) restrict2 |
689 |
|
|
{ |
690 |
|
|
BLOCK_START("Graphics::drawRectangle") |
691 |
|
|
const float offset = filled ? 0 : 0.5F; |
692 |
|
|
|
693 |
|
|
disableTexturingAndBlending(); |
694 |
|
|
restoreColor(); |
695 |
|
|
|
696 |
|
|
glBegin(filled ? GL_QUADS : GL_LINE_LOOP); |
697 |
|
|
glVertex2f(static_cast<float>(rect.x) + offset, |
698 |
|
|
static_cast<float>(rect.y) + offset); |
699 |
|
|
glVertex2f(static_cast<float>(rect.x + rect.width) - offset, |
700 |
|
|
static_cast<float>(rect.y) + offset); |
701 |
|
|
glVertex2f(static_cast<float>(rect.x + rect.width) - offset, |
702 |
|
|
static_cast<float>(rect.y + rect.height) - offset); |
703 |
|
|
glVertex2f(static_cast<float>(rect.x) + offset, |
704 |
|
|
static_cast<float>(rect.y + rect.height) - offset); |
705 |
|
|
glEnd(); |
706 |
|
|
BLOCK_END("Graphics::drawRectangle") |
707 |
|
|
} |
708 |
|
|
|
709 |
|
|
void SafeOpenGLGraphics::bindTexture(const GLenum target, |
710 |
|
|
const GLuint texture) |
711 |
|
|
{ |
712 |
|
|
if (mTextureBinded != texture) |
713 |
|
|
{ |
714 |
|
|
mTextureBinded = texture; |
715 |
|
|
glBindTexture(target, texture); |
716 |
|
|
} |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
void SafeOpenGLGraphics::setColorAlpha(const float alpha) restrict2 |
720 |
|
|
{ |
721 |
|
|
if (!mIsByteColor && mFloatColor == alpha) |
722 |
|
|
return; |
723 |
|
|
|
724 |
|
|
glColor4f(1.0F, 1.0F, 1.0F, alpha); |
725 |
|
|
mIsByteColor = false; |
726 |
|
|
mFloatColor = alpha; |
727 |
|
|
} |
728 |
|
|
|
729 |
|
|
void SafeOpenGLGraphics::restoreColor() restrict2 |
730 |
|
|
{ |
731 |
|
|
if (mIsByteColor && mByteColor == mColor) |
732 |
|
|
return; |
733 |
|
|
|
734 |
|
|
glColor4ub(static_cast<GLubyte>(mColor.r), |
735 |
|
|
static_cast<GLubyte>(mColor.g), |
736 |
|
|
static_cast<GLubyte>(mColor.b), |
737 |
|
|
static_cast<GLubyte>(mColor.a)); |
738 |
|
|
mIsByteColor = true; |
739 |
|
|
mByteColor = mColor; |
740 |
|
|
} |
741 |
|
|
|
742 |
|
|
void SafeOpenGLGraphics::clearScreen() const restrict2 |
743 |
|
|
{ |
744 |
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
745 |
|
|
} |
746 |
|
|
|
747 |
|
|
void SafeOpenGLGraphics::drawImageRect(const int x, const int y, |
748 |
|
|
const int w, const int h, |
749 |
|
|
const ImageRect &restrict imgRect) |
750 |
|
|
restrict2 |
751 |
|
|
{ |
752 |
|
|
#include "render/graphics_drawImageRect.hpp" |
753 |
|
|
} |
754 |
|
|
|
755 |
|
|
void SafeOpenGLGraphics::calcImageRect(ImageVertexes *restrict const vert, |
756 |
|
|
const int x, const int y, |
757 |
|
|
const int w, const int h, |
758 |
|
|
const ImageRect &restrict imgRect) |
759 |
|
|
restrict2 |
760 |
|
|
{ |
761 |
|
|
#include "render/graphics_calcImageRect.hpp" |
762 |
|
|
} |
763 |
|
|
|
764 |
|
|
#endif // USE_OPENGL |