Remove the template code from animated.hpp and put it into animated.cpp.

This code was indirectly included in at least 25 files: the 25 files
for which a dependency on string_utils.hpp has been added by a
consequence of its removal from animated.hpp. It should induce a small
compile time improvement.
This commit is contained in:
Guillaume Melquiond 2005-02-21 09:05:51 +00:00
parent 3fef2eb121
commit fa1f3f6444
28 changed files with 294 additions and 259 deletions

View file

@ -33,6 +33,7 @@ wesnoth_SOURCES = about.cpp \
ai.cpp \
ai_attack.cpp \
ai_move.cpp \
animated.cpp \
astarnode.cpp \
builder.cpp \
cavegen.cpp \
@ -214,6 +215,7 @@ wesnoth_editor_SOURCES = editor/editor.cpp \
editor/editor_undo.cpp \
about.cpp \
actions.cpp \
animated.cpp \
astarnode.cpp \
builder.cpp \
cavegen.cpp \
@ -289,6 +291,7 @@ wesnoth_editor_SOURCES = editor/editor.cpp \
ai.hpp \
ai_attack.hpp \
ai_move.hpp \
animated.hpp \
array.hpp \
astarnode.hpp \
builder.hpp \

262
src/animated.cpp Normal file
View file

@ -0,0 +1,262 @@
/* $Id$ */
/*
Copyright (C) 2004 by Philippe Plantier <ayin@anathas.org>
Copyright (C) 2005 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
//#include <string>
//#include <vector>
#include "SDL.h"
#include "animated.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"
template<typename T, typename T_void_value>
const T animated<T,T_void_value>::void_value_ = T_void_value()();
template<typename T, typename T_void_value>
animated<T,T_void_value>::animated() :
starting_frame_time_(INT_MAX),
ending_frame_time_(INT_MIN),
started_(false),
no_current_frame_(true),
does_not_change_(false),
real_start_ticks_(0),
start_ticks_(0),
acceleration_(1)
{}
template<typename T, typename T_void_value>
animated<T,T_void_value>::animated(const std::string &cfg, const string_initializer& init):
starting_frame_time_(INT_MAX),
started_(false),
no_current_frame_(true),
does_not_change_(false),
real_start_ticks_(0),
start_ticks_(0),
acceleration_(1)
{
std::vector<std::string> items = utils::split(cfg);
int current_time = 0;
std::vector<std::string>::const_iterator itor = items.begin();
for(; itor != items.end(); ++itor) {
const std::vector<std::string>& items = utils::split(*itor, ':');
std::string str;
int time;
if(items.size() > 1) {
str = items.front();
time = atoi(items.back().c_str());
} else {
str = *itor;
time = 100;
}
frames_.push_back(frame(current_time, init(str)));
current_time += time;
}
starting_frame_time_ = 0;
ending_frame_time_ = current_time;
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::add_frame(int start)
{
frames_.push_back(frame(start));
starting_frame_time_ = minimum<int>(starting_frame_time_, start);
ending_frame_time_ = maximum<int>(ending_frame_time_, start);
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::add_frame(int start, const T& value)
{
frames_.push_back(frame(start, value));
starting_frame_time_ = minimum<int>(starting_frame_time_, start);
ending_frame_time_ = maximum<int>(ending_frame_time_, start);
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::start_animation(int start_frame, int cycles, int acceleration)
{
started_ = true;
start_frame_ = start_frame;
start_ticks_ = real_start_ticks_ = current_time_ = SDL_GetTicks() * acceleration;
cycles_ = cycles;
current_cycle_ = 0;
acceleration_ = acceleration;
// current_frame_ = frames_.begin();
current_frame_ = 0;
if (ending_frame_time_ >= start_frame_) {
duration_ = ending_frame_time_ - start_frame_;
} else {
duration_ = 0;
}
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::update_current_frame_internal()
{
// std::cerr << "--- updating frame ---\n";
if(does_not_change_)
return;
frame_changed_ = false;
// Always update current_time_, for the animation_time functions to work.
current_time_ = SDL_GetTicks() * acceleration_;
if(!started_)
return;
if(frames_.empty()) {
no_current_frame_ = true;
does_not_change_ = true;
return;
}
if(frames_.size() == 1 && cycles_ == INFINITE_CYCLES) {
does_not_change_ = true;
frame_changed_ = false;
}
if (duration_ > 0) {
// Ticks is the actual time since animation started.
int ticks = current_time_ - start_ticks_;
// Handles cycle overflow
if(ticks > duration_) {
int ncycles = ticks/duration_;
current_cycle_ = minimum<int>(cycles_, current_cycle_ + ncycles);
start_ticks_ += ncycles * duration_;
ticks -= ncycles * duration_;
// current_frame_ = frames_.begin();
current_frame_ = 0;
frame_changed_ = true;
}
// Checks if the animation is finished
if(cycles_ != INFINITE_CYCLES && current_cycle_ >= cycles_) {
// If the animation is finished, the current frame is the last
// one
current_frame_ = frames_.size() - 1;
frame_changed_ = true;
// std::cerr << "Animation finished\n";
no_current_frame_ = false;
started_ = false;
return;
}
if(ticks < (frames_[current_frame_].milliseconds - start_frame_)) {
// std::cerr << "Animation not yet started\n";
frame_changed_ = true;
no_current_frame_ = true;
return;
}
// Looks for the current frame
typename std::vector<frame>::size_type i = current_frame_ + 1;
for(; i != frames_.size(); ++i) {
if(ticks < (frames_[i].milliseconds - start_frame_))
break;
current_frame_ = i;
frame_changed_ = true;
// std::cerr << "Skipping to next frame\n";
}
no_current_frame_ = false;
} else {
// If the duration is void, the animation is automatically finished.
// current_cycle_ = cycles_;
if(cycles_ != -1)
started_ = false;
does_not_change_ = true;
frame_changed_ = false;
// current_frame_ = frames_.size() - 1;
// frame_changed_ = false;
// std::cerr << "Returning last frame\n";
no_current_frame_ = false;
}
}
template<typename T, typename T_void_value>
bool animated<T,T_void_value>::frame_changed() const
{
return frame_changed_;
}
template<typename T, typename T_void_value>
bool animated<T,T_void_value>::animation_finished() const
{
if(!started_)
return true;
//if(current_cycle_ == cycles_)
// return true;
return false;
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_animation_time() const
{
if(does_not_change_)
return SDL_GetTicks() * acceleration_ - real_start_ticks_ + start_frame_;
return current_time_ - real_start_ticks_ + start_frame_;
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_frame_time() const
{
return current_time_ - start_ticks_ + start_frame_;
}
template<typename T, typename T_void_value>
const T& animated<T,T_void_value>::get_current_frame() const
{
if(no_current_frame_ == true)
return void_value_;
const frame& cur = frames_[current_frame_];
if(!cur.has_value)
return void_value_;
return cur.value;
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_first_frame_time() const
{
if (starting_frame_time_ != INT_MAX && starting_frame_time_ != INT_MIN)
return starting_frame_time_;
return 0;
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_last_frame_time() const
{
if (ending_frame_time_ != INT_MAX && ending_frame_time_ != INT_MIN)
return ending_frame_time_;
return 0;
}
// Force compilation of the following template instantiations
#include "image.hpp"
#include "unit_types.hpp"
template class animated< image::locator >;
template class animated< std::string >;
template class animated< unit_animation::frame >;

View file

@ -16,9 +16,6 @@
#include <string>
#include <vector>
#include "SDL.h"
#include "util.hpp"
#include "serialization/string_utils.hpp"
template<typename T>
class void_value
@ -69,8 +66,7 @@ public:
bool animation_finished() const;
int get_animation_time() const;
int get_frame_time() const;
inline const T& get_current_frame() const;
const T& get_base_frame() const;
const T& get_current_frame() const;
private:
struct frame
@ -113,249 +109,5 @@ private:
std::vector<frame> frames_;
};
template<typename T, typename T_void_value>
const T animated<T,T_void_value>::void_value_ = T_void_value()();
template<typename T, typename T_void_value>
animated<T,T_void_value>::animated() :
starting_frame_time_(INT_MAX),
ending_frame_time_(INT_MIN),
started_(false),
no_current_frame_(true),
does_not_change_(false),
real_start_ticks_(0),
start_ticks_(0),
acceleration_(1)
{}
template<typename T, typename T_void_value>
animated<T,T_void_value>::animated(const std::string &cfg, const string_initializer& init):
starting_frame_time_(INT_MAX),
started_(false),
no_current_frame_(true),
does_not_change_(false),
real_start_ticks_(0),
start_ticks_(0),
acceleration_(1)
{
std::vector<std::string> items = utils::split(cfg);
int current_time = 0;
std::vector<std::string>::const_iterator itor = items.begin();
for(; itor != items.end(); ++itor) {
const std::vector<std::string>& items = utils::split(*itor, ':');
std::string str;
int time;
if(items.size() > 1) {
str = items.front();
time = atoi(items.back().c_str());
} else {
str = *itor;
time = 100;
}
frames_.push_back(frame(current_time, init(str)));
current_time += time;
}
starting_frame_time_ = 0;
ending_frame_time_ = current_time;
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::add_frame(int start)
{
frames_.push_back(frame(start));
starting_frame_time_ = minimum<int>(starting_frame_time_, start);
ending_frame_time_ = maximum<int>(ending_frame_time_, start);
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::add_frame(int start, const T& value)
{
frames_.push_back(frame(start, value));
starting_frame_time_ = minimum<int>(starting_frame_time_, start);
ending_frame_time_ = maximum<int>(ending_frame_time_, start);
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::start_animation(int start_frame, int cycles, int acceleration)
{
started_ = true;
start_frame_ = start_frame;
start_ticks_ = real_start_ticks_ = current_time_ = SDL_GetTicks() * acceleration;
cycles_ = cycles;
current_cycle_ = 0;
acceleration_ = acceleration;
// current_frame_ = frames_.begin();
current_frame_ = 0;
if (ending_frame_time_ >= start_frame_) {
duration_ = ending_frame_time_ - start_frame_;
} else {
duration_ = 0;
}
}
template<typename T, typename T_void_value>
void animated<T,T_void_value>::update_current_frame_internal()
{
// std::cerr << "--- updating frame ---\n";
if(does_not_change_)
return;
frame_changed_ = false;
// Always update current_time_, for the animation_time functions to work.
current_time_ = SDL_GetTicks() * acceleration_;
if(!started_)
return;
if(frames_.empty()) {
no_current_frame_ = true;
does_not_change_ = true;
return;
}
if(frames_.size() == 1 && cycles_ == INFINITE_CYCLES) {
does_not_change_ = true;
frame_changed_ = false;
}
if (duration_ > 0) {
// Ticks is the actual time since animation started.
int ticks = current_time_ - start_ticks_;
// Handles cycle overflow
if(ticks > duration_) {
int ncycles = ticks/duration_;
current_cycle_ = minimum<int>(cycles_, current_cycle_ + ncycles);
start_ticks_ += ncycles * duration_;
ticks -= ncycles * duration_;
// current_frame_ = frames_.begin();
current_frame_ = 0;
frame_changed_ = true;
}
// Checks if the animation is finished
if(cycles_ != INFINITE_CYCLES && current_cycle_ >= cycles_) {
// If the animation is finished, the current frame is the last
// one
current_frame_ = frames_.size() - 1;
frame_changed_ = true;
// std::cerr << "Animation finished\n";
no_current_frame_ = false;
started_ = false;
return;
}
if(ticks < (frames_[current_frame_].milliseconds - start_frame_)) {
// std::cerr << "Animation not yet started\n";
frame_changed_ = true;
no_current_frame_ = true;
return;
}
// Looks for the current frame
typename std::vector<frame>::size_type i = current_frame_ + 1;
for(; i != frames_.size(); ++i) {
if(ticks < (frames_[i].milliseconds - start_frame_))
break;
current_frame_ = i;
frame_changed_ = true;
// std::cerr << "Skipping to next frame\n";
}
no_current_frame_ = false;
} else {
// If the duration is void, the animation is automatically finished.
// current_cycle_ = cycles_;
if(cycles_ != -1)
started_ = false;
does_not_change_ = true;
frame_changed_ = false;
// current_frame_ = frames_.size() - 1;
// frame_changed_ = false;
// std::cerr << "Returning last frame\n";
no_current_frame_ = false;
}
}
template<typename T, typename T_void_value>
bool animated<T,T_void_value>::frame_changed() const
{
return frame_changed_;
}
template<typename T, typename T_void_value>
bool animated<T,T_void_value>::animation_finished() const
{
if(!started_)
return true;
//if(current_cycle_ == cycles_)
// return true;
return false;
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_animation_time() const
{
if(does_not_change_)
return SDL_GetTicks() * acceleration_ - real_start_ticks_ + start_frame_;
return current_time_ - real_start_ticks_ + start_frame_;
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_frame_time() const
{
return current_time_ - start_ticks_ + start_frame_;
}
template<typename T, typename T_void_value>
const T& animated<T,T_void_value>::get_current_frame() const
{
if(no_current_frame_ == true)
return void_value_;
const frame& cur = frames_[current_frame_];
if(!cur.has_value)
return void_value_;
return cur.value;
}
template<typename T, typename T_void_value>
const T& animated<T,T_void_value>::get_base_frame() const
{
if(frames_.empty())
return void_value_;
return frames_[0];
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_first_frame_time() const
{
if (starting_frame_time_ != INT_MAX && starting_frame_time_ != INT_MIN)
return starting_frame_time_;
return 0;
}
template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_last_frame_time() const
{
if (ending_frame_time_ != INT_MAX && ending_frame_time_ != INT_MIN)
return ending_frame_time_;
return 0;
}
#endif

View file

@ -19,6 +19,7 @@
#include "terrain.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#define ERR_NG lg::err(lg::engine)

View file

@ -5,6 +5,7 @@
#include "pathfind.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#define LOG_NG lg::info(lg::engine)

View file

@ -27,6 +27,7 @@
#include "show_dialog.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include "widgets/menu.hpp"
#include "widgets/progressbar.hpp"

View file

@ -31,6 +31,7 @@
#include "../team.hpp"
#include "../util.hpp"
#include "../video.hpp"
#include "serialization/string_utils.hpp"
#include "editor.hpp"
#include "map_manip.hpp"

View file

@ -25,6 +25,7 @@
#include "../team.hpp"
#include "../util.hpp"
#include "../wesconfig.h"
#include "serialization/string_utils.hpp"
#include <cctype>
#include <cstdlib>

View file

@ -23,6 +23,7 @@
#include "team.hpp"
#include "tooltips.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <cstdio>

View file

@ -51,6 +51,7 @@
#include "unit.hpp"
#include "video.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include "widgets/button.hpp"
#include "widgets/menu.hpp"

View file

@ -25,6 +25,7 @@
#include "unit_display.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include <cstdlib>
#include <deque>

View file

@ -21,6 +21,7 @@
#include "preferences.hpp"
#include "statistics.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <cstdio>

View file

@ -6,6 +6,7 @@
#include "sdl_utils.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <map>

View file

@ -25,6 +25,7 @@
#include "unit.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include "widgets/button.hpp"
#include "widgets/menu.hpp"
#include "widgets/scrollbar.hpp"

View file

@ -12,6 +12,7 @@
*/
#include "leader_list.hpp"
#include "serialization/string_utils.hpp"
#include "widgets/menu.hpp"
leader_list_manager::leader_list_manager(const config::child_list& side_list,

View file

@ -19,6 +19,7 @@
#include "pathfind.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <cctype>

View file

@ -20,6 +20,7 @@
#include "race.hpp"
#include "scoped_resource.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"
#define ERR_CF lg::err(lg::config)
#define LOG_NG lg::info(lg::engine)

View file

@ -12,13 +12,14 @@
*/
#include "global.hpp"
#include "multiplayer_connect.hpp"
#include "dialogs.hpp"
#include "font.hpp"
#include "game_config.hpp"
#include "multiplayer_connect.hpp"
#include "preferences.hpp"
#include "show_dialog.hpp"
#include "dialogs.hpp"
#include "game_config.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#define LOG_NW lg::info(lg::network)
#define ERR_NW lg::err(lg::network)

View file

@ -17,6 +17,7 @@
#include "wassert.hpp"
#include "util.hpp"
#include "replay.hpp"
#include "serialization/string_utils.hpp"
#define LOG_NW lg::info(lg::network)
#define ERR_NW lg::err(lg::network)

View file

@ -34,6 +34,7 @@
#include "unit_display.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include "widgets/menu.hpp"
#include <cmath>

View file

@ -26,6 +26,7 @@
#include "show_dialog.hpp"
#include "sound.hpp"
#include "util.hpp"
#include "serialization/string_utils.hpp"
#include "widgets/button.hpp"
#include "widgets/label.hpp"
#include "widgets/menu.hpp"

View file

@ -2,6 +2,7 @@
#include "race.hpp"
#include "replay.hpp"
#include "serialization/string_utils.hpp"
#include <cstdlib>

View file

@ -22,6 +22,7 @@
#include "team.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <cstdlib>

View file

@ -7,6 +7,7 @@
#include "theme.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include <cstdlib>
#include <sstream>

View file

@ -24,6 +24,7 @@
#include "unit.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <cstdlib>

View file

@ -20,18 +20,12 @@
#include "unit_types.hpp"
#include "util.hpp"
#include "wassert.hpp"
#include "serialization/string_utils.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
//these headers are used to check for file existence
#ifdef linux
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
unit_animation::frame::frame(const config& cfg)
{
xoffset = atoi(cfg["xoffset"].c_str());

View file

@ -22,6 +22,7 @@
#include "../log.hpp"
#include "../util.hpp"
#include "../video.hpp"
#include "serialization/string_utils.hpp"
namespace gui {

View file

@ -7,6 +7,7 @@
#include "../show_dialog.hpp"
#include "../util.hpp"
#include "../video.hpp"
#include "serialization/string_utils.hpp"
#include <numeric>