SDLImageLoader.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "SDLImageLoader.h"
  12. #include "SDLImage.h"
  13. #include "../../lib/Point.h"
  14. #include <SDL_surface.h>
  15. SDLImageLoader::SDLImageLoader(SDLImageConst * Img):
  16. image(Img),
  17. lineStart(nullptr),
  18. position(nullptr)
  19. {
  20. }
  21. void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
  22. {
  23. //Init image
  24. image->surf = SDL_CreateRGBSurface(0, SpriteSize.x, SpriteSize.y, 8, 0, 0, 0, 0);
  25. image->margins = Margins;
  26. image->fullSize = FullSize;
  27. //Prepare surface
  28. SDL_Palette * p = SDL_AllocPalette(DEFAULT_PALETTE_COLORS);
  29. SDL_SetPaletteColors(p, pal, 0, DEFAULT_PALETTE_COLORS);
  30. SDL_SetSurfacePalette(image->surf, p);
  31. SDL_FreePalette(p);
  32. SDL_LockSurface(image->surf);
  33. lineStart = position = (ui8*)image->surf->pixels;
  34. }
  35. inline void SDLImageLoader::load(size_t size, const ui8 * data)
  36. {
  37. if (size)
  38. {
  39. memcpy((void *)position, data, size);
  40. position += size;
  41. }
  42. }
  43. inline void SDLImageLoader::load(size_t size, ui8 color)
  44. {
  45. if (size)
  46. {
  47. memset((void *)position, color, size);
  48. position += size;
  49. }
  50. }
  51. inline void SDLImageLoader::endLine()
  52. {
  53. lineStart += image->surf->pitch;
  54. position = lineStart;
  55. }
  56. SDLImageLoader::~SDLImageLoader()
  57. {
  58. SDL_UnlockSurface(image->surf);
  59. SDL_SetColorKey(image->surf, SDL_TRUE, 0);
  60. //TODO: RLE if compressed and bpp>1
  61. }