00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/display_effect.hpp"
00018
00019 #include "scripting/squirrel_util.hpp"
00020 #include "supertux/globals.hpp"
00021 #include "video/drawing_context.hpp"
00022
00023 static const float BORDER_SIZE = 75;
00024
00025 DisplayEffect::DisplayEffect(std::string name) :
00026 screen_fade(NO_FADE),
00027 screen_fadetime(0),
00028 screen_fading(0),
00029 border_fade(NO_FADE),
00030 border_fadetime(0),
00031 border_fading(),
00032 border_size(0),
00033 black(false),
00034 borders(false)
00035 {
00036 this->name = name;
00037 }
00038
00039 DisplayEffect::~DisplayEffect()
00040 {
00041 }
00042
00043 void
00044 DisplayEffect::expose(HSQUIRRELVM vm, SQInteger table_idx)
00045 {
00046 if (name.empty()) return;
00047 expose_object(vm, table_idx, dynamic_cast<scripting::DisplayEffect *>(this), name, false);
00048 }
00049
00050 void
00051 DisplayEffect::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00052 {
00053 if (name.empty()) return;
00054 scripting::unexpose_object(vm, table_idx, name);
00055 }
00056
00057 void
00058 DisplayEffect::update(float elapsed_time)
00059 {
00060 switch(screen_fade) {
00061 case NO_FADE:
00062 break;
00063 case FADE_IN:
00064 screen_fading -= elapsed_time;
00065 if(screen_fading < 0) {
00066 screen_fade = NO_FADE;
00067 }
00068 break;
00069 case FADE_OUT:
00070 screen_fading -= elapsed_time;
00071 if(screen_fading < 0) {
00072 screen_fade = NO_FADE;
00073 black = true;
00074 }
00075 break;
00076 default:
00077 assert(false);
00078 }
00079
00080 switch(border_fade) {
00081 case NO_FADE:
00082 break;
00083 case FADE_IN:
00084 border_fading -= elapsed_time;
00085 if(border_fading < 0) {
00086 border_fade = NO_FADE;
00087 }
00088 border_size = (border_fadetime - border_fading)
00089 / border_fadetime * BORDER_SIZE;
00090 break;
00091 case FADE_OUT:
00092 border_fading -= elapsed_time;
00093 if(border_fading < 0) {
00094 borders = false;
00095 border_fade = NO_FADE;
00096 }
00097 border_size = border_fading / border_fadetime * BORDER_SIZE;
00098 break;
00099 default:
00100 assert(false);
00101 }
00102 }
00103
00104 void
00105 DisplayEffect::draw(DrawingContext& context)
00106 {
00107 context.push_transform();
00108 context.set_translation(Vector(0, 0));
00109
00110 if(black || screen_fade != NO_FADE) {
00111 float alpha;
00112 if(black) {
00113 alpha = 1.0f;
00114 } else {
00115 switch(screen_fade) {
00116 case FADE_IN:
00117 alpha = screen_fading / screen_fadetime;
00118 break;
00119 case FADE_OUT:
00120 alpha = (screen_fadetime - screen_fading) / screen_fadetime;
00121 break;
00122 default:
00123 alpha = 0;
00124 assert(false);
00125 }
00126 }
00127 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
00128 Color(0, 0, 0, alpha), LAYER_GUI-10);
00129 }
00130
00131 if (borders) {
00132 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, border_size),
00133 Color(0, 0, 0, 1.0f), LAYER_GUI-10);
00134 context.draw_filled_rect(Vector(0, SCREEN_HEIGHT - border_size), Vector(SCREEN_WIDTH, border_size),
00135 Color(0, 0, 0, 1.0f), LAYER_GUI-10);
00136 }
00137
00138 context.pop_transform();
00139 }
00140
00141 void
00142 DisplayEffect::fade_out(float fadetime)
00143 {
00144 black = false;
00145 screen_fadetime = fadetime;
00146 screen_fading = fadetime;
00147 screen_fade = FADE_OUT;
00148 }
00149
00150 void
00151 DisplayEffect::fade_in(float fadetime)
00152 {
00153 black = false;
00154 this->screen_fadetime = fadetime;
00155 screen_fading = fadetime;
00156 screen_fade = FADE_IN;
00157 }
00158
00159 void
00160 DisplayEffect::set_black(bool enabled)
00161 {
00162 black = enabled;
00163 }
00164
00165 bool
00166 DisplayEffect::is_black()
00167 {
00168 return black;
00169 }
00170
00171 void
00172 DisplayEffect::sixteen_to_nine(float fadetime)
00173 {
00174 if(fadetime == 0) {
00175 borders = true;
00176 border_size = BORDER_SIZE;
00177 } else {
00178 borders = true;
00179 border_size = 0;
00180 border_fade = FADE_IN;
00181 border_fadetime = fadetime;
00182 border_fading = border_fadetime;
00183 }
00184 }
00185
00186 void
00187 DisplayEffect::four_to_three(float fadetime)
00188 {
00189 if(fadetime == 0) {
00190 borders = false;
00191 } else {
00192 border_size = BORDER_SIZE;
00193 border_fade = FADE_OUT;
00194 border_fadetime = fadetime;
00195 border_fading = border_fadetime;
00196 }
00197 }
00198
00199