Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

126 строки
3.3 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2017 Sam Hocevar <sam@hocevar.net>
  5. // Copyright © 2016—2017 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #include <lol/engine-internal.h>
  14. #include "resource-private.h"
  15. #include <algorithm> /* for std::swap */
  16. namespace lol
  17. {
  18. /* HACK: We cannot make this an ImageLoader member function, because the
  19. * REGISTER_IMAGE_CODEC macro forward-declares free functions from
  20. * the "lol" namespace. An apparent bug in Visual Studio's compiler
  21. * makes it think these functions are actually in the top-level
  22. * namespace when the forward declaration is in a class member function.
  23. * To avoid the problem, we make the forward declaration in a free
  24. * function.
  25. * The bug was reported to Microsoft and fixed by them, but the fix
  26. * is not yet available.
  27. * https://connect.microsoft.com/VisualStudio/feedback/details/730878/ */
  28. static bool RegisterAllCodecs(array<ResourceCodec *> &codeclist)
  29. {
  30. #if defined __ANDROID__
  31. REGISTER_IMAGE_CODEC(AndroidImageCodec)
  32. #endif
  33. #if defined LOL_USE_GDIPLUS
  34. REGISTER_IMAGE_CODEC(GdiPlusImageCodec)
  35. #endif
  36. #if defined __APPLE__ && defined __MACH__ && defined __arm__
  37. REGISTER_IMAGE_CODEC(IosImageCodec)
  38. #endif
  39. #if defined LOL_USE_SDL_IMAGE
  40. REGISTER_IMAGE_CODEC(SdlImageCodec)
  41. #endif
  42. #if defined LOL_USE_IMLIB2
  43. REGISTER_IMAGE_CODEC(Imlib2ImageCodec)
  44. #endif
  45. REGISTER_IMAGE_CODEC(ZedImageCodec)
  46. REGISTER_IMAGE_CODEC(ZedPaletteImageCodec)
  47. REGISTER_IMAGE_CODEC(OricImageCodec)
  48. REGISTER_IMAGE_CODEC(DummyImageCodec)
  49. return true;
  50. }
  51. /*
  52. * Our static image loader
  53. */
  54. static class StaticResourceLoader
  55. {
  56. friend class ResourceLoader;
  57. public:
  58. inline StaticResourceLoader()
  59. {
  60. RegisterAllCodecs(m_codecs);
  61. }
  62. private:
  63. array<ResourceCodec *> m_codecs;
  64. }
  65. g_resource_loader;
  66. /*
  67. * The public resource loader
  68. */
  69. ResourceCodecData* ResourceLoader::Load(char const *path)
  70. {
  71. ResourceCodec* last_codec = nullptr;
  72. for (auto codec : g_resource_loader.m_codecs)
  73. {
  74. last_codec = codec;
  75. auto data = codec->Load(path);
  76. if (data != nullptr)
  77. {
  78. msg::debug("image::load: codec %s succesfully loaded %s.\n",
  79. codec->GetName(), path);
  80. return data;
  81. }
  82. }
  83. //Log error, because we shouldn't be here
  84. msg::error("image::load: last codec %s, error loading resource %s.\n",
  85. last_codec->GetName(), path);
  86. return nullptr;
  87. }
  88. bool ResourceLoader::Save(char const *path, ResourceCodecData* data)
  89. {
  90. ResourceCodec* last_codec = nullptr;
  91. for (auto codec : g_resource_loader.m_codecs)
  92. {
  93. last_codec = codec;
  94. if (codec->Save(path, data))
  95. {
  96. msg::debug("image::save: codec %s succesfully saved %s.\n",
  97. codec->GetName(), path);
  98. return true;
  99. }
  100. }
  101. //Log error, because we shouldn't be here
  102. msg::error("image::save: last codec %s, error saving resource %s.\n",
  103. last_codec->GetName(), path);
  104. return false;
  105. }
  106. } /* namespace lol */