Images.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. {
  183. const ui32 rowStride = (w + 3) & ~3;
  184. const ui32 size = rowStride * h;
  185. buffer = new ui8[size];
  186. if (rowStride == w)
  187. {
  188. memcpy(buffer, pixBuff, size);
  189. return;
  190. }
  191. for (ui32 y=0; y<h; ++y)
  192. {
  193. memset(&buffer[rowStride*(y+1)-4], 0, 4);
  194. memcpy(&buffer[rowStride*y], &pixBuff[w*y], w);
  195. }
  196. width = rowStride;
  197. }
  198. CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
  199. CImage(w, h),
  200. palette(pal)
  201. {
  202. const ui32 rowStride = (w + 3) & ~3;
  203. buffer = new ui8[rowStride * h];
  204. switch (format)
  205. {
  206. case 1:
  207. {
  208. const ua_ui32_ptr rowsOffsets = (ua_ui32_ptr)pixBuff;
  209. for (ui32 y=0; y<h; ++y)
  210. {
  211. const ui8* srcRowPtr = pixBuff + SDL_SwapLE32(rowsOffsets[y]);
  212. ui8* dstRowPtr = buffer + (y * rowStride);
  213. ui32 rowLength = 0;
  214. do {
  215. ui8 segmentType = *(srcRowPtr++);
  216. size_t segmentLength = *(srcRowPtr++) + 1;
  217. if (segmentType == 0xFF)
  218. {
  219. memcpy(dstRowPtr, srcRowPtr, segmentLength);
  220. srcRowPtr += segmentLength;
  221. }
  222. else
  223. {
  224. memset(dstRowPtr, segmentType, segmentLength);
  225. }
  226. dstRowPtr += segmentLength;
  227. rowLength += segmentLength;
  228. }
  229. while (rowLength < w);
  230. }
  231. return;
  232. }
  233. default:
  234. return;
  235. }
  236. }
  237. CPalettedBitmap::~CPalettedBitmap()
  238. {
  239. delete buffer;
  240. palette.Unlink();
  241. }
  242. void CPalettedBitmap::textureTransfer()
  243. {
  244. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, buffer);
  245. palette.loadToVideoRAM();
  246. }
  247. void CPalettedBitmap::putAt(Point p)
  248. {
  249. loadToVideoRAM();
  250. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  251. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  252. GL2D::usePaletteBitmapShader(p.x, p.y);
  253. glRecti(p.x, p.y, p.x + width, p.y + height);
  254. }
  255. void CPalettedBitmap::putAt(Point p, TransformFlags flags)
  256. {
  257. putAt(p);
  258. }
  259. void CPalettedBitmap::putAt(Point p, TransformFlags flags, float scale)
  260. {
  261. loadToVideoRAM();
  262. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  263. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  264. GL2D::usePaletteBitmapShader(p.x, p.y);
  265. glRecti(p.x, p.y, p.x + (ui32)(width*scale), p.y + (ui32)(height*scale));
  266. }
  267. void CPalettedBitmap::putAt(Point p, TransformFlags flags, Rect clipRect)
  268. {
  269. putAt(p);
  270. }
  271. void CPalettedBitmap::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  272. {
  273. loadToVideoRAM();
  274. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  275. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  276. GL2D::usePaletteBitmapShader(p.x, p.y, cm);
  277. glRecti(p.x, p.y, p.x + width, p.y + height);
  278. }
  279. void CPalettedBitmap::putWithPlrColor(Point p, ColorRGBA c)
  280. {
  281. putAt(p);
  282. }
  283. void CPalettedBitmap::putWithPlrColor(Point p, ColorRGBA c, float scale)
  284. {
  285. putAt(p, NONE, scale);
  286. }
  287. /*********** CPalBitmapWithMargin ***********/
  288. CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
  289. CPaletteRGBA& pal, const ui8 pixBuff[]) :
  290. CPalettedBitmap(iw, ih, pal, pixBuff),
  291. leftMargin(lm), topMargin(tm),
  292. intWidth(iw), intHeight(ih)
  293. {
  294. width = fw;
  295. height = fh;
  296. }
  297. void CPalBitmapWithMargin::textureTransfer()
  298. {
  299. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, intWidth, intHeight, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, buffer);
  300. palette.loadToVideoRAM();
  301. }
  302. CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
  303. CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
  304. CPalettedBitmap(iw, ih, pal, pixBuff, format),
  305. leftMargin(lm), topMargin(tm),
  306. intWidth(iw), intHeight(ih)
  307. {
  308. width = fw;
  309. height = fh;
  310. }
  311. void CPalBitmapWithMargin::putAt(Point p)
  312. {
  313. loadToVideoRAM();
  314. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  315. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  316. GL2D::usePaletteBitmapShader(p.x, p.y);
  317. glRecti(p.x, p.y, p.x + intWidth, p.y + intHeight);
  318. }
  319. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags)
  320. {
  321. putAt(p);
  322. }
  323. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, float scale)
  324. {
  325. putAt(p);
  326. }
  327. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, Rect clipRect)
  328. {
  329. putAt(p);
  330. }
  331. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  332. {
  333. loadToVideoRAM();
  334. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  335. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  336. GL2D::usePaletteBitmapShader(p.x, p.y, cm);
  337. glRecti(p.x, p.y, p.x + width, p.y + height);
  338. }
  339. void CPalBitmapWithMargin::putWithPlrColor(Point p, ColorRGBA c)
  340. {
  341. putAt(p);
  342. }
  343. void CPalBitmapWithMargin::putWithPlrColor(Point p, ColorRGBA c, float scale)
  344. {
  345. putAt(p, NONE, scale);
  346. }
  347. }