alpha.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // alpha.cpp
  19. //
  20. // History:
  21. // 10/04/96 JMI Started.
  22. //
  23. //
  24. //////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Cheezy ass alpha blit.
  27. //
  28. //////////////////////////////////////////////////////////////////////////////
  29. //////////////////////////////////////////////////////////////////////////////
  30. // Includes.
  31. //////////////////////////////////////////////////////////////////////////////
  32. /*
  33. #include "System.h"
  34. #ifdef PATHS_IN_INCLUDES
  35. #include "GREEN/Image/Image.h"
  36. #include "GREEN/BLiT/BLIT.H"
  37. #else
  38. #include "Image.h"
  39. #include "BLIT.H"
  40. #endif
  41. #else
  42. */
  43. #include "RSPiX.h"
  44. #include "alpha.h"
  45. //////////////////////////////////////////////////////////////////////////////
  46. // Macros.
  47. //////////////////////////////////////////////////////////////////////////////
  48. #define TRANS_INDEX 14
  49. //////////////////////////////////////////////////////////////////////////////
  50. // HACKS.
  51. //////////////////////////////////////////////////////////////////////////////
  52. #if 0
  53. //========================================================================================
  54. extern short rspBlitT(U8 u8Trans, RImage* pimSrc,RImage* pimDst,short sSrcX,short sSrcY,short sDstX,
  55. short sDstY,short sW,short sH,RRect* prDst,const RRect* prSrc);
  56. //========================================================================================
  57. #endif
  58. //////////////////////////////////////////////////////////////////////////////
  59. // Instantiate statics.
  60. //////////////////////////////////////////////////////////////////////////////
  61. ///////////////////////////////////////////////////////////////////////////
  62. // Con/Destruction.
  63. ///////////////////////////////////////////////////////////////////////////
  64. ///////////////////////////////////////////////////////////////////////////
  65. // Functions.
  66. ///////////////////////////////////////////////////////////////////////////
  67. ///////////////////////////////////////////////////////////////////////////
  68. //
  69. // Whatever this ends up doing, it will suck and eventually be replaced
  70. // so that's the comment or something.
  71. //
  72. ///////////////////////////////////////////////////////////////////////////
  73. static short Alpha( // Returns 0 on success.
  74. RImage* pimSrc, // Source to blit through pimMask.
  75. RImage* pimMask, // Mask.
  76. RImage* pimDst, // Destination for masked blit.
  77. short sSrcX, // X coordinate in source.
  78. short sSrcY, // Y coordinate in source.
  79. short sDstX, // X coordinate in dest.
  80. short sDstY, // Y coordinate in dest.
  81. short sW, // Width to blt.
  82. short sH) // Height to blt.
  83. {
  84. short sRes = 0; // Assume success.
  85. short sMaskX = 0;
  86. short sMaskY = 0;
  87. // X/W Clip.
  88. if (sSrcX < 0)
  89. {
  90. sW += sSrcX;
  91. sDstX -= sSrcX;
  92. sMaskX -= sSrcX;
  93. sSrcX = 0;
  94. }
  95. if (sDstX < 0)
  96. {
  97. sW += sDstX;
  98. sSrcX -= sDstX;
  99. sMaskX -= sDstX;
  100. sDstX = 0;
  101. }
  102. if (sSrcX + sW > pimSrc->m_sWidth)
  103. {
  104. sW = pimSrc->m_sWidth - sSrcX;
  105. }
  106. if (sDstX + sW > pimDst->m_sWidth)
  107. {
  108. sW = pimDst->m_sWidth - sDstX;
  109. }
  110. if (sMaskX + sW > pimMask->m_sWidth)
  111. {
  112. sW = pimMask->m_sWidth;
  113. }
  114. // Y/H Clip.
  115. if (sSrcY < 0)
  116. {
  117. sH += sSrcY;
  118. sDstY -= sSrcY;
  119. sMaskY -= sSrcY;
  120. sSrcY = 0;
  121. }
  122. if (sDstY < 0)
  123. {
  124. sH += sDstY;
  125. sSrcY -= sDstY;
  126. sMaskY -= sDstY;
  127. sDstY = 0;
  128. }
  129. if (sSrcY + sH > pimSrc->m_sHeight)
  130. {
  131. sH = pimSrc->m_sHeight - sSrcY;
  132. }
  133. if (sDstY + sH > pimDst->m_sHeight)
  134. {
  135. sH = pimDst->m_sHeight - sDstY;
  136. }
  137. if (sMaskY + sH > pimMask->m_sHeight)
  138. {
  139. sH = pimMask->m_sHeight;
  140. }
  141. // If there's anything left . . .
  142. if (sW > 0 && sH > 0)
  143. {
  144. U8* pu8SrcRow = pimSrc->m_pData + sSrcX + sSrcY * pimSrc->m_lPitch;
  145. U8* pu8SrcBlt;
  146. U8* pu8MaskRow = pimMask->m_pData + sMaskX + sMaskY * pimMask->m_lPitch;
  147. U8* pu8MaskBlt;
  148. U8* pu8DstRow = pimDst->m_pData + sDstX + sDstY * pimDst->m_lPitch;
  149. U8* pu8DstBlt;
  150. short sWidth;
  151. while (sH--)
  152. {
  153. pu8SrcBlt = pu8SrcRow;
  154. pu8MaskBlt = pu8MaskRow;
  155. pu8DstBlt = pu8DstRow;
  156. sWidth = sW;
  157. while (sWidth--)
  158. {
  159. if (*pu8MaskBlt++ != 0)
  160. {
  161. *pu8DstBlt = *pu8SrcBlt;
  162. }
  163. pu8DstBlt++;
  164. pu8SrcBlt++;
  165. }
  166. pu8SrcRow += pimSrc->m_lPitch;
  167. pu8MaskRow += pimMask->m_lPitch;
  168. pu8DstRow += pimDst->m_lPitch;
  169. }
  170. }
  171. return sRes;
  172. }
  173. ///////////////////////////////////////////////////////////////////////////
  174. //
  175. // BLiTs using loaded m_imMask (8 bpp only) as mask.
  176. // 0 in mask indicates opaque. Other values are punch through.
  177. //
  178. ///////////////////////////////////////////////////////////////////////////
  179. short CAlpha::Blit( // Returns 0 on success.
  180. RImage* pimSrc, // Source image.
  181. RImage* pimDst, // Destination image.
  182. short sSrcX, // Source coordinate in pimSrc to start effect. Can
  183. // be negative.
  184. short sSrcY, // Source coordinate in pimSrc to start effect. Can
  185. // be negative.
  186. short sDstX, // Destination coordinate in pimDst for pimSrc(0,0).
  187. short sDstY, // Destination coordinate in pimDst for pimSrc(0,0).
  188. RRect* prc) // Rectangle to clip Dst to.
  189. {
  190. short sRes = 0; // Assume success.
  191. // If there is any data . . .
  192. if (m_imMask.m_pData != NULL || m_imMask.m_pSpecial != NULL)
  193. {
  194. RImage imDecompress;
  195. // Allocate image.
  196. if (imDecompress.CreateImage(
  197. pimSrc->m_sWidth,
  198. pimSrc->m_sHeight,
  199. RImage::BMP8,
  200. 0, // Use default pitch.
  201. pimSrc->m_sDepth) == 0)
  202. {
  203. // Decompress sprite.
  204. rspBlit(pimSrc, &imDecompress, 0, 0);
  205. // Blit transparency into decompression buffer.
  206. rspBlitT(TRANS_INDEX,
  207. &m_imMask,
  208. &imDecompress,
  209. 0, 0,
  210. sSrcX, sSrcY,
  211. m_imMask.m_sWidth, m_imMask.m_sHeight,
  212. NULL,
  213. NULL);
  214. // Blit to screen.
  215. rspBlitT(
  216. 0,
  217. &imDecompress,
  218. pimDst,
  219. 0, 0,
  220. sDstX, sDstY,
  221. imDecompress.m_sWidth, imDecompress.m_sHeight,
  222. prc,
  223. NULL);
  224. }
  225. else
  226. {
  227. TRACE("Blit(): Unable to allocate image to decompress pimSrc.\n");
  228. sRes = -2;
  229. }
  230. }
  231. else
  232. {
  233. TRACE("Blit(): No mask! Use regular blt.\n");
  234. sRes = -1;
  235. }
  236. return sRes;
  237. }
  238. ///////////////////////////////////////////////////////////////////////////
  239. //
  240. // Loads the specified file into m_imMask using RImage::Load() and calls
  241. // Convert(FSPR1) to prepare the data.
  242. //
  243. ///////////////////////////////////////////////////////////////////////////
  244. short CAlpha::Load( // Returns 0 on success.
  245. char* pszFileName) // Filename to load.
  246. {
  247. short sRes = 0; // Assume success.
  248. if (m_imMask.Load(pszFileName) == 0)
  249. {
  250. #if 0
  251. rspSetConvertToFSPR1(128, 0);
  252. if (m_imMask.Convert(FSPR1) == FSPR1)
  253. {
  254. // Success.
  255. }
  256. else
  257. {
  258. TRACE("Load(): RImage::Convert(FSPR1) failed.\n");
  259. sRes = -2;
  260. }
  261. #endif
  262. m_sShadowW = m_imMask.m_sWidth;
  263. m_sShadowH = 5;
  264. m_sShadowX = 0;
  265. m_sShadowY = m_imMask.m_sHeight - m_sShadowH;
  266. }
  267. else
  268. {
  269. TRACE("Load(): RImage::Load() failed.\n");
  270. sRes = -1;
  271. }
  272. return sRes;
  273. }
  274. //////////////////////////////////////////////////////////////////////////////
  275. // EOF
  276. //////////////////////////////////////////////////////////////////////////////