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"
27 #include "app/globals.h"
29 #include "gameconfig.h"
31 using namespace SuperTux;
33 DrawingContext::DrawingContext(SDL_Surface* targetsurface)
36 screen = targetsurface;
38 screen = SDL_GetVideoSurface();
42 DrawingContext::~DrawingContext()
47 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
48 int layer, uint32_t drawing_effect)
52 DrawingRequest request;
54 request.type = SURFACE;
55 request.pos = transform.apply(position);
57 if(request.pos.x >= SCREEN_WIDTH || request.pos.y >= SCREEN_HEIGHT
58 || request.pos.x + surface->w < 0 || request.pos.y + surface->h < 0)
61 request.layer = layer;
62 request.drawing_effect = transform.drawing_effect | drawing_effect;
63 request.zoom = transform.zoom;
64 request.alpha = transform.alpha;
65 request.request_data = const_cast<Surface*> (surface);
67 drawingrequests.push_back(request);
71 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
72 const Vector& size, const Vector& dest, int layer, uint32_t drawing_effect)
76 DrawingRequest request;
78 request.type = SURFACE_PART;
79 request.pos = transform.apply(dest);
80 request.layer = layer;
81 request.drawing_effect = transform.drawing_effect | drawing_effect;
82 request.alpha = transform.alpha;
84 SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
85 surfacepartrequest->size = size;
86 surfacepartrequest->source = source;
87 surfacepartrequest->surface = surface;
89 // clip on screen borders
90 if(request.pos.x < 0) {
91 surfacepartrequest->size.x += request.pos.x;
92 if(surfacepartrequest->size.x <= 0)
94 surfacepartrequest->source.x -= request.pos.x;
97 if(request.pos.y < 0) {
98 surfacepartrequest->size.y += request.pos.y;
99 if(surfacepartrequest->size.y <= 0)
101 surfacepartrequest->source.y -= request.pos.y;
104 request.request_data = surfacepartrequest;
106 drawingrequests.push_back(request);
110 DrawingContext::draw_text(const Font* font, const std::string& text,
111 const Vector& position, FontAlignment alignment, int layer,
112 uint32_t drawing_effect)
114 DrawingRequest request;
117 request.pos = transform.apply(position);
118 request.layer = layer;
119 request.drawing_effect = transform.drawing_effect | drawing_effect;
120 request.zoom = transform.zoom;
121 request.alpha = transform.alpha;
123 TextRequest* textrequest = new TextRequest;
124 textrequest->font = font;
125 textrequest->text = text;
126 textrequest->alignment = alignment;
127 request.request_data = textrequest;
129 drawingrequests.push_back(request);
133 DrawingContext::draw_center_text(const Font* font, const std::string& text,
134 const Vector& position, int layer, uint32_t drawing_effect)
136 draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
137 CENTER_ALLIGN, layer, drawing_effect);
141 DrawingContext::draw_gradient(Color top, Color bottom, int layer)
143 DrawingRequest request;
145 request.type = GRADIENT;
146 request.pos = Vector(0,0);
147 request.layer = layer;
149 request.drawing_effect = transform.drawing_effect;
150 request.zoom = transform.zoom;
151 request.alpha = transform.alpha;
153 GradientRequest* gradientrequest = new GradientRequest;
154 gradientrequest->top = top;
155 gradientrequest->bottom = bottom;
156 request.request_data = gradientrequest;
158 drawingrequests.push_back(request);
162 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
163 Color color, int layer)
165 DrawingRequest request;
167 request.type = FILLRECT;
168 request.pos = transform.apply(topleft);
169 request.layer = layer;
171 request.drawing_effect = transform.drawing_effect;
172 request.zoom = transform.zoom;
173 request.alpha = transform.alpha;
175 FillRectRequest* fillrectrequest = new FillRectRequest;
176 fillrectrequest->size = size;
177 fillrectrequest->color = color;
178 request.request_data = fillrectrequest;
180 drawingrequests.push_back(request);
184 DrawingContext::draw_surface_part(DrawingRequest& request)
186 SurfacePartRequest* surfacepartrequest
187 = (SurfacePartRequest*) request.request_data;
189 surfacepartrequest->surface->impl->draw_part(
190 surfacepartrequest->source.x, surfacepartrequest->source.y,
191 request.pos.x, request.pos.y,
192 surfacepartrequest->size.x, surfacepartrequest->size.y, request.alpha,
193 request.drawing_effect);
195 delete surfacepartrequest;
199 DrawingContext::draw_gradient(DrawingRequest& request)
201 GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
202 const Color& top = gradientrequest->top;
203 const Color& bottom = gradientrequest->bottom;
209 glColor3ub(top.red, top.green, top.blue);
211 glVertex2f(SCREEN_WIDTH, 0);
212 glColor3ub(bottom.red, bottom.green, bottom.blue);
213 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
214 glVertex2f(0, SCREEN_HEIGHT);
222 fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, top.red, top.green, top.blue);
226 float redstep = (float(bottom.red)-float(top.red)) / float(SCREEN_HEIGHT);
227 float greenstep = (float(bottom.green)-float(top.green)) / float(SCREEN_HEIGHT);
228 float bluestep = (float(bottom.blue) - float(top.blue)) / float(SCREEN_HEIGHT);
230 for(float y = 0; y < SCREEN_HEIGHT; y += 2)
231 fillrect(0, (int)y, SCREEN_WIDTH, 2,
232 int(float(top.red) + redstep * y),
233 int(float(top.green) + greenstep * y),
234 int(float(top.blue) + bluestep * y), 255);
241 delete gradientrequest;
245 DrawingContext::draw_text(DrawingRequest& request)
247 TextRequest* textrequest = (TextRequest*) request.request_data;
249 textrequest->font->draw(textrequest->text, request.pos,
250 textrequest->alignment, request.drawing_effect, request.alpha);
256 DrawingContext::draw_filled_rect(DrawingRequest& request)
258 FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
260 float x = request.pos.x;
261 float y = request.pos.y;
262 float w = fillrectrequest->size.x;
263 float h = fillrectrequest->size.y;
269 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
270 glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
271 fillrectrequest->color.blue, fillrectrequest->color.alpha);
276 glVertex2f(x+w, y+h);
285 SDL_Surface *temp = NULL;
292 if(fillrectrequest->color.alpha != 255)
294 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
295 screen->format->Rmask,
296 screen->format->Gmask,
297 screen->format->Bmask,
298 screen->format->Amask);
306 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format,
307 fillrectrequest->color.red, fillrectrequest->color.green,
308 fillrectrequest->color.blue));
310 SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
312 SDL_BlitSurface(temp,0,screen,&rect);
314 SDL_FreeSurface(temp);
317 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,
318 fillrectrequest->color.red, fillrectrequest->color.green,
319 fillrectrequest->color.blue));
326 delete fillrectrequest;
330 DrawingContext::do_drawing()
333 assert(transformstack.empty());
335 transformstack.clear();
337 std::stable_sort(drawingrequests.begin(), drawingrequests.end());
339 for(DrawingRequests::iterator i = drawingrequests.begin();
340 i != drawingrequests.end(); ++i) {
344 const Surface* surface = (const Surface*) i->request_data;
347 surface->impl->draw_stretched(i->pos.x * i->zoom, i->pos.y * i->zoom,
348 (int)(surface->w * i->zoom), (int)(surface->h * i->zoom),
349 i->alpha, i->drawing_effect);
351 surface->impl->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
355 draw_surface_part(*i);
364 draw_filled_rect(*i);
371 SDL_GL_SwapBuffers();
375 drawingrequests.clear();
379 DrawingContext::push_transform()
381 transformstack.push_back(transform);
385 DrawingContext::pop_transform()
387 assert(!transformstack.empty());
389 transform = transformstack.back();
390 transformstack.pop_back();
394 DrawingContext::set_drawing_effect(int effect)
396 transform.drawing_effect = effect;
400 DrawingContext::set_zooming(float zoom)
402 transform.zoom = zoom;
406 DrawingContext::set_alpha(int alpha)
408 transform.alpha = alpha;