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