texture_manager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "video/texture_manager.hpp"
  17. #include <SDL_image.h>
  18. #include <assert.h>
  19. #include <sstream>
  20. #include <physfs.h>
  21. #include "math/rect.hpp"
  22. #include "physfs/physfs_sdl.hpp"
  23. #include "util/file_system.hpp"
  24. #include "util/log.hpp"
  25. #include "util/reader_document.hpp"
  26. #include "util/reader_mapping.hpp"
  27. #include "video/color.hpp"
  28. #include "video/gl.hpp"
  29. #include "video/sampler.hpp"
  30. #include "video/sdl_surface.hpp"
  31. #include "video/texture.hpp"
  32. #include "video/video_system.hpp"
  33. namespace {
  34. GLenum string2wrap(const std::string& text)
  35. {
  36. if (text == "clamp-to-edge")
  37. {
  38. return GL_CLAMP_TO_EDGE;
  39. }
  40. else if (text == "repeat")
  41. {
  42. return GL_REPEAT;
  43. }
  44. else if (text == "mirrored-repeat")
  45. {
  46. return GL_MIRRORED_REPEAT;
  47. }
  48. else
  49. {
  50. log_warning << "unknown texture wrap: " << text << std::endl;
  51. return GL_CLAMP_TO_EDGE;
  52. }
  53. }
  54. GLenum string2filter(const std::string& text)
  55. {
  56. if (text == "nearest")
  57. {
  58. return GL_NEAREST;
  59. }
  60. else if (text == "linear")
  61. {
  62. return GL_LINEAR;
  63. }
  64. else
  65. {
  66. log_warning << "unknown texture filter: " << text << std::endl;
  67. return GL_LINEAR;
  68. }
  69. }
  70. SDLSurfacePtr create_image_surface(const std::string& filename)
  71. {
  72. if (PHYSFS_exists(filename.c_str()))
  73. return SDLSurface::from_file(filename);
  74. // The image doesn't exist, so attempt to load a ".deprecated" version
  75. log_warning << "Image '" << filename << "' doesn't exist. Attempting to load \".deprecated\" version." << std::endl;
  76. return SDLSurface::from_file(FileSystem::strip_extension(filename) + ".deprecated" +
  77. FileSystem::extension(filename));
  78. }
  79. } // namespace
  80. const std::string TextureManager::s_dummy_texture = "images/engine/missing.png";
  81. TextureManager::TextureManager() :
  82. m_image_textures(),
  83. m_surfaces(),
  84. m_load_successful(false)
  85. {
  86. }
  87. TextureManager::~TextureManager()
  88. {
  89. for (const auto& texture : m_image_textures)
  90. {
  91. if (!texture.second.expired())
  92. {
  93. log_warning << "Texture '" << std::get<0>(texture.first) << "' not freed" << std::endl;
  94. }
  95. }
  96. m_image_textures.clear();
  97. m_surfaces.clear();
  98. }
  99. TexturePtr
  100. TextureManager::get(const ReaderMapping& mapping, const std::optional<Rect>& region)
  101. {
  102. std::string filename;
  103. if (!mapping.get("file", filename))
  104. {
  105. log_warning << "'file' tag missing" << std::endl;
  106. }
  107. else
  108. {
  109. filename = FileSystem::join(mapping.get_doc().get_directory(), filename);
  110. }
  111. std::optional<Rect> rect;
  112. std::vector<int> rect_v;
  113. if (mapping.get("rect", rect_v))
  114. {
  115. if (rect_v.size() == 4)
  116. {
  117. rect = Rect(rect_v[0], rect_v[1], rect_v[2], rect_v[3]);
  118. }
  119. else
  120. {
  121. log_warning << "'rect' requires four elements" << std::endl;
  122. }
  123. }
  124. GLenum wrap_s = GL_CLAMP_TO_EDGE;
  125. GLenum wrap_t = GL_CLAMP_TO_EDGE;
  126. std::vector<std::string> wrap_v;
  127. if (mapping.get("wrap", wrap_v))
  128. {
  129. if (wrap_v.size() == 1)
  130. {
  131. wrap_s = string2wrap(wrap_v[0]);
  132. wrap_t = string2wrap(wrap_v[0]);
  133. }
  134. else if (wrap_v.size() == 2)
  135. {
  136. wrap_s = string2wrap(wrap_v[0]);
  137. wrap_t = string2wrap(wrap_v[1]);
  138. }
  139. else
  140. {
  141. log_warning << "unknown number of wrap arguments" << std::endl;
  142. }
  143. }
  144. GLenum filter = GL_LINEAR;
  145. std::string filter_s;
  146. if (mapping.get("filter", filter_s))
  147. {
  148. filter = string2filter(filter_s);
  149. }
  150. Vector animate(0.0f, 0.0f);
  151. std::vector<float> animate_v;
  152. if (mapping.get("animate", animate_v))
  153. {
  154. if (animate_v.size() == 2)
  155. {
  156. animate.x = animate_v[0];
  157. animate.y = animate_v[1];
  158. }
  159. }
  160. if (region)
  161. {
  162. if (!rect)
  163. {
  164. rect = region;
  165. }
  166. else
  167. {
  168. rect->left += region->left;
  169. rect->top += region->top;
  170. rect->right = rect->left + region->get_width();
  171. rect->bottom = rect->top + region->get_height();
  172. }
  173. }
  174. return get(filename, rect, Sampler(filter, wrap_s, wrap_t, animate));
  175. }
  176. TexturePtr
  177. TextureManager::get(const std::string& _filename)
  178. {
  179. std::string filename = FileSystem::normalize(_filename);
  180. Texture::Key key(filename, Rect(0, 0, 0, 0));
  181. auto i = m_image_textures.find(key);
  182. TexturePtr texture;
  183. if (i != m_image_textures.end())
  184. texture = i->second.lock();
  185. if (!texture) {
  186. texture = create_image_texture(filename, Sampler());
  187. texture->m_cache_key = key;
  188. m_image_textures[key] = texture;
  189. }
  190. return texture;
  191. }
  192. TexturePtr
  193. TextureManager::get(const std::string& _filename,
  194. const std::optional<Rect>& rect,
  195. const Sampler& sampler)
  196. {
  197. std::string filename = FileSystem::normalize(_filename);
  198. Texture::Key key;
  199. if (rect)
  200. {
  201. key = Texture::Key(filename, *rect);
  202. }
  203. else
  204. {
  205. key = Texture::Key(filename, Rect());
  206. }
  207. auto i = m_image_textures.find(key);
  208. TexturePtr texture;
  209. if (i != m_image_textures.end())
  210. texture = i->second.lock();
  211. if (!texture) {
  212. if (rect)
  213. {
  214. texture = create_image_texture(filename, *rect, sampler);
  215. }
  216. else
  217. {
  218. texture = create_image_texture(filename, sampler);
  219. }
  220. texture->m_cache_key = key;
  221. m_image_textures[key] = texture;
  222. }
  223. return texture;
  224. }
  225. void
  226. TextureManager::reap_cache_entry(const Texture::Key& key)
  227. {
  228. auto i = m_image_textures.find(key);
  229. if (i == m_image_textures.end())
  230. {
  231. log_warning << "no cache entry for '" << std::get<0>(key) << "'" << std::endl;
  232. }
  233. else
  234. {
  235. assert(i->second.expired());
  236. m_image_textures.erase(i);
  237. }
  238. }
  239. TexturePtr
  240. TextureManager::create_image_texture(const std::string& filename, const Rect& rect, const Sampler& sampler)
  241. {
  242. m_load_successful = true;
  243. try
  244. {
  245. return create_image_texture_raw(filename, rect, sampler);
  246. }
  247. catch(const std::exception& err)
  248. {
  249. log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
  250. m_load_successful = false;
  251. return create_dummy_texture();
  252. }
  253. }
  254. const SDL_Surface&
  255. TextureManager::get_surface(const std::string& filename)
  256. {
  257. auto i = m_surfaces.find(filename);
  258. if (i != m_surfaces.end())
  259. {
  260. return *i->second;
  261. }
  262. SDLSurfacePtr surface = create_image_surface(filename);
  263. return *(m_surfaces[filename] = std::move(surface));
  264. }
  265. TexturePtr
  266. TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect, const Sampler& sampler)
  267. {
  268. assert(rect.valid());
  269. const SDL_Surface& src_surface = get_surface(filename);
  270. SDLSurfacePtr convert;
  271. if (src_surface.format->Rmask == 0 &&
  272. src_surface.format->Gmask == 0 &&
  273. src_surface.format->Bmask == 0 &&
  274. src_surface.format->Amask == 0)
  275. {
  276. log_debug << "Wrong surface format for image " << filename << ". Compensating." << std::endl;
  277. convert.reset(SDL_ConvertSurfaceFormat(const_cast<SDL_Surface*>(&src_surface), SDL_PIXELFORMAT_RGBA8888, 0));
  278. }
  279. const SDL_Surface& surface = convert ? *convert : src_surface;
  280. SDLSurfacePtr subimage;
  281. if (!Rect(0, 0, surface.w, surface.h).contains(rect))
  282. {
  283. log_warning << filename << ": invalid subregion requested: image="
  284. << surface.w << "x" << surface.h << ", rect=" << rect << std::endl;
  285. subimage = SDLSurfacePtr(SDL_CreateRGBSurface(0,
  286. rect.get_width(),
  287. rect.get_height(),
  288. surface.format->BitsPerPixel,
  289. surface.format->Rmask,
  290. surface.format->Gmask,
  291. surface.format->Bmask,
  292. surface.format->Amask));
  293. Rect clipped_rect(std::max(0, rect.left),
  294. std::max(0, rect.top),
  295. std::min(subimage->w, rect.right),
  296. std::min(subimage->w, rect.bottom));
  297. SDL_Rect srcrect = clipped_rect.to_sdl();
  298. SDL_BlitSurface(const_cast<SDL_Surface*>(&surface), &srcrect, subimage.get(), nullptr);
  299. }
  300. else
  301. {
  302. subimage = SDLSurfacePtr(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(surface.pixels) +
  303. rect.top * surface.pitch +
  304. rect.left * surface.format->BytesPerPixel,
  305. rect.get_width(), rect.get_height(),
  306. surface.format->BitsPerPixel,
  307. surface.pitch,
  308. surface.format->Rmask,
  309. surface.format->Gmask,
  310. surface.format->Bmask,
  311. surface.format->Amask));
  312. if (!subimage)
  313. {
  314. throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
  315. }
  316. }
  317. return VideoSystem::current()->new_texture(*subimage, sampler);
  318. }
  319. TexturePtr
  320. TextureManager::create_image_texture(const std::string& filename, const Sampler& sampler)
  321. {
  322. m_load_successful = true;
  323. try
  324. {
  325. return create_image_texture_raw(filename, sampler);
  326. }
  327. catch (const std::exception& err)
  328. {
  329. log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
  330. m_load_successful = false;
  331. return create_dummy_texture();
  332. }
  333. }
  334. TexturePtr
  335. TextureManager::create_image_texture_raw(const std::string& filename, const Sampler& sampler)
  336. {
  337. SDLSurfacePtr surface = create_image_surface(filename);
  338. TexturePtr texture = VideoSystem::current()->new_texture(*surface, sampler);
  339. surface.reset(nullptr);
  340. return texture;
  341. }
  342. TexturePtr
  343. TextureManager::create_dummy_texture()
  344. {
  345. // on error, try loading placeholder file
  346. try
  347. {
  348. TexturePtr tex = create_image_texture_raw(s_dummy_texture, Sampler());
  349. return tex;
  350. }
  351. catch (const std::exception& err)
  352. {
  353. // on error (when loading placeholder), try using empty surface,
  354. // when that fails to, just give up
  355. SDLSurfacePtr image(SDL_CreateRGBSurface(0, 128, 128, 8, 0, 0, 0, 0));
  356. if (!image)
  357. {
  358. throw;
  359. }
  360. else
  361. {
  362. log_warning << "Couldn't load texture '" << s_dummy_texture << "' (now using empty one): " << err.what() << std::endl;
  363. TexturePtr texture = VideoSystem::current()->new_texture(*image);
  364. return texture;
  365. }
  366. }
  367. }
  368. void
  369. TextureManager::debug_print(std::ostream& out) const
  370. {
  371. size_t total_texture_pixels = 0;
  372. out << "textures:begin" << std::endl;
  373. for(const auto& it : m_image_textures)
  374. {
  375. const auto& key = it.first;
  376. if (it.second.lock()) {
  377. total_texture_pixels += std::get<1>(key).get_area();
  378. }
  379. out << " texture "
  380. << " filename:" << std::get<0>(key) << " " << std::get<1>(key)
  381. << " " << "use_count:" << it.second.use_count() << std::endl;
  382. }
  383. out << "textures:end" << std::endl;
  384. size_t total_surface_pixels = 0;
  385. out << "surfaces:begin" << std::endl;
  386. for(const auto& it : m_surfaces)
  387. {
  388. const auto& filename = it.first;
  389. const auto& surface = it.second;
  390. total_surface_pixels += surface->w * surface->h;
  391. out << " surface filename:" << filename << " " << surface->w << "x" << surface->h << std::endl;
  392. }
  393. out << "surfaces:end" << std::endl;
  394. out << "total texture count:" << m_image_textures.size() << std::endl;
  395. out << "total texture pixels:" << total_texture_pixels << std::endl;
  396. out << "total surface count:" << m_surfaces.size() << std::endl;
  397. out << "total surface pixels:" << total_surface_pixels << std::endl;
  398. }
  399. /* EOF */