3 // SuperTux - A Jump'n Run
4 // Copyright (C) 2004 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 "drawing_context.h"
27 #include "app/globals.h"
30 using namespace SuperTux;
32 DrawingContext::DrawingContext()
36 DrawingContext::~DrawingContext()
41 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
42 int layer, uint32_t drawing_effect)
46 DrawingRequest request;
48 request.type = SURFACE;
49 request.pos = transform.apply(position);
51 if(request.pos.x >= screen->w || request.pos.y >= screen->h
52 || request.pos.x + surface->w < 0 || request.pos.y + surface->h < 0)
55 request.layer = layer;
56 request.drawing_effect = transform.drawing_effect | drawing_effect;
57 request.zoom = transform.zoom;
58 request.alpha = transform.alpha;
59 request.request_data = const_cast<Surface*> (surface);
61 drawingrequests.push_back(request);
65 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
66 const Vector& size, const Vector& dest, int layer, uint32_t drawing_effect)
70 DrawingRequest request;
72 request.type = SURFACE_PART;
73 request.pos = transform.apply(dest);
74 request.layer = layer;
75 request.drawing_effect = transform.drawing_effect | drawing_effect;
76 request.alpha = transform.alpha;
78 SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
79 surfacepartrequest->size = size;
80 surfacepartrequest->source = source;
81 surfacepartrequest->surface = surface;
83 // clip on screen borders
84 if(request.pos.x < 0) {
85 surfacepartrequest->size.x += request.pos.x;
86 if(surfacepartrequest->size.x <= 0)
88 surfacepartrequest->source.x -= request.pos.x;
91 if(request.pos.y < 0) {
92 surfacepartrequest->size.y += request.pos.y;
93 if(surfacepartrequest->size.y <= 0)
95 surfacepartrequest->source.y -= request.pos.y;
98 request.request_data = surfacepartrequest;
100 drawingrequests.push_back(request);
104 DrawingContext::draw_text(const Font* font, const std::string& text,
105 const Vector& position, FontAlignment alignment, int layer,
106 uint32_t drawing_effect)
108 DrawingRequest request;
111 request.pos = transform.apply(position);
112 request.layer = layer;
113 request.drawing_effect = transform.drawing_effect | drawing_effect;
114 request.zoom = transform.zoom;
115 request.alpha = transform.alpha;
117 TextRequest* textrequest = new TextRequest;
118 textrequest->font = font;
119 textrequest->text = text;
120 textrequest->alignment = alignment;
121 request.request_data = textrequest;
123 drawingrequests.push_back(request);
127 DrawingContext::draw_center_text(const Font* font, const std::string& text,
128 const Vector& position, int layer, uint32_t drawing_effect)
130 draw_text(font, text, Vector(position.x + screen->w/2, position.y),
131 CENTER_ALLIGN, layer, drawing_effect);
135 DrawingContext::draw_gradient(Color top, Color bottom, int layer)
137 DrawingRequest request;
139 request.type = GRADIENT;
140 request.pos = Vector(0,0);
141 request.layer = layer;
143 request.drawing_effect = transform.drawing_effect;
144 request.zoom = transform.zoom;
145 request.alpha = transform.alpha;
147 GradientRequest* gradientrequest = new GradientRequest;
148 gradientrequest->top = top;
149 gradientrequest->bottom = bottom;
150 request.request_data = gradientrequest;
152 drawingrequests.push_back(request);
156 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
157 Color color, int layer)
159 DrawingRequest request;
161 request.type = FILLRECT;
162 request.pos = transform.apply(topleft);
163 request.layer = layer;
165 request.drawing_effect = transform.drawing_effect;
166 request.zoom = transform.zoom;
167 request.alpha = transform.alpha;
169 FillRectRequest* fillrectrequest = new FillRectRequest;
170 fillrectrequest->size = size;
171 fillrectrequest->color = color;
172 request.request_data = fillrectrequest;
174 drawingrequests.push_back(request);
178 DrawingContext::draw_surface_part(DrawingRequest& request)
180 SurfacePartRequest* surfacepartrequest
181 = (SurfacePartRequest*) request.request_data;
183 surfacepartrequest->surface->impl->draw_part(
184 surfacepartrequest->source.x, surfacepartrequest->source.y,
185 request.pos.x, request.pos.y,
186 surfacepartrequest->size.x, surfacepartrequest->size.y, request.alpha,
187 request.drawing_effect);
189 delete surfacepartrequest;
193 DrawingContext::draw_gradient(DrawingRequest& request)
195 GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
196 const Color& top = gradientrequest->top;
197 const Color& bottom = gradientrequest->bottom;
203 glColor3ub(top.red, top.green, top.blue);
205 glVertex2f(screen->w, 0);
206 glColor3ub(bottom.red, bottom.green, bottom.blue);
207 glVertex2f(screen->w, screen->h);
208 glVertex2f(0, screen->h);
216 fillrect(0, 0, screen->w, screen->h, top.red, top.green, top.blue);
220 float redstep = (float(bottom.red)-float(top.red)) / float(screen->h);
221 float greenstep = (float(bottom.green)-float(top.green)) / float(screen->h);
222 float bluestep = (float(bottom.blue) - float(top.blue)) / float(screen->h);
224 for(float y = 0; y < screen->h; y += 2)
225 fillrect(0, (int)y, screen->w, 2,
226 int(float(top.red) + redstep * y),
227 int(float(top.green) + greenstep * y),
228 int(float(top.blue) + bluestep * y), 255);
235 delete gradientrequest;
239 DrawingContext::draw_text(DrawingRequest& request)
241 TextRequest* textrequest = (TextRequest*) request.request_data;
243 textrequest->font->draw(textrequest->text, request.pos,
244 textrequest->alignment, request.drawing_effect, request.alpha);
250 DrawingContext::draw_filled_rect(DrawingRequest& request)
252 FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
254 float x = request.pos.x;
255 float y = request.pos.y;
256 float w = fillrectrequest->size.x;
257 float h = fillrectrequest->size.y;
263 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
264 glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
265 fillrectrequest->color.blue, fillrectrequest->color.alpha);
270 glVertex2f(x+w, y+h);
279 SDL_Surface *temp = NULL;
286 if(fillrectrequest->color.alpha != 255)
288 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
289 screen->format->Rmask,
290 screen->format->Gmask,
291 screen->format->Bmask,
292 screen->format->Amask);
300 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format,
301 fillrectrequest->color.red, fillrectrequest->color.green,
302 fillrectrequest->color.blue));
304 SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
306 SDL_BlitSurface(temp,0,screen,&rect);
308 SDL_FreeSurface(temp);
311 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,
312 fillrectrequest->color.red, fillrectrequest->color.green,
313 fillrectrequest->color.blue));
320 delete fillrectrequest;
324 DrawingContext::do_drawing()
327 assert(transformstack.empty());
329 transformstack.clear();
331 std::stable_sort(drawingrequests.begin(), drawingrequests.end());
333 for(DrawingRequests::iterator i = drawingrequests.begin();
334 i != drawingrequests.end(); ++i) {
338 const Surface* surface = (const Surface*) i->request_data;
341 surface->impl->draw_stretched(i->pos.x * i->zoom, i->pos.y * i->zoom,
342 (int)(surface->w * i->zoom), (int)(surface->h * i->zoom),
343 i->alpha, i->drawing_effect);
345 surface->impl->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
349 draw_surface_part(*i);
358 draw_filled_rect(*i);
365 SDL_GL_SwapBuffers();
369 drawingrequests.clear();
373 DrawingContext::push_transform()
375 transformstack.push_back(transform);
379 DrawingContext::pop_transform()
381 assert(!transformstack.empty());
383 transform = transformstack.back();
384 transformstack.pop_back();
388 DrawingContext::set_drawing_effect(int effect)
390 transform.drawing_effect = effect;
394 DrawingContext::set_zooming(float zoom)
396 transform.zoom = zoom;
400 DrawingContext::set_alpha(int alpha)
402 transform.alpha = alpha;