2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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/>.
19 #include "video/sdl/sdl_lightmap.hpp"
20 #include "video/sdl/sdl_surface_data.hpp"
21 #include "video/sdl/sdl_texture.hpp"
22 #include "video/sdl/sdl_renderer.hpp"
24 SDLLightmap::SDLLightmap() :
25 renderer(static_cast<SDLRenderer*>(Renderer::instance())->get_sdl_renderer()),
32 width = 800; //screen->w / LIGHTMAP_DIV;
33 height = 600; //screen->h / LIGHTMAP_DIV;
35 SDL_Renderer* renderer = static_cast<SDLRenderer*>(Renderer::instance())->get_sdl_renderer();
36 texture = SDL_CreateTexture(renderer,
37 SDL_PIXELFORMAT_RGB888,
38 SDL_TEXTUREACCESS_TARGET,
42 std::stringstream msg;
43 msg << "Couldn't create lightmap texture: " << SDL_GetError();
44 throw std::runtime_error(msg.str());
48 SDLLightmap::~SDLLightmap()
50 SDL_DestroyTexture(texture);
54 SDLLightmap::start_draw(const Color &ambient_color)
56 SDL_SetRenderTarget(renderer, texture);
58 Uint8 r = static_cast<Uint8>(ambient_color.red * 255);
59 Uint8 g = static_cast<Uint8>(ambient_color.green * 255);
60 Uint8 b = static_cast<Uint8>(ambient_color.blue * 255);
62 SDL_SetRenderDrawColor(renderer, r, g, b, 255);
63 SDL_RenderClear(renderer);
67 SDLLightmap::end_draw()
69 SDL_SetRenderTarget(renderer, NULL);
73 SDLLightmap::do_draw()
75 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
83 SDL_RenderCopy(renderer, texture, NULL, &dst_rect);
87 SDLLightmap::draw_surface(const DrawingRequest& request)
89 //FIXME: support parameters request.alpha, request.angle, request.blend
90 const Surface* surface = (const Surface*) request.request_data;
91 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
94 dst_rect.x = request.pos.x;
95 dst_rect.y = request.pos.y;
96 dst_rect.w = sdltexture->get_image_width();
97 dst_rect.h = sdltexture->get_image_height();
99 SDL_SetTextureBlendMode(sdltexture->get_texture(), SDL_BLENDMODE_ADD);
100 SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
104 SDLLightmap::draw_surface_part(const DrawingRequest& request)
106 //FIXME: support parameters request.alpha, request.angle, request.blend
107 const Surface* surface = (const Surface*) request.request_data;
108 boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
111 dst_rect.x = request.pos.x;
112 dst_rect.y = request.pos.y;
113 dst_rect.w = sdltexture->get_image_width();
114 dst_rect.h = sdltexture->get_image_height();
116 SDL_SetTextureBlendMode(sdltexture->get_texture(), SDL_BLENDMODE_ADD);
117 SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
121 SDLLightmap::draw_gradient(const DrawingRequest& request)
123 log_info << "draw_gradient" << std::endl;
127 SDLLightmap::draw_filled_rect(const DrawingRequest& request)
129 log_info << "draw_filled_rect" << std::endl;
131 const FillRectRequest* fillrectrequest
132 = (FillRectRequest*) request.request_data;
135 rect.x = request.pos.x;
136 rect.y = request.pos.y;
137 rect.w = fillrectrequest->size.x;
138 rect.h = fillrectrequest->size.y;
140 Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
141 Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
142 Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
143 Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
145 log_info << fillrectrequest->color.red << " " << fillrectrequest->color.green << std::endl;
147 if((rect.w != 0) && (rect.h != 0))
149 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
150 SDL_SetRenderDrawColor(renderer, r, g, b, a);
151 SDL_RenderFillRect(renderer, &rect);
156 SDLLightmap::get_light(const DrawingRequest& request) const
158 const GetLightRequest* getlightrequest
159 = (GetLightRequest*) request.request_data;
162 rect.x = static_cast<int>(request.pos.x * width / SCREEN_WIDTH);
163 rect.y = static_cast<int>(request.pos.y * height / SCREEN_HEIGHT);
167 SDL_SetRenderTarget(renderer, texture);
169 int ret = SDL_RenderReadPixels(renderer, &rect,
170 SDL_PIXELFORMAT_RGB888,
175 log_warning << "failed to read pixels: " << SDL_GetError() << std::endl;
177 SDL_SetRenderTarget(renderer, 0);
179 *(getlightrequest->color_ptr) = Color(pixel[2] / 255.0f,