Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

156 Zeilen
3.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. #include "image-private.h"
  15. using namespace std;
  16. namespace lol
  17. {
  18. /* HACK: We cannot make this an ImageCodec member function, because the
  19. * REGISTER_IMAGE_LOADER 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 RegisterAllLoaders()
  29. {
  30. #if defined __ANDROID__
  31. REGISTER_IMAGE_LOADER(AndroidImageData)
  32. #endif
  33. REGISTER_IMAGE_LOADER(DummyImageData)
  34. #if defined USE_GDIPLUS
  35. REGISTER_IMAGE_LOADER(GdiPlusImageData)
  36. #endif
  37. #if defined __APPLE__ && defined __MACH__ && defined __arm__
  38. REGISTER_IMAGE_LOADER(IosImageData)
  39. #endif
  40. #if defined __CELLOS_LV2__
  41. REGISTER_IMAGE_LOADER(Ps3ImageData)
  42. #endif
  43. #if defined USE_SDL_IMAGE
  44. REGISTER_IMAGE_LOADER(SdlImageData)
  45. #endif
  46. return true;
  47. }
  48. /*
  49. * Our static image loader
  50. */
  51. static class ImageLoader
  52. {
  53. public:
  54. Image *Create(char const *path);
  55. void Destroy(Image *img);
  56. private:
  57. Map<String, Image *> m_images;
  58. }
  59. image_loader;
  60. Image *ImageLoader::Create(char const *path)
  61. {
  62. /* Initialise loaders (see above) */
  63. static bool init = RegisterAllLoaders();
  64. UNUSED(init);
  65. /* Is our image already in the bank? If so, no need to create it. */
  66. Image *img;
  67. if (m_images.HasKey(path))
  68. {
  69. img = m_images[path];
  70. }
  71. else
  72. {
  73. img = new Image();
  74. img->m_data = ImageCodec::Load(path);
  75. m_images[path] = img;
  76. }
  77. ++img->m_data->m_refcount;
  78. return img;
  79. }
  80. void ImageLoader::Destroy(Image *img)
  81. {
  82. ASSERT(img->m_data->m_refcount > 0);
  83. if (--img->m_data->m_refcount == 0)
  84. {
  85. /* FIXME: unload images etc. here */
  86. /* XXX: we’re gonna hit a problem here because we didn’t keep
  87. * the image path within the object, so unless we store it
  88. * ourselves we’re good for a O(n) lookup… which we can’t do
  89. * on Map objects anyway. */
  90. /* TODO: 1. remove image from Map
  91. 2. delete img; */
  92. }
  93. }
  94. /*
  95. * Static image methods
  96. */
  97. Image *Image::Create(char const *path)
  98. {
  99. return image_loader.Create(path);
  100. }
  101. void Image::Destroy(Image *img)
  102. {
  103. return image_loader.Destroy(img);
  104. }
  105. /*
  106. * Public Image class
  107. */
  108. Image::Image()
  109. : m_data(nullptr)
  110. {
  111. }
  112. ivec2 Image::GetSize() const
  113. {
  114. return m_data->m_size;
  115. }
  116. PixelFormat Image::GetFormat() const
  117. {
  118. return m_data->m_format;
  119. }
  120. uint8_t *Image::GetData() const
  121. {
  122. return m_data->GetData();
  123. }
  124. Image::~Image()
  125. {
  126. m_data->Close();
  127. delete m_data;
  128. }
  129. } /* namespace lol */