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"
37 #define LIGHTMAP_DIV 4
39 static inline int next_po2(int val)
48 DrawingContext::DrawingContext():
49 ambient_color( 1.0f, 1.0f, 1.0f, 1.0f )
51 screen = SDL_GetVideoSurface();
53 lightmap_width = screen->w / LIGHTMAP_DIV;
54 lightmap_height = screen->h / LIGHTMAP_DIV;
55 unsigned int width = next_po2(lightmap_width);
56 unsigned int height = next_po2(lightmap_height);
58 lightmap = new Texture(width, height, GL_RGB);
60 lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
61 lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
62 texture_manager->register_texture(lightmap);
64 requests = &drawing_requests;
67 DrawingContext::~DrawingContext()
69 texture_manager->remove_texture(lightmap);
74 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
75 float angle, const Color& color, const Blend& blend,
80 DrawingRequest request;
82 request.type = SURFACE;
83 request.pos = transform.apply(position);
85 if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
86 || request.pos.x + surface->get_width() < 0
87 || request.pos.y + surface->get_height() < 0)
90 request.layer = layer;
91 request.drawing_effect = transform.drawing_effect;
92 request.alpha = transform.alpha;
93 request.angle = angle;
94 request.color = color;
95 request.blend = blend;
97 request.request_data = const_cast<Surface*> (surface);
99 requests->push_back(request);
103 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
106 draw_surface(surface, position, 0.0f, Color(1.0f, 1.0f, 1.0f), Blend(), layer);
110 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
111 const Vector& size, const Vector& dest, int layer)
113 assert(surface != 0);
115 DrawingRequest request;
117 request.type = SURFACE_PART;
118 request.pos = transform.apply(dest);
119 request.layer = layer;
120 request.drawing_effect = transform.drawing_effect;
121 request.alpha = transform.alpha;
123 SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
124 surfacepartrequest->size = size;
125 surfacepartrequest->source = source;
126 surfacepartrequest->surface = surface;
128 // clip on screen borders
129 if(request.pos.x < 0) {
130 surfacepartrequest->size.x += request.pos.x;
131 if(surfacepartrequest->size.x <= 0)
133 surfacepartrequest->source.x -= request.pos.x;
136 if(request.pos.y < 0) {
137 surfacepartrequest->size.y += request.pos.y;
138 if(surfacepartrequest->size.y <= 0)
140 surfacepartrequest->source.y -= request.pos.y;
143 request.request_data = surfacepartrequest;
145 requests->push_back(request);
149 DrawingContext::draw_text(const Font* font, const std::string& text,
150 const Vector& position, FontAlignment alignment, int layer)
152 DrawingRequest request;
155 request.pos = transform.apply(position);
156 request.layer = layer;
157 request.drawing_effect = transform.drawing_effect;
158 request.alpha = transform.alpha;
160 TextRequest* textrequest = new TextRequest;
161 textrequest->font = font;
162 textrequest->text = text;
163 textrequest->alignment = alignment;
164 request.request_data = textrequest;
166 requests->push_back(request);
170 DrawingContext::draw_center_text(const Font* font, const std::string& text,
171 const Vector& position, int layer)
173 draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
174 CENTER_ALLIGN, layer);
178 DrawingContext::draw_gradient(const Color& top, const Color& bottom, int layer)
180 DrawingRequest request;
182 request.type = GRADIENT;
183 request.pos = Vector(0,0);
184 request.layer = layer;
186 request.drawing_effect = transform.drawing_effect;
187 request.alpha = transform.alpha;
189 GradientRequest* gradientrequest = new GradientRequest;
190 gradientrequest->top = top;
191 gradientrequest->bottom = bottom;
192 request.request_data = gradientrequest;
194 requests->push_back(request);
198 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
199 const Color& color, int layer)
201 DrawingRequest request;
203 request.type = FILLRECT;
204 request.pos = transform.apply(topleft);
205 request.layer = layer;
207 request.drawing_effect = transform.drawing_effect;
208 request.alpha = transform.alpha;
210 FillRectRequest* fillrectrequest = new FillRectRequest;
211 fillrectrequest->size = size;
212 fillrectrequest->color = color;
213 fillrectrequest->color.alpha = color.alpha * transform.alpha;
214 request.request_data = fillrectrequest;
216 requests->push_back(request);
220 DrawingContext::draw_filled_rect(const Rect& rect, const Color& color,
223 DrawingRequest request;
225 request.type = FILLRECT;
226 request.pos = transform.apply(rect.p1);
227 request.layer = layer;
229 request.drawing_effect = transform.drawing_effect;
230 request.alpha = transform.alpha;
232 FillRectRequest* fillrectrequest = new FillRectRequest;
233 fillrectrequest->size = Vector(rect.get_width(), rect.get_height());
234 fillrectrequest->color = color;
235 fillrectrequest->color.alpha = color.alpha * transform.alpha;
236 request.request_data = fillrectrequest;
238 requests->push_back(request);
242 DrawingContext::draw_surface_part(DrawingRequest& request)
244 SurfacePartRequest* surfacepartrequest
245 = (SurfacePartRequest*) request.request_data;
247 surfacepartrequest->surface->draw_part(
248 surfacepartrequest->source.x, surfacepartrequest->source.y,
249 request.pos.x, request.pos.y,
250 surfacepartrequest->size.x, surfacepartrequest->size.y,
251 request.alpha, request.drawing_effect);
253 delete surfacepartrequest;
257 DrawingContext::draw_gradient(DrawingRequest& request)
259 GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
260 const Color& top = gradientrequest->top;
261 const Color& bottom = gradientrequest->bottom;
263 glDisable(GL_TEXTURE_2D);
265 glColor4f(top.red, top.green, top.blue, top.alpha);
267 glVertex2f(SCREEN_WIDTH, 0);
268 glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
269 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
270 glVertex2f(0, SCREEN_HEIGHT);
272 glEnable(GL_TEXTURE_2D);
274 delete gradientrequest;
278 DrawingContext::draw_text(DrawingRequest& request)
280 TextRequest* textrequest = (TextRequest*) request.request_data;
282 textrequest->font->draw(textrequest->text, request.pos,
283 textrequest->alignment, request.drawing_effect, request.alpha);
289 DrawingContext::draw_filled_rect(DrawingRequest& request)
291 FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
293 float x = request.pos.x;
294 float y = request.pos.y;
295 float w = fillrectrequest->size.x;
296 float h = fillrectrequest->size.y;
298 glDisable(GL_TEXTURE_2D);
299 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
300 fillrectrequest->color.blue, fillrectrequest->color.alpha);
305 glVertex2f(x+w, y+h);
308 glEnable(GL_TEXTURE_2D);
310 delete fillrectrequest;
314 DrawingContext::do_drawing()
317 assert(transformstack.empty());
318 assert(target_stack.empty());
320 transformstack.clear();
321 target_stack.clear();
323 //Use Lightmap if ambient color is not white.
324 bool use_lightmap = ( ambient_color.red != 1.0f || ambient_color.green != 1.0f ||
325 ambient_color.blue != 1.0f );
327 // PART1: create lightmap
329 glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
330 glMatrixMode(GL_PROJECTION);
332 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
333 glMatrixMode(GL_MODELVIEW);
336 glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
337 glClear(GL_COLOR_BUFFER_BIT);
338 handle_drawing_requests(lightmap_requests);
339 lightmap_requests.clear();
342 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
343 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
345 glViewport(0, 0, screen->w, screen->h);
346 glMatrixMode(GL_PROJECTION);
348 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
349 glMatrixMode(GL_MODELVIEW);
354 //glClear(GL_COLOR_BUFFER_BIT);
355 handle_drawing_requests(drawing_requests);
356 drawing_requests.clear();
359 // multiple the lightmap with the framebuffer
360 glBlendFunc(GL_DST_COLOR, GL_ZERO);
362 glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
365 glTexCoord2f(0, lightmap_uv_bottom);
368 glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
369 glVertex2f(SCREEN_WIDTH, 0);
371 glTexCoord2f(lightmap_uv_right, 0);
372 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
375 glVertex2f(0, SCREEN_HEIGHT);
379 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
382 assert_gl("drawing");
384 SDL_GL_SwapBuffers();
388 DrawingContext::handle_drawing_requests(DrawingRequests& requests)
390 std::stable_sort(requests.begin(), requests.end());
392 for(DrawingRequests::iterator i = requests.begin();
393 i != requests.end(); ++i) {
397 const Surface* surface = (const Surface*) i->request_data;
398 if (i->angle == 0.0f)
399 surface->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
401 surface->draw(i->pos.x, i->pos.y, i->alpha, i->angle, i->color, i->blend, i->drawing_effect);
405 draw_surface_part(*i);
414 draw_filled_rect(*i);
421 DrawingContext::push_transform()
423 transformstack.push_back(transform);
427 DrawingContext::pop_transform()
429 assert(!transformstack.empty());
431 transform = transformstack.back();
432 transformstack.pop_back();
436 DrawingContext::set_drawing_effect(DrawingEffect effect)
438 transform.drawing_effect = effect;
442 DrawingContext::get_drawing_effect() const
444 return transform.drawing_effect;
448 DrawingContext::set_alpha(float alpha)
450 transform.alpha = alpha;
454 DrawingContext::get_alpha() const
456 return transform.alpha;
460 DrawingContext::push_target()
462 target_stack.push_back(target);
466 DrawingContext::pop_target()
468 set_target(target_stack.back());
469 target_stack.pop_back();
473 DrawingContext::set_target(Target target)
475 this->target = target;
476 if(target == LIGHTMAP)
477 requests = &lightmap_requests;
479 requests = &drawing_requests;
483 DrawingContext::set_ambient_color( Color new_color )
485 ambient_color = new_color;