Optimized gradient when top color is the same as the bottom one.
[supertux.git] / src / screen / 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 <algorithm>
20 #include <cassert>
21 #include <iostream>
22
23 #include "drawing_context.h"
24 #include "texture.h"
25 #include "globals.h"
26 #include "font.h"
27
28 DrawingContext::DrawingContext()
29 {
30 }
31
32 DrawingContext::~DrawingContext()
33 {
34 }
35
36 void
37 DrawingContext::draw_surface(const Surface* surface, const Vector& position,
38     int layer, Uint32 drawing_effect)
39 {
40   assert(surface != 0);
41   
42   DrawingRequest request;
43
44   request.type = SURFACE;
45   request.layer = layer;
46   request.request_data = const_cast<Surface*> (surface);
47   request.pos = transform.apply(position);
48   request.drawing_effect = drawing_effect;
49
50   drawingrequests.push_back(request);
51 }
52
53 void
54 DrawingContext::draw_surface_part(const Surface* surface, const Vector& source,
55     const Vector& size, const Vector& dest, int layer, Uint32 drawing_effect)
56 {
57   assert(surface != 0);
58
59   DrawingRequest request;
60
61   request.type = SURFACE_PART;
62   request.layer = layer;
63   request.pos = transform.apply(dest);
64   request.drawing_effect = drawing_effect;
65   
66   SurfacePartRequest* surfacepartrequest = new SurfacePartRequest();
67   surfacepartrequest->size = size;
68   surfacepartrequest->source = source;
69   surfacepartrequest->surface = surface;
70   request.request_data = surfacepartrequest;
71
72   drawingrequests.push_back(request);
73 }
74
75 void
76 DrawingContext::draw_text(Font* font, const std::string& text,
77     const Vector& position, int layer)
78 {
79   DrawingRequest request;
80
81   request.type = TEXT;
82   request.layer = layer;
83   request.pos = transform.apply(position);
84
85   TextRequest* textrequest = new TextRequest;
86   textrequest->font = font;
87   textrequest->text = text;
88   request.request_data = textrequest;
89
90   drawingrequests.push_back(request);
91 }
92
93 void
94 DrawingContext::draw_text_center(Font* font, const std::string& text,
95     const Vector& position, int layer)
96 {
97   DrawingRequest request;
98
99   request.type = TEXT;
100   request.layer = layer;
101   request.pos = transform.apply(position) + Vector(screen->w/2 - 
102       font->get_text_width(text)/2, 0);
103
104   TextRequest* textrequest = new TextRequest;
105   textrequest->font = font;
106   textrequest->text = text;
107   request.request_data = textrequest;
108
109   drawingrequests.push_back(request);
110 }
111
112 void
113 DrawingContext::draw_gradient(Color top, Color bottom, int layer)
114 {
115   DrawingRequest request;
116
117   request.type = GRADIENT;
118   request.layer = layer;
119   request.pos = Vector(0,0);
120
121   GradientRequest* gradientrequest = new GradientRequest;
122   gradientrequest->top = top;
123   gradientrequest->bottom = bottom;
124   request.request_data = gradientrequest;
125
126   drawingrequests.push_back(request);
127 }
128
129 void
130 DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size,
131         Color color, int layer)
132 {
133   DrawingRequest request;
134
135   request.type = FILLRECT;
136   request.layer = layer;
137   request.pos = transform.apply(topleft);
138
139   FillRectRequest* fillrectrequest = new FillRectRequest;
140   fillrectrequest->size = size;
141   fillrectrequest->color = color;
142   request.request_data = fillrectrequest;
143
144   drawingrequests.push_back(request);
145 }
146
147 void
148 DrawingContext::draw_surface_part(DrawingRequest& request)
149 {
150   SurfacePartRequest* surfacepartrequest
151     = (SurfacePartRequest*) request.request_data;
152
153   surfacepartrequest->surface->impl->draw_part(
154       surfacepartrequest->source.x, surfacepartrequest->source.y,
155       request.pos.x, request.pos.y,
156       surfacepartrequest->size.x, surfacepartrequest->size.y, 255,
157       request.drawing_effect);
158
159   delete surfacepartrequest;
160 }
161
162 void
163 DrawingContext::draw_gradient(DrawingRequest& request)
164 {
165   GradientRequest* gradientrequest = (GradientRequest*) request.request_data;
166   const Color& top = gradientrequest->top;
167   const Color& bottom = gradientrequest->bottom;
168   
169 #ifndef NOOPENGL
170   if(use_gl)
171     {
172       glBegin(GL_QUADS);
173       glColor3ub(top.red, top.green, top.blue);
174       glVertex2f(0, 0);
175       glVertex2f(screen->w, 0);
176       glColor3ub(bottom.red, bottom.green, bottom.blue);
177       glVertex2f(screen->w, screen->h);
178       glVertex2f(0, screen->h);
179       glEnd();
180     }
181   else
182   {
183 #endif
184     if(&top == &bottom)
185       {
186       fillrect(0, 0, screen->w, screen->h, top.red, top.green, top.blue);
187       }
188     else
189       {
190       float redstep = (float(bottom.red)-float(top.red)) / float(screen->h);
191       float greenstep = (float(bottom.green)-float(top.green)) / float(screen->h);
192       float bluestep = (float(bottom.blue) - float(top.blue)) / float(screen->h);
193
194       for(float y = 0; y < screen->h; y += 2)
195         fillrect(0, (int)y, screen->w, 2,
196             int(float(top.red) + redstep * y),
197             int(float(top.green) + greenstep * y),
198             int(float(top.blue) + bluestep * y), 255);
199       }
200 #ifndef NOOPENGL
201
202     }
203 #endif
204
205   delete gradientrequest;
206 }
207
208 void
209 DrawingContext::draw_text(DrawingRequest& request)
210 {
211   TextRequest* textrequest = (TextRequest*) request.request_data;
212   
213   textrequest->font->draw(textrequest->text, request.pos);
214
215   delete textrequest;
216 }
217
218 void
219 DrawingContext::draw_filled_rect(DrawingRequest& request)
220 {
221   FillRectRequest* fillrectrequest = (FillRectRequest*) request.request_data;
222
223   float x = request.pos.x;
224   float y = request.pos.y;
225   float w = fillrectrequest->size.x;
226   float h = fillrectrequest->size.y;
227 #ifndef NOOPENGL
228   if(use_gl)
229     {
230       glEnable(GL_BLEND);
231       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
232       glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
233           fillrectrequest->color.blue, fillrectrequest->color.alpha);
234
235       glBegin(GL_POLYGON);
236       glVertex2f(x, y);
237       glVertex2f(x+w, y);
238       glVertex2f(x+w, y+h);
239       glVertex2f(x, y+h);
240       glEnd();
241       glDisable(GL_BLEND);
242     }
243   else
244     {
245 #endif
246       SDL_Rect src, rect;
247       SDL_Surface *temp = NULL;
248                                                                                 
249       rect.x = (int)x;
250       rect.y = (int)y;
251       rect.w = (int)w;
252       rect.h = (int)h;
253                                                                                 
254       if(fillrectrequest->color.alpha != 255)
255         {
256           temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
257                                       screen->format->Rmask,
258                                       screen->format->Gmask,
259                                       screen->format->Bmask,
260                                       screen->format->Amask);
261                                                                                 
262                                                                                 
263           src.x = 0;
264           src.y = 0;
265           src.w = rect.w;
266           src.h = rect.h;
267                                                                                 
268           SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, 
269                 fillrectrequest->color.red, fillrectrequest->color.green,
270                 fillrectrequest->color.blue));
271                                                                                 
272           SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
273                                                                                 
274           SDL_BlitSurface(temp,0,screen,&rect);
275                                                                                 
276           SDL_FreeSurface(temp);
277         }
278       else
279         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 
280               fillrectrequest->color.red, fillrectrequest->color.green,
281               fillrectrequest->color.blue));
282                                                                                 
283 #ifndef NOOPENGL
284                                                                                 
285     }
286 #endif
287
288   delete fillrectrequest;
289 }
290
291 void
292 DrawingContext::do_drawing()
293 {
294   std::stable_sort(drawingrequests.begin(), drawingrequests.end());
295
296   for(DrawingRequests::iterator i = drawingrequests.begin();
297       i != drawingrequests.end(); ++i) {
298     switch(i->type) {
299       case SURFACE:
300       {
301         const Surface* surface = (const Surface*) i->request_data;
302         surface->impl->draw(i->pos.x, i->pos.y, 255, i->drawing_effect);
303         break;
304       }
305       case SURFACE_PART:
306         draw_surface_part(*i);
307         break;
308       case GRADIENT:
309         draw_gradient(*i);
310         break;
311       case TEXT:
312         draw_text(*i);
313         break;
314       case FILLRECT:
315         draw_filled_rect(*i);
316         break;
317     }
318   }
319
320   // update screen
321   if(use_gl)
322     SDL_GL_SwapBuffers();
323   else
324     SDL_Flip(screen);
325
326   drawingrequests.clear();
327 }
328
329 void
330 DrawingContext::push_transform()
331 {
332   transformstack.push_back(transform);
333 }
334
335 void
336 DrawingContext::pop_transform()
337 {
338   assert(!transformstack.empty());
339
340   transform = transformstack.back();
341   transformstack.pop_back();
342 }
343