Images.cpp 9.3 KB

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