From d1c262e4287b42191aff3b59311d449760fd70a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rosen?= Date: Sat, 16 May 2009 17:50:19 +0000 Subject: [PATCH] commit all my pending perf improvements, ...there is too much stuff lying around I need to save all that --- src/unit.cpp | 30 ------------------ src/unit.hpp | 22 +++++++++++-- src/unit_animation.cpp | 71 +++++++++++++++++++++++++++++++++--------- src/unit_animation.hpp | 3 ++ 4 files changed, 79 insertions(+), 47 deletions(-) diff --git a/src/unit.cpp b/src/unit.cpp index 1aeafb676c3..c614c1e7bde 100644 --- a/src/unit.cpp +++ b/src/unit.cpp @@ -2076,29 +2076,15 @@ bool unit::invalidate(const map_location &loc) const gamemap & map = disp->get_map(); const t_translation::t_terrain terrain = map.get_terrain(loc); const terrain_type& terrain_info = map.get_terrain_info(terrain); - if(!params.submerge) params.submerge= is_flying() ? 0.0 : terrain_info.unit_submerge(); - if(invisible(loc,disp->get_units(),disp->get_teams()) && - params.highlight_ratio > 0.5) { - params.highlight_ratio = 0.5; - } - if(loc == disp->selected_hex() && params.highlight_ratio == 1.0) { - params.highlight_ratio = 1.5; - } int height_adjust = static_cast(terrain_info.unit_height_adjust() * disp->get_zoom_factor()); if (is_flying() && height_adjust < 0) { height_adjust = 0; } params.y -= height_adjust; params.halo_y -= height_adjust; - if (get_state(STATE_POISONED) ){ - params.blend_with = disp->rgb(0,255,0); - params.blend_ratio = 0.25; - } params.image_mod = image_mods(); - //get_animation()->update_last_draw_time(); - frame_parameters adjusted_params= get_animation()->get_current_params(params); result |= get_animation()->invalidate(params); } @@ -3020,22 +3006,6 @@ unit& unit::clone(bool is_temporary) return *this; } -void unit::refresh(const game_display &disp,const map_location &loc) -{ - if (state_ == STATE_FORGET && anim_ && anim_->animation_finished_potential()) { - set_standing(loc); - return; - } - if (state_ != STATE_STANDING || get_current_animation_tick() < next_idling_ - || incapacitated()) - return; - if (get_current_animation_tick() > next_idling_ + 1000) { - // prevent all units animating at the same time - set_standing(loc); - } else { - set_idling(disp, loc); - } -} unit_movement_resetter::unit_movement_resetter(unit &u, bool operate) : u_(u), moves_(u.movement_) diff --git a/src/unit.hpp b/src/unit.hpp index 7018dc5aa77..4d8f36069d6 100644 --- a/src/unit.hpp +++ b/src/unit.hpp @@ -26,8 +26,8 @@ #include "unit_types.hpp" #include "unit_map.hpp" #include "variable.hpp" +#include "game_display.hpp" -class game_display; class gamestatus; class game_state; class config_writer; @@ -145,7 +145,25 @@ public: void end_turn(); void new_scenario(); /** Called on every draw */ - void refresh(const game_display& disp,const map_location& loc); + void refresh(const game_display& disp,const map_location& loc) { + if (state_ != STATE_STANDING || get_current_animation_tick() < next_idling_ || incapacitated()) + return; + if (state_ == STATE_FORGET && anim_ && anim_->animation_finished_potential()) { + set_standing(loc); + return; + } + if (get_current_animation_tick() > next_idling_ + 1000) { + // prevent all units animating at the same time + if (disp.idle_anim()) { + next_idling_ = get_current_animation_tick() + + static_cast((20000 + rand() % 20000) * disp.idle_anim_rate()); + } else { + next_idling_ = INT_MAX; + } + } else { + set_idling(disp, loc); + } + } bool take_hit(int damage) { hit_points_ -= damage; return hit_points_ <= 0; } void heal(int amount); diff --git a/src/unit_animation.cpp b/src/unit_animation.cpp index fca8be7b8f9..7889f9b22bb 100644 --- a/src/unit_animation.cpp +++ b/src/unit_animation.cpp @@ -130,6 +130,7 @@ unit_animation::unit_animation(int start_time, src_(), dst_(), invalidated_(false), + play_offscreen_(true), overlaped_hex_() { add_frame(frame.duration(),frame,!frame.does_not_change()); @@ -151,7 +152,10 @@ unit_animation::unit_animation(const config& cfg,const std::string frame_string sub_anims_(), unit_anim_(cfg,frame_string), src_(), - dst_() + dst_(), + invalidated_(false), + play_offscreen_(true), + overlaped_hex_() { // if(!cfg["debug"].empty()) printf("DEBUG WML: FINAL\n%s\n\n",cfg.debug().c_str()); foreach (const config::any_child &fr, cfg.all_children_range()) @@ -207,6 +211,7 @@ unit_animation::unit_animation(const config& cfg,const std::string frame_string foreach (const config &filter, cfg.child_range("filter_second_attack")) { secondary_attack_filter_.push_back(filter); } + play_offscreen_=utils::string_bool(cfg["offscreen"],true); } @@ -333,6 +338,7 @@ void unit_animation::fill_initial_animations( std::vector & anim tmp_anim = *itor; tmp_anim.event_ = utils::split("standing"); + tmp_anim.play_offscreen_ = false; animations.push_back(tmp_anim); tmp_anim = *itor; @@ -477,6 +483,7 @@ void unit_animation::add_anims( std::vector & animations, const foreach (config &anim, expanded_cfg.child_range("standing_anim")) { anim["apply_to"] = "standing,default"; + if (anim["offscreen"].empty()) anim["offscreen"] = "no"; if (anim["layer"].empty()) anim["layer"] = default_layer; animations.push_back(unit_animation(anim)); } @@ -485,6 +492,7 @@ void unit_animation::add_anims( std::vector & animations, const foreach (config &anim, expanded_cfg.child_range("idle_anim")) { anim["apply_to"] = "idling"; + if (anim["offscreen"].empty()) anim["offscreen"] = "no"; if (anim["layer"].empty()) anim["layer"] = default_layer; animations.push_back(unit_animation(anim)); } @@ -680,6 +688,14 @@ bool unit_animation::particule::need_update() const return false; } +bool unit_animation::particule::need_minimal_update() const +{ + if(get_current_frame_begin_time() != last_frame_begin_time_ ) { + return true; + } + return false; +} + unit_animation::particule::particule( const config& cfg, const std::string frame_string ) : animated(), @@ -719,6 +735,19 @@ bool unit_animation::need_update() const return false; } +bool unit_animation::need_minimal_update() const +{ + if(!play_offscreen_) { + return false; + } + if(unit_anim_.need_minimal_update()) return true; + std::map::const_iterator anim_itor =sub_anims_.begin(); + for( /*null*/; anim_itor != sub_anims_.end() ; anim_itor++) { + if(anim_itor->second.need_minimal_update()) return true; + } + return false; +} + bool unit_animation::animation_finished() const { if(!unit_anim_.animation_finished()) return false; @@ -826,24 +855,33 @@ bool unit_animation::invalidate(const frame_parameters& value) { if(invalidated_) return false; game_display*disp = game_display::get_singleton(); + bool complete_redraw =disp->tile_nearly_on_screen(src_) || disp->tile_nearly_on_screen(dst_); if(overlaped_hex_.empty()) { - std::map::iterator anim_itor =sub_anims_.begin(); - overlaped_hex_ = unit_anim_.get_overlaped_hex(value,src_,dst_,true); - for( /*null*/; anim_itor != sub_anims_.end() ; anim_itor++) { - std::set tmp = anim_itor->second.get_overlaped_hex(value,src_,dst_,true); - overlaped_hex_.insert(tmp.begin(),tmp.end()); + if(complete_redraw) { + std::map::iterator anim_itor =sub_anims_.begin(); + overlaped_hex_ = unit_anim_.get_overlaped_hex(value,src_,dst_,true); + for( /*null*/; anim_itor != sub_anims_.end() ; anim_itor++) { + std::set tmp = anim_itor->second.get_overlaped_hex(value,src_,dst_,true); + overlaped_hex_.insert(tmp.begin(),tmp.end()); + } + } else { + // off screen animations only invalidate their own hex, no propagation, + // but we stil need this to play sounds + overlaped_hex_.insert(src_); } + } - if(need_update() ) { - disp->invalidate(overlaped_hex_); - invalidated_ = true; - return true; + if(complete_redraw) { + if( need_update()) { + disp->invalidate(overlaped_hex_); + invalidated_ = true; + return true; + } else { + invalidated_ = disp->propagate_invalidation(overlaped_hex_); + return invalidated_; + } } else { - std::vector intersection; - set_intersection(overlaped_hex_.begin(),overlaped_hex_.end(), - disp->get_invalidated().begin(),disp->get_invalidated().end(), - std::back_inserter(intersection)); - if(!intersection.empty()) { + if(need_minimal_update()) { disp->invalidate(overlaped_hex_); invalidated_ = true; return true; @@ -852,6 +890,9 @@ bool unit_animation::invalidate(const frame_parameters& value) } } } + + + void unit_animation::particule::redraw(const frame_parameters& value,const map_location &src, const map_location &dst, const bool primary) { const unit_frame& current_frame= get_current_frame(); diff --git a/src/unit_animation.hpp b/src/unit_animation.hpp index b5939f5f324..418ff9e2680 100644 --- a/src/unit_animation.hpp +++ b/src/unit_animation.hpp @@ -47,6 +47,7 @@ class unit_animation void add_frame(int duration, const unit_frame& value,bool force_change =false){ unit_anim_.add_frame(duration,value,force_change) ; }; bool need_update() const; + bool need_minimal_update() const; bool animation_finished() const; bool animation_finished_potential() const; void update_last_draw_time(); @@ -84,6 +85,7 @@ class unit_animation explicit particule(const config& cfg,const std::string frame_string ="frame"); virtual ~particule(); bool need_update() const; + bool need_minimal_update() const; void override(int start_time,int duration, const std::string highlight="", const std::string blend_ratio ="",Uint32 blend_color = 0,const std::string offset="",const std::string layer=""); void redraw( const frame_parameters& value,const map_location &src, const map_location &dst, const bool primary=false); std::set get_overlaped_hex(const frame_parameters& value,const map_location &src, const map_location &dst, const bool primary = false); @@ -117,6 +119,7 @@ class unit_animation map_location dst_; // optimisation bool invalidated_; + bool play_offscreen_; std::set overlaped_hex_; };