No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

126 líneas
2.4 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. Array<String> pathlist = System::GetPathList(path);
  48. for (int i = 0; i < pathlist.Count(); i++)
  49. {
  50. m_img = IMG_Load(pathlist[i].C());
  51. if (m_img)
  52. break;
  53. }
  54. if (!m_img)
  55. {
  56. #if !LOL_RELEASE
  57. Log::Error("could not load image %s\n", path);
  58. #endif
  59. return false;
  60. }
  61. size = ivec2(m_img->w, m_img->h);
  62. if (m_img->format->BytesPerPixel != 4)
  63. {
  64. SDL_Surface *tmp = Create32BppSurface(size);
  65. SDL_BlitSurface(m_img, nullptr, tmp, nullptr);
  66. SDL_FreeSurface(m_img);
  67. m_img = tmp;
  68. }
  69. format = m_img->format->Amask ? Image::FORMAT_RGBA : Image::FORMAT_RGB;
  70. return true;
  71. }
  72. bool SdlImageData::Close()
  73. {
  74. SDL_FreeSurface(m_img);
  75. return true;
  76. }
  77. void * SdlImageData::GetData() const
  78. {
  79. return m_img->pixels;
  80. }
  81. SDL_Surface *SdlImageData::Create32BppSurface(ivec2 size)
  82. {
  83. uint32_t rmask, gmask, bmask, amask;
  84. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  85. rmask = 0xff000000;
  86. gmask = 0x00ff0000;
  87. bmask = 0x0000ff00;
  88. amask = 0x000000ff;
  89. #else
  90. rmask = 0x000000ff;
  91. gmask = 0x0000ff00;
  92. bmask = 0x00ff0000;
  93. amask = 0xff000000;
  94. #endif
  95. return SDL_CreateRGBSurface(SDL_SWSURFACE, size.x, size.y, 32,
  96. rmask, gmask, bmask, amask);
  97. }
  98. } /* namespace lol */
  99. #endif /* defined USE_SDL_IMAGE */