Images.cpp 9.2 KB

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