1
0

r_waterbump.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. #include <windows.h>
  25. #include <stdlib.h>
  26. #include <vfw.h>
  27. #include <commctrl.h>
  28. #include <math.h>
  29. #include "resource.h"
  30. #include "r_defs.h"
  31. #include "../Agave/Language/api_language.h"
  32. #ifndef LASER
  33. #define MOD_NAME "Trans / Water Bump"
  34. #define C_THISCLASS C_WaterBumpClass
  35. class C_THISCLASS : public C_RBASE {
  36. protected:
  37. public:
  38. C_THISCLASS();
  39. virtual ~C_THISCLASS();
  40. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  41. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_WATER_BUMP,desc,128):desc); }
  42. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  43. virtual void load_config(unsigned char *data, int len);
  44. virtual int save_config(unsigned char *data);
  45. void SineBlob(int x, int y, int radius, int height, int page);
  46. void CalcWater(int npage, int density);
  47. void CalcWaterSludge(int npage, int density);
  48. void HeightBlob(int x, int y, int radius, int height, int page);
  49. int enabled;
  50. int *buffers[2];
  51. int buffer_w,buffer_h;
  52. int page;
  53. int density;
  54. int depth;
  55. int random_drop;
  56. int drop_position_x;
  57. int drop_position_y;
  58. int drop_radius;
  59. int method;
  60. };
  61. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  62. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  63. // configuration read/write
  64. C_THISCLASS::C_THISCLASS() // set up default configuration
  65. {
  66. int i;
  67. enabled=1; density=6; depth=600; random_drop=0; drop_position_x=1; drop_position_y=1; drop_radius=40; method=0;
  68. buffer_w=0; buffer_h=0;
  69. for(i=0;i<2;i++)
  70. buffers[i]=NULL;
  71. page=0;
  72. }
  73. C_THISCLASS::~C_THISCLASS()
  74. {
  75. int i;
  76. for(i=0;i<2;i++) {
  77. if(buffers[i]) GlobalFree(buffers[i]);
  78. buffers[i]=NULL;
  79. }
  80. }
  81. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  82. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  83. {
  84. int pos=0;
  85. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  86. if (len-pos >= 4) { density=GET_INT(); pos+=4; }
  87. if (len-pos >= 4) { depth=GET_INT(); pos+=4; }
  88. if (len-pos >= 4) { random_drop=GET_INT(); pos+=4; }
  89. if (len-pos >= 4) { drop_position_x=GET_INT(); pos+=4; }
  90. if (len-pos >= 4) { drop_position_y=GET_INT(); pos+=4; }
  91. if (len-pos >= 4) { drop_radius=GET_INT(); pos+=4; }
  92. if (len-pos >= 4) { method=GET_INT(); pos+=4; }
  93. }
  94. #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
  95. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  96. {
  97. int pos=0;
  98. PUT_INT(enabled); pos+=4;
  99. PUT_INT(density); pos+=4;
  100. PUT_INT(depth); pos+=4;
  101. PUT_INT(random_drop); pos+=4;
  102. PUT_INT(drop_position_x); pos+=4;
  103. PUT_INT(drop_position_y); pos+=4;
  104. PUT_INT(drop_radius); pos+=4;
  105. PUT_INT(method); pos+=4;
  106. return pos;
  107. }
  108. void C_THISCLASS::SineBlob(int x, int y, int radius, int height, int page)
  109. {
  110. int cx, cy;
  111. int left,top,right,bottom;
  112. int square;
  113. double dist;
  114. int radsquare = radius * radius;
  115. double length = (1024.0/(float)radius)*(1024.0/(float)radius);
  116. if(x<0) x = 1+radius+ rand()%(buffer_w-2*radius-1);
  117. if(y<0) y = 1+radius+ rand()%(buffer_h-2*radius-1);
  118. radsquare = (radius*radius);
  119. left=-radius; right = radius;
  120. top=-radius; bottom = radius;
  121. // Perform edge clipping...
  122. if(x - radius < 1) left -= (x-radius-1);
  123. if(y - radius < 1) top -= (y-radius-1);
  124. if(x + radius > buffer_w-1) right -= (x+radius-buffer_w+1);
  125. if(y + radius > buffer_h-1) bottom-= (y+radius-buffer_h+1);
  126. for(cy = top; cy < bottom; cy++)
  127. {
  128. for(cx = left; cx < right; cx++)
  129. {
  130. square = cy*cy + cx*cx;
  131. if(square < radsquare)
  132. {
  133. dist = sqrt(square*length);
  134. buffers[page][buffer_w*(cy+y) + cx+x]
  135. += (int)((cos(dist)+0xffff)*(height)) >> 19;
  136. }
  137. }
  138. }
  139. }
  140. void C_THISCLASS::HeightBlob(int x, int y, int radius, int height, int page)
  141. {
  142. int rquad;
  143. int cx, cy, cyq;
  144. int left, top, right, bottom;
  145. rquad = radius * radius;
  146. // Make a randomly-placed blob...
  147. if(x<0) x = 1+radius+ rand()%(buffer_w-2*radius-1);
  148. if(y<0) y = 1+radius+ rand()%(buffer_h-2*radius-1);
  149. left=-radius; right = radius;
  150. top=-radius; bottom = radius;
  151. // Perform edge clipping...
  152. if(x - radius < 1) left -= (x-radius-1);
  153. if(y - radius < 1) top -= (y-radius-1);
  154. if(x + radius > buffer_w-1) right -= (x+radius-buffer_w+1);
  155. if(y + radius > buffer_h-1) bottom-= (y+radius-buffer_h+1);
  156. for(cy = top; cy < bottom; cy++)
  157. {
  158. cyq = cy*cy;
  159. for(cx = left; cx < right; cx++)
  160. {
  161. if(cx*cx + cyq < rquad)
  162. buffers[page][buffer_w*(cy+y) + (cx+x)] += height;
  163. }
  164. }
  165. }
  166. void C_THISCLASS::CalcWater(int npage, int density)
  167. {
  168. int newh;
  169. int count = buffer_w + 1;
  170. int *newptr = buffers[npage];
  171. int *oldptr = buffers[!npage];
  172. int x, y;
  173. for (y = (buffer_h-1)*buffer_w; count < y; count += 2)
  174. {
  175. for (x = count+buffer_w-2; count < x; count++)
  176. {
  177. // This does the eight-pixel method. It looks much better.
  178. newh = ((oldptr[count + buffer_w]
  179. + oldptr[count - buffer_w]
  180. + oldptr[count + 1]
  181. + oldptr[count - 1]
  182. + oldptr[count - buffer_w - 1]
  183. + oldptr[count - buffer_w + 1]
  184. + oldptr[count + buffer_w - 1]
  185. + oldptr[count + buffer_w + 1]
  186. ) >> 2 )
  187. - newptr[count];
  188. newptr[count] = newh - (newh >> density);
  189. }
  190. }
  191. }
  192. /*
  193. void C_THISCLASS::CalcWaterSludge(int npage, int density)
  194. {
  195. int newh;
  196. int count = buffer_w + 1;
  197. int *newptr = buffers[npage];
  198. int *oldptr = buffers[!npage];
  199. int x, y;
  200. for (y = (buffer_h-1)*buffer_w; count < y; count += 2)
  201. {
  202. for (x = count+buffer_w-2; count < x; count++)
  203. {
  204. // This is the "sludge" method...
  205. newh = (oldptr[count]<<2)
  206. + oldptr[count-1-buffer_w]
  207. + oldptr[count+1-buffer_w]
  208. + oldptr[count-1+buffer_w]
  209. + oldptr[count+1+buffer_w]
  210. + ((oldptr[count-1]
  211. + oldptr[count+1]
  212. + oldptr[count-buffer_w]
  213. + oldptr[count+buffer_w])<<1);
  214. newptr[count] = (newh-(newh>>6)) >> density;
  215. }
  216. }
  217. }
  218. */
  219. // render function
  220. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  221. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  222. // w and h are the width and height of the screen, in pixels.
  223. // isBeat is 1 if a beat has been detected.
  224. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  225. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  226. {
  227. if (!enabled) return 0;
  228. int l,i;
  229. l=w*h;
  230. if(buffer_w!=w||buffer_h!=h) {
  231. for(i=0;i<2;i++) {
  232. if(buffers[i])GlobalFree(buffers[i]);
  233. buffers[i]=NULL;
  234. }
  235. }
  236. if(buffers[0]==NULL) {
  237. for(i=0;i<2;i++) {
  238. buffers[i]=(int *)GlobalAlloc(GPTR,w*h*sizeof(int));
  239. }
  240. buffer_w=w;
  241. buffer_h=h;
  242. }
  243. if (isBeat&0x80000000) return 0;
  244. if(isBeat) {
  245. if(random_drop) {
  246. int max=w;
  247. if(h>w) max=h;
  248. SineBlob(-1,-1,drop_radius*max/100,-depth,page);
  249. } else {
  250. int x,y;
  251. switch(drop_position_x) {
  252. case 0: x=w/4; break;
  253. case 1: x=w/2; break;
  254. case 2: x=w*3/4; break;
  255. }
  256. switch(drop_position_y) {
  257. case 0: y=h/4; break;
  258. case 1: y=h/2; break;
  259. case 2: y=h*3/4; break;
  260. }
  261. SineBlob(x,y,drop_radius,-depth,page);
  262. }
  263. // HeightBlob(-1,-1,80/2,1400,page);
  264. }
  265. {
  266. int dx, dy;
  267. int x, y;
  268. int ofs,len=buffer_h*buffer_w;
  269. int offset=buffer_w + 1;
  270. int *ptr = buffers[page];
  271. for (y = (buffer_h-1)*buffer_w; offset < y; offset += 2)
  272. {
  273. for (x = offset+buffer_w-2; offset < x; offset++)
  274. {
  275. dx = ptr[offset] - ptr[offset+1];
  276. dy = ptr[offset] - ptr[offset+buffer_w];
  277. ofs=offset + buffer_w*(dy>>3) + (dx>>3);
  278. if((ofs<len)&&(ofs>-1))
  279. fbout[offset] = framebuffer[ofs];
  280. else
  281. fbout[offset] = framebuffer[offset];
  282. offset++;
  283. dx = ptr[offset] - ptr[offset+1];
  284. dy = ptr[offset] - ptr[offset+buffer_w];
  285. ofs=offset + buffer_w*(dy>>3) + (dx>>3);
  286. if((ofs<len)&&(ofs>-1))
  287. fbout[offset] = framebuffer[ofs];
  288. else
  289. fbout[offset] = framebuffer[offset];
  290. }
  291. }
  292. }
  293. CalcWater(!page,density);
  294. page=!page;
  295. return 1;
  296. }
  297. // configuration dialog stuff
  298. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  299. {
  300. switch (uMsg)
  301. {
  302. case WM_INITDIALOG:
  303. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  304. SendDlgItemMessage(hwndDlg,IDC_DAMP,TBM_SETRANGEMIN,0,2);
  305. SendDlgItemMessage(hwndDlg,IDC_DAMP,TBM_SETRANGEMAX,0,10);
  306. SendDlgItemMessage(hwndDlg,IDC_DAMP,TBM_SETPOS,1,g_ConfigThis->density);
  307. SendDlgItemMessage(hwndDlg,IDC_DEPTH,TBM_SETRANGEMIN,0,100);
  308. SendDlgItemMessage(hwndDlg,IDC_DEPTH,TBM_SETRANGEMAX,0,2000);
  309. SendDlgItemMessage(hwndDlg,IDC_DEPTH,TBM_SETPOS,1,g_ConfigThis->depth);
  310. SendDlgItemMessage(hwndDlg,IDC_RADIUS,TBM_SETRANGEMIN,0,10);
  311. SendDlgItemMessage(hwndDlg,IDC_RADIUS,TBM_SETRANGEMAX,0,100);
  312. SendDlgItemMessage(hwndDlg,IDC_RADIUS,TBM_SETPOS,1,g_ConfigThis->drop_radius);
  313. CheckDlgButton(hwndDlg,IDC_RANDOM_DROP,g_ConfigThis->random_drop);
  314. CheckDlgButton(hwndDlg,IDC_DROP_LEFT,g_ConfigThis->drop_position_x==0);
  315. CheckDlgButton(hwndDlg,IDC_DROP_CENTER,g_ConfigThis->drop_position_x==1);
  316. CheckDlgButton(hwndDlg,IDC_DROP_RIGHT,g_ConfigThis->drop_position_x==2);
  317. CheckDlgButton(hwndDlg,IDC_DROP_TOP,g_ConfigThis->drop_position_y==0);
  318. CheckDlgButton(hwndDlg,IDC_DROP_MIDDLE,g_ConfigThis->drop_position_y==1);
  319. CheckDlgButton(hwndDlg,IDC_DROP_BOTTOM,g_ConfigThis->drop_position_y==2);
  320. return 1;
  321. case WM_DRAWITEM:
  322. return 0;
  323. case WM_COMMAND:
  324. if (LOWORD(wParam) == IDC_CHECK1)
  325. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  326. if (LOWORD(wParam) == IDC_RANDOM_DROP)
  327. g_ConfigThis->random_drop=IsDlgButtonChecked(hwndDlg,IDC_RANDOM_DROP);
  328. if (LOWORD(wParam) == IDC_DROP_LEFT)
  329. g_ConfigThis->drop_position_x=0;
  330. if (LOWORD(wParam) == IDC_DROP_CENTER)
  331. g_ConfigThis->drop_position_x=1;
  332. if (LOWORD(wParam) == IDC_DROP_RIGHT)
  333. g_ConfigThis->drop_position_x=2;
  334. if (LOWORD(wParam) == IDC_DROP_TOP)
  335. g_ConfigThis->drop_position_y=0;
  336. if (LOWORD(wParam) == IDC_DROP_MIDDLE)
  337. g_ConfigThis->drop_position_y=1;
  338. if (LOWORD(wParam) == IDC_DROP_BOTTOM)
  339. g_ConfigThis->drop_position_y=2;
  340. return 0;
  341. case WM_HSCROLL:
  342. {
  343. HWND swnd = (HWND) lParam;
  344. int t = (int) SendMessage(swnd,TBM_GETPOS,0,0);
  345. if (swnd == GetDlgItem(hwndDlg,IDC_DAMP))
  346. g_ConfigThis->density=t;
  347. if (swnd == GetDlgItem(hwndDlg,IDC_DEPTH))
  348. g_ConfigThis->depth=t;
  349. if (swnd == GetDlgItem(hwndDlg,IDC_RADIUS))
  350. g_ConfigThis->drop_radius=t;
  351. }
  352. return 0;
  353. }
  354. return 0;
  355. }
  356. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  357. {
  358. g_ConfigThis = this;
  359. return WASABI_API_CREATEDIALOG(IDD_CFG_WATERBUMP,hwndParent,g_DlgProc);
  360. }
  361. // export stuff
  362. C_RBASE *R_WaterBump(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  363. {
  364. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  365. return (C_RBASE *) new C_THISCLASS();
  366. }
  367. #else
  368. C_RBASE *R_WaterBump(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  369. {return NULL; }
  370. #endif