void WadImageCache::save_cache() { if (!m_cache_dirty) return; InfoTree pt; for (cache_iter_t it = m_used.begin(); it != m_used.end(); ++it) { std::string name = it->second.first; WadImageDescriptor desc = boost::tuples::get<0>(it->first); pt.put(name + ".path", desc.file.GetPath()); pt.put(name + ".checksum", desc.checksum); pt.put(name + ".index", desc.index); pt.put(name + ".tag", desc.tag); pt.put(name + ".width", boost::tuples::get<1>(it->first)); pt.put(name + ".height", boost::tuples::get<2>(it->first)); pt.put(name + ".filesize", it->second.second); } FileSpecifier info; info.SetToImageCacheDir(); info.AddPart("Cache.ini"); try { pt.save_ini(info); m_cache_dirty = false; } catch (InfoTree::ini_error e) { logError("Could not save image cache to %s (%s)", info.GetPath(), e.what()); return; } }
void WadImageCache::delete_storage_for_name(std::string& name) const { FileSpecifier file; file.SetToImageCacheDir(); file.AddPart(name); file.Delete(); }
SDL_Surface *WadImageCache::image_from_name(std::string& name) const { FileSpecifier file; file.SetToImageCacheDir(); file.AddPart(name); OpenedFile of; if (!file.Open(of)) return NULL; #ifdef HAVE_SDL_IMAGE SDL_Surface *img = IMG_Load_RW(of.GetRWops(), 0); #else SDL_Surface *img = SDL_LoadBMP_RW(of.GetRWops(), 0); #endif return img; }
void WadImageCache::initialize_cache() { FileSpecifier info; info.SetToImageCacheDir(); info.AddPart("Cache.ini"); if (!info.Exists()) return; InfoTree pt; try { pt = InfoTree::load_ini(info); } catch (InfoTree::ini_error e) { logError("Could not read image cache from %s (%s)", info.GetPath(), e.what()); } for (InfoTree::iterator it = pt.begin(); it != pt.end(); ++it) { std::string name = it->first; InfoTree ptc = it->second; WadImageDescriptor desc; std::string path; ptc.read("path", path); desc.file = FileSpecifier(path); ptc.read("checksum", desc.checksum); ptc.read("index", desc.index); ptc.read("tag", desc.tag); int width = 0; ptc.read("width", width); int height = 0; ptc.read("height", height); size_t filesize = 0; ptc.read("filesize", filesize); cache_key_t key = cache_key_t(desc, width, height); cache_value_t val = cache_value_t(name, filesize); m_used.push_front(cache_pair_t(key, val)); m_cacheinfo[key] = m_used.begin(); m_cachesize += filesize; } }
std::string WadImageCache::image_to_new_name(SDL_Surface *image, int32 *filesize) const { // create name boost::uuids::random_generator gen; boost::uuids::uuid u = gen(); std::string ustr = boost::uuids::to_string(u); FileSpecifier File; File.SetToImageCacheDir(); File.AddPart(ustr); FileSpecifier TempFile; TempFile.SetTempName(File); int ret; //#if defined(HAVE_PNG) && defined(HAVE_SDL_IMAGE) // ret = aoIMG_SavePNG(TempFile.GetPath(), image, IMG_COMPRESS_DEFAULT, NULL, 0); #ifdef HAVE_SDL_IMAGE ret = IMG_SavePNG(image, TempFile.GetPath()); #else ret = SDL_SaveBMP(image, TempFile.GetPath()); #endif if (ret == 0 && TempFile.Rename(File)) { if (filesize) { OpenedFile of; if (File.Open(of)) of.GetLength(*filesize); } return ustr; } if (filesize) *filesize = 0; return ""; }