Images.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. #include "StdInc.h"
  2. #include <SDL_opengl.h>
  3. #include <SDL_endian.h>
  4. #include "Images.h"
  5. namespace Gfx
  6. {
  7. /*********** CImage ***********/
  8. CImage::~CImage()
  9. {
  10. unloadFromVideoRAM();
  11. }
  12. void CImage::loadToVideoRAM()
  13. {
  14. if (texHandle > 0) return;
  15. glGenTextures(1, &texHandle);
  16. glBindTexture(GL_TEXTURE_RECTANGLE, texHandle);
  17. textureTransfer();
  18. }
  19. void CImage::unloadFromVideoRAM()
  20. {
  21. glDeleteTextures(1, &texHandle);
  22. texHandle = 0;
  23. }
  24. void CImage::bindTexture()
  25. {
  26. if (texHandle > 0)
  27. {
  28. glBindTexture(GL_TEXTURE_RECTANGLE, texHandle);
  29. return;
  30. }
  31. glGenTextures(1, &texHandle);
  32. glBindTexture(GL_TEXTURE_RECTANGLE, texHandle);
  33. textureTransfer();
  34. }
  35. /*********** CBitmap32::QuadInstance ***********/
  36. CBitmap32::QuadInstance::QuadInstance(Point p) : coords()
  37. {
  38. for (int i=0; i<4; ++i) coords[i].vertex = p;
  39. }
  40. void CBitmap32::QuadInstance::setOffset(TransformFlags flags, si32 x, si32 y)
  41. {
  42. if (flags & ROTATE_90_DEG)
  43. {
  44. }
  45. else
  46. {
  47. }
  48. }
  49. void CBitmap32::QuadInstance::transform(TransformFlags flags, ui32 w0, ui32 h0, ui32 w, ui32 h)
  50. {
  51. if (flags & MIRROR_HORIZ)
  52. {
  53. coords[0].vertex.x += w;
  54. coords[3].vertex.x += w;
  55. }
  56. else
  57. {
  58. coords[1].vertex.x += w;
  59. coords[2].vertex.x += w;
  60. }
  61. if (flags & MIRROR_VERTIC)
  62. {
  63. coords[0].vertex.y += h;
  64. coords[1].vertex.y += h;
  65. }
  66. else
  67. {
  68. coords[2].vertex.y += h;
  69. coords[3].vertex.y += h;
  70. }
  71. if (flags & ROTATE_90_DEG)
  72. {
  73. coords[0].texture.x = w0;
  74. coords[1].texture.x = w0;
  75. coords[1].texture.y = h0;
  76. coords[2].texture.y = h0;
  77. }
  78. else
  79. {
  80. coords[1].texture.x = w0;
  81. coords[2].texture.x = w0;
  82. coords[2].texture.y = h0;
  83. coords[3].texture.y = h0;
  84. }
  85. }
  86. void CBitmap32::QuadInstance::putToGL() const
  87. {
  88. glBegin(GL_QUADS);
  89. for (int i=0; i<4; ++i)
  90. {
  91. const CoordBind& row = coords[i];
  92. glTexCoord2i(row.texture.x, row.texture.y);
  93. glVertex2i(row.vertex.x, row.vertex.y);
  94. }
  95. glEnd();
  96. }
  97. /*********** CBitmap32 ***********/
  98. CBitmap32::CBitmap32(ui32 w, ui32 h, const ColorRGB pixBuff[], bool bgra) : CImage(w, h), formatBGRA(bgra)
  99. {
  100. const ui32 pixNum = w * h;
  101. buffer = new ColorRGBA[pixNum];
  102. for (ui32 it=0; it<pixNum; ++it)
  103. {
  104. memcpy(&buffer[it], &pixBuff[it], 3);
  105. buffer[it].comp.A = 255;
  106. }
  107. }
  108. CBitmap32::CBitmap32(ui32 w, ui32 h, const ColorRGBA pixBuff[], bool bgra) : CImage(w, h), formatBGRA(bgra)
  109. {
  110. const ui32 pixNum = w * h;
  111. buffer = new ColorRGBA[pixNum];
  112. memcpy(buffer, pixBuff, pixNum * sizeof(ColorRGBA));
  113. }
  114. CBitmap32::~CBitmap32()
  115. {
  116. delete buffer;
  117. }
  118. void CBitmap32::textureTransfer()
  119. {
  120. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA8, width, height, 0, formatBGRA ? GL_BGRA : GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  121. }
  122. void CBitmap32::putAt(Point p)
  123. {
  124. GL2D::useNoShader();
  125. bindTexture();
  126. glBegin(GL_QUADS);
  127. glTexCoord2i(0, 0);
  128. glVertex2i(p.x, p.y);
  129. glTexCoord2i(width, 0);
  130. glVertex2i(p.x + width, p.y);
  131. glTexCoord2i(width, height);
  132. glVertex2i(p.x + width, p.y + height);
  133. glTexCoord2i(0, height);
  134. glVertex2i(p.x, p.y + height);
  135. glEnd();
  136. }
  137. void CBitmap32::putAt(Point p, TransformFlags flags)
  138. {
  139. QuadInstance qi(p);
  140. qi.transform(flags, width, height, width, height);
  141. GL2D::useNoShader();
  142. bindTexture();
  143. qi.putToGL();
  144. }
  145. void CBitmap32::putAt(Point p, TransformFlags flags, float scale)
  146. {
  147. QuadInstance qi(p);
  148. qi.transform(flags, width, height, (ui32)(width*scale), (ui32)(height*scale));
  149. GL2D::useNoShader();
  150. bindTexture();
  151. qi.putToGL();
  152. }
  153. void CBitmap32::putAt(Point p, TransformFlags flags, Rect clipRect)
  154. {
  155. QuadInstance qi(p);
  156. qi.setOffset(flags, clipRect.lt.x, clipRect.lt.y);
  157. qi.transform(flags, clipRect.rb.x - p.x, clipRect.rb.y - p.y, clipRect.width(), clipRect.height());
  158. GL2D::useNoShader();
  159. bindTexture();
  160. qi.putToGL();
  161. }
  162. void CBitmap32::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  163. {
  164. QuadInstance qi(p);
  165. qi.transform(flags, width, height, width, height);
  166. GL2D::useColorizeShader(cm);
  167. bindTexture();
  168. qi.putToGL();
  169. }
  170. void CBitmap32::putWithPlrColor(Point p, ColorRGBA c)
  171. {
  172. putAt(p);
  173. }
  174. void CBitmap32::putWithPlrColor(Point p, ColorRGBA c, float scale)
  175. {
  176. putAt(p, NONE, scale);
  177. }
  178. /*********** CPalettedBitmap ***********/
  179. CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[]) :
  180. CImage(w, h),
  181. palette(pal),
  182. realWidth((w + 3) & ~3),
  183. realHeight(h)
  184. {
  185. const ui32 size = realWidth * h;
  186. buffer = new ui8[size];
  187. if (realWidth == w)
  188. {
  189. memcpy(buffer, pixBuff, size);
  190. return;
  191. }
  192. for (ui32 y=0; y<h; ++y)
  193. {
  194. memcpy(&buffer[realWidth*y], &pixBuff[w*y], w);
  195. }
  196. realWidth = w;
  197. }
  198. CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
  199. CImage(w, h),
  200. palette(pal),
  201. realWidth((w + 3) & ~3),
  202. realHeight(h)
  203. {
  204. buffer = new ui8[realWidth * h];
  205. switch (format)
  206. {
  207. case 1:
  208. {
  209. const ua_ui32_ptr rowsOffsets = (ua_ui32_ptr)pixBuff;
  210. for (ui32 y=0; y<h; ++y)
  211. {
  212. const ui8* srcRowPtr = pixBuff + SDL_SwapLE32(rowsOffsets[y]);
  213. ui8* dstRowPtr = buffer + (y * realWidth);
  214. ui32 rowLength = 0;
  215. do {
  216. ui8 segmentType = *(srcRowPtr++);
  217. size_t segmentLength = *(srcRowPtr++) + 1;
  218. if (segmentType == 0xFF)
  219. {
  220. memcpy(dstRowPtr, srcRowPtr, segmentLength);
  221. srcRowPtr += segmentLength;
  222. }
  223. else
  224. {
  225. memset(dstRowPtr, segmentType, segmentLength);
  226. }
  227. dstRowPtr += segmentLength;
  228. rowLength += segmentLength;
  229. }
  230. while (rowLength < w);
  231. }
  232. realWidth = w;
  233. return;
  234. }
  235. default:
  236. return;
  237. }
  238. }
  239. CPalettedBitmap::~CPalettedBitmap()
  240. {
  241. delete buffer;
  242. palette.Unlink();
  243. }
  244. void CPalettedBitmap::textureTransfer()
  245. {
  246. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, realWidth, realHeight, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, buffer);
  247. palette.loadToVideoRAM();
  248. }
  249. void CPalettedBitmap::putAt(Point p)
  250. {
  251. loadToVideoRAM();
  252. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  253. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  254. GL2D::usePaletteBitmapShader(p.x, p.y);
  255. glRecti(p.x, p.y, p.x + realWidth, p.y + realHeight);
  256. }
  257. void CPalettedBitmap::putAt(Point p, TransformFlags flags)
  258. {
  259. putAt(p);
  260. }
  261. void CPalettedBitmap::putAt(Point p, TransformFlags flags, float scale)
  262. {
  263. loadToVideoRAM();
  264. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  265. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  266. GL2D::usePaletteBitmapShader(p.x, p.y);
  267. glRecti(p.x, p.y, p.x + (ui32)(realWidth*scale), p.y + (ui32)(realHeight*scale));
  268. }
  269. void CPalettedBitmap::putAt(Point p, TransformFlags flags, Rect clipRect)
  270. {
  271. putAt(p);
  272. }
  273. void CPalettedBitmap::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  274. {
  275. loadToVideoRAM();
  276. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  277. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  278. GL2D::usePaletteBitmapShader(p.x, p.y, cm);
  279. glRecti(p.x, p.y, p.x + width, p.y + height);
  280. }
  281. void CPalettedBitmap::putWithPlrColor(Point p, ColorRGBA c)
  282. {
  283. putAt(p);
  284. }
  285. void CPalettedBitmap::putWithPlrColor(Point p, ColorRGBA c, float scale)
  286. {
  287. putAt(p, NONE, scale);
  288. }
  289. /*********** CPalBitmapWithMargin ***********/
  290. CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
  291. CPaletteRGBA& pal, const ui8 pixBuff[]) :
  292. CPalettedBitmap(iw, ih, pal, pixBuff),
  293. leftMargin(lm), topMargin(tm)
  294. {
  295. width = fw;
  296. height = fh;
  297. }
  298. CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
  299. CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
  300. CPalettedBitmap(iw, ih, pal, pixBuff, format),
  301. leftMargin(lm), topMargin(tm)
  302. {
  303. width = fw;
  304. height = fh;
  305. }
  306. void CPalBitmapWithMargin::putAt(Point p)
  307. {
  308. loadToVideoRAM();
  309. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  310. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  311. GL2D::usePaletteBitmapShader(p.x + leftMargin, p.y + topMargin);
  312. glRecti(p.x + leftMargin, p.y + topMargin, p.x + realWidth, p.y + realHeight);
  313. }
  314. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags)
  315. {
  316. putAt(p);
  317. }
  318. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, float scale)
  319. {
  320. putAt(p);
  321. }
  322. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, Rect clipRect)
  323. {
  324. putAt(p);
  325. }
  326. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  327. {
  328. loadToVideoRAM();
  329. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  330. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  331. GL2D::usePaletteBitmapShader(p.x + leftMargin, p.y + topMargin);
  332. glRecti(p.x + leftMargin, p.y + topMargin, p.x + realWidth, p.y + realHeight);
  333. }
  334. void CPalBitmapWithMargin::putWithPlrColor(Point p, ColorRGBA c)
  335. {
  336. putAt(p);
  337. }
  338. void CPalBitmapWithMargin::putWithPlrColor(Point p, ColorRGBA c, float scale)
  339. {
  340. putAt(p, NONE, scale);
  341. }
  342. }