1 /*
2   Copyright (C) 2009 Facundo Domínguez
3 
4   This file is part of Spacejunk.
5 
6   Spacejunk is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "fade.h"
21 #include "graphic.h"
22 #include "sge.h"
23 
24 
step()25 bool Fade::step() {
26     switch (mode) {
27     case IN:
28         if (fade_in(delta,Graphic::ticks()-inittime)) {
29             light_fade_end();
30             mode=NONE;
31             return false;
32         }
33         break;
34     case OUT:
35         if (fade_out(delta,Graphic::ticks()-inittime)) {
36             dark_fade_end();
37             mode=NONE;
38             return false;
39         }
40         break;
41     default:
42         return false;
43     }
44     return true;
45 };
46 
setMode(FadeMode fm,int delta)47 void Fade::setMode(FadeMode fm,int delta) {
48     mode=fm;
49     inittime=Graphic::ticks();
50     this->delta=delta;
51     switch (mode) {
52     case IN:
53         dark_fade_end();
54         break;
55     case OUT:
56     case NONE:
57         ;
58     }
59 };
60 
dark_fade_end()61 void Fade::dark_fade_end() {
62     SDL_Surface * screen=Graphic::getScreen();
63     if (screen->format->palette) {
64         memset(mycolors, 0, sizeof(mycolors));
65         SDL_SetPalette(screen, SDL_PHYSPAL, mycolors, 0, screen->format->palette->ncolors);
66     } else {
67         fade(0);
68     }
69 }
70 
light_fade_end()71 void Fade::light_fade_end() {
72     SDL_Surface * screen=Graphic::getScreen();
73     if (screen->format->palette) {
74         SDL_SetPalette(screen, SDL_PHYSPAL, screen->format->palette->colors, 0, screen->format->palette->ncolors);
75     }
76 }
77 
fade(float f)78 void Fade::fade(float f) {
79     SDL_Surface * screen=Graphic::getScreen();
80 
81     if (screen->format->palette) {
82         memcpy(mycolors,screen->format->palette->colors, screen->format->palette->ncolors*sizeof(SDL_Color));
83         for (int i=0;i<screen->format->palette->ncolors;i++) {
84             mycolors[i].r=Uint8(mycolors[i].r*f);
85             mycolors[i].g=Uint8(mycolors[i].g*f);
86             mycolors[i].b=Uint8(mycolors[i].b*f);
87         }
88         SDL_SetPalette(screen,SDL_PHYSPAL,mycolors,0,screen->format->palette->ncolors);
89     } else {
90         sge_FilledRectAlpha(screen,0,0,screen->w-1,screen->h-1,SDL_MapRGB(screen->format,0,0,0), Uint16((1-f)*255));
91     }
92 }
93 
fade_in(int max,int delta)94 bool Fade::fade_in(int max,int delta) {
95     if (delta<=max) {
96         fade(float(delta)/max);
97         return false;
98     } else return true;
99 }
100 
101 
fade_out(int max,int delta)102 bool Fade::fade_out(int max,int delta) {
103     if (delta<=max) {
104         fade(float(max-delta)/max);
105         return false;
106     } else return true;
107 }
108 
109