4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <SDL_image.h>
28 #include "drawing_context.hpp"
29 #include "surface.hpp"
32 #include "gameconfig.hpp"
34 #include "texture.hpp"
35 #include "texture_manager.hpp"
36 #define LIGHTMAP_DIV 5
38 static inline int next_po2(int val)
47 DrawingContext::DrawingContext()
48 : ambient_color( 1.0f, 1.0f, 1.0f, 1.0f )
50 screen = SDL_GetVideoSurface();
52 lightmap_width = screen->w / LIGHTMAP_DIV;
53 lightmap_height = screen->h / LIGHTMAP_DIV;
54 unsigned int width = next_po2(lightmap_width);
55 unsigned int height = next_po2(lightmap_height);
57 lightmap = new Texture(width, height, GL_RGB);
59 lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
60 lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
61 texture_manager->register_texture(lightmap);
63 requests = &drawing_requests;
66 DrawingContext::~DrawingContext()
68 texture_manager->remove_texture(lightmap);
73 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
74 float angle, const Color& color, const Blend& blend,
79 DrawingRequest request;
81 request.type = SURFACE;
82 request.pos = transform.apply(position);
84 if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
85 || request.pos.x + surface->get_width() < 0
86 || request.pos.y + surface->get_height() < 0)
89 request.layer = layer;
90 request.drawing_effect = transform.drawing_effect;
91 request.alpha = transform.alpha;
92 request.angle = angle;
93 request.color = color;
94 request.blend = blend;
96 request.request_data = const_cast<Surface*> (surface);
98 requests->push_back(request);
102 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
105 draw_surface(surface, position, 0.0f, Color(1.0f, 1.0f, 1.0f), Blend(), layer);
109 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
110 const Vector& size, const Vector& dest, int layer)
112 assert(surface != 0);
114 DrawingRequest request;
116 request.type = SURFACE_PART;
117 request.pos = transform.apply(dest);
118 request.layer = layer;
119 request.drawing_effect = transform.drawing_effect;
120 request.alpha = transform.alpha;
122 SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
123 surfacepartrequest->size = size;
124 surfacepartrequest->source = source;
125 surfacepartrequest->surface = surface;
127 // clip on screen borders
128 if(request.pos.x < 0) {
129 surfacepartrequest->size.x += request.pos.x;
130 if(surfacepartrequest->size.x <= 0)
132 surfacepartrequest->source.x -= request.pos.x;
135 if(request.pos.y < 0) {
136 surfacepartrequest->size.y += request.pos.y;
137 if(surfacepartrequest->size.y <= 0)
139 surfacepartrequest->source.y -= request.pos.y;
142 request.request_data = surfacepartrequest;
144 requests->push_back(request);
148 DrawingContext::draw_text(const Font* font, const std::string& text,
149 const Vector& position, FontAlignment alignment, int layer)
151 DrawingRequest request;
154 request.pos = transform.apply(position);
155 request.layer = layer;
156 request.drawing_effect = transform.drawing_effect;
157 request.alpha = transform.alpha;
159 TextRequest* textrequest = new TextRequest;
160 textrequest->font = font;
161 textrequest->text = text;
162 textrequest->alignment = alignment;
163 request.request_data = textrequest;
165 requests->push_back(request);
169 DrawingContext::draw_center_text(const Font* font, const std::string& text,
170 const Vector& position, int layer)
172 draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
173 ALIGN_CENTER, layer);
177 DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
179 DrawingRequest request;
181 request.type = GRADIENT;
182 request.pos = Vector(0,0);
183 request.layer = layer;
185 request.drawing_effect = transform.drawing_effect;
186 request.alpha = transform.alpha;
188 GradientRequest* gradientrequest = new GradientRequest;
189 gradientrequest->top = top;
190 gradientrequest->bottom = bottom;
191 request.request_data = gradientrequest;
193 requests->push_back(request);
197 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
198 const Color& color, int layer)
200 DrawingRequest request;
202 request.type = FILLRECT;
203 request.pos = transform.apply(topleft);
204 request.layer = layer;
206 request.drawing_effect = transform.drawing_effect;
207 request.alpha = transform.alpha;
209 FillRectRequest* fillrectrequest = new FillRectRequest;
210 fillrectrequest->size = size;
211 fillrectrequest->color = color;
212 fillrectrequest->color.alpha = color.alpha * transform.alpha;
213 request.request_data = fillrectrequest;
215 requests->push_back(request);
219 DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
222 DrawingRequest request;
224 request.type = FILLRECT;
225 request.pos = transform.apply(rect.p1);
226 request.layer = layer;
228 request.drawing_effect = transform.drawing_effect;
229 request.alpha = transform.alpha;
231 FillRectRequest* fillrectrequest = new FillRectRequest;
232 fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
233 fillrectrequest->color = color;
234 fillrectrequest->color.alpha = color.alpha * transform.alpha;
235 request.request_data = fillrectrequest;
237 requests->push_back(request);
241 DrawingContext::get_light(const Vector& position, Color* color)
243 if( ambient_color.red == 1.0f && ambient_color.green == 1.0f
244 && ambient_color.blue == 1.0f ) {
245 *color = Color( 1.0f, 1.0f, 1.0f);
249 DrawingRequest request;
250 request.type = GETLIGHT;
251 request.pos = transform.apply(position);
253 //There is no light offscreen.
254 if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
255 || request.pos.x < 0 || request.pos.y < 0){
256 *color = Color( 0, 0, 0);
260 request.layer = LAYER_GUI; //make sure all get_light requests are handled last.
261 GetLightRequest* getlightrequest = new GetLightRequest;
262 getlightrequest->color_ptr = color;
263 request.request_data = getlightrequest;
264 lightmap_requests.push_back(request);
268 DrawingContext::get_light(DrawingRequest& request)
270 GetLightRequest* getlightrequest = (GetLightRequest*) request.request_data;
273 for( int i = 0; i<3; i++)
274 pixels[i] = 0.0f; //set to black
276 float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
277 float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
278 glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
279 *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
280 //printf("get_light %f/%f =>%f/%f r%f g%f b%f\n", request.pos.x, request.pos.y, posX, posY, pixels[0], pixels[1], pixels[2]);
282 delete getlightrequest;
286 DrawingContext::draw_surface_part(DrawingRequest& request)
288 SurfacePartRequest* surfacepartrequest
289 = (SurfacePartRequest*) request.request_data;
291 surfacepartrequest->surface->draw_part(
292 surfacepartrequest->source.x, surfacepartrequest->source.y,
293 request.pos.x, request.pos.y,
294 surfacepartrequest->size.x, surfacepartrequest->size.y,
295 request.alpha, request.drawing_effect);
297 delete surfacepartrequest;
301 DrawingContext::draw_gradient(DrawingRequest& request)
303 GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
304 const Color& top = gradientrequest->top;
305 const Color& bottom = gradientrequest->bottom;
307 glDisable(GL_TEXTURE_2D);
309 glColor4f(top.red, top.green, top.blue, top.alpha);
311 glVertex2f(SCREEN_WIDTH, 0);
312 glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
313 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
314 glVertex2f(0, SCREEN_HEIGHT);
316 glEnable(GL_TEXTURE_2D);
318 delete gradientrequest;
322 DrawingContext::draw_text(DrawingRequest& request)
324 TextRequest* textrequest = (TextRequest*) request.request_data;
326 textrequest->font->draw(textrequest->text, request.pos,
327 textrequest->alignment, request.drawing_effect, request.alpha);
333 DrawingContext::draw_filled_rect(DrawingRequest& request)
335 FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
337 float x = request.pos.x;
338 float y = request.pos.y;
339 float w = fillrectrequest->size.x;
340 float h = fillrectrequest->size.y;
342 glDisable(GL_TEXTURE_2D);
343 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
344 fillrectrequest->color.blue, fillrectrequest->color.alpha);
349 glVertex2f(x+w, y+h);
352 glEnable(GL_TEXTURE_2D);
354 delete fillrectrequest;
358 DrawingContext::draw_lightmap(DrawingRequest& request)
360 const Texture* texture = reinterpret_cast<Texture*> (request.request_data);
362 // multiple the lightmap with the framebuffer
363 glBlendFunc(GL_DST_COLOR, GL_ZERO);
365 glBindTexture(GL_TEXTURE_2D, texture->get_handle());
368 glTexCoord2f(0, lightmap_uv_bottom);
371 glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
372 glVertex2f(SCREEN_WIDTH, 0);
374 glTexCoord2f(lightmap_uv_right, 0);
375 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
378 glVertex2f(0, SCREEN_HEIGHT);
382 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
386 DrawingContext::do_drawing()
389 assert(transformstack.empty());
390 assert(target_stack.empty());
392 transformstack.clear();
393 target_stack.clear();
395 //Use Lightmap if ambient color is not white.
396 bool use_lightmap = ( ambient_color.red != 1.0f || ambient_color.green != 1.0f ||
397 ambient_color.blue != 1.0f );
399 // PART1: create lightmap
401 glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
402 glMatrixMode(GL_PROJECTION);
404 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
405 glMatrixMode(GL_MODELVIEW);
408 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
409 glClear(GL_COLOR_BUFFER_BIT);
410 handle_drawing_requests(lightmap_requests);
411 lightmap_requests.clear();
414 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
415 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
417 glViewport(0, 0, screen->w, screen->h);
418 glMatrixMode(GL_PROJECTION);
420 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
421 glMatrixMode(GL_MODELVIEW);
425 // add a lightmap drawing request into the queue
426 DrawingRequest request;
427 request.type = LIGHTMAPREQUEST;
428 request.layer = LAYER_HUD - 1;
429 request.request_data = lightmap;
430 requests->push_back(request);
433 //glClear(GL_COLOR_BUFFER_BIT);
434 handle_drawing_requests(drawing_requests);
435 drawing_requests.clear();
436 assert_gl("drawing");
438 SDL_GL_SwapBuffers();
442 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
444 std::stable_sort(requests.begin(), requests.end());
446 for(DrawingRequests::iterator i = requests.begin();
447 i != requests.end(); ++i) {
451 const Surface* surface = (const Surface*) i->request_data;
452 if (i->angle == 0.0f &&
453 i->color.red == 1.0f && i->color.green == 1.0f &&
454 i->color.blue == 1.0f && i->color.alpha == 1.0f )
455 surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
457 surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect);
461 draw_surface_part(*i);
470 draw_filled_rect(*i);
472 case LIGHTMAPREQUEST:
483 DrawingContext::push_transform()
485 transformstack.push_back(transform);
489 DrawingContext::pop_transform()
491 assert(!transformstack.empty());
493 transform = transformstack.back();
494 transformstack.pop_back();
498 DrawingContext::set_drawing_effect(DrawingEffect effect)
500 transform.drawing_effect = effect;
504 DrawingContext::get_drawing_effect() const
506 return transform.drawing_effect;
510 DrawingContext::set_alpha(float alpha)
512 transform.alpha = alpha;
516 DrawingContext::get_alpha() const
518 return transform.alpha;
522 DrawingContext::push_target()
524 target_stack.push_back(target);
528 DrawingContext::pop_target()
530 set_target(target_stack.back());
531 target_stack.pop_back();
535 DrawingContext::set_target(Target target)
537 this->target = target;
538 if(target == LIGHTMAP)
539 requests = &lightmap_requests;
541 requests = &drawing_requests;
545 DrawingContext::set_ambient_color( Color new_color )
547 ambient_color = new_color;