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 "../video/drawing_context.h"
26 #include "../video/surface.h"
27 #include "../app/globals.h"
28 #include "../video/font.h"
30 using namespace SuperTux;
32 DrawingContext::DrawingContext()
34 transform.draw_effect = NONE_EFFECT;
36 transform.alpha = 255;
39 DrawingContext::~DrawingContext()
44 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
45 int layer, Uint32 drawing_effect)
49 DrawingRequest request;
51 request.type = SURFACE;
52 request.layer = layer;
53 request.request_data = const_cast<Surface*> (surface);
54 request.pos = transform.apply(position);
56 if(request.pos.x >= screen->w || request.pos.y >= screen->h
57 || request.pos.x + surface->w < 0 || request.pos.y + surface->h < 0)
60 request.drawing_effect = drawing_effect;
61 request.drawing_effect = transform.draw_effect | drawing_effect;
62 request.zoom = transform.zoom;
63 request.alpha = transform.alpha;
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 drawing_effect)
74 DrawingRequest request;
76 request.type = SURFACE_PART;
77 request.layer = layer;
78 request.pos = transform.apply(dest);
79 request.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;
86 request.request_data = surfacepartrequest;
88 drawingrequests.push_back(request);
92 DrawingContext::draw_text(Font* font, const std::string& text,
93 const Vector& position, int allignment, int layer,
94 Uint32 drawing_effect)
96 DrawingRequest request;
99 request.layer = layer;
100 request.pos = transform.apply(position);
101 request.drawing_effect = drawing_effect;
102 request.alpha = transform.alpha;
104 TextRequest* textrequest = new TextRequest;
105 textrequest->font = font;
106 textrequest->text = text;
107 textrequest->allignment = allignment;
108 request.request_data = textrequest;
110 drawingrequests.push_back(request);
114 DrawingContext::draw_center_text(Font* font, const std::string& text,
115 const Vector& position, int layer, Uint32 drawing_effect)
117 draw_text(font, text, Vector(position.x + screen->w/2, position.y),
118 CENTER_ALLIGN, layer, drawing_effect);
122 DrawingContext::draw_gradient(Color top, Color bottom, int layer)
124 DrawingRequest request;
126 request.type = GRADIENT;
127 request.layer = layer;
128 request.pos = Vector(0,0);
130 GradientRequest* gradientrequest = new GradientRequest;
131 gradientrequest->top = top;
132 gradientrequest->bottom = bottom;
133 request.request_data = gradientrequest;
135 drawingrequests.push_back(request);
139 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
140 Color color, int layer)
142 DrawingRequest request;
144 request.type = FILLRECT;
145 request.layer = layer;
146 request.pos = transform.apply(topleft);
148 FillRectRequest* fillrectrequest = new FillRectRequest;
149 fillrectrequest->size = size;
150 fillrectrequest->color = color;
151 request.request_data = fillrectrequest;
153 drawingrequests.push_back(request);
157 DrawingContext::draw_surface_part(DrawingRequest& request)
159 SurfacePartRequest* surfacepartrequest
160 = (SurfacePartRequest*) request.request_data;
162 surfacepartrequest->surface->impl->draw_part(
163 surfacepartrequest->source.x, surfacepartrequest->source.y,
164 request.pos.x, request.pos.y,
165 surfacepartrequest->size.x, surfacepartrequest->size.y, request.alpha,
166 request.drawing_effect);
168 delete surfacepartrequest;
172 DrawingContext::draw_gradient(DrawingRequest& request)
174 GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
175 const Color& top = gradientrequest->top;
176 const Color& bottom = gradientrequest->bottom;
182 glColor3ub(top.red, top.green, top.blue);
184 glVertex2f(screen->w, 0);
185 glColor3ub(bottom.red, bottom.green, bottom.blue);
186 glVertex2f(screen->w, screen->h);
187 glVertex2f(0, screen->h);
195 fillrect(0, 0, screen->w, screen->h, top.red, top.green, top.blue);
199 float redstep = (float(bottom.red)-float(top.red)) / float(screen->h);
200 float greenstep = (float(bottom.green)-float(top.green)) / float(screen->h);
201 float bluestep = (float(bottom.blue) - float(top.blue)) / float(screen->h);
203 for(float y = 0; y < screen->h; y += 2)
204 fillrect(0, (int)y, screen->w, 2,
205 int(float(top.red) + redstep * y),
206 int(float(top.green) + greenstep * y),
207 int(float(top.blue) + bluestep * y), 255);
214 delete gradientrequest;
218 DrawingContext::draw_text(DrawingRequest& request)
220 TextRequest* textrequest = (TextRequest*) request.request_data;
222 textrequest->font->draw(textrequest->text, request.pos, textrequest->allignment, request.drawing_effect, request.alpha);
228 DrawingContext::draw_filled_rect(DrawingRequest& request)
230 FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
232 float x = request.pos.x;
233 float y = request.pos.y;
234 float w = fillrectrequest->size.x;
235 float h = fillrectrequest->size.y;
241 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
242 glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
243 fillrectrequest->color.blue, fillrectrequest->color.alpha);
248 glVertex2f(x+w, y+h);
257 SDL_Surface *temp = NULL;
264 if(fillrectrequest->color.alpha != 255)
266 temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
267 screen->format->Rmask,
268 screen->format->Gmask,
269 screen->format->Bmask,
270 screen->format->Amask);
278 SDL_FillRect(temp, &src, SDL_MapRGB(screen->format,
279 fillrectrequest->color.red, fillrectrequest->color.green,
280 fillrectrequest->color.blue));
282 SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
284 SDL_BlitSurface(temp,0,screen,&rect);
286 SDL_FreeSurface(temp);
289 SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,
290 fillrectrequest->color.red, fillrectrequest->color.green,
291 fillrectrequest->color.blue));
298 delete fillrectrequest;
302 DrawingContext::do_drawing()
304 std::stable_sort(drawingrequests.begin(), drawingrequests.end());
306 for(DrawingRequests::iterator i = drawingrequests.begin();
307 i != drawingrequests.end(); ++i) {
311 const Surface* surface = (const Surface*) i->request_data;
314 surface->impl->draw_stretched(i->pos.x * i->zoom, i->pos.y * i->zoom,
315 (int)(surface->w * i->zoom), (int)(surface->h * i->zoom),
316 i->alpha, i->drawing_effect);
318 surface->impl->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
322 draw_surface_part(*i);
331 draw_filled_rect(*i);
338 SDL_GL_SwapBuffers();
342 drawingrequests.clear();
346 DrawingContext::push_transform()
348 transformstack.push_back(transform);
352 DrawingContext::pop_transform()
354 assert(!transformstack.empty());
356 transform = transformstack.back();
357 transformstack.pop_back();
361 DrawingContext::set_drawing_effect(int effect)
363 transform.draw_effect = effect;
367 DrawingContext::set_zooming(float zoom)
369 transform.zoom = zoom;
373 DrawingContext::set_alpha(int alpha)
375 transform.alpha = alpha;