Implemented basic lightmap rendering, still a bit of an incomplete hack job, drawing...
[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
24 SDLLightmap::SDLLightmap() :
25   renderer(static_cast<SDLRenderer*>(Renderer::instance())->get_sdl_renderer()),
26   width(),
27   height(),
28   LIGHTMAP_DIV()
29 {
30   LIGHTMAP_DIV = 8;
31
32   width = 800; //screen->w / LIGHTMAP_DIV;
33   height = 600; //screen->h / LIGHTMAP_DIV;
34
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,
39                               width, height);
40   if (!texture)
41   {
42     std::stringstream msg;
43     msg << "Couldn't create lightmap texture: " << SDL_GetError();
44     throw std::runtime_error(msg.str());
45   }
46 }
47
48 SDLLightmap::~SDLLightmap()
49 {
50   SDL_DestroyTexture(texture);
51 }
52
53 void
54 SDLLightmap::start_draw(const Color &ambient_color)
55 {
56   SDL_SetRenderTarget(renderer, texture);
57  
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);
61
62   SDL_SetRenderDrawColor(renderer, r, g, b, 255);
63   SDL_RenderClear(renderer);
64 }
65
66 void
67 SDLLightmap::end_draw()
68 {
69   SDL_SetRenderTarget(renderer, NULL);
70 }
71
72 void
73 SDLLightmap::do_draw()
74 {
75   SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_MOD);
76
77   SDL_Rect dst_rect;
78   dst_rect.x = 0;
79   dst_rect.y = 0;
80   dst_rect.w = width;
81   dst_rect.h = height;
82
83   SDL_RenderCopy(renderer, texture, NULL, &dst_rect);
84 }
85
86 void
87 SDLLightmap::draw_surface(const DrawingRequest& request)
88 {
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());
92
93   SDL_Rect dst_rect;
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();
98
99   SDL_SetTextureBlendMode(sdltexture->get_texture(), SDL_BLENDMODE_ADD);
100   SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
101 }
102
103 void
104 SDLLightmap::draw_surface_part(const DrawingRequest& request)
105 {
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());
109
110   SDL_Rect dst_rect;
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();
115
116   SDL_SetTextureBlendMode(sdltexture->get_texture(), SDL_BLENDMODE_ADD);
117   SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
118 }
119
120 void
121 SDLLightmap::draw_gradient(const DrawingRequest& request)
122 {
123   log_info << "draw_gradient" << std::endl;
124 }
125
126 void
127 SDLLightmap::draw_filled_rect(const DrawingRequest& request)
128 {
129   log_info << "draw_filled_rect" << std::endl;
130
131   const FillRectRequest* fillrectrequest
132     = (FillRectRequest*) request.request_data;
133
134   SDL_Rect rect;
135   rect.x = request.pos.x;
136   rect.y = request.pos.y;
137   rect.w = fillrectrequest->size.x;
138   rect.h = fillrectrequest->size.y;
139
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);
144
145   log_info << fillrectrequest->color.red << " " << fillrectrequest->color.green << std::endl;
146
147   if((rect.w != 0) && (rect.h != 0))
148   {
149     SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
150     SDL_SetRenderDrawColor(renderer, r, g, b, a);
151     SDL_RenderFillRect(renderer, &rect);
152   }
153 }
154
155 void
156 SDLLightmap::get_light(const DrawingRequest& request) const
157 {
158 #if OLD_SDL1
159   const GetLightRequest* getlightrequest 
160     = (GetLightRequest*) request.request_data;
161
162   int x = (int) (request.pos.x * width / SCREEN_WIDTH);
163   int y = (int) (request.pos.y * height / SCREEN_HEIGHT);
164   int loc = y * width + x;
165   *(getlightrequest->color_ptr) = Color(((float)red_channel[loc])/255, ((float)green_channel[loc])/255, ((float)blue_channel[loc])/255);
166 #endif
167 }
168
169 /* EOF */