Images.cpp 9.3 KB

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