fade.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software 
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00010 
00011 #include <cassert>
00012 #include <cstdarg>
00013 #include <math.h>
00014 #include "utils.h"
00015 #include "fade.h"
00016 #include "video.h"
00017 #include "gui.h"
00018 
00019 using namespace std;
00020 using namespace hoa_video::private_video;
00021 
00022 namespace hoa_video {
00023 
00024 namespace private_video {
00025 
00026 ScreenFader::ScreenFader()
00027   : current_color(0.0f, 0.0f, 0.0f, 0.0f),
00028   is_fading(false)
00029 {
00030   current_time = 0;
00031   end_time = 0;
00032   fade_modulation = 1.0f;
00033   use_fade_overlay = false;
00034 }
00035 
00036 //-----------------------------------------------------------------------------
00037 // FadeTo: Begins a fade to the given color in num_seconds
00038 //         returns false if invalid parameter is passed
00039 //-----------------------------------------------------------------------------
00040 void ScreenFader::FadeTo(const Color &final, float num_seconds)
00041 {
00042   if (num_seconds <= 0.0f)
00043     end_time = 0;
00044   else
00045     end_time = static_cast<int32>(num_seconds * 1000); // Convert seconds to milliseconds
00046 
00047   initial_color = current_color;
00048   final_color   = final;
00049   current_time = 0;
00050 
00051   
00052   is_fading = true;
00053   
00054   // figure out if this is a simple fade or if an overlay is required
00055   // A simple fade is defined as a fade from clear to black, from black
00056   // to clear, or from somewhere between clear and black to either clear
00057   // or black. More simply, it's a fade where both the initial and final
00058   // color's RGB values are zeroed out
00059 
00060   use_fade_overlay = true;  
00061 
00062   if( (initial_color[0] == 0.0f &&
00063        initial_color[1] == 0.0f &&
00064        initial_color[2] == 0.0f &&
00065        final_color[0]   == 0.0f &&
00066        final_color[1]   == 0.0f &&
00067        final_color[2]   == 0.0f))
00068   {
00069     use_fade_overlay = false;
00070   }
00071   else
00072   {
00073     fade_modulation = 1.0f;
00074   }
00075   
00076   Update(0);  // do initial update
00077 }
00078 
00079 
00080 
00081 //-----------------------------------------------------------------------------
00082 // Update: updates screen fader- figures out new interpolated fade color,
00083 //         whether to fade using overlays or modulation, etc.
00084 //-----------------------------------------------------------------------------
00085 void ScreenFader::Update(int32 t)
00086 {
00087   if(!is_fading)
00088     return;
00089         
00090   if(current_time >= end_time)
00091   {
00092     current_color = final_color;
00093     is_fading     = false;
00094     
00095     if(use_fade_overlay)
00096     {
00097       // check if we have faded to black or clear. If so, we can use modulation
00098       if(final_color[3] == 0.0f ||
00099         (final_color[0] == 0.0f &&
00100          final_color[1] == 0.0f &&
00101          final_color[2] == 0.0f))
00102       {
00103         use_fade_overlay = false;
00104         fade_modulation = 1.0f - final_color[3];
00105       }
00106     }
00107     else
00108       fade_modulation = 1.0f - final_color[3];
00109   }
00110   else
00111   {
00112     // calculate the new interpolated color
00113     float a = (float)current_time / (float)end_time;
00114 
00115     current_color[3] = Lerp(a, initial_color[3], final_color[3]);
00116 
00117     // if we are fading to or from clear, then only the alpha should get
00118     // interpolated.
00119     if(final_color[3] == 0.0f)
00120     {
00121       current_color[0] = initial_color[0];
00122       current_color[1] = initial_color[1];
00123       current_color[2] = initial_color[2];
00124     }
00125     if(initial_color[3] == 0.0f)
00126     {
00127       current_color[0] = final_color[0];
00128       current_color[1] = final_color[1];
00129       current_color[2] = final_color[2];
00130     }
00131     else
00132     {
00133       current_color[0] = Lerp(a, initial_color[0], final_color[0]);
00134       current_color[1] = Lerp(a, initial_color[1], final_color[1]);
00135       current_color[2] = Lerp(a, initial_color[2], final_color[2]);
00136     }
00137     
00138     if(!use_fade_overlay)
00139       fade_modulation = 1.0f - current_color[3];
00140     else
00141       fade_overlay_color = current_color;
00142   }
00143 
00144   current_time += t;
00145 } // void FadeScreen::Update(int32 t)
00146 
00147 } // namespace private_video
00148 
00149 //-----------------------------------------------------------------------------
00150 // FadeScreen: sets up a fade to the given color over "fade_time" number of seconds
00151 //-----------------------------------------------------------------------------
00152 void GameVideo::FadeScreen(const Color &color, float fade_time)
00153 {
00154   _fader.FadeTo(color, fade_time);
00155 }
00156 
00157 
00158 
00159 //-----------------------------------------------------------------------------
00160 // IsFading: returns true if screen is in the middle of a fade
00161 //-----------------------------------------------------------------------------
00162 bool GameVideo::IsFading()
00163 {
00164   return _fader.IsFading();
00165 }
00166 
00167 }  // namespace hoa_video

Generated on Fri Jul 6 23:11:13 2007 for Hero of Allacrost by  doxygen 1.5.1