/* Copyright (C) 2009 Facundo Domínguez This file is part of Spacejunk. Spacejunk is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Foobar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Foobar. If not, see . */ #include "fade.h" #include "graphic.h" #include "sge.h" bool Fade::step() { switch (mode) { case IN: if (fade_in(delta,Graphic::ticks()-inittime)) { light_fade_end(); mode=NONE; return false; } break; case OUT: if (fade_out(delta,Graphic::ticks()-inittime)) { dark_fade_end(); mode=NONE; return false; } break; default: return false; } return true; }; void Fade::setMode(FadeMode fm,int delta) { mode=fm; inittime=Graphic::ticks(); this->delta=delta; switch (mode) { case IN: dark_fade_end(); break; case OUT: case NONE: ; } }; void Fade::dark_fade_end() { SDL_Surface * screen=Graphic::getScreen(); if (screen->format->palette) { memset(mycolors, 0, sizeof(mycolors)); SDL_SetPalette(screen, SDL_PHYSPAL, mycolors, 0, screen->format->palette->ncolors); } else { fade(0); } } void Fade::light_fade_end() { SDL_Surface * screen=Graphic::getScreen(); if (screen->format->palette) { SDL_SetPalette(screen, SDL_PHYSPAL, screen->format->palette->colors, 0, screen->format->palette->ncolors); } } void Fade::fade(float f) { SDL_Surface * screen=Graphic::getScreen(); if (screen->format->palette) { memcpy(mycolors,screen->format->palette->colors, screen->format->palette->ncolors*sizeof(SDL_Color)); for (int i=0;iformat->palette->ncolors;i++) { mycolors[i].r=Uint8(mycolors[i].r*f); mycolors[i].g=Uint8(mycolors[i].g*f); mycolors[i].b=Uint8(mycolors[i].b*f); } SDL_SetPalette(screen,SDL_PHYSPAL,mycolors,0,screen->format->palette->ncolors); } else { sge_FilledRectAlpha(screen,0,0,screen->w-1,screen->h-1,SDL_MapRGB(screen->format,0,0,0), Uint16((1-f)*255)); } } bool Fade::fade_in(int max,int delta) { if (delta<=max) { fade(float(delta)/max); return false; } else return true; } bool Fade::fade_out(int max,int delta) { if (delta<=max) { fade(float(max-delta)/max); return false; } else return true; }