-Some cleanups in text scrolling code
[supertux.git] / lib / video / drawing_context.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
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.
10 //
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.
15 //
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.
19 #include <config.h>
20
21 #include <algorithm>
22 #include <cassert>
23 #include <iostream>
24
25 #include "drawing_context.h"
26 #include "surface.h"
27 #include "app/globals.h"
28 #include "font.h"
29
30 using namespace SuperTux;
31
32 DrawingContext::DrawingContext()
33 {
34 }
35
36 DrawingContext::~DrawingContext()
37 {
38 }
39
40 void
41 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
42     int layer, uint32_t drawing_effect)
43 {
44   assert(surface != 0);
45   
46   DrawingRequest request;
47
48   request.type = SURFACE;
49   request.pos = transform.apply(position);
50
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)
53     return;
54
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);  
60
61   drawingrequests.push_back(request);
62 }
63
64 void
65 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
66     const Vector& size, const Vector& dest, int layer, uint32_t drawing_effect)
67 {
68   assert(surface != 0);
69
70   DrawingRequest request;
71
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;
77   
78   SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
79   surfacepartrequest->size = size;
80   surfacepartrequest->source = source;
81   surfacepartrequest->surface = surface;
82
83   // clip on screen borders
84   if(request.pos.x < 0) {
85     surfacepartrequest->size.x += request.pos.x;
86     if(surfacepartrequest->size.x <= 0)
87       return;
88     surfacepartrequest->source.x -= request.pos.x;
89     request.pos.x = 0;
90   }
91   if(request.pos.y < 0) {
92     surfacepartrequest->size.y += request.pos.y;
93     if(surfacepartrequest->size.y <= 0)
94       return;
95     surfacepartrequest->source.y -= request.pos.y;
96     request.pos.y = 0;
97   }
98   request.request_data = surfacepartrequest;
99
100   drawingrequests.push_back(request);
101 }
102
103 void
104 DrawingContext::draw_text(const Font* font, const std::string& text,
105     const Vector& position, FontAlignment alignment, int layer,
106     uint32_t drawing_effect)
107 {
108   DrawingRequest request;
109
110   request.type = TEXT;
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;
116
117   TextRequest* textrequest = new TextRequest;
118   textrequest->font = font;
119   textrequest->text = text;
120   textrequest->alignment = alignment;
121   request.request_data = textrequest;
122
123   drawingrequests.push_back(request);
124 }
125
126 void
127 DrawingContext::draw_center_text(const Font* font, const std::string& text,
128     const Vector& position, int layer, uint32_t drawing_effect)
129 {
130   draw_text(font, text, Vector(position.x + screen->w/2, position.y),
131       CENTER_ALLIGN, layer, drawing_effect);
132 }
133
134 void
135 DrawingContext::draw_gradient(Color top, Color bottom, int layer)
136 {
137   DrawingRequest request;
138
139   request.type = GRADIENT;
140   request.pos = Vector(0,0);
141   request.layer = layer;
142
143   request.drawing_effect = transform.drawing_effect;
144   request.zoom = transform.zoom;
145   request.alpha = transform.alpha;
146
147   GradientRequest* gradientrequest = new GradientRequest;
148   gradientrequest->top = top;
149   gradientrequest->bottom = bottom;
150   request.request_data = gradientrequest;
151
152   drawingrequests.push_back(request);
153 }
154
155 void
156 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
157         Color color, int layer)
158 {
159   DrawingRequest request;
160
161   request.type = FILLRECT;
162   request.pos = transform.apply(topleft);
163   request.layer = layer;
164
165   request.drawing_effect = transform.drawing_effect;
166   request.zoom = transform.zoom;
167   request.alpha = transform.alpha;                    
168
169   FillRectRequest* fillrectrequest = new FillRectRequest;
170   fillrectrequest->size = size;
171   fillrectrequest->color = color;
172   request.request_data = fillrectrequest;
173
174   drawingrequests.push_back(request);
175 }
176
177 void
178 DrawingContext::draw_surface_part(DrawingRequest& request)
179 {
180   SurfacePartRequest* surfacepartrequest
181     = (SurfacePartRequest*) request.request_data;
182
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);
188
189   delete surfacepartrequest;
190 }
191
192 void
193 DrawingContext::draw_gradient(DrawingRequest& request)
194 {
195   GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
196   const Color& top = gradientrequest->top;
197   const Color& bottom = gradientrequest->bottom;
198   
199 #ifndef NOOPENGL
200   if(use_gl)
201     {
202       glBegin(GL_QUADS);
203       glColor3ub(top.red, top.green, top.blue);
204       glVertex2f(0, 0);
205       glVertex2f(screen->w, 0);
206       glColor3ub(bottom.red, bottom.green, bottom.blue);
207       glVertex2f(screen->w, screen->h);
208       glVertex2f(0, screen->h);
209       glEnd();
210     }
211   else
212   {
213 #endif
214     if(&top == &bottom)
215       {
216       fillrect(0, 0, screen->w, screen->h, top.red, top.green, top.blue);
217       }
218     else
219       {
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);
223
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);
229       }
230 #ifndef NOOPENGL
231
232     }
233 #endif
234
235   delete gradientrequest;
236 }
237
238 void
239 DrawingContext::draw_text(DrawingRequest& request)
240 {
241   TextRequest* textrequest = (TextRequest*) request.request_data;
242
243   textrequest->font->draw(textrequest->text, request.pos,
244       textrequest->alignment, request.drawing_effect, request.alpha);
245
246   delete textrequest;
247 }
248
249 void
250 DrawingContext::draw_filled_rect(DrawingRequest& request)
251 {
252   FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
253
254   float x = request.pos.x;
255   float y = request.pos.y;
256   float w = fillrectrequest->size.x;
257   float h = fillrectrequest->size.y;
258
259 #ifndef NOOPENGL
260   if(use_gl)
261     {
262       glEnable(GL_BLEND);
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);
266
267       glBegin(GL_POLYGON);
268       glVertex2f(x, y);
269       glVertex2f(x+w, y);
270       glVertex2f(x+w, y+h);
271       glVertex2f(x, y+h);
272       glEnd();
273       glDisable(GL_BLEND);
274     }
275   else
276     {
277 #endif
278       SDL_Rect src, rect;
279       SDL_Surface *temp = NULL;
280                                                                                 
281       rect.x = (int)x;
282       rect.y = (int)y;
283       rect.w = (int)w;
284       rect.h = (int)h;
285                                                                                 
286       if(fillrectrequest->color.alpha != 255)
287         {
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);
293                                                                                 
294                                                                                 
295           src.x = 0;
296           src.y = 0;
297           src.w = rect.w;
298           src.h = rect.h;
299                                                                                 
300           SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, 
301                 fillrectrequest->color.red, fillrectrequest->color.green,
302                 fillrectrequest->color.blue));
303                                                                                 
304           SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
305                                                                                 
306           SDL_BlitSurface(temp,0,screen,&rect);
307                                                                                 
308           SDL_FreeSurface(temp);
309         }
310       else
311         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 
312               fillrectrequest->color.red, fillrectrequest->color.green,
313               fillrectrequest->color.blue));
314                                                                                 
315 #ifndef NOOPENGL
316                                                                                 
317     }
318 #endif
319
320   delete fillrectrequest;
321 }
322
323 void
324 DrawingContext::do_drawing()
325 {
326   std::stable_sort(drawingrequests.begin(), drawingrequests.end());
327
328   for(DrawingRequests::iterator i = drawingrequests.begin();
329       i != drawingrequests.end(); ++i) {
330     switch(i->type) {
331       case SURFACE:
332       {
333         const Surface* surface = (const Surface*) i->request_data;
334
335         if(i->zoom != 1.0)
336           surface->impl->draw_stretched(i->pos.x * i->zoom, i->pos.y * i->zoom,
337                          (int)(surface->w * i->zoom), (int)(surface->h * i->zoom),
338                          i->alpha, i->drawing_effect);
339         else
340           surface->impl->draw(i->pos.x, i->pos.y, i->alpha, i->drawing_effect);
341         break;
342       }
343       case SURFACE_PART:
344         draw_surface_part(*i);
345         break;
346       case GRADIENT:
347         draw_gradient(*i);
348         break;
349       case TEXT:
350         draw_text(*i);
351         break;
352       case FILLRECT:
353         draw_filled_rect(*i);
354         break;
355     }
356   }
357
358   // update screen
359   if(use_gl)
360     SDL_GL_SwapBuffers();
361   else
362     SDL_Flip(screen);
363
364   drawingrequests.clear();
365 }
366
367 void
368 DrawingContext::push_transform()
369 {
370   transformstack.push_back(transform);
371 }
372
373 void
374 DrawingContext::pop_transform()
375 {
376   assert(!transformstack.empty());
377
378   transform = transformstack.back();
379   transformstack.pop_back();
380 }
381
382 void
383 DrawingContext::set_drawing_effect(int effect)
384 {
385   transform.drawing_effect = effect;
386 }
387
388 void
389 DrawingContext::set_zooming(float zoom)
390 {
391   transform.zoom = zoom;
392 }
393
394 void
395 DrawingContext::set_alpha(int alpha)
396 {
397   transform.alpha = alpha;
398 }