CBitmapHandler.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "StdInc.h"
  2. #include "../lib/Filesystem/CResourceLoader.h"
  3. #include "../lib/Filesystem/CFileInfo.h"
  4. #include "SDL.h"
  5. #include "SDL_image.h"
  6. #include "CBitmapHandler.h"
  7. #include "CDefHandler.h"
  8. #include "UIFramework/SDL_Extensions.h"
  9. #include "../lib/vcmi_endian.h"
  10. /*
  11. * CBitmapHandler.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. bool isPCX(const ui8 *header)//check whether file can be PCX according to header
  20. {
  21. int fSize = read_le_u32(header + 0);
  22. int width = read_le_u32(header + 4);
  23. int height = read_le_u32(header + 8);
  24. return fSize == width*height || fSize == width*height*3;
  25. }
  26. enum Epcxformat
  27. {
  28. PCX8B,
  29. PCX24B
  30. };
  31. SDL_Surface * BitmapHandler::loadH3PCX(ui8 * pcx, size_t size)
  32. {
  33. SDL_Surface * ret;
  34. Epcxformat format;
  35. int it=0;
  36. ui32 fSize = read_le_u32(pcx + it); it+=4;
  37. ui32 width = read_le_u32(pcx + it); it+=4;
  38. ui32 height = read_le_u32(pcx + it); it+=4;
  39. if (fSize==width*height*3)
  40. format=PCX24B;
  41. else if (fSize==width*height)
  42. format=PCX8B;
  43. else
  44. return nullptr;
  45. if (format==PCX8B)
  46. {
  47. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
  48. it = 0xC;
  49. for (int i=0; i<height; i++)
  50. {
  51. memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width);
  52. it+= width;
  53. }
  54. //palette - last 256*3 bytes
  55. it = size-256*3;
  56. for (int i=0;i<256;i++)
  57. {
  58. SDL_Color tp;
  59. tp.r = pcx[it++];
  60. tp.g = pcx[it++];
  61. tp.b = pcx[it++];
  62. tp.unused = SDL_ALPHA_OPAQUE;
  63. ret->format->palette->colors[i] = tp;
  64. }
  65. }
  66. else
  67. {
  68. ret = CSDL_Ext::createSurfaceWithBpp<3>(width, height);
  69. //it == 0xC;
  70. for (int i=0; i<height; i++)
  71. {
  72. memcpy((char*)ret->pixels + ret->pitch * i, pcx + it, width*3);
  73. it+= width*3;
  74. }
  75. }
  76. return ret;
  77. }
  78. SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname, bool setKey)
  79. {
  80. if(!fname.size())
  81. {
  82. tlog2 << "Call to loadBitmap with void fname!\n";
  83. return NULL;
  84. }
  85. if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE)))
  86. {
  87. return nullptr;
  88. }
  89. SDL_Surface * ret=NULL;
  90. auto readFile = CResourceHandler::get()->loadData(
  91. ResourceID(path + fname, EResType::IMAGE));
  92. if (isPCX(readFile.first.get()))
  93. {//H3-style PCX
  94. ret = loadH3PCX(readFile.first.get(), readFile.second);
  95. if (ret)
  96. {
  97. if(ret->format->BytesPerPixel == 1 && setKey)
  98. {
  99. const SDL_Color &c = ret->format->palette->colors[0];
  100. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b));
  101. }
  102. }
  103. else
  104. tlog1<<"Failed to open "<<fname<<" as H3 PCX!\n";
  105. }
  106. else
  107. { //loading via SDL_Image
  108. CFileInfo info(CResourceHandler::get()->getResourceName(ResourceID(path + fname, EResType::IMAGE)));
  109. ret = IMG_LoadTyped_RW(
  110. //create SDL_RW with our data (will be deleted by SDL)
  111. SDL_RWFromConstMem((void*)readFile.first.release(), readFile.second),
  112. 1, // mark it for auto-deleting
  113. &info.getExtension()[0] + 1); //pass extension without dot (+1 character)
  114. if (!ret)
  115. tlog1<<"Failed to open "<<fname<<" via SDL_Image\n";
  116. if (ret->format->palette)
  117. {
  118. //set correct value for alpha\unused channel
  119. for (int i=0; i< ret->format->palette->ncolors; i++)
  120. ret->format->palette->colors[i].unused = 255;
  121. }
  122. }
  123. return ret;
  124. }
  125. SDL_Surface * BitmapHandler::loadBitmap(std::string fname, bool setKey)
  126. {
  127. SDL_Surface *bitmap;
  128. if (!(bitmap = loadBitmapFromDir("DATA/", fname, setKey)) &&
  129. !(bitmap = loadBitmapFromDir("SPRITES/", fname, setKey)))
  130. tlog0<<"Error: Failed to find file "<<fname<<"\n";
  131. return bitmap;
  132. }