Hardcoded stay-on-platform behaviour as follows: Mr. Bomb, Mr. Tree and the Totem...
[supertux.git] / src / video / screen.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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
20 #include <config.h>
21
22 #include <iostream>
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring>
26 #include <cerrno>
27 #include <assert.h>
28
29 #include <unistd.h>
30
31 #include <SDL.h>
32
33 #include "gameconfig.hpp"
34 #include "screen.hpp"
35 #include "main.hpp"
36 #include "video/drawing_context.hpp"
37 #include "audio/sound_manager.hpp"
38 #include "math/vector.hpp"
39
40 static const float LOOP_DELAY = 20.0;
41
42 void fillrect(float x, float y, float w, float h, const Color& col)
43 {
44   if(w < 0) {
45     x += w;
46     w = -w;
47   }
48   if(h < 0) {
49     y += h;
50     h = -h;
51   }
52
53   glColor4f(col.red, col.green, col.blue, col.alpha);
54
55   glDisable(GL_TEXTURE_2D);
56   glBegin(GL_POLYGON);
57   glVertex2f(x, y);
58   glVertex2f(x+w, y);
59   glVertex2f(x+w, y+h);
60   glVertex2f(x, y+h);
61   glEnd();
62   glEnable(GL_TEXTURE_2D);
63
64   glColor4f(0, 0, 0, 1);
65 }
66
67 void fadeout(float fade_time)
68 {
69   float alpha_inc  = LOOP_DELAY / fade_time;
70   Color c(0, 0, 0, alpha_inc);
71   float alpha = 1.0;
72
73   while(alpha >= 0) {
74     alpha -= alpha_inc;
75     fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, c);
76     // left side
77     
78     SDL_GL_SwapBuffers();
79     sound_manager->update();
80     
81     SDL_Delay(int(LOOP_DELAY));
82     alpha -= alpha_inc; 
83   }
84
85   fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, Color());
86 }
87
88 void shrink_fade(const Vector& point, float fade_time)
89 {
90   float left_inc  = point.x / (fade_time / LOOP_DELAY);
91   float right_inc = (SCREEN_WIDTH - point.x) / (fade_time / LOOP_DELAY);
92   float up_inc    = point.y / (fade_time / LOOP_DELAY);
93   float down_inc  = (SCREEN_HEIGHT - point.y) / (fade_time / LOOP_DELAY);
94                                                                                 
95   float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
96   Color c;
97                                                                                 
98   while(left_cor < point.x && right_cor < SCREEN_WIDTH - point.x &&
99       up_cor < point.y && down_cor < SCREEN_HEIGHT - point.y) {
100     left_cor  += left_inc;
101     right_cor += right_inc;
102     up_cor    += up_inc;
103     down_cor  += down_inc;
104                                                                                 
105     fillrect(0, 0, left_cor, SCREEN_HEIGHT, c);  // left side
106     fillrect(SCREEN_WIDTH - right_cor, 0, right_cor, SCREEN_HEIGHT, c);  // right side
107     fillrect(0, 0, SCREEN_WIDTH, up_cor, c);  // up side
108     fillrect(0, SCREEN_HEIGHT - down_cor, SCREEN_WIDTH, down_cor+1, c);  // down side
109
110     SDL_GL_SwapBuffers();
111   
112     sound_manager->update();
113     SDL_Delay(int(LOOP_DELAY));
114   }
115 }
116