CPlasmaRoutine.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // PlasmaRoutine.cpp: implementation of the CPlasmaRoutine class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CPlasmaRoutine.h"
  6. #include <math.h>
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CPlasmaRoutine::CPlasmaRoutine()
  14. {
  15. m_iHeight = 0;
  16. m_iWidth = 0;
  17. m_pPlasmaBits = NULL;
  18. m_iAlpha = 255;
  19. m_a1=0;
  20. m_a2=0;
  21. m_a3=0;
  22. m_a4=0;
  23. m_b1=0;
  24. m_b2=0;
  25. m_b3=0;
  26. m_b4=0;
  27. m_iModifier1=1;
  28. m_iModifier2=2;
  29. m_iModifier3=1;
  30. m_iModifier4=2;
  31. m_iXModifier1 = -1;
  32. m_iXModifier2 = 3;
  33. m_iYModifier1 = 1;
  34. m_iYModifier2 = -2;
  35. // Set up our defalt plasma colors
  36. m_PlasmaColors[0]=RGB(0,0,0);// From black
  37. m_PlasmaColors[1]=RGB(0,0,255);// To Blue
  38. m_PlasmaColors[2]=RGB(0,0,255);// From Blue
  39. m_PlasmaColors[3]=RGB(0,255,0);// To Green
  40. m_PlasmaColors[4]=RGB(0,255,0);// From Green
  41. m_PlasmaColors[5]=RGB(0,255,255);// To Cyan
  42. m_PlasmaColors[6]=RGB(0,255,255);// Cyan
  43. m_PlasmaColors[7]=RGB(0,255,255);
  44. m_PlasmaColors[8]=RGB(0,255,255);// Cyan
  45. m_PlasmaColors[9]=RGB(0,255,255);
  46. m_PlasmaColors[10]=RGB(0,255,255);// From White
  47. m_PlasmaColors[11]=RGB(0,255,0); // To dark green
  48. m_PlasmaColors[12]=RGB(0,255,0);// From Dark Blue
  49. m_PlasmaColors[13]=RGB(0,0,255);// To dark green
  50. m_PlasmaColors[14]=RGB(0,0,255);// From Dark Blue
  51. m_PlasmaColors[15]=RGB(0,0,0);// To Black
  52. }
  53. CPlasmaRoutine::~CPlasmaRoutine()
  54. {
  55. if(m_pPlasmaBits != NULL)
  56. {
  57. delete []m_pPlasmaBits;
  58. m_pPlasmaBits = NULL;
  59. }
  60. }
  61. void CPlasmaRoutine::InitCostBLTable()
  62. {
  63. for(int t=0;t<256;t++)
  64. m_icostbl[t] = (int)( 30 * cos(t * (3.14159/64) ) );
  65. }
  66. inline void CPlasmaRoutine::SetRGB(int iIndex,int R,int G,int B)
  67. {
  68. COLORREF color = RGB(R+50,G+50,B+50);
  69. m_pPalletteBuffer[iIndex] = color;
  70. }
  71. void CPlasmaRoutine::InitPallette()
  72. {
  73. // Create a gradient between all the colors we have for our plasma
  74. CreateGradient(m_PlasmaColors[0],m_PlasmaColors[1],32,&m_pPalletteBuffer[0]); // From black to Blue
  75. CreateGradient(m_PlasmaColors[2],m_PlasmaColors[3],32,&m_pPalletteBuffer[32]); // From Green to Blue
  76. CreateGradient(m_PlasmaColors[4],m_PlasmaColors[5],32,&m_pPalletteBuffer[64]); // From Green to White
  77. CreateGradient(m_PlasmaColors[6],m_PlasmaColors[6],32,&m_pPalletteBuffer[96]);// From Cyan to Cyan
  78. CreateGradient(m_PlasmaColors[8],m_PlasmaColors[9],32,&m_pPalletteBuffer[128]);// Cyan To Cyan
  79. CreateGradient(m_PlasmaColors[10],m_PlasmaColors[11],32,&m_pPalletteBuffer[160]);// While to dark Green
  80. CreateGradient(m_PlasmaColors[12],m_PlasmaColors[13],32,&m_pPalletteBuffer[192]);// Dark Blue to dark green
  81. CreateGradient(m_PlasmaColors[14],m_PlasmaColors[15],32,&m_pPalletteBuffer[224]);// Dark Blue to black
  82. }
  83. inline void CPlasmaRoutine::CreateGradient(COLORREF clrStart,COLORREF clrEnd,long lSteps,COLORREF* pBuffer)
  84. {
  85. // I created this routine to make real smooth gradients...
  86. // It may not be real optimized, but it works....and that is what matters to me right now...
  87. int r, g, b; // First distance, then starting value
  88. int rTotal,gTotal,bTotal;
  89. int roffset,goffset,boffset;
  90. int scalerR;
  91. int scalerG;
  92. int scalerB;
  93. roffset = goffset = boffset = 0;
  94. scalerR = scalerG = scalerB = 0;
  95. // Get the color differences and scalers
  96. rTotal = GetRValue(clrEnd) - GetRValue(clrStart);
  97. if(rTotal < 0)
  98. scalerR = -1;
  99. else if(rTotal > 0)
  100. scalerR = 1;
  101. gTotal = GetGValue(clrEnd) - GetGValue(clrStart);
  102. if(gTotal < 0)
  103. scalerG = -1;
  104. else if(gTotal > 0)
  105. scalerG = 1;
  106. bTotal = GetBValue(clrEnd) - GetBValue(clrStart);
  107. if(bTotal < 0)
  108. scalerB = -1;
  109. else if(bTotal > 0)
  110. scalerB = 1;
  111. // reset to positives
  112. rTotal=abs(rTotal);
  113. gTotal=abs(gTotal);
  114. bTotal=abs(bTotal);
  115. // Get the starting color values...
  116. r = GetRValue(clrStart);
  117. g = GetGValue(clrStart);
  118. b = GetBValue(clrStart);
  119. for(int i=0;i<lSteps;i++)
  120. {
  121. roffset = ::MulDiv(i, rTotal, lSteps);
  122. goffset = ::MulDiv(i, gTotal, lSteps);
  123. boffset = ::MulDiv(i, bTotal, lSteps);
  124. roffset*=scalerR;
  125. goffset*=scalerG;
  126. boffset*=scalerB;
  127. COLORREF color = RGB( (b+boffset),(g+goffset),(r+roffset));
  128. pBuffer[i] = color;
  129. }
  130. }
  131. void CPlasmaRoutine::CalcPlasma()
  132. {
  133. // Initialize with outer variables
  134. m_a1 = m_b1;
  135. m_a2 = m_b2;
  136. unsigned char *tscr = (unsigned char*)&m_pPlasmaBits[0];
  137. for(long y=0;y<m_iHeight;y++)
  138. {
  139. // Initialize with outer variables
  140. m_a3 = m_b3;
  141. m_a4 = m_b4;
  142. for(long x=0;x<m_iWidth;x++)
  143. {
  144. *tscr++ = m_icostbl[m_a1] +
  145. m_icostbl[m_a2] +
  146. m_icostbl[m_a3] +
  147. m_icostbl[m_a4] ;
  148. // Higher values result in many slower plasmas
  149. m_a3 += m_iModifier1;//4;
  150. m_a4 += m_iModifier2;//1;
  151. }
  152. // Same as the previous comment
  153. m_a1 += m_iModifier3;//1;
  154. m_a2 += m_iModifier4;//4;
  155. }
  156. m_b1 += m_iYModifier1;//y modifier 1
  157. m_b2 += m_iYModifier2;//y modifier 2
  158. m_b3 += m_iXModifier1;//x modifier 1
  159. m_b4 += m_iXModifier2;//x modifier 2
  160. }
  161. void CPlasmaRoutine::Create(int iWidth,int iHeight)
  162. {
  163. if(m_pPlasmaBits != NULL)
  164. delete [] m_pPlasmaBits;
  165. m_pPlasmaBits = new BYTE[(iWidth*iHeight)]; // 402*120 = 48240 ×Ö½ÚÄÚ´æÐ¹Â©;
  166. m_iHeight = iHeight;
  167. m_iWidth = iWidth;
  168. // zero out our plasma
  169. memset(m_pPlasmaBits,0,(iWidth*iHeight));
  170. // Init the Cost Table
  171. InitCostBLTable();
  172. // Init our pallette
  173. InitPallette();
  174. // Go ahead and calculate a plasma
  175. CalcPlasma();
  176. }
  177. void CPlasmaRoutine::Render(DWORD* pBits,int iwidth,int iheight,int iLineLength)
  178. {
  179. // Right now Im just going to blit it right onto the video memory
  180. unsigned char* pSrcBitlin;// = m_pFireBits+(m_iWidth*3);// get rid of our fire source
  181. BYTE *dst;//=(BYTE*)Dib->pVideoMemory;
  182. COLORREF myColor;
  183. BYTE r;
  184. BYTE g;
  185. BYTE b;
  186. for(int i=0;i<m_iHeight;i++)
  187. {
  188. if(i <= iheight)
  189. {
  190. dst = (BYTE*)&pBits[(iLineLength*i)];
  191. pSrcBitlin =&m_pPlasmaBits[m_iWidth*i];
  192. for(int x=0;x<m_iWidth;x++)
  193. {
  194. if(x <= iLineLength)
  195. {
  196. myColor =m_pPalletteBuffer[pSrcBitlin[x]];
  197. r = GetRValue(myColor);
  198. g = GetGValue(myColor);
  199. b = GetBValue(myColor);
  200. dst[0]=(BYTE)(((r-dst[0])*m_iAlpha+(dst[0]<<8))>>8);
  201. dst[1]=(BYTE)(((g-dst[1])*m_iAlpha+(dst[1]<<8))>>8);
  202. dst[2]=(BYTE)(((b-dst[2])*m_iAlpha+(dst[2]<<8))>>8);
  203. dst+=4;
  204. }
  205. }
  206. }
  207. }
  208. CalcPlasma();
  209. }
  210. void CPlasmaRoutine::SetDefaultValues(VARIANT* pExtParms)
  211. {
  212. m_iAlpha = 255;
  213. pExtParms[2].intVal =m_iAlpha;
  214. m_iModifier1=1;
  215. m_iModifier2=2;
  216. m_iModifier3=1;
  217. m_iModifier4=2;
  218. pExtParms[3].intVal = m_iModifier1;
  219. pExtParms[4].intVal = m_iModifier2;
  220. pExtParms[5].intVal = m_iModifier3;
  221. pExtParms[6].intVal = m_iModifier4;
  222. m_iXModifier1 = -1;
  223. m_iXModifier2 = 3;
  224. m_iYModifier1 = 1;
  225. m_iYModifier2 = -2;
  226. pExtParms[7].intVal = m_iXModifier1;
  227. pExtParms[8].intVal = m_iXModifier2;
  228. pExtParms[9].intVal = m_iYModifier1;
  229. pExtParms[10].intVal = m_iYModifier2;
  230. // Set up our defalt plasma colors
  231. m_PlasmaColors[0]=RGB(0,0,0);// From black
  232. m_PlasmaColors[1]=RGB(0,0,255);// To Blue
  233. m_PlasmaColors[2]=RGB(0,0,255);// From Blue
  234. m_PlasmaColors[3]=RGB(0,255,0);// To Green
  235. m_PlasmaColors[4]=RGB(0,255,0);// From Green
  236. m_PlasmaColors[5]=RGB(0,255,255);// To Cyan
  237. m_PlasmaColors[6]=RGB(0,255,255);// Cyan
  238. m_PlasmaColors[7]=RGB(0,255,255);
  239. m_PlasmaColors[8]=RGB(0,255,255);// Cyan
  240. m_PlasmaColors[9]=RGB(0,255,255);
  241. m_PlasmaColors[10]=RGB(0,255,255);// From White
  242. m_PlasmaColors[11]=RGB(0,255,0); // To dark green
  243. m_PlasmaColors[12]=RGB(0,255,0);// From Dark Blue
  244. m_PlasmaColors[13]=RGB(0,0,255);// To dark green
  245. m_PlasmaColors[14]=RGB(0,0,255);// From Dark Blue
  246. m_PlasmaColors[15]=RGB(0,0,0);// To Black
  247. pExtParms[11].ulVal = m_PlasmaColors[0];
  248. pExtParms[12].ulVal = m_PlasmaColors[1];
  249. pExtParms[13].ulVal = m_PlasmaColors[2];
  250. pExtParms[14].ulVal = m_PlasmaColors[3];
  251. pExtParms[15].ulVal = m_PlasmaColors[4];
  252. pExtParms[16].ulVal = m_PlasmaColors[5];
  253. pExtParms[17].ulVal = m_PlasmaColors[6];
  254. pExtParms[18].ulVal = m_PlasmaColors[7];
  255. pExtParms[19].ulVal = m_PlasmaColors[8];
  256. pExtParms[20].ulVal = m_PlasmaColors[9];
  257. pExtParms[21].ulVal = m_PlasmaColors[10];
  258. pExtParms[22].ulVal = m_PlasmaColors[11];
  259. pExtParms[23].ulVal = m_PlasmaColors[12];
  260. pExtParms[24].ulVal = m_PlasmaColors[13];
  261. pExtParms[25].ulVal = m_PlasmaColors[14];
  262. pExtParms[26].ulVal = m_PlasmaColors[15];
  263. InitPallette();
  264. }
  265. void CPlasmaRoutine::InitializePlasma(VARIANT* pExtParms)
  266. {
  267. m_iAlpha = pExtParms[2].intVal;
  268. m_iModifier1 = pExtParms[3].intVal;
  269. m_iModifier2 = pExtParms[4].intVal;
  270. m_iModifier3 = pExtParms[5].intVal;
  271. m_iModifier4 = pExtParms[6].intVal;
  272. m_iXModifier1 = pExtParms[7].intVal;
  273. m_iXModifier2 = pExtParms[8].intVal;
  274. m_iYModifier1 = pExtParms[9].intVal;
  275. m_iYModifier2 = pExtParms[10].intVal;
  276. m_PlasmaColors[0]=pExtParms[11].ulVal;
  277. m_PlasmaColors[1]=pExtParms[12].ulVal;
  278. m_PlasmaColors[2]=pExtParms[13].ulVal;
  279. m_PlasmaColors[3]=pExtParms[14].ulVal;
  280. m_PlasmaColors[4]=pExtParms[15].ulVal;
  281. m_PlasmaColors[5]=pExtParms[16].ulVal;
  282. m_PlasmaColors[6]=pExtParms[17].ulVal;
  283. m_PlasmaColors[7]=pExtParms[18].ulVal;
  284. m_PlasmaColors[8]=pExtParms[19].ulVal;
  285. m_PlasmaColors[9]=pExtParms[20].ulVal;
  286. m_PlasmaColors[10]=pExtParms[21].ulVal;
  287. m_PlasmaColors[11]=pExtParms[22].ulVal;
  288. m_PlasmaColors[12]=pExtParms[23].ulVal;
  289. m_PlasmaColors[13]=pExtParms[24].ulVal;
  290. m_PlasmaColors[14]=pExtParms[25].ulVal;
  291. m_PlasmaColors[15]=pExtParms[26].ulVal;
  292. if(pExtParms[0].intVal != m_iWidth || pExtParms[1].intVal != m_iHeight)
  293. Create(pExtParms[0].intVal,pExtParms[1].intVal);
  294. else
  295. InitPallette();
  296. }