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

124 строки
2.7 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. #include <lol/engine-internal.h>
  11. #if defined USE_SDL_IMAGE
  12. #if defined HAVE_SDL_SDL_H
  13. # include <SDL/SDL.h>
  14. #else
  15. # include <SDL.h>
  16. #endif
  17. #if defined HAVE_SDL_SDL_IMAGE_H
  18. # include <SDL/SDL_image.h>
  19. #else
  20. # include <SDL_image.h>
  21. #endif
  22. #include "../../image/image-private.h"
  23. namespace lol
  24. {
  25. /*
  26. * Image implementation class
  27. */
  28. class SdlImageCodec : public ImageCodec
  29. {
  30. public:
  31. virtual bool Load(Image *image, char const *path);
  32. virtual bool Save(Image *image, char const *path);
  33. static SDL_Surface *Create32BppSurface(ivec2 size);
  34. };
  35. DECLARE_IMAGE_CODEC(SdlImageCodec, 50)
  36. bool SdlImageCodec::Load(Image *image, char const *path)
  37. {
  38. SDL_Surface *surface = nullptr;
  39. array<String> pathlist = System::GetPathList(path);
  40. for (int i = 0; i < pathlist.Count(); i++)
  41. {
  42. surface = IMG_Load(pathlist[i].C());
  43. if (surface)
  44. break;
  45. }
  46. if (!surface)
  47. {
  48. #if !LOL_BUILD_RELEASE
  49. Log::Error("could not load image %s\n", path);
  50. #endif
  51. return false;
  52. }
  53. ivec2 size(surface->w, surface->h);
  54. if (surface->format->BytesPerPixel != 4)
  55. {
  56. SDL_Surface *tmp = Create32BppSurface(size);
  57. SDL_BlitSurface(surface, nullptr, tmp, nullptr);
  58. SDL_FreeSurface(surface);
  59. surface = tmp;
  60. }
  61. image->SetSize(size);
  62. u8vec4 *data = image->Lock<PixelFormat::RGBA_8>();
  63. memcpy(data, surface->pixels, 4 * size.x * size.y);
  64. image->Unlock(data);
  65. SDL_FreeSurface(surface);
  66. return true;
  67. }
  68. bool SdlImageCodec::Save(Image *image, char const *path)
  69. {
  70. ivec2 size = image->GetSize();
  71. SDL_Surface *surface = Create32BppSurface(size);
  72. u8vec4 *data = image->Lock<PixelFormat::RGBA_8>();
  73. memcpy(surface->pixels, data, 4 * size.x * size.y);
  74. image->Unlock(data);
  75. int ret = SDL_SaveBMP(surface, path);
  76. SDL_FreeSurface(surface);
  77. return ret == 0;
  78. }
  79. SDL_Surface *SdlImageCodec::Create32BppSurface(ivec2 size)
  80. {
  81. uint32_t rmask, gmask, bmask, amask;
  82. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  83. rmask = 0xff000000;
  84. gmask = 0x00ff0000;
  85. bmask = 0x0000ff00;
  86. amask = 0x000000ff;
  87. #else
  88. rmask = 0x000000ff;
  89. gmask = 0x0000ff00;
  90. bmask = 0x00ff0000;
  91. amask = 0xff000000;
  92. #endif
  93. return SDL_CreateRGBSurface(SDL_SWSURFACE, size.x, size.y, 32,
  94. rmask, gmask, bmask, amask);
  95. }
  96. } /* namespace lol */
  97. #endif /* defined USE_SDL_IMAGE */