Images.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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[]) : CImage(w, h)
  99. {
  100. const ui32 size = w * h;
  101. buffer = new ColorRGBA[size];
  102. for (ui32 it=0; it<size; ++it)
  103. {
  104. memcpy(&buffer[it], &pixBuff[it], 3);
  105. buffer[it].comp.A = 255;
  106. }
  107. }
  108. CBitmap32::~CBitmap32()
  109. {
  110. delete buffer;
  111. }
  112. void CBitmap32::textureTransfer()
  113. {
  114. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  115. }
  116. void CBitmap32::putAt(Point p)
  117. {
  118. GL2D::useNoShader();
  119. bindTexture();
  120. glBegin(GL_QUADS);
  121. glTexCoord2i(0, 0);
  122. glVertex2i(p.x, p.y);
  123. glTexCoord2i(width, 0);
  124. glVertex2i(p.x + width, p.y);
  125. glTexCoord2i(width, height);
  126. glVertex2i(p.x + width, p.y + height);
  127. glTexCoord2i(0, height);
  128. glVertex2i(p.x, p.y + height);
  129. glEnd();
  130. }
  131. void CBitmap32::putAt(Point p, TransformFlags flags)
  132. {
  133. QuadInstance qi(p);
  134. qi.transform(flags, width, height, width, height);
  135. GL2D::useNoShader();
  136. bindTexture();
  137. qi.putToGL();
  138. }
  139. void CBitmap32::putAt(Point p, TransformFlags flags, float scale)
  140. {
  141. QuadInstance qi(p);
  142. qi.transform(flags, width, height, width*scale, height*scale);
  143. GL2D::useNoShader();
  144. bindTexture();
  145. qi.putToGL();
  146. }
  147. void CBitmap32::putAt(Point p, TransformFlags flags, Rect clipRect)
  148. {
  149. QuadInstance qi(p);
  150. qi.setOffset(flags, clipRect.lt.x, clipRect.lt.y);
  151. qi.transform(flags, clipRect.rb.x - p.x, clipRect.rb.y - p.y, clipRect.width(), clipRect.height());
  152. GL2D::useNoShader();
  153. bindTexture();
  154. qi.putToGL();
  155. }
  156. void CBitmap32::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  157. {
  158. QuadInstance qi(p);
  159. qi.transform(flags, width, height, width, height);
  160. GL2D::useColorizeShader(cm);
  161. bindTexture();
  162. qi.putToGL();
  163. }
  164. void CBitmap32::putWithPlrColor(Point p, ColorRGBA c)
  165. {
  166. putAt(p);
  167. }
  168. void CBitmap32::putWithPlrColor(Point p, ColorRGBA c, float scale)
  169. {
  170. putAt(p, NONE, scale);
  171. }
  172. /*********** CPalettedBitmap ***********/
  173. CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[]) :
  174. CImage(w, h),
  175. palette(pal)
  176. {
  177. const ui32 rowStride = (w + 3) & ~3;
  178. const ui32 size = rowStride * h;
  179. buffer = new ui8[size];
  180. if (rowStride == w)
  181. {
  182. memcpy(buffer, pixBuff, size);
  183. return;
  184. }
  185. for (ui32 y=0; y<h; ++y)
  186. {
  187. memset(&buffer[rowStride*(y+1)-4], 0, 4);
  188. memcpy(&buffer[rowStride*y], &pixBuff[w*y], w);
  189. }
  190. width = rowStride;
  191. }
  192. CPalettedBitmap::CPalettedBitmap(ui32 w, ui32 h, CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
  193. CImage(w, h),
  194. palette(pal)
  195. {
  196. const ui32 rowStride = (w + 3) & ~3;
  197. buffer = new ui8[rowStride * h];
  198. switch (format)
  199. {
  200. case 1:
  201. {
  202. const ua_ui32_ptr rowsOffsets = (ua_ui32_ptr)pixBuff;
  203. for (ui32 y=0; y<h; ++y)
  204. {
  205. const ui8* srcRowPtr = pixBuff + SDL_SwapLE32(rowsOffsets[y]);
  206. ui8* dstRowPtr = buffer + (y * rowStride);
  207. ui32 rowLength = 0;
  208. do {
  209. ui8 segmentType = *(srcRowPtr++);
  210. size_t segmentLength = *(srcRowPtr++) + 1;
  211. if (segmentType == 0xFF)
  212. {
  213. memcpy(dstRowPtr, srcRowPtr, segmentLength);
  214. srcRowPtr += segmentLength;
  215. }
  216. else
  217. {
  218. memset(dstRowPtr, segmentType, segmentLength);
  219. }
  220. dstRowPtr += segmentLength;
  221. rowLength += segmentLength;
  222. }
  223. while (rowLength < w);
  224. }
  225. return;
  226. }
  227. default:
  228. return;
  229. }
  230. }
  231. CPalettedBitmap::~CPalettedBitmap()
  232. {
  233. delete buffer;
  234. palette.Unlink();
  235. }
  236. void CPalettedBitmap::textureTransfer()
  237. {
  238. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, buffer);
  239. palette.loadToVideoRAM();
  240. }
  241. void CPalettedBitmap::putAt(Point p)
  242. {
  243. loadToVideoRAM();
  244. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  245. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  246. GL2D::usePaletteBitmapShader(p.x, p.y);
  247. glRecti(p.x, p.y, p.x + width, p.y + height);
  248. }
  249. void CPalettedBitmap::putAt(Point p, TransformFlags flags)
  250. {
  251. putAt(p);
  252. }
  253. void CPalettedBitmap::putAt(Point p, TransformFlags flags, float scale)
  254. {
  255. loadToVideoRAM();
  256. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  257. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  258. GL2D::usePaletteBitmapShader(p.x, p.y);
  259. glRecti(p.x, p.y, p.x + width*scale, p.y + height*scale);
  260. }
  261. void CPalettedBitmap::putAt(Point p, TransformFlags flags, Rect clipRect)
  262. {
  263. putAt(p);
  264. }
  265. void CPalettedBitmap::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  266. {
  267. loadToVideoRAM();
  268. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  269. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  270. GL2D::usePaletteBitmapShader(p.x, p.y, cm);
  271. glRecti(p.x, p.y, p.x + width, p.y + height);
  272. }
  273. void CPalettedBitmap::putWithPlrColor(Point p, ColorRGBA c)
  274. {
  275. putAt(p);
  276. }
  277. void CPalettedBitmap::putWithPlrColor(Point p, ColorRGBA c, float scale)
  278. {
  279. putAt(p, NONE, scale);
  280. }
  281. /*********** CPalBitmapWithMargin ***********/
  282. CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
  283. CPaletteRGBA& pal, const ui8 pixBuff[]) :
  284. CPalettedBitmap(iw, ih, pal, pixBuff),
  285. leftMargin(lm), topMargin(tm),
  286. intWidth(iw), intHeight(ih)
  287. {
  288. width = fw;
  289. height = fh;
  290. }
  291. void CPalBitmapWithMargin::textureTransfer()
  292. {
  293. glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_R8UI, intWidth, intHeight, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, buffer);
  294. palette.loadToVideoRAM();
  295. }
  296. CPalBitmapWithMargin::CPalBitmapWithMargin(ui32 fw, ui32 fh, ui32 lm, ui32 tm, ui32 iw, ui32 ih,
  297. CPaletteRGBA& pal, const ui8 pixBuff[], ui32 format) :
  298. CPalettedBitmap(iw, ih, pal, pixBuff, format),
  299. leftMargin(lm), topMargin(tm),
  300. intWidth(iw), intHeight(ih)
  301. {
  302. width = fw;
  303. height = fh;
  304. }
  305. void CPalBitmapWithMargin::putAt(Point p)
  306. {
  307. loadToVideoRAM();
  308. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  309. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  310. GL2D::usePaletteBitmapShader(p.x, p.y);
  311. glRecti(p.x, p.y, p.x + intWidth, p.y + intHeight);
  312. }
  313. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags)
  314. {
  315. putAt(p);
  316. }
  317. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, float scale)
  318. {
  319. putAt(p);
  320. }
  321. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, Rect clipRect)
  322. {
  323. putAt(p);
  324. }
  325. void CPalBitmapWithMargin::putAt(Point p, TransformFlags flags, const ColorMatrix cm)
  326. {
  327. loadToVideoRAM();
  328. GL2D::assignTexture(GL_TEXTURE1, GL_TEXTURE_1D, palette.getTexHandle());
  329. GL2D::assignTexture(GL_TEXTURE0, GL_TEXTURE_RECTANGLE, texHandle);
  330. GL2D::usePaletteBitmapShader(p.x, p.y, cm);
  331. glRecti(p.x, p.y, p.x + width, p.y + height);
  332. }
  333. void CPalBitmapWithMargin::putWithPlrColor(Point p, ColorRGBA c)
  334. {
  335. putAt(p);
  336. }
  337. void CPalBitmapWithMargin::putWithPlrColor(Point p, ColorRGBA c, float scale)
  338. {
  339. putAt(p, NONE, scale);
  340. }
  341. }