Separate duration from fadeout in the floating label system

This commit is contained in:
Celtic Minstrel 2021-03-26 08:42:35 -04:00
parent c81c77aecc
commit 1141e97620
2 changed files with 19 additions and 11 deletions

View file

@ -48,7 +48,7 @@ floating_label::floating_label(const std::string& text, const surface& surf)
, buf_(nullptr)
, buf_pos_()
#endif
, fadeout_(true)
, fadeout_(0)
, time_start_(0)
, text_(text)
, font_size_(SIZE_SMALL)
@ -194,9 +194,10 @@ void floating_label::draw(int time, surface screen)
sdl_blit(get_surface(time), nullptr, screen, &rect);
}
void floating_label::set_lifetime(int lifetime)
void floating_label::set_lifetime(int lifetime, int fadeout)
{
lifetime_ = lifetime;
fadeout_ = fadeout;
time_start_ = SDL_GetTicks();
}
@ -212,11 +213,13 @@ SDL_Point floating_label::get_loc(int time)
surface floating_label::get_surface(int time)
{
if(fadeout_ && lifetime_ >= 0 && surf_ != nullptr) {
// fade out moving floating labels
if(lifetime_ >= 0 && fadeout_ > 0) {
int time_alive = get_time_alive(time);
int alpha_add = -255 * time_alive / lifetime_;
return adjust_surface_alpha_add(surf_, alpha_add);
if(time_alive >= lifetime_ && surf_ != nullptr) {
// fade out moving floating labels
int alpha_add = -255 * (time_alive - lifetime_) / fadeout_;
return adjust_surface_alpha_add(surf_, alpha_add);
}
}
return surf_;
}
@ -261,10 +264,14 @@ void scroll_floating_labels(double xmove, double ymove)
}
}
void remove_floating_label(int handle)
void remove_floating_label(int handle, int fadeout)
{
const label_map::iterator i = labels.find(handle);
if(i != labels.end()) {
if(fadeout > 0) {
i->second.set_lifetime(0, fadeout);
return;
}
labels.erase(i);
}

View file

@ -52,7 +52,7 @@ public:
ymove_ = ymove;
}
// set the number of frames to display the text for, or -1 to display until removed
void set_lifetime(int lifetime);
void set_lifetime(int lifetime, int fadeout = 100);
void set_color(const color_t& color) {color_ = color;}
void set_bg_color(const color_t& bg_color) {
bgcolor_ = bg_color;
@ -73,7 +73,7 @@ public:
surface create_surface();
bool expired(int time) const { return lifetime_ >= 0 && get_time_alive(time) > lifetime_; }
bool expired(int time) const { return lifetime_ >= 0 && get_time_alive(time) > lifetime_ + fadeout_; }
void show(const bool value) { visible_ = value; }
@ -87,7 +87,7 @@ private:
surface get_surface(int time);
surface surf_, buf_;
SDL_Rect buf_pos_;
bool fadeout_;
int fadeout_;
int time_start_;
std::string text_;
int font_size_;
@ -119,7 +119,8 @@ void move_floating_label(int handle, double xmove, double ymove);
void scroll_floating_labels(double xmove, double ymove);
/** removes the floating label given by 'handle' from the screen */
void remove_floating_label(int handle);
/** if fadeout is given, the label fades out over that duration */
void remove_floating_label(int handle, int fadeout = 0);
/** hides or shows a floating label */
void show_floating_label(int handle, bool show);