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,
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;
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)
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;
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)
111 DrawingRequest request;
114 request.pos = transform.apply(position);
115 request.layer = layer;
116 request.drawing_effect = transform.drawing_effect;
117 request.zoom = transform.zoom;
118 request.alpha = transform.alpha;
120 TextRequest* textrequest = new TextRequest;
121 textrequest->font = font;
122 textrequest->text = text;
123 textrequest->alignment = alignment;
124 request.request_data = textrequest;
126 drawingrequests.push_back(request);
130 DrawingContext::draw_center_text(const Font* font, const std::string& text,
131 const Vector& position, int layer)
133 draw_text(font, text, Vector(position.x + SCREEN_WIDTH/2, position.y),
134 CENTER_ALLIGN, layer);
138 DrawingContext::draw_gradient(Color top, Color bottom, int layer)
140 DrawingRequest request;
142 request.type = GRADIENT;
143 request.pos = Vector(0,0);
144 request.layer = layer;
146 request.drawing_effect = transform.drawing_effect;
147 request.zoom = transform.zoom;
148 request.alpha = transform.alpha;
150 GradientRequest* gradientrequest = new GradientRequest;
151 gradientrequest->top = top;
152 gradientrequest->bottom = bottom;
153 request.request_data = gradientrequest;
155 drawingrequests.push_back(request);
159 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
160 Color color, int layer)
162 DrawingRequest request;
164 request.type = FILLRECT;
165 request.pos = transform.apply(topleft);
166 request.layer = layer;
168 request.drawing_effect = transform.drawing_effect;
169 request.zoom = transform.zoom;
170 request.alpha = transform.alpha;
172 FillRectRequest* fillrectrequest = new FillRectRequest;
173 fillrectrequest->size = size;
174 fillrectrequest->color = color;
175 fillrectrequest->color.alpha
176 = (int) ((float) fillrectrequest->color.alpha
177 * ((float) transform.alpha / 255.0));
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(uint32_t effect)
396 transform.drawing_effect = effect;
400 DrawingContext::get_drawing_effect() const
402 return transform.drawing_effect;
406 DrawingContext::set_zooming(float zoom)
408 transform.zoom = zoom;
412 DrawingContext::set_alpha(uint8_t alpha)
414 transform.alpha = alpha;
418 DrawingContext::get_alpha() const
420 return transform.alpha;