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