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