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

120 строки
2.3 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. #if defined USE_SDL_IMAGE
  14. #if defined HAVE_SDL_SDL_H
  15. # include <SDL/SDL.h>
  16. #else
  17. # include <SDL.h>
  18. #endif
  19. #if defined HAVE_SDL_SDL_IMAGE_H
  20. # include <SDL/SDL_image.h>
  21. #else
  22. # include <SDL_image.h>
  23. #endif
  24. #include "core.h"
  25. #include "../../image/image-private.h"
  26. using namespace std;
  27. namespace lol
  28. {
  29. /*
  30. * Image implementation class
  31. */
  32. DECLARE_IMAGE_LOADER(SdlImageData, 50)
  33. {
  34. public:
  35. virtual bool Open(char const *);
  36. virtual bool Close();
  37. virtual void *GetData() const;
  38. static SDL_Surface *Create32BppSurface(ivec2 size);
  39. private:
  40. SDL_Surface *m_img;
  41. };
  42. /*
  43. * Public Image class
  44. */
  45. bool SdlImageData::Open(char const *path)
  46. {
  47. String fullpath = String(System::GetDataDir()) + String(path);
  48. m_img = IMG_Load(&fullpath[0]);
  49. if (!m_img)
  50. {
  51. #if !LOL_RELEASE
  52. Log::Error("could not load %s\n", &fullpath[0]);
  53. #endif
  54. return false;
  55. }
  56. size = ivec2(m_img->w, m_img->h);
  57. if (m_img->format->BytesPerPixel != 4)
  58. {
  59. SDL_Surface *tmp = Create32BppSurface(size);
  60. SDL_BlitSurface(m_img, NULL, tmp, NULL);
  61. SDL_FreeSurface(m_img);
  62. m_img = tmp;
  63. }
  64. format = m_img->format->Amask ? Image::FORMAT_RGBA : Image::FORMAT_RGB;
  65. return true;
  66. }
  67. bool SdlImageData::Close()
  68. {
  69. SDL_FreeSurface(m_img);
  70. return true;
  71. }
  72. void * SdlImageData::GetData() const
  73. {
  74. return m_img->pixels;
  75. }
  76. SDL_Surface *SdlImageData::Create32BppSurface(ivec2 size)
  77. {
  78. uint32_t rmask, gmask, bmask, amask;
  79. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  80. rmask = 0xff000000;
  81. gmask = 0x00ff0000;
  82. bmask = 0x0000ff00;
  83. amask = 0x000000ff;
  84. #else
  85. rmask = 0x000000ff;
  86. gmask = 0x0000ff00;
  87. bmask = 0x00ff0000;
  88. amask = 0xff000000;
  89. #endif
  90. return SDL_CreateRGBSurface(SDL_SWSURFACE, size.x, size.y, 32,
  91. rmask, gmask, bmask, amask);
  92. }
  93. } /* namespace lol */
  94. #endif /* defined USE_SDL_IMAGE */