- moved tilemanager into its own class
[supertux.git] / src / screen / screen.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
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
20 #include <iostream>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <cerrno>
25
26 #include <unistd.h>
27
28 #include "SDL.h"
29 #include "SDL_image.h"
30
31 #ifndef WIN32
32 #include <sys/types.h>
33 #include <ctype.h>
34 #endif
35
36 #include "defines.h"
37 #include "globals.h"
38 #include "screen.h"
39 #include "setup.h"
40 #include "drawing_context.h"
41 #include "type.h"
42
43 /* Needed for line calculations */
44 #define SGN(x) ((x)>0 ? 1 : ((x)==0 ? 0:(-1)))
45 #define ABS(x) ((x)>0 ? (x) : (-x))
46
47 /* --- FADE IN --- */
48
49 /** Fades the given surface into a black one. If fade_out is true, it will fade out, else
50 it will fade in */
51
52 #if 0
53 void fade(Surface *surface, int seconds, bool fade_out);
54
55 void fade(const std::string& surface, int seconds, bool fade_out)
56 {
57 Surface* sur = new Surface(datadir + surface, IGNORE_ALPHA);
58 fade(sur, seconds, fade_out);
59 delete sur;
60 }
61
62 void fade(Surface *surface, int seconds, bool fade_out)
63 {
64 float alpha;
65 if (fade_out)
66   alpha = 0;
67 else
68   alpha = 255;
69
70   int cur_time, old_time;
71   cur_time = SDL_GetTicks();
72
73   while(alpha >= 0 && alpha < 256)
74     {
75     surface->draw(0,0,(int)alpha);
76     flipscreen();
77
78     old_time = cur_time;
79     cur_time = SDL_GetTicks();
80
81     /* Calculate the next alpha value */
82     float calc = (float) ((cur_time - old_time) / seconds);
83     if(fade_out)
84       alpha += 255 * calc;
85     else
86       alpha -= 255 * calc;
87     }
88 }
89 #endif
90
91 /* 'Stolen' from the SDL documentation.
92  * Set the pixel at (x, y) to the given value
93  * NOTE: The surface must be locked before calling this!
94  */
95 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
96 {
97   int bpp = surface->format->BytesPerPixel;
98   /* Here p is the address to the pixel we want to set */
99   Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
100
101   switch(bpp)
102     {
103     case 1:
104       *p = pixel;
105       break;
106
107     case 2:
108       *(Uint16 *)p = pixel;
109       break;
110
111     case 3:
112       if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
113         {
114           p[0] = (pixel >> 16) & 0xff;
115           p[1] = (pixel >> 8) & 0xff;
116           p[2] = pixel & 0xff;
117         }
118       else
119         {
120           p[0] = pixel & 0xff;
121           p[1] = (pixel >> 8) & 0xff;
122           p[2] = (pixel >> 16) & 0xff;
123         }
124       break;
125
126     case 4:
127       *(Uint32 *)p = pixel;
128       break;
129     }
130 }
131
132 /* Draw a single pixel on the screen. */
133 void drawpixel(int x, int y, Uint32 pixel)
134 {
135   /* Lock the screen for direct access to the pixels */
136   if ( SDL_MUSTLOCK(screen) )
137     {
138       if ( SDL_LockSurface(screen) < 0 )
139         {
140           fprintf(stderr, "Can't lock screen: %s\n", SDL_GetError());
141           return;
142         }
143     }
144
145   if(!(x < 0 || y < 0 || x > screen->w || y > screen->h))
146     putpixel(screen, x, y, pixel);
147
148   if ( SDL_MUSTLOCK(screen) )
149     {
150       SDL_UnlockSurface(screen);
151     }
152   /* Update just the part of the display that we've changed */
153   SDL_UpdateRect(screen, x, y, 1, 1);
154 }
155
156 void drawline(int x1, int y1, int x2, int y2, int r, int g, int b, int a)
157 {
158 #ifndef NOOPENGL
159   if(use_gl)
160     {
161       glEnable(GL_BLEND);
162       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163       glColor4ub(r, g, b,a);
164
165       glBegin(GL_LINES);
166       glVertex2f(x1, y1);
167       glVertex2f(x2, y2);
168       glEnd();
169       glDisable(GL_BLEND);
170     }
171   else
172     {
173 #endif
174
175       /* Basic unantialiased Bresenham line algorithm */
176       int lg_delta, sh_delta, cycle, lg_step, sh_step;
177       Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
178
179       lg_delta = x2 - x1;
180       sh_delta = y2 - y1;
181       lg_step = SGN(lg_delta);
182       lg_delta = ABS(lg_delta);
183       sh_step = SGN(sh_delta);
184       sh_delta = ABS(sh_delta);
185       if (sh_delta < lg_delta)
186         {
187           cycle = lg_delta >> 1;
188           while (x1 != x2)
189             {
190               drawpixel(x1, y1, color);
191               cycle += sh_delta;
192               if (cycle > lg_delta)
193                 {
194                   cycle -= lg_delta;
195                   y1 += sh_step;
196                 }
197               x1 += lg_step;
198             }
199           drawpixel(x1, y1, color);
200         }
201       cycle = sh_delta >> 1;
202       while (y1 != y2)
203         {
204           drawpixel(x1, y1, color);
205           cycle += lg_delta;
206           if (cycle > sh_delta)
207             {
208               cycle -= sh_delta;
209               x1 += lg_step;
210             }
211           y1 += sh_step;
212         }
213       drawpixel(x1, y1, color);
214 #ifndef NOOPENGL
215
216     }
217 #endif
218 }
219
220 /* --- FILL A RECT --- */
221
222 void fillrect(float x, float y, float w, float h, int r, int g, int b, int a)
223 {
224 if(w < 0)
225         {
226         x += w;
227         w = -w;
228         }
229 if(h < 0)
230         {
231         y += h;
232         h = -h;
233         }
234
235 #ifndef NOOPENGL
236   if(use_gl)
237     {
238       glEnable(GL_BLEND);
239       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
240       glColor4ub(r, g, b,a);
241
242       glBegin(GL_POLYGON);
243       glVertex2f(x, y);
244       glVertex2f(x+w, y);
245       glVertex2f(x+w, y+h);
246       glVertex2f(x, y+h);
247       glEnd();
248       glDisable(GL_BLEND);
249     }
250   else
251     {
252 #endif
253       SDL_Rect src, rect;
254       SDL_Surface *temp = NULL;
255
256       rect.x = (int)x;
257       rect.y = (int)y;
258       rect.w = (int)w;
259       rect.h = (int)h;
260
261       if(a != 255)
262         {
263           temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
264                                       screen->format->Rmask,
265                                       screen->format->Gmask,
266                                       screen->format->Bmask,
267                                       screen->format->Amask);
268
269
270           src.x = 0;
271           src.y = 0;
272           src.w = rect.w;
273           src.h = rect.h;
274
275           SDL_FillRect(temp, &src, SDL_MapRGB(screen->format, r, g, b));
276
277           SDL_SetAlpha(temp, SDL_SRCALPHA, a);
278
279           SDL_BlitSurface(temp,0,screen,&rect);
280
281           SDL_FreeSurface(temp);
282         }
283       else
284         SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, r, g, b));
285
286 #ifndef NOOPENGL
287
288     }
289 #endif
290 }
291
292
293 #define LOOP_DELAY 20.0
294
295 void fadeout(int fade_time)
296 {
297   float alpha_inc  = 256 / (fade_time / LOOP_DELAY);
298   float alpha = 256;
299
300   while(alpha > 0)
301     {
302     alpha -= alpha_inc;
303     fillrect(0, 0, screen->w, screen->h, 0,0,0, (int)alpha_inc);  // left side
304                                                    
305     DrawingContext context; // ugly...
306     context.do_drawing();
307
308     SDL_Delay(int(LOOP_DELAY));
309     }
310
311   fillrect(0, 0, screen->w, screen->h, 0, 0, 0, 255);
312   
313   DrawingContext context;
314   context.draw_text_center(white_text, "Loading...",
315       Vector(0, screen->h/2), LAYER_FOREGROUND1);
316   context.do_drawing();
317 }
318
319 void shrink_fade(const Vector& point, int fade_time)
320 {
321   float left_inc  = point.x / ((float)fade_time / LOOP_DELAY);
322   float right_inc = (screen->w - point.x) / ((float)fade_time / LOOP_DELAY);
323   float up_inc    = point.y / ((float)fade_time / LOOP_DELAY);
324   float down_inc  = (screen->h - point.y) / ((float)fade_time / LOOP_DELAY);
325                                                                                 
326   float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
327                                                                                 
328   while(left_cor < point.x && right_cor < screen->w - point.x &&
329       up_cor < point.y && down_cor < screen->h - point.y)
330   {
331     left_cor  += left_inc;
332     right_cor += right_inc;
333     up_cor    += up_inc;
334     down_cor  += down_inc;
335                                                                                 
336     fillrect(0, 0, left_cor, screen->h, 0,0,0);  // left side
337     fillrect(screen->w - right_cor, 0, right_cor, screen->h, 0,0,0);  // right side
338     fillrect(0, 0, screen->w, up_cor, 0,0,0);  // up side
339     fillrect(0, screen->h - down_cor, screen->w, down_cor+1, 0,0,0);  // down side                                                                                
340     DrawingContext context; // ugly...
341     context.do_drawing();
342     
343     SDL_Delay(int(LOOP_DELAY));
344   }
345 }
346