SDLImageLoader.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * SDLImageLoader.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CAnimation.h"
  12. #include "SDL_Extensions.h"
  13. #include "ColorFilter.h"
  14. #include "../CBitmapHandler.h"
  15. #include "../Graphics.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include "../../lib/filesystem/ISimpleResourceLoader.h"
  18. #include "../../lib/JsonNode.h"
  19. #include "../../lib/CRandomGenerator.h"
  20. #include "../../lib/vcmi_endian.h"
  21. #include <SDL_surface.h>
  22. /*************************************************************************
  23. * Classes for image loaders - helpers for loading from def files *
  24. *************************************************************************/
  25. SDLImageLoader::SDLImageLoader(SDLImage * Img):
  26. image(Img),
  27. lineStart(nullptr),
  28. position(nullptr)
  29. {
  30. }
  31. void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
  32. {
  33. //Init image
  34. image->surf = SDL_CreateRGBSurface(0, SpriteSize.x, SpriteSize.y, 8, 0, 0, 0, 0);
  35. image->margins = Margins;
  36. image->fullSize = FullSize;
  37. //Prepare surface
  38. SDL_Palette * p = SDL_AllocPalette(SDLImage::DEFAULT_PALETTE_COLORS);
  39. SDL_SetPaletteColors(p, pal, 0, SDLImage::DEFAULT_PALETTE_COLORS);
  40. SDL_SetSurfacePalette(image->surf, p);
  41. SDL_FreePalette(p);
  42. SDL_LockSurface(image->surf);
  43. lineStart = position = (ui8*)image->surf->pixels;
  44. }
  45. inline void SDLImageLoader::Load(size_t size, const ui8 * data)
  46. {
  47. if (size)
  48. {
  49. memcpy((void *)position, data, size);
  50. position += size;
  51. }
  52. }
  53. inline void SDLImageLoader::Load(size_t size, ui8 color)
  54. {
  55. if (size)
  56. {
  57. memset((void *)position, color, size);
  58. position += size;
  59. }
  60. }
  61. inline void SDLImageLoader::EndLine()
  62. {
  63. lineStart += image->surf->pitch;
  64. position = lineStart;
  65. }
  66. SDLImageLoader::~SDLImageLoader()
  67. {
  68. SDL_UnlockSurface(image->surf);
  69. SDL_SetColorKey(image->surf, SDL_TRUE, 0);
  70. //TODO: RLE if compressed and bpp>1
  71. }