r_water.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. // alphachannel safe 11/21/99
  25. #include <windows.h>
  26. #include <commctrl.h>
  27. #include "r_defs.h"
  28. #include "resource.h"
  29. #include "timing.h"
  30. #include "../Agave/Language/api_language.h"
  31. #ifndef LASER
  32. #define C_THISCLASS C_WaterClass
  33. #define MOD_NAME "Trans / Water"
  34. class C_THISCLASS : public C_RBASE2 {
  35. protected:
  36. public:
  37. C_THISCLASS();
  38. virtual ~C_THISCLASS();
  39. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  40. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_WATER,desc,128):desc); }
  41. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  42. virtual void load_config(unsigned char *data, int len);
  43. virtual int save_config(unsigned char *data);
  44. virtual int smp_getflags() { return 1; }
  45. virtual int smp_begin(int max_threads, char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  46. virtual void smp_render(int this_thread, int max_threads, char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  47. virtual int smp_finish(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h); // return value is that of render() for fbstuff etc
  48. unsigned int *lastframe;
  49. int lastframe_len;
  50. int enabled;
  51. };
  52. #define PUT_INT(y) data[pos]=(y)&255; data[pos+1]=(y>>8)&255; data[pos+2]=(y>>16)&255; data[pos+3]=(y>>24)&255
  53. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  54. void C_THISCLASS::load_config(unsigned char *data, int len)
  55. {
  56. int pos=0;
  57. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  58. }
  59. int C_THISCLASS::save_config(unsigned char *data)
  60. {
  61. int pos=0;
  62. PUT_INT(enabled); pos+=4;
  63. return pos;
  64. }
  65. C_THISCLASS::C_THISCLASS()
  66. {
  67. enabled=1;
  68. lastframe_len=0;
  69. lastframe=NULL;
  70. }
  71. C_THISCLASS::~C_THISCLASS()
  72. {
  73. if (lastframe) GlobalFree(lastframe);
  74. }
  75. #define _R(x) (( x ) & 0xff)
  76. #define _G(x) ((( x )) & 0xff00)
  77. #define _B(x) ((( x )) & 0xff0000)
  78. #define _RGB(r,g,b) (( r ) | (( g ) & 0xff00) | (( b ) & 0xff0000))
  79. static const int zero=0;
  80. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  81. {
  82. smp_begin(1,visdata,isBeat,framebuffer,fbout,w,h);
  83. if (isBeat & 0x80000000) return 0;
  84. smp_render(0,1,visdata,isBeat,framebuffer,fbout,w,h);
  85. return smp_finish(visdata,isBeat,framebuffer,fbout,w,h);
  86. }
  87. int C_THISCLASS::smp_begin(int max_threads, char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  88. {
  89. if (!enabled) return 0;
  90. if (!lastframe || w*h != lastframe_len)
  91. {
  92. if (lastframe) GlobalFree(lastframe);
  93. lastframe_len=w*h;
  94. lastframe=(unsigned int *)GlobalAlloc(GPTR,w*h*sizeof(int));
  95. }
  96. return max_threads;
  97. }
  98. int C_THISCLASS::smp_finish(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h) // return value is that of render() for fbstuff etc
  99. {
  100. return !!enabled;
  101. }
  102. void C_THISCLASS::smp_render(int this_thread, int max_threads, char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  103. {
  104. if (!enabled) return;
  105. unsigned int *f = (unsigned int *) framebuffer;
  106. unsigned int *of = (unsigned int *) fbout;
  107. unsigned int *lfo = (unsigned int *) lastframe;
  108. int start_l = ( this_thread * h ) / max_threads;
  109. int end_l;
  110. if (this_thread >= max_threads - 1) end_l = h;
  111. else end_l = ( (this_thread+1) * h ) / max_threads;
  112. int outh=end_l-start_l;
  113. if (outh<1) return;
  114. int skip_pix=start_l*w;
  115. f += skip_pix;
  116. of+= skip_pix;
  117. lfo += skip_pix;
  118. int at_top=0, at_bottom=0;
  119. if (!this_thread) at_top=1;
  120. if (this_thread >= max_threads - 1) at_bottom=1;
  121. timingEnter(0);
  122. {
  123. if (at_top)
  124. // top line
  125. {
  126. int x;
  127. // left edge
  128. {
  129. int r=_R(f[1]); int g=_G(f[1]); int b=_B(f[1]);
  130. r += _R(f[w]); g += _G(f[w]); b += _B(f[w]);
  131. f++;
  132. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  133. lfo++;
  134. if (r < 0) r=0;
  135. else if (r > 255) r=255;
  136. if (g < 0) g=0;
  137. else if (g > 255*256) g=255*256;
  138. if (b < 0) b=0;
  139. else if (b > 255*65536) b=255*65536;
  140. *of++=_RGB(r,g,b);
  141. }
  142. // middle of line
  143. x=(w-2);
  144. while (x--)
  145. {
  146. int r=_R(f[1]); int g=_G(f[1]); int b=_B(f[1]);
  147. r += _R(f[-1]); g += _G(f[-1]); b += _B(f[-1]);
  148. r += _R(f[w]); g += _G(f[w]); b += _B(f[w]);
  149. f++;
  150. r/=2; g/=2; b/=2;
  151. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  152. lfo++;
  153. if (r < 0) r=0;
  154. else if (r > 255) r=255;
  155. if (g < 0) g=0;
  156. else if (g > 255*256) g=255*256;
  157. if (b < 0) b=0;
  158. else if (b > 255*65536) b=255*65536;
  159. *of++=_RGB(r,g,b);
  160. }
  161. // right block
  162. {
  163. int r=_R(f[-1]); int g=_G(f[-1]); int b=_B(f[-1]);
  164. r += _R(f[w]); g += _G(f[w]); b += _B(f[w]);
  165. f++;
  166. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  167. lfo++;
  168. if (r < 0) r=0;
  169. else if (r > 255) r=255;
  170. if (g < 0) g=0;
  171. else if (g > 255*256) g=255*256;
  172. if (b < 0) b=0;
  173. else if (b > 255*65536) b=255*65536;
  174. *of++=_RGB(r,g,b);
  175. }
  176. }
  177. // middle block
  178. {
  179. int y=outh-at_top-at_bottom;
  180. while (y--)
  181. {
  182. int x;
  183. // left edge
  184. {
  185. int r=_R(f[1]); int g=_G(f[1]); int b=_B(f[1]);
  186. r += _R(f[w]); g += _G(f[w]); b += _B(f[w]);
  187. r += _R(f[-w]); g += _G(f[-w]); b += _B(f[-w]);
  188. f++;
  189. r/=2; g/=2; b/=2;
  190. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  191. lfo++;
  192. if (r < 0) r=0;
  193. else if (r > 255) r=255;
  194. if (g < 0) g=0;
  195. else if (g > 255*256) g=255*256;
  196. if (b < 0) b=0;
  197. else if (b > 255*65536) b=255*65536;
  198. *of++=_RGB(r,g,b);
  199. }
  200. // middle of line
  201. x=(w-2);
  202. #ifdef NO_MMX
  203. while (x--)
  204. {
  205. int r=_R(f[1]); int g=_G(f[1]); int b=_B(f[1]);
  206. r += _R(f[-1]); g += _G(f[-1]); b += _B(f[-1]);
  207. r += _R(f[w]); g += _G(f[w]); b += _B(f[w]);
  208. r += _R(f[-w]); g += _G(f[-w]); b += _B(f[-w]);
  209. f++;
  210. r/=2; g/=2; b/=2;
  211. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  212. lfo++;
  213. if (r < 0) r=0;
  214. else if (r > 255) r=255;
  215. if (g < 0) g=0;
  216. else if (g > 255*256) g=255*256;
  217. if (b < 0) b=0;
  218. else if (b > 255*65536) b=255*65536;
  219. *of++=_RGB(r,g,b);
  220. }
  221. #else
  222. __asm
  223. {
  224. mov esi, f
  225. mov edi, of
  226. mov edx, lfo
  227. mov ecx, x
  228. mov ebx, w
  229. shl ebx, 2
  230. shr ecx, 1
  231. sub esi, ebx
  232. align 16
  233. mmx_water_loop1:
  234. movd mm0, [esi+ebx+4]
  235. movd mm1, [esi+ebx-4]
  236. punpcklbw mm0, [zero]
  237. movd mm2, [esi+ebx*2]
  238. punpcklbw mm1, [zero]
  239. movd mm3, [esi]
  240. punpcklbw mm2, [zero]
  241. movd mm4, [edx]
  242. paddw mm0, mm1
  243. punpcklbw mm3, [zero]
  244. movd mm7, [esi+ebx+8]
  245. punpcklbw mm4, [zero]
  246. paddw mm2, mm3
  247. movd mm6, [esi+ebx]
  248. paddw mm0, mm2
  249. psrlw mm0, 1
  250. punpcklbw mm7, [zero]
  251. movd mm2, [esi+ebx*2+4]
  252. psubw mm0, mm4
  253. movd mm3, [esi+4]
  254. packuswb mm0, mm0
  255. movd [edi], mm0
  256. punpcklbw mm6, [zero]
  257. movd mm4, [edx+4]
  258. punpcklbw mm2, [zero]
  259. paddw mm7, mm6
  260. punpcklbw mm3, [zero]
  261. punpcklbw mm4, [zero]
  262. paddw mm2, mm3
  263. paddw mm7, mm2
  264. add edx, 8
  265. psrlw mm7, 1
  266. add esi, 8
  267. psubw mm7, mm4
  268. packuswb mm7, mm7
  269. movd [edi+4], mm7
  270. add edi, 8
  271. dec ecx
  272. jnz mmx_water_loop1
  273. add esi, ebx
  274. mov f, esi
  275. mov of, edi
  276. mov lfo, edx
  277. };
  278. #endif
  279. // right block
  280. {
  281. int r=_R(f[-1]); int g=_G(f[-1]); int b=_B(f[-1]);
  282. r += _R(f[w]); g += _G(f[w]); b += _B(f[w]);
  283. r += _R(f[-w]); g += _G(f[-w]); b += _B(f[-w]);
  284. f++;
  285. r/=2; g/=2; b/=2;
  286. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  287. lfo++;
  288. if (r < 0) r=0;
  289. else if (r > 255) r=255;
  290. if (g < 0) g=0;
  291. else if (g > 255*256) g=255*256;
  292. if (b < 0) b=0;
  293. else if (b > 255*65536) b=255*65536;
  294. *of++=_RGB(r,g,b);
  295. }
  296. }
  297. }
  298. // bottom line
  299. if (at_bottom)
  300. {
  301. int x;
  302. // left edge
  303. {
  304. int r=_R(f[1]); int g=_G(f[1]); int b=_B(f[1]);
  305. r += _R(f[-w]); g += _G(f[-w]); b += _B(f[-w]);
  306. f++;
  307. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  308. lfo++;
  309. if (r < 0) r=0;
  310. else if (r > 255) r=255;
  311. if (g < 0) g=0;
  312. else if (g > 255*256) g=255*256;
  313. if (b < 0) b=0;
  314. else if (b > 255*65536) b=255*65536;
  315. *of++=_RGB(r,g,b);
  316. }
  317. // middle of line
  318. x=(w-2);
  319. while (x--)
  320. {
  321. int r=_R(f[1]); int g=_G(f[1]); int b=_B(f[1]);
  322. r += _R(f[-1]); g += _G(f[-1]); b += _B(f[-1]);
  323. r += _R(f[-w]); g += _G(f[-w]); b += _B(f[-w]);
  324. f++;
  325. r/=2; g/=2; b/=2;
  326. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  327. lfo++;
  328. if (r < 0) r=0;
  329. else if (r > 255) r=255;
  330. if (g < 0) g=0;
  331. else if (g > 255*256) g=255*256;
  332. if (b < 0) b=0;
  333. else if (b > 255*65536) b=255*65536;
  334. *of++=_RGB(r,g,b);
  335. }
  336. // right block
  337. {
  338. int r=_R(f[-1]); int g=_G(f[-1]); int b=_B(f[-1]);
  339. r += _R(f[-w]); g += _G(f[-w]); b += _B(f[-w]);
  340. f++;
  341. r-=_R(lfo[0]); g-=_G(lfo[0]); b-=_B(lfo[0]);
  342. lfo++;
  343. if (r < 0) r=0;
  344. else if (r > 255) r=255;
  345. if (g < 0) g=0;
  346. else if (g > 255*256) g=255*256;
  347. if (b < 0) b=0;
  348. else if (b > 255*65536) b=255*65536;
  349. *of++=_RGB(r,g,b);
  350. }
  351. }
  352. }
  353. memcpy(lastframe+skip_pix,framebuffer+skip_pix,w*outh*sizeof(int));
  354. #ifndef NO_MMX
  355. __asm emms;
  356. #endif
  357. timingLeave(0);
  358. }
  359. C_RBASE *R_Water(char *desc)
  360. {
  361. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  362. return (C_RBASE *) new C_THISCLASS();
  363. }
  364. static C_THISCLASS *g_this;
  365. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  366. {
  367. switch (uMsg)
  368. {
  369. case WM_INITDIALOG:
  370. if (g_this->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  371. return 1;
  372. case WM_COMMAND:
  373. if (LOWORD(wParam) == IDC_CHECK1)
  374. {
  375. if (IsDlgButtonChecked(hwndDlg,IDC_CHECK1))
  376. g_this->enabled=1;
  377. else
  378. g_this->enabled=0;
  379. }
  380. return 0;
  381. }
  382. return 0;
  383. }
  384. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent)
  385. {
  386. g_this = this;
  387. return WASABI_API_CREATEDIALOG(IDD_CFG_WATER,hwndParent,g_DlgProc);
  388. }
  389. #else
  390. C_RBASE *R_Water(char *desc) { return NULL; }
  391. #endif