Author: alink
Date: Sat Jun  7 06:57:00 2008
New Revision: 27021

URL: http://svn.gna.org/viewcvs/wesnoth?rev=27021&view=rev
Log:
Optimization of the first "Initializing display" phase (when caching)
Since the terrain builder need to check the existence of a lot of images,
check once all the "terrain" directories and cache which files exists,
instead of searching each file in each directory.

Modified:
    trunk/src/builder.cpp
    trunk/src/image.cpp
    trunk/src/image.hpp

Modified: trunk/src/builder.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/builder.cpp?rev=27021&r1=27020&r2=27021&view=diff
==============================================================================
--- trunk/src/builder.cpp (original)
+++ trunk/src/builder.cpp Sat Jun  7 06:57:00 2008
@@ -153,6 +153,7 @@
        // since it might give problems (or not?)
        //image::flush_cache();
 
+       image::precache_file_existence("terrain/");
        parse_config(cfg);
        parse_config(level);
        add_off_map_rule(offmap_image);
@@ -268,7 +269,8 @@
                                std::string s = variant->second.image_string;
                                s = s.substr(0, s.find_first_of(",:"));
 
-                               if(!image::exists("terrain/" + s + ".png"))
+                               // we already precached file existence in the 
constructor
+                               if(!image::exists("terrain/" + s + ".png", 
true))
                                        return false;
                        }
                }

Modified: trunk/src/image.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/image.cpp?rev=27021&r1=27020&r2=27021&view=diff
==============================================================================
--- trunk/src/image.cpp (original)
+++ trunk/src/image.cpp Sat Jun  7 06:57:00 2008
@@ -48,7 +48,10 @@
 
 // const int cache_version_ = 0;
 
-std::map<image::locator,bool> image_existance_map;
+std::map<image::locator,bool> image_existence_map;
+
+// directories where we already cached file existence
+std::set<std::string> precached_dirs;
 
 std::map<surface, surface> reversed_images_;
 
@@ -97,7 +100,8 @@
        mini_terrain_cache.clear();
        mini_fogged_terrain_cache.clear();
        reversed_images_.clear();
-       image_existance_map.clear();
+       image_existence_map.clear();
+       precached_dirs.clear();
 }
 
 int locator::last_index_ = 0;
@@ -737,22 +741,65 @@
        return rev;
 }
 
-bool exists(const image::locator& i_locator)
+bool exists(const image::locator& i_locator, bool precached)
 {
        typedef image::locator loc;
        loc::type type = i_locator.get_type();
        if (type != loc::FILE && type != loc::SUB_FILE)
                return false;
 
+       if (precached) {
+               std::map<image::locator,bool>::const_iterator b =  
image_existence_map.find(i_locator);
+               if (b != image_existence_map.end())
+                       return b->second;
+               else
+                       return false;
+       }
+
        // The insertion will fail if there is already an element in the cache
        std::pair< std::map< image::locator, bool >::iterator, bool >
-               it = image_existance_map.insert(std::make_pair(i_locator, 
false));
+               it = image_existence_map.insert(std::make_pair(i_locator, 
false));
        bool &cache = it.first->second;
        if (it.second)
                cache = !get_binary_file_location("images", 
i_locator.get_filename()).empty();
        return cache;
 }
 
+void precache_file_existence_internal(const std::string& dir, const 
std::string& subdir)
+{
+       const std::string checked_dir = dir + "/" + subdir;
+       if (precached_dirs.find(checked_dir) != precached_dirs.end())
+               return;
+       precached_dirs.insert(checked_dir);
+
+       std::vector<std::string> files_found;
+       std::vector<std::string> dirs_found;
+       get_files_in_dir(checked_dir, &files_found, &dirs_found,
+                       FILE_NAME_ONLY, NO_FILTER, DONT_REORDER);
+
+       for(std::vector<std::string>::const_iterator f = files_found.begin();
+                       f != files_found.end(); ++f) {
+               image_existence_map[subdir + *f] = true;
+       }
+
+       for(std::vector<std::string>::const_iterator d = dirs_found.begin();
+                       d != dirs_found.end(); ++d) {
+               precache_file_existence_internal(dir, subdir + *d + "/");
+       }
+}
+
+void precache_file_existence(const std::string& subdir)
+{
+       const std::vector<std::string>& paths = get_binary_paths("images");
+
+       for(std::vector<std::string>::const_iterator p = paths.begin();
+                        p != paths.end(); ++p) {
+
+               const std::string dir = *p + "/" + subdir;
+               precache_file_existence_internal(*p, subdir);
+       }
+}
+
 
 } // end namespace image
 

Modified: trunk/src/image.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/image.hpp?rev=27021&r1=27020&r2=27021&view=diff
==============================================================================
--- trunk/src/image.hpp (original)
+++ trunk/src/image.hpp Sat Jun  7 06:57:00 2008
@@ -210,8 +210,12 @@
        ///and must be freed using SDL_FreeSurface()
        surface reverse_image(const surface &surf);
 
-       //returns true if the given image actually exists, without loading it.
-       bool exists(const locator& i_locator);
+       ///returns true if the given image actually exists, without loading it.
+       /// precached : if we already have precached the directory containing 
the file
+       bool exists(const locator& i_locator, bool precached = false);
+
+       /// precache the existence of files in the subdir (ex: "terrain/")
+       void precache_file_existence(const std::string& subdir = "");
 }
 
 #endif


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to