Replaced boost::shared_ptr<> with std::shared_ptr<>
[supertux.git] / src / video / gl / gl_painter.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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 #include "video/gl/gl_painter.hpp"
18
19 #include "video/drawing_request.hpp"
20 #include "video/gl/gl_surface_data.hpp"
21 #include "video/gl/gl_texture.hpp"
22
23 GLuint GLPainter::s_last_texture = static_cast<GLuint>(-1);
24
25 namespace {
26
27 inline void intern_draw(float left, float top, float right, float bottom,
28                         float uv_left, float uv_top,
29                         float uv_right, float uv_bottom,
30                         float angle, float alpha,
31                         const Color& color,
32                         const Blend& blend,
33                         DrawingEffect effect)
34 {
35   if(effect & HORIZONTAL_FLIP)
36     std::swap(uv_left, uv_right);
37
38   if(effect & VERTICAL_FLIP)
39     std::swap(uv_top, uv_bottom);
40
41   glBlendFunc(blend.sfactor, blend.dfactor);
42   glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
43
44   // unrotated blit
45   if (angle == 0.0f) {
46     float vertices[] = {
47       left, top,
48       right, top,
49       right, bottom,
50       left, bottom,
51     };
52     glVertexPointer(2, GL_FLOAT, 0, vertices);
53
54     float uvs[] = {
55       uv_left, uv_top,
56       uv_right, uv_top,
57       uv_right, uv_bottom,
58       uv_left, uv_bottom,
59     };
60     glTexCoordPointer(2, GL_FLOAT, 0, uvs);
61
62     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
63   } else {
64     // rotated blit
65     float center_x = (left + right) / 2;
66     float center_y = (top + bottom) / 2;
67
68     float sa = sinf(angle/180.0f*M_PI);
69     float ca = cosf(angle/180.0f*M_PI);
70
71     left  -= center_x;
72     right -= center_x;
73
74     top    -= center_y;
75     bottom -= center_y;
76
77     float vertices[] = {
78       left*ca - top*sa + center_x, left*sa + top*ca + center_y,
79       right*ca - top*sa + center_x, right*sa + top*ca + center_y,
80       right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
81       left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
82     };
83     glVertexPointer(2, GL_FLOAT, 0, vertices);
84
85     float uvs[] = {
86       uv_left, uv_top,
87       uv_right, uv_top,
88       uv_right, uv_bottom,
89       uv_left, uv_bottom,
90     };
91     glTexCoordPointer(2, GL_FLOAT, 0, uvs);
92
93     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
94   }
95
96   // FIXME: find a better way to restore the blend mode
97   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
98   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99 }
100
101 } // namespace
102
103 void
104 GLPainter::draw_surface(const DrawingRequest& request)
105 {
106   const Surface* surface = static_cast<const SurfaceRequest*>(request.request_data)->surface;
107   if(surface == NULL)
108   {
109     return;
110   }
111   GLTexture* gltexture = static_cast<GLTexture*>(surface->get_texture().get());
112   if(gltexture == NULL)
113   {
114     return;
115   }
116   GLSurfaceData *surface_data = static_cast<GLSurfaceData*>(surface->get_surface_data());
117   if(surface_data == NULL)
118   {
119     return;
120   }
121
122   GLuint th = gltexture->get_handle();
123   if (th != s_last_texture) {
124     s_last_texture = th;
125     glBindTexture(GL_TEXTURE_2D, th);
126   }
127   intern_draw(request.pos.x, request.pos.y,
128               request.pos.x + surface->get_width(),
129               request.pos.y + surface->get_height(),
130               surface_data->get_uv_left(),
131               surface_data->get_uv_top(),
132               surface_data->get_uv_right(),
133               surface_data->get_uv_bottom(),
134               request.angle,
135               request.alpha,
136               request.color,
137               request.blend,
138               request.drawing_effect);
139 }
140
141 void
142 GLPainter::draw_surface_part(const DrawingRequest& request)
143 {
144   const SurfacePartRequest* surfacepartrequest
145     = (SurfacePartRequest*) request.request_data;
146   const Surface* surface = surfacepartrequest->surface;
147   std::shared_ptr<GLTexture> gltexture = std::dynamic_pointer_cast<GLTexture>(surface->get_texture());
148   GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
149
150   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
151   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
152
153   float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p1.x) / surface->get_width();
154   float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p1.y) / surface->get_height();
155   float uv_right = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p2.x) / surface->get_width();
156   float uv_bottom = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p2.y) / surface->get_height();
157
158   GLuint th = gltexture->get_handle();
159   if (th != s_last_texture) {
160     s_last_texture = th;
161     glBindTexture(GL_TEXTURE_2D, th);
162   }
163   intern_draw(request.pos.x, request.pos.y,
164               request.pos.x + surfacepartrequest->dstsize.width,
165               request.pos.y + surfacepartrequest->dstsize.height,
166               uv_left,
167               uv_top,
168               uv_right,
169               uv_bottom,
170               0.0,
171               request.alpha,
172               request.color,
173               Blend(),
174               request.drawing_effect);
175 }
176
177 void
178 GLPainter::draw_gradient(const DrawingRequest& request)
179 {
180   const GradientRequest* gradientrequest
181     = (GradientRequest*) request.request_data;
182   const Color& top = gradientrequest->top;
183   const Color& bottom = gradientrequest->bottom;
184
185   glDisable(GL_TEXTURE_2D);
186   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
187   glEnableClientState(GL_COLOR_ARRAY);
188
189   float vertices[] = {
190     0, 0,
191     float(SCREEN_WIDTH), 0,
192     float(SCREEN_WIDTH), float(SCREEN_HEIGHT),
193     0, float(SCREEN_HEIGHT)
194   };
195   glVertexPointer(2, GL_FLOAT, 0, vertices);
196
197   float colors[] = {
198     top.red, top.green, top.blue, top.alpha,
199     top.red, top.green, top.blue, top.alpha,
200     bottom.red, bottom.green, bottom.blue, bottom.alpha,
201     bottom.red, bottom.green, bottom.blue, bottom.alpha,
202   };
203   glColorPointer(4, GL_FLOAT, 0, colors);
204
205   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
206
207   glDisableClientState(GL_COLOR_ARRAY);
208   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
209
210   glEnable(GL_TEXTURE_2D);
211   glColor4f(1, 1, 1, 1);
212 }
213
214 void
215 GLPainter::draw_filled_rect(const DrawingRequest& request)
216 {
217   const FillRectRequest* fillrectrequest
218     = (FillRectRequest*) request.request_data;
219
220   glDisable(GL_TEXTURE_2D);
221   glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
222             fillrectrequest->color.blue, fillrectrequest->color.alpha);
223   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
224
225   if (fillrectrequest->radius != 0.0f)
226   {
227     // draw round rect
228     // Keep radius in the limits, so that we get a circle instead of
229     // just graphic junk
230     float radius = std::min(fillrectrequest->radius,
231                             std::min(fillrectrequest->size.x/2,
232                                      fillrectrequest->size.y/2));
233
234     // inner rectangle
235     Rectf irect(request.pos.x    + radius,
236                 request.pos.y    + radius,
237                 request.pos.x + fillrectrequest->size.x - radius,
238                 request.pos.y + fillrectrequest->size.y - radius);
239
240     int n = 8;
241     int p = 0;
242     std::vector<float> vertices((n+1) * 4 * 2);
243
244     for(int i = 0; i <= n; ++i)
245     {
246       float x = sinf(i * (M_PI/2) / n) * radius;
247       float y = cosf(i * (M_PI/2) / n) * radius;
248
249       vertices[p++] = irect.get_left() - x;
250       vertices[p++] = irect.get_top()  - y;
251
252       vertices[p++] = irect.get_right() + x;
253       vertices[p++] = irect.get_top()   - y;
254     }
255
256     for(int i = 0; i <= n; ++i)
257     {
258       float x = cosf(i * (M_PI/2) / n) * radius;
259       float y = sinf(i * (M_PI/2) / n) * radius;
260
261       vertices[p++] = irect.get_left()   - x;
262       vertices[p++] = irect.get_bottom() + y;
263
264       vertices[p++] = irect.get_right()  + x;
265       vertices[p++] = irect.get_bottom() + y;
266     }
267
268     glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
269     glDrawArrays(GL_TRIANGLE_STRIP, 0,  vertices.size()/2);
270   }
271   else
272   {
273     float x = request.pos.x;
274     float y = request.pos.y;
275     float w = fillrectrequest->size.x;
276     float h = fillrectrequest->size.y;
277
278     float vertices[] = {
279       x,   y,
280       x+w, y,
281       x+w, y+h,
282       x,   y+h
283     };
284     glVertexPointer(2, GL_FLOAT, 0, vertices);
285
286     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
287   }
288
289   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
290   glEnable(GL_TEXTURE_2D);
291   glColor4f(1, 1, 1, 1);
292 }
293
294 void
295 GLPainter::draw_inverse_ellipse(const DrawingRequest& request)
296 {
297   const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
298
299   glDisable(GL_TEXTURE_2D);
300   glColor4f(ellipse->color.red,  ellipse->color.green,
301             ellipse->color.blue, ellipse->color.alpha);
302
303   float x = request.pos.x;
304   float y = request.pos.y;
305   float w = ellipse->size.x/2.0f;
306   float h = ellipse->size.y/2.0f;
307
308   static const int slices = 16;
309   static const int points = (slices+1) * 12;
310
311   float vertices[points * 2];
312   int   p = 0;
313
314   // Bottom
315   vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
316   vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
317   vertices[p++] = x;            vertices[p++] = y+h;
318
319   // Top
320   vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
321   vertices[p++] = 0;            vertices[p++] = 0;
322   vertices[p++] = x;            vertices[p++] = y-h;
323
324   // Left
325   vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
326   vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
327   vertices[p++] = x+w;          vertices[p++] = y;
328
329   // Right
330   vertices[p++] = 0;            vertices[p++] = 0;
331   vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
332   vertices[p++] = x-w;          vertices[p++] = y;
333
334   for(int i = 0; i < slices; ++i)
335   {
336     float ex1 = sinf(M_PI/2 / slices * i) * w;
337     float ey1 = cosf(M_PI/2 / slices * i) * h;
338
339     float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
340     float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
341
342     // Bottom/Right
343     vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
344     vertices[p++] = x + ex1;      vertices[p++] = y + ey1;
345     vertices[p++] = x + ex2;      vertices[p++] = y + ey2;
346
347     // Top/Left
348     vertices[p++] = 0;            vertices[p++] = 0;
349     vertices[p++] = x - ex1;      vertices[p++] = y - ey1;
350     vertices[p++] = x - ex2;      vertices[p++] = y - ey2;
351
352     // Top/Right
353     vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
354     vertices[p++] = x + ex1;      vertices[p++] = y - ey1;
355     vertices[p++] = x + ex2;      vertices[p++] = y - ey2;
356
357     // Bottom/Left
358     vertices[p++] = 0;            vertices[p++] = SCREEN_HEIGHT;
359     vertices[p++] = x - ex1;      vertices[p++] = y + ey1;
360     vertices[p++] = x - ex2;      vertices[p++] = y + ey2;
361   }
362
363   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
364   glVertexPointer(2, GL_FLOAT, 0, vertices);
365
366   glDrawArrays(GL_TRIANGLES, 0, points);
367
368   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
369
370   glEnable(GL_TEXTURE_2D);
371   glColor4f(1, 1, 1, 1);
372 }
373
374 /* EOF */