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"
23 #include "video/sdl/sdl_painter.hpp"
25 SDLLightmap::SDLLightmap() :
26 renderer(static_cast<SDLRenderer*>(Renderer::instance())->get_sdl_renderer()),
33 width = 800; //screen->w / LIGHTMAP_DIV;
34 height = 600; //screen->h / LIGHTMAP_DIV;
36 SDL_Renderer* renderer = static_cast<SDLRenderer*>(Renderer::instance())->get_sdl_renderer();
37 texture = SDL_CreateTexture(renderer,
38 SDL_PIXELFORMAT_RGB888,
39 SDL_TEXTUREACCESS_TARGET,
43 std::stringstream msg;
44 msg << "Couldn't create lightmap texture: " << SDL_GetError();
45 throw std::runtime_error(msg.str());
49 SDLLightmap::~SDLLightmap()
51 SDL_DestroyTexture(texture);
55 SDLLightmap::start_draw(const Color &ambient_color)
57 SDL_SetRenderTarget(renderer, texture);
59 Uint8 r = static_cast<Uint8>(ambient_color.red * 255);
60 Uint8 g = static_cast<Uint8>(ambient_color.green * 255);
61 Uint8 b = static_cast<Uint8>(ambient_color.blue * 255);
63 SDL_SetRenderDrawColor(renderer, r, g, b, 255);
64 SDL_RenderClear(renderer);
68 SDLLightmap::end_draw()
70 SDL_SetRenderTarget(renderer, NULL);
74 SDLLightmap::do_draw()
76 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
84 SDL_RenderCopy(renderer, texture, NULL, &dst_rect);
88 SDLLightmap::draw_surface(const DrawingRequest& request)
90 SDLPainter::draw_surface(renderer, request);
94 SDLLightmap::draw_surface_part(const DrawingRequest& request)
96 SDLPainter::draw_surface_part(renderer, request);
100 SDLLightmap::draw_gradient(const DrawingRequest& request)
102 SDLPainter::draw_gradient(renderer, request);
106 SDLLightmap::draw_filled_rect(const DrawingRequest& request)
108 SDLPainter::draw_filled_rect(renderer, request);
112 SDLLightmap::get_light(const DrawingRequest& request) const
114 const GetLightRequest* getlightrequest
115 = (GetLightRequest*) request.request_data;
118 rect.x = static_cast<int>(request.pos.x * width / SCREEN_WIDTH);
119 rect.y = static_cast<int>(request.pos.y * height / SCREEN_HEIGHT);
123 SDL_SetRenderTarget(renderer, texture);
125 int ret = SDL_RenderReadPixels(renderer, &rect,
126 SDL_PIXELFORMAT_RGB888,
131 log_warning << "failed to read pixels: " << SDL_GetError() << std::endl;
133 SDL_SetRenderTarget(renderer, 0);
135 *(getlightrequest->color_ptr) = Color(pixel[2] / 255.0f,