1 // $Id: drawing_context.cpp 2334 2005-04-04 16:26:14Z grumbel $
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"
29 #include "gameconfig.h"
31 DrawingContext::DrawingContext(SDL_Surface* targetsurface)
34 screen = targetsurface;
36 screen = SDL_GetVideoSurface();
40 DrawingContext::~DrawingContext()
45 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
46 int layer, uint32_t drawing_effect)
50 DrawingRequest request;
52 request.type = SURFACE;
53 request.pos = transform.apply(position);
55 if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
56 || request.pos.x + surface->w < 0 || request.pos.y + surface->h < 0)
59 request.layer = layer;
60 request.drawing_effect = transform.drawing_effect | drawing_effect;
61 request.zoom = transform.zoom;
62 request.alpha = transform.alpha;
63 request.request_data = const_cast<Surface*> (surface);
65 drawingrequests.push_back(request);
69 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
70 const Vector& size, const Vector& dest, int layer, uint32_t drawing_effect)
74 DrawingRequest request;
76 request.type = SURFACE_PART;
77 request.pos = transform.apply(dest);
78 request.layer = layer;
79 request.drawing_effect = transform.drawing_effect | drawing_effect;
80 request.alpha = transform.alpha;
82 SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
83 surfacepartrequest->size = size;
84 surfacepartrequest->source = source;
85 surfacepartrequest->surface = surface;
87 // clip on screen borders
88 if(request.pos.x < 0) {
89 surfacepartrequest->size.x += request.pos.x;
90 if(surfacepartrequest->size.x <= 0)
92 surfacepartrequest->source.x -= request.pos.x;
95 if(request.pos.y < 0) {
96 surfacepartrequest->size.y += request.pos.y;
97 if(surfacepartrequest->size.y <= 0)
99 surfacepartrequest->source.y -= request.pos.y;
102 request.request_data = surfacepartrequest;
104 drawingrequests.push_back(request);
108 DrawingContext::draw_text(const Font* font, const std::string& text,
109 const Vector& position, FontAlignment alignment, int layer,
110 uint32_t drawing_effect)
112 DrawingRequest request;
115 request.pos = transform.apply(position);
116 request.layer = layer;
117 request.drawing_effect = transform.drawing_effect | drawing_effect;
118 request.zoom = transform.zoom;
119 request.alpha = transform.alpha;
121 TextRequest* textrequest = new TextRequest;
122 textrequest->font = font;
123 textrequest->text = text;
124 textrequest->alignment = alignment;
125 request.request_data = textrequest;
127 drawingrequests.push_back(request);
131 DrawingContext::draw_center_text(const Font* font, const std::string& text,
132 const Vector& position, int layer, uint32_t drawing_effect)
134 draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
135 CENTER_ALLIGN, layer, drawing_effect);
139 DrawingContext::draw_gradient(Color top, Color bottom, int layer)
141 DrawingRequest request;
143 request.type = GRADIENT;
144 request.pos = Vector(0,0);
145 request.layer = layer;
147 request.drawing_effect = transform.drawing_effect;
148 request.zoom = transform.zoom;
149 request.alpha = transform.alpha;
151 GradientRequest* gradientrequest = new GradientRequest;
152 gradientrequest->top = top;
153 gradientrequest->bottom = bottom;
154 request.request_data = gradientrequest;
156 drawingrequests.push_back(request);
160 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
161 Color color, int layer)
163 DrawingRequest request;
165 request.type = FILLRECT;
166 request.pos = transform.apply(topleft);
167 request.layer = layer;
169 request.drawing_effect = transform.drawing_effect;
170 request.zoom = transform.zoom;
171 request.alpha = transform.alpha;
173 FillRectRequest* fillrectrequest = new FillRectRequest;
174 fillrectrequest->size = size;
175 fillrectrequest->color = color;
176 request.request_data = fillrectrequest;
178 drawingrequests.push_back(request);
182 DrawingContext::draw_surface_part(DrawingRequest& request)
184 SurfacePartRequest* surfacepartrequest
185 = (SurfacePartRequest*) request.request_data;
187 surfacepartrequest->surface->impl->draw_part(
188 surfacepartrequest->source.x, surfacepartrequest->source.y,
189 request.pos.x, request.pos.y,
190 surfacepartrequest->size.x, surfacepartrequest->size.y, request.alpha,
191 request.drawing_effect);
193 delete surfacepartrequest;
197 DrawingContext::draw_gradient(DrawingRequest& request)
199 GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
200 const Color& top = gradientrequest->top;
201 const Color& bottom = gradientrequest->bottom;
207 glColor3ub(top.red, top.green, top.blue);
209 glVertex2f(SCREEN_WIDTH, 0);
210 glColor3ub(bottom.red, bottom.green, bottom.blue);
211 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
212 glVertex2f(0, SCREEN_HEIGHT);
220 fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, top.red, top.green, top.blue);
224 float redstep = (float(bottom.red)-float(top.red)) / float(SCREEN_HEIGHT);
225 float greenstep = (float(bottom.green)-float(top.green)) / float(SCREEN_HEIGHT);
226 float bluestep = (float(bottom.blue) - float(top.blue)) / float(SCREEN_HEIGHT);
228 for(float y = 0; y < SCREEN_HEIGHT; y += 2)
229 fillrect(0, (int)y, SCREEN_WIDTH, 2,
230 int(float(top.red) + redstep * y),
231 int(float(top.green) + greenstep * y),
232 int(float(top.blue) + bluestep * y), 255);
239 delete gradientrequest;
243 DrawingContext::draw_text(DrawingRequest& request)
245 TextRequest* textrequest = (TextRequest*) request.request_data;
247 textrequest->font->draw(textrequest->text, request.pos,
248 textrequest->alignment, request.drawing_effect, request.alpha);
254 DrawingContext::draw_filled_rect(DrawingRequest& request)
256 FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
258 float x = request.pos.x;
259 float y = request.pos.y;
260 float w = fillrectrequest->size.x;
261 float h = fillrectrequest->size.y;
267 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
268 glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
269 fillrectrequest->color.blue, fillrectrequest->color.alpha);
274 glVertex2f(x+w, y+h);
283 SDL_Surface *temp = NULL;
290 if(fillrectrequest->color.alpha != 255)
292 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
293 screen->format->Rmask,
294 screen->format->Gmask,
295 screen->format->Bmask,
296 screen->format->Amask);
304 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format,
305 fillrectrequest->color.red, fillrectrequest->color.green,
306 fillrectrequest->color.blue));
308 SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
310 SDL_BlitSurface(temp,0,screen,&rect);
312 SDL_FreeSurface(temp);
315 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,
316 fillrectrequest->color.red, fillrectrequest->color.green,
317 fillrectrequest->color.blue));
324 delete fillrectrequest;
328 DrawingContext::do_drawing()
331 assert(transformstack.empty());
333 transformstack.clear();
335 std::stable_sort(drawingrequests.begin(), drawingrequests.end());
337 for(DrawingRequests::iterator i = drawingrequests.begin();
338 i != drawingrequests.end(); ++i) {
342 const Surface* surface = (const Surface*) i->request_data;
345 surface->impl->draw_stretched(i->pos.x * i->zoom, i->pos.y * i->zoom,
346 (int)(surface->w * i->zoom), (int)(surface->h * i->zoom),
347 i->alpha, i->drawing_effect);
349 surface->impl->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
353 draw_surface_part(*i);
362 draw_filled_rect(*i);
369 SDL_GL_SwapBuffers();
373 drawingrequests.clear();
377 DrawingContext::push_transform()
379 transformstack.push_back(transform);
383 DrawingContext::pop_transform()
385 assert(!transformstack.empty());
387 transform = transformstack.back();
388 transformstack.pop_back();
392 DrawingContext::set_drawing_effect(int effect)
394 transform.drawing_effect = effect;
398 DrawingContext::set_zooming(float zoom)
400 transform.zoom = zoom;
404 DrawingContext::set_alpha(int alpha)
406 transform.alpha = alpha;