mirror of
https://github.com/wesnoth/wesnoth
synced 2025-04-29 03:23:20 +00:00
Moved image cache definitions out of the header file.
Fixed double element copy on cache copy and cache query. Simplified code by explicit resizing. Fully flushed caches on F5.
This commit is contained in:
parent
0a067e7171
commit
5c827ccc38
@ -36,12 +36,87 @@
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#include <list>
|
||||
#include <set>
|
||||
|
||||
static lg::log_domain log_display("display");
|
||||
#define ERR_DP LOG_STREAM(err, log_display)
|
||||
#define LOG_DP LOG_STREAM(info, log_display)
|
||||
|
||||
/**
|
||||
* Iterators from this dummy list are needed to ensure that iterator member
|
||||
* of cache_item is always non-singular iterator thus avoiding
|
||||
* "Copy-contruct from singular iterator" error when libstdc++ debug mode
|
||||
* is enabled. Note copying a singular iterator is undefined behaviour by
|
||||
* the C++ standard.
|
||||
*/
|
||||
static std::list<int> dummy_list;
|
||||
|
||||
template<typename T>
|
||||
struct cache_item
|
||||
{
|
||||
cache_item(): loaded(false), item(), position(dummy_list.end())
|
||||
{
|
||||
}
|
||||
|
||||
cache_item(const T &item): loaded(true), item(item), position(dummy_list.end())
|
||||
{
|
||||
}
|
||||
|
||||
bool loaded;
|
||||
T item;
|
||||
std::list<int>::iterator position;
|
||||
};
|
||||
|
||||
namespace image {
|
||||
|
||||
template<typename T>
|
||||
class cache_type
|
||||
{
|
||||
public:
|
||||
cache_type(): cache_size_(0), cache_max_size_(2000), lru_list_(), content_()
|
||||
{
|
||||
}
|
||||
|
||||
cache_item<T> &get_element(int index);
|
||||
void on_load(int index);
|
||||
|
||||
void flush()
|
||||
{
|
||||
content_.clear();
|
||||
lru_list_.clear();
|
||||
cache_size_ = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
int cache_size_;
|
||||
int cache_max_size_;
|
||||
std::list<int> lru_list_;
|
||||
std::vector<cache_item<T> > content_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool locator::in_cache(cache_type<T> &cache) const
|
||||
{
|
||||
return index_ == -1 ? false : cache.get_element(index_).loaded;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T &locator::locate_in_cache(cache_type<T> &cache) const
|
||||
{
|
||||
static T dummy;
|
||||
return index_ == -1 ? dummy : cache.get_element(index_).item;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void locator::add_to_cache(cache_type<T> &cache, const T &data) const
|
||||
{
|
||||
if (index_ != -1 ) cache.get_element(index_) = cache_item<T>(data);
|
||||
cache.on_load(index_);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
image::locator::locator_finder_t locator_finder;
|
||||
@ -76,20 +151,11 @@ namespace image {
|
||||
|
||||
std::list<int> dummy_list;
|
||||
|
||||
template<typename T>
|
||||
void cache_type<T>::flush()
|
||||
{
|
||||
typename std::vector<cache_item<T> >::iterator beg = content_.begin();
|
||||
typename std::vector<cache_item<T> >::iterator end = content_.end();
|
||||
|
||||
for(; beg != end; ++beg) {
|
||||
beg->loaded = false;
|
||||
beg->item = T();
|
||||
}
|
||||
}
|
||||
mini_terrain_cache_map mini_terrain_cache;
|
||||
mini_terrain_cache_map mini_fogged_terrain_cache;
|
||||
|
||||
static int last_index_ = 0;
|
||||
|
||||
void flush_cache()
|
||||
{
|
||||
images_.flush();
|
||||
@ -105,10 +171,9 @@ void flush_cache()
|
||||
reversed_images_.clear();
|
||||
image_existence_map.clear();
|
||||
precached_dirs.clear();
|
||||
last_index_ = 0;
|
||||
}
|
||||
|
||||
int locator::last_index_ = 0;
|
||||
|
||||
void locator::init_index()
|
||||
{
|
||||
std::map<value, int>& finder = locator_finder[hash_value(val_)];
|
||||
@ -1115,11 +1180,10 @@ bool precached_file_exists(const std::string& file)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
cache_item<T>& cache_type<T>::get_element(int index){
|
||||
cache_item<T>& cache_type<T>::get_element(int index)
|
||||
{
|
||||
assert (index != -1);
|
||||
while(static_cast<size_t>(index) >= content_.size()) {
|
||||
content_.push_back(cache_item<T>());
|
||||
}
|
||||
if (unsigned(index) >= content_.size()) content_.resize(index + 1);
|
||||
cache_item<T>& elt = content_[index];
|
||||
if(elt.loaded) {
|
||||
assert(*elt.position == index);
|
||||
|
@ -20,12 +20,10 @@
|
||||
#include "terrain_translation.hpp"
|
||||
|
||||
#include "SDL.h"
|
||||
#include "cassert"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
///this module manages the cache of images. With an image name, you can get
|
||||
///the surface corresponding to that image.
|
||||
@ -43,57 +41,9 @@ namespace image {
|
||||
#else
|
||||
const int tile_size = 72;
|
||||
#endif
|
||||
/**
|
||||
* Iterators from this dummy list are needed to ensure that iterator member
|
||||
* of cache_item is always non-singular iterator thus avoiding
|
||||
* "Copy-contruct from singular iterator" error when libstdc++ debug mode
|
||||
* is enabled. Note copying a singular iterator is undefined behaviour by
|
||||
* the C++ standard.
|
||||
*/
|
||||
extern std::list<int> dummy_list;
|
||||
|
||||
template<typename T>
|
||||
struct cache_item {
|
||||
cache_item() :
|
||||
loaded(false),
|
||||
item() ,
|
||||
position(dummy_list.end())
|
||||
{
|
||||
}
|
||||
|
||||
cache_item(T item) :
|
||||
loaded(true),
|
||||
item(item),
|
||||
position(dummy_list.end())
|
||||
{
|
||||
}
|
||||
|
||||
bool loaded;
|
||||
T item;
|
||||
std::list<int>::iterator position;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class cache_type
|
||||
{
|
||||
public:
|
||||
cache_type() :
|
||||
cache_size_(0),
|
||||
cache_max_size_(2000),
|
||||
lru_list_(),
|
||||
content_()
|
||||
{
|
||||
}
|
||||
|
||||
cache_item<T>& get_element(int index);
|
||||
void on_load(int index);
|
||||
void flush();
|
||||
private:
|
||||
int cache_size_;
|
||||
int cache_max_size_;
|
||||
std::list<int> lru_list_;
|
||||
std::vector<cache_item<T> > content_;
|
||||
};
|
||||
class cache_type;
|
||||
|
||||
//a generic image locator. Abstracts the location of an image.
|
||||
class locator
|
||||
@ -178,17 +128,12 @@ namespace image {
|
||||
surface load_from_disk() const;
|
||||
|
||||
template <typename T>
|
||||
bool in_cache(cache_type<T>& cache) const
|
||||
{ return index_ == -1 ? false : cache.get_element(index_).loaded; }
|
||||
bool in_cache(cache_type<T> &cache) const;
|
||||
template <typename T>
|
||||
T locate_in_cache(cache_type<T>& cache) const
|
||||
{ return index_ == -1 ? T() : cache.get_element(index_).item; }
|
||||
const T &locate_in_cache(cache_type<T> &cache) const;
|
||||
template <typename T>
|
||||
void add_to_cache(cache_type<T>& cache, const T &data) const
|
||||
{ if(index_ != -1 ) cache.get_element(index_) = cache_item<T>(data); cache.on_load(index_); }
|
||||
void add_to_cache(cache_type<T> &cache, const T &data) const;
|
||||
|
||||
protected:
|
||||
static int last_index_;
|
||||
private:
|
||||
|
||||
surface load_image_file() const;
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
#include "reports.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace {
|
||||
const std::string report_names[] = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user