2 // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
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.
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.
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/>.
17 #include "video/sdl/sdl_painter.hpp"
21 #include "util/log.hpp"
22 #include "video/drawing_request.hpp"
23 #include "video/sdl/sdl_texture.hpp"
27 SDL_BlendMode blend2sdl(const Blend& blend)
29 if (blend.sfactor == GL_ONE &&
30 blend.dfactor == GL_ZERO)
32 return SDL_BLENDMODE_NONE;
34 else if (blend.sfactor == GL_SRC_ALPHA &&
35 blend.dfactor == GL_ONE_MINUS_SRC_ALPHA)
37 return SDL_BLENDMODE_BLEND;
39 else if (blend.sfactor == GL_SRC_ALPHA &&
40 blend.dfactor == GL_ONE)
42 return SDL_BLENDMODE_ADD;
44 else if (blend.sfactor == GL_DST_COLOR &&
45 blend.dfactor == GL_ZERO)
47 return SDL_BLENDMODE_MOD;
51 log_warning << "unknown blend mode combinations: sfactor=" << blend.sfactor << " dfactor=" << blend.dfactor << std::endl;
52 return SDL_BLENDMODE_BLEND;
59 SDLPainter::draw_surface(SDL_Renderer* renderer, const DrawingRequest& request)
61 const Surface* surface = (const Surface*) request.request_data;
62 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
65 dst_rect.x = request.pos.x;
66 dst_rect.y = request.pos.y;
67 dst_rect.w = sdltexture->get_image_width();
68 dst_rect.h = sdltexture->get_image_height();
70 Uint8 r = static_cast<Uint8>(request.color.red * 255);
71 Uint8 g = static_cast<Uint8>(request.color.green * 255);
72 Uint8 b = static_cast<Uint8>(request.color.blue * 255);
73 Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
74 SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
75 SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
76 SDL_SetTextureBlendMode(sdltexture->get_texture(), blend2sdl(request.blend));
78 SDL_RendererFlip flip = SDL_FLIP_NONE;
79 if (surface->get_flipx() || request.drawing_effect == HORIZONTAL_FLIP)
81 flip = SDL_FLIP_HORIZONTAL;
83 else if (request.drawing_effect == VERTICAL_FLIP)
85 flip = SDL_FLIP_VERTICAL;
88 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, request.angle, NULL, flip);
92 SDLPainter::draw_surface_part(SDL_Renderer* renderer, const DrawingRequest& request)
94 //FIXME: support parameters request.blend
95 const SurfacePartRequest* surface = (const SurfacePartRequest*) request.request_data;
96 const SurfacePartRequest* surfacepartrequest = (SurfacePartRequest*) request.request_data;
98 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());
101 src_rect.x = surfacepartrequest->source.x;
102 src_rect.y = surfacepartrequest->source.y;
103 src_rect.w = surfacepartrequest->size.x;
104 src_rect.h = surfacepartrequest->size.y;
107 dst_rect.x = request.pos.x;
108 dst_rect.y = request.pos.y;
109 dst_rect.w = surfacepartrequest->size.x;
110 dst_rect.h = surfacepartrequest->size.y;
112 Uint8 r = static_cast<Uint8>(request.color.red * 255);
113 Uint8 g = static_cast<Uint8>(request.color.green * 255);
114 Uint8 b = static_cast<Uint8>(request.color.blue * 255);
115 Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
116 SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
117 SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
118 SDL_SetTextureBlendMode(sdltexture->get_texture(), blend2sdl(request.blend));
120 SDL_RendererFlip flip = SDL_FLIP_NONE;
121 if (surface->surface->get_flipx() || request.drawing_effect == HORIZONTAL_FLIP)
123 flip = SDL_FLIP_HORIZONTAL;
125 else if (request.drawing_effect == VERTICAL_FLIP)
127 flip = SDL_FLIP_VERTICAL;
130 SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, request.angle, NULL, flip);
134 SDLPainter::draw_gradient(SDL_Renderer* renderer, const DrawingRequest& request)
136 const GradientRequest* gradientrequest
137 = (GradientRequest*) request.request_data;
138 const Color& top = gradientrequest->top;
139 const Color& bottom = gradientrequest->bottom;
141 // calculate the maximum number of steps needed for the gradient
142 int n = static_cast<int>(std::max(std::max(fabsf(top.red - bottom.red),
143 fabsf(top.green - bottom.green)),
144 std::max(fabsf(top.blue - bottom.blue),
145 fabsf(top.alpha - bottom.alpha))) * 255);
146 for(int i = 0; i < n; ++i)
150 rect.y = SCREEN_HEIGHT * i / n;
151 rect.w = SCREEN_WIDTH;
152 rect.h = (SCREEN_HEIGHT * (i+1) / n) - rect.y;
154 float p = static_cast<float>(i+1) / static_cast<float>(n);
155 Uint8 r = static_cast<Uint8>(((1.0f - p) * top.red + p * bottom.red) * 255);
156 Uint8 g = static_cast<Uint8>(((1.0f - p) * top.green + p * bottom.green) * 255);
157 Uint8 b = static_cast<Uint8>(((1.0f - p) * top.blue + p * bottom.blue) * 255);
158 Uint8 a = static_cast<Uint8>(((1.0f - p) * top.alpha + p * bottom.alpha) * 255);
160 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
161 SDL_SetRenderDrawColor(renderer, r, g, b, a);
162 SDL_RenderFillRect(renderer, &rect);
167 SDLPainter::draw_filled_rect(SDL_Renderer* renderer, const DrawingRequest& request)
169 const FillRectRequest* fillrectrequest
170 = (FillRectRequest*) request.request_data;
173 rect.x = request.pos.x;
174 rect.y = request.pos.y;
175 rect.w = fillrectrequest->size.x;
176 rect.h = fillrectrequest->size.y;
178 Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
179 Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
180 Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
181 Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
183 int radius = std::min(std::min(rect.h / 2, rect.w / 2),
184 static_cast<int>(fillrectrequest->radius));
190 // rounded top and bottom parts
191 std::vector<SDL_Rect> rects;
192 rects.reserve(2*slices + 1);
193 for(int i = 0; i < slices; ++i)
195 float p = (static_cast<float>(i) + 0.5f) / static_cast<float>(slices);
196 int xoff = radius - static_cast<int>(sqrtf(1.0f - p*p) * radius);
199 tmp.x = rect.x + xoff;
200 tmp.y = rect.y + (radius - i);
201 tmp.w = rect.w - 2*(xoff);
203 rects.push_back(tmp);
206 tmp2.x = rect.x + xoff;
207 tmp2.y = rect.y + rect.h - radius + i;
208 tmp2.w = rect.w - 2*xoff;
213 rects.push_back(tmp2);
217 if (2*radius < rect.h)
222 tmp.y = rect.y + radius + 1;
224 tmp.h = rect.h - 2*radius - 1;
225 rects.push_back(tmp);
228 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
229 SDL_SetRenderDrawColor(renderer, r, g, b, a);
230 SDL_RenderFillRects(renderer, &*rects.begin(), rects.size());
234 if((rect.w != 0) && (rect.h != 0))
236 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
237 SDL_SetRenderDrawColor(renderer, r, g, b, a);
238 SDL_RenderFillRect(renderer, &rect);
244 SDLPainter::draw_inverse_ellipse(SDL_Renderer* renderer, const DrawingRequest& request)
246 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
248 float x = request.pos.x;
249 float w = ellipse->size.x;
250 float h = ellipse->size.y;
252 int top = request.pos.y - (h / 2);
254 const int max_slices = 256;
255 SDL_Rect rects[2*max_slices+2];
256 int slices = std::min(static_cast<int>(ellipse->size.y), max_slices);
257 for(int i = 0; i < slices; ++i)
259 float p = ((static_cast<float>(i) + 0.5f) / static_cast<float>(slices)) * 2.0f - 1.0f;
260 int xoff = static_cast<int>(sqrtf(1.0f - p*p) * w / 2);
262 SDL_Rect& left = rects[2*i+0];
263 SDL_Rect& right = rects[2*i+1];
266 left.y = top + (i * h / slices);
268 left.h = (top + ((i+1) * h / slices)) - left.y;
272 right.w = SCREEN_WIDTH - right.x;
276 SDL_Rect& top_rect = rects[2*slices+0];
277 SDL_Rect& bottom_rect = rects[2*slices+1];
281 top_rect.w = SCREEN_WIDTH;
285 bottom_rect.y = top + h;
286 bottom_rect.w = SCREEN_WIDTH;
287 bottom_rect.h = SCREEN_HEIGHT - bottom_rect.y;
289 Uint8 r = static_cast<Uint8>(ellipse->color.red * 255);
290 Uint8 g = static_cast<Uint8>(ellipse->color.green * 255);
291 Uint8 b = static_cast<Uint8>(ellipse->color.blue * 255);
292 Uint8 a = static_cast<Uint8>(ellipse->color.alpha * 255);
294 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
295 SDL_SetRenderDrawColor(renderer, r, g, b, a);
296 SDL_RenderFillRects(renderer, rects, 2*slices+2);