- Reworked Surface class and drawing stuff:
[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, int r, int g, int b, int a)
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   glColor4ub(r, g, b,a);
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
64 void fadeout(int fade_time)
65 {
66   float alpha_inc  = 256 / (fade_time / LOOP_DELAY);
67   float alpha = 256;
68
69   while(alpha > 0) {
70     alpha -= alpha_inc;
71     fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0,0,0, (int)alpha_inc);  // left side
72     
73     SDL_GL_SwapBuffers();
74     sound_manager->update();
75     
76     SDL_Delay(int(LOOP_DELAY));
77   }
78
79   fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0, 255);
80 }
81
82 void shrink_fade(const Vector& point, int fade_time)
83 {
84   float left_inc  = point.x / ((float)fade_time / LOOP_DELAY);
85   float right_inc = (SCREEN_WIDTH - point.x) / ((float)fade_time / LOOP_DELAY);
86   float up_inc    = point.y / ((float)fade_time / LOOP_DELAY);
87   float down_inc  = (SCREEN_HEIGHT - point.y) / ((float)fade_time / LOOP_DELAY);
88                                                                                 
89   float left_cor = 0, right_cor = 0, up_cor = 0, down_cor = 0;
90                                                                                 
91   while(left_cor < point.x && right_cor < SCREEN_WIDTH - point.x &&
92       up_cor < point.y && down_cor < SCREEN_HEIGHT - point.y) {
93     left_cor  += left_inc;
94     right_cor += right_inc;
95     up_cor    += up_inc;
96     down_cor  += down_inc;
97                                                                                 
98     fillrect(0, 0, left_cor, SCREEN_HEIGHT, 0,0,0);  // left side
99     fillrect(SCREEN_WIDTH - right_cor, 0, right_cor, SCREEN_HEIGHT, 0,0,0);  // right side
100     fillrect(0, 0, SCREEN_WIDTH, up_cor, 0,0,0);  // up side
101     fillrect(0, SCREEN_HEIGHT - down_cor, SCREEN_WIDTH, down_cor+1, 0,0,0);  // down side                                                                                
102
103     SDL_GL_SwapBuffers();
104   
105     sound_manager->update();
106     SDL_Delay(int(LOOP_DELAY));
107   }
108 }