1d7f6c43eba053c44336a7f83b746b4c856ac489
[supertux.git] / src / video / sdl / sdl_lightmap.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 <iostream>
18
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"
24
25 SDLLightmap::SDLLightmap() :
26   m_renderer(static_cast<SDLRenderer&>(VideoSystem::current()->get_renderer()).get_sdl_renderer()),
27   m_texture(),
28   m_width(),
29   m_height(),
30   m_LIGHTMAP_DIV()
31 {
32   m_LIGHTMAP_DIV = 5;
33
34   m_width = SCREEN_WIDTH;
35   m_height = SCREEN_HEIGHT;
36
37   m_texture = SDL_CreateTexture(m_renderer,
38                                 SDL_PIXELFORMAT_RGB888,
39                                 SDL_TEXTUREACCESS_TARGET,
40                                 m_width / m_LIGHTMAP_DIV,
41                                 m_height / m_LIGHTMAP_DIV);
42   if (!m_texture)
43   {
44     std::stringstream msg;
45     msg << "Couldn't create lightmap texture: " << SDL_GetError();
46     throw std::runtime_error(msg.str());
47   }
48 }
49
50 SDLLightmap::~SDLLightmap()
51 {
52   SDL_DestroyTexture(m_texture);
53 }
54
55 void
56 SDLLightmap::start_draw(const Color &ambient_color)
57 {
58   SDL_SetRenderTarget(m_renderer, m_texture);
59
60   Uint8 r = static_cast<Uint8>(ambient_color.red * 255);
61   Uint8 g = static_cast<Uint8>(ambient_color.green * 255);
62   Uint8 b = static_cast<Uint8>(ambient_color.blue * 255);
63
64   SDL_SetRenderDrawColor(m_renderer, r, g, b, 255);
65   SDL_RenderClear(m_renderer);
66   SDL_RenderSetScale(m_renderer, 1.0f / m_LIGHTMAP_DIV, 1.0f / m_LIGHTMAP_DIV);
67 }
68
69 void
70 SDLLightmap::end_draw()
71 {
72   SDL_RenderSetScale(m_renderer, 1.0f, 1.0f);
73   SDL_SetRenderTarget(m_renderer, NULL);
74 }
75
76 void
77 SDLLightmap::do_draw()
78 {
79   SDL_SetTextureBlendMode(m_texture, SDL_BLENDMODE_MOD);
80
81   SDL_Rect dst_rect;
82   dst_rect.x = 0;
83   dst_rect.y = 0;
84   dst_rect.w = m_width;
85   dst_rect.h = m_height;
86
87   SDL_RenderCopy(m_renderer, m_texture, NULL, &dst_rect);
88 }
89
90 void
91 SDLLightmap::draw_surface(const DrawingRequest& request)
92 {
93   SDLPainter::draw_surface(m_renderer, request);
94 }
95
96 void
97 SDLLightmap::draw_surface_part(const DrawingRequest& request)
98 {
99   SDLPainter::draw_surface_part(m_renderer, request);
100 }
101
102 void
103 SDLLightmap::draw_gradient(const DrawingRequest& request)
104 {
105   SDLPainter::draw_gradient(m_renderer, request);
106 }
107
108 void
109 SDLLightmap::draw_filled_rect(const DrawingRequest& request)
110 {
111   SDLPainter::draw_filled_rect(m_renderer, request);
112 }
113
114 void
115 SDLLightmap::get_light(const DrawingRequest& request) const
116 {
117   const GetLightRequest* getlightrequest
118     = static_cast<GetLightRequest*>(request.request_data);
119
120   SDL_Rect rect;
121   rect.x = static_cast<int>(request.pos.x / m_LIGHTMAP_DIV);
122   rect.y = static_cast<int>((m_height - request.pos.y) / m_LIGHTMAP_DIV);
123   rect.w = 1;
124   rect.h = 1;
125
126   SDL_SetRenderTarget(m_renderer, m_texture);
127   Uint8 pixel[4];
128   int ret = SDL_RenderReadPixels(m_renderer, &rect,
129                                  SDL_PIXELFORMAT_RGB888,
130                                  pixel,
131                                  1);
132   if (ret != 0)
133   {
134     log_warning << "failed to read pixels: " << SDL_GetError() << std::endl;
135   }
136   SDL_SetRenderTarget(m_renderer, 0);
137
138   *(getlightrequest->color_ptr) = Color(pixel[2] / 255.0f,
139                                         pixel[1] / 255.0f,
140                                         pixel[0] / 255.0f);
141 }
142
143 /* EOF */