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