r_mirror.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <commctrl.h>
  26. #include "resource.h"
  27. #include "r_defs.h"
  28. #include "../Agave/Language/api_language.h"
  29. #ifndef LASER
  30. #define MOD_NAME "Trans / Mirror"
  31. #define C_THISCLASS C_MirrorClass
  32. class C_THISCLASS : public C_RBASE {
  33. protected:
  34. public:
  35. C_THISCLASS();
  36. virtual ~C_THISCLASS() { }
  37. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  38. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_MIRROR,desc,128):desc); }
  39. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  40. virtual void load_config(unsigned char *data, int len);
  41. virtual int save_config(unsigned char *data);
  42. int framecount;
  43. int enabled;
  44. int mode;
  45. int onbeat;
  46. int rbeat;
  47. int smooth;
  48. int slower;
  49. };
  50. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  51. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  52. #define HORIZONTAL1 1
  53. #define HORIZONTAL2 2
  54. #define VERTICAL1 4
  55. #define VERTICAL2 8
  56. #define D_HORIZONTAL1 0
  57. #define D_HORIZONTAL2 8
  58. #define D_VERTICAL1 16
  59. #define D_VERTICAL2 24
  60. #define M_HORIZONTAL1 0xFF
  61. #define M_HORIZONTAL2 0xFF00
  62. #define M_VERTICAL1 0xFF0000
  63. #define M_VERTICAL2 0xFF000000
  64. // configuration read/write
  65. C_THISCLASS::C_THISCLASS() // set up default configuration
  66. {
  67. framecount=0;
  68. enabled=1;
  69. onbeat=0;
  70. smooth=0;
  71. slower=4;
  72. mode=HORIZONTAL1;
  73. }
  74. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  75. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  76. {
  77. int pos=0;
  78. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  79. if (len-pos >= 4) { mode=GET_INT(); pos+=4; }
  80. if (len-pos >= 4) { onbeat=GET_INT(); pos+=4; }
  81. if (len-pos >= 4) { smooth=GET_INT(); pos+=4; }
  82. if (len-pos >= 4) { slower=GET_INT(); pos+=4; }
  83. }
  84. #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
  85. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  86. {
  87. int pos=0;
  88. PUT_INT(enabled); pos+=4;
  89. PUT_INT(mode); pos+=4;
  90. PUT_INT(onbeat); pos+=4;
  91. PUT_INT(smooth); pos+=4;
  92. PUT_INT(slower); pos+=4;
  93. return pos;
  94. }
  95. static unsigned int __inline BLEND_ADAPT(unsigned int a, unsigned int b, /*float*/int divisor)
  96. {
  97. return ((((a >> 4) & 0x0F0F0F) * (16-divisor) + (((b >> 4) & 0x0F0F0F) * divisor)));
  98. }
  99. // render function
  100. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  101. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  102. // w and h are the width and height of the screen, in pixels.
  103. // isBeat is 1 if a beat has been detected.
  104. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  105. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  106. {
  107. int hi,t,j,halfw,halfh,m,d;
  108. int *thismode=&mode;
  109. static int lastMode;
  110. static int divisors=0;
  111. static int inc=0;
  112. int divis;
  113. int *fbp;
  114. if (isBeat&0x80000000) return 0;
  115. if (!enabled) return 0;
  116. t=w*h;
  117. halfw = w / 2 ;
  118. halfh = h / 2 ;
  119. if (onbeat)
  120. {
  121. if (isBeat) rbeat = (rand()%16) & mode;
  122. thismode=&rbeat;
  123. }
  124. if (*thismode != lastMode)
  125. {
  126. int dif = *thismode ^ lastMode;
  127. int i;
  128. for (i=1,m=0xFF,d=0;i<16;i<<=1,m<<=8,d+=8)
  129. if (dif & i)
  130. {
  131. inc = (inc & ~m) | ((lastMode & i) ? 0xFF : 1) << d;
  132. if (!(divisors & m)) divisors = (divisors & ~m) | ((lastMode & i) ? 16 : 1) << d;
  133. }
  134. lastMode = *thismode;
  135. }
  136. fbp=framebuffer;
  137. if (*thismode & VERTICAL1 || (smooth && (divisors & 0x00FF0000)))
  138. {
  139. divis = (divisors & M_VERTICAL1) >> D_VERTICAL1;
  140. for ( hi=0 ; hi < h ; hi++)
  141. {
  142. if (smooth && divis)
  143. {
  144. int *tmp=fbp+w-1;
  145. int n=halfw;
  146. while (n--) *tmp-- = BLEND_ADAPT(*tmp, *fbp++, divis);
  147. }
  148. else
  149. {
  150. int *tmp=fbp+w-1;
  151. int n=halfw;
  152. while (n--) *tmp-- = *fbp++;
  153. }
  154. fbp+=halfw;
  155. }
  156. }
  157. fbp=framebuffer;
  158. if (*thismode & VERTICAL2 || (smooth && (divisors & 0xFF000000)))
  159. {
  160. divis = (divisors & M_VERTICAL2) >> D_VERTICAL2;
  161. for ( hi=0 ; hi < h ; hi++)
  162. {
  163. if (smooth && divis)
  164. {
  165. int *tmp=fbp+w-1;
  166. int n=halfw;
  167. while (n--) *fbp++ = BLEND_ADAPT(*fbp,*tmp--,divis);
  168. }
  169. else
  170. {
  171. int *tmp=fbp+w-1;
  172. int n=halfw;
  173. while (n--) *fbp++ = *tmp--;
  174. }
  175. fbp+=halfw;
  176. }
  177. }
  178. fbp=framebuffer;
  179. j=t-w;
  180. if (*thismode & HORIZONTAL1 || (smooth && (divisors & 0x000000FF)))
  181. {
  182. divis = (divisors & M_HORIZONTAL1) >> D_HORIZONTAL1;
  183. for ( hi=0 ; hi < halfh ; hi++)
  184. {
  185. if (smooth && divis)
  186. {
  187. int n=w;
  188. while (n--) fbp++[j]=BLEND_ADAPT(fbp[j], *fbp, divis);
  189. }
  190. else
  191. {
  192. memcpy(fbp+j,fbp,w*sizeof(int));
  193. fbp+=w;
  194. }
  195. j-=2*w;
  196. }
  197. }
  198. fbp=framebuffer;
  199. j=t-w;
  200. if (*thismode & HORIZONTAL2 || (smooth && (divisors & 0x0000FF00)))
  201. {
  202. divis = (divisors & M_HORIZONTAL2) >> D_HORIZONTAL2;
  203. for ( hi=0 ; hi < halfh ; hi++)
  204. {
  205. if (smooth && divis)
  206. {
  207. int n=w;
  208. while (n--)
  209. *fbp++ = BLEND_ADAPT(*fbp, fbp[j], divis);
  210. }
  211. else
  212. {
  213. memcpy(fbp,fbp+j,w*sizeof(int));
  214. fbp+=w;
  215. }
  216. j-=2*w;
  217. }
  218. }
  219. if (smooth && !(++framecount % slower))
  220. {
  221. int i;
  222. for (i=1,m=0xFF,d=0;i<16;i<<=1,m<<=8,d+=8)
  223. {
  224. if (divisors & m)
  225. divisors = (divisors & ~m) | ((((divisors & m) >> d) + (unsigned char)((inc & m) >> d)) % 16) << d;
  226. }
  227. }
  228. return 0;
  229. }
  230. int getMode(HWND hwndDlg)
  231. {
  232. int a;
  233. a=IsDlgButtonChecked(hwndDlg,IDC_HORIZONTAL1)?HORIZONTAL1:0;
  234. a|=IsDlgButtonChecked(hwndDlg,IDC_HORIZONTAL2)?HORIZONTAL2:0;
  235. a|=IsDlgButtonChecked(hwndDlg,IDC_VERTICAL1)?VERTICAL1:0;
  236. a|=IsDlgButtonChecked(hwndDlg,IDC_VERTICAL2)?VERTICAL2:0;
  237. return a;
  238. }
  239. // configuration dialog stuff
  240. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  241. {
  242. switch (uMsg)
  243. {
  244. case WM_INITDIALOG:
  245. SendDlgItemMessage(hwndDlg, IDC_SLOWER, TBM_SETTICFREQ, 1, 0);
  246. SendDlgItemMessage(hwndDlg, IDC_SLOWER, TBM_SETRANGE, TRUE, MAKELONG(1, 16));
  247. SendDlgItemMessage(hwndDlg, IDC_SLOWER, TBM_SETPOS, TRUE, g_ConfigThis->slower);
  248. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  249. if (g_ConfigThis->smooth) CheckDlgButton(hwndDlg,IDC_SMOOTH,BST_CHECKED);
  250. if (g_ConfigThis->mode & VERTICAL1)
  251. CheckDlgButton(hwndDlg,IDC_VERTICAL1,BST_CHECKED);
  252. if (g_ConfigThis->mode & VERTICAL2)
  253. CheckDlgButton(hwndDlg,IDC_VERTICAL2,BST_CHECKED);
  254. if (g_ConfigThis->mode & HORIZONTAL1)
  255. CheckDlgButton(hwndDlg,IDC_HORIZONTAL1,BST_CHECKED);
  256. if (g_ConfigThis->mode & HORIZONTAL2)
  257. CheckDlgButton(hwndDlg,IDC_HORIZONTAL2,BST_CHECKED);
  258. if (g_ConfigThis->onbeat)
  259. CheckDlgButton(hwndDlg,IDC_ONBEAT,BST_CHECKED);
  260. else
  261. CheckDlgButton(hwndDlg,IDC_STAT,BST_CHECKED);
  262. return 1;
  263. case WM_NOTIFY:
  264. {
  265. if (LOWORD(wParam) == IDC_SLOWER)
  266. g_ConfigThis->slower = SendDlgItemMessage(hwndDlg, IDC_SLOWER, TBM_GETPOS, 0, 0);
  267. return 0;
  268. }
  269. case WM_COMMAND:
  270. if (LOWORD(wParam) == IDC_CHECK1)
  271. {
  272. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  273. }
  274. if (LOWORD(wParam) == IDC_HORIZONTAL1)
  275. {
  276. g_ConfigThis->mode = getMode(hwndDlg);
  277. if ((g_ConfigThis->mode & HORIZONTAL1) && (g_ConfigThis->mode & HORIZONTAL2) && !(g_ConfigThis->onbeat))
  278. {
  279. CheckDlgButton(hwndDlg,IDC_HORIZONTAL2,BST_UNCHECKED);
  280. g_ConfigThis->mode = getMode(hwndDlg);
  281. }
  282. }
  283. if (LOWORD(wParam) == IDC_HORIZONTAL2)
  284. {
  285. g_ConfigThis->mode = getMode(hwndDlg);
  286. if ((g_ConfigThis->mode & HORIZONTAL1) && (g_ConfigThis->mode & HORIZONTAL2) && !(g_ConfigThis->onbeat))
  287. {
  288. CheckDlgButton(hwndDlg,IDC_HORIZONTAL1,BST_UNCHECKED);
  289. g_ConfigThis->mode = getMode(hwndDlg);
  290. }
  291. }
  292. if (LOWORD(wParam) == IDC_VERTICAL1)
  293. {
  294. g_ConfigThis->mode = getMode(hwndDlg);
  295. if ((g_ConfigThis->mode & VERTICAL1) && (g_ConfigThis->mode & VERTICAL2) && !(g_ConfigThis->onbeat))
  296. {
  297. CheckDlgButton(hwndDlg,IDC_VERTICAL2,BST_UNCHECKED);
  298. g_ConfigThis->mode = getMode(hwndDlg);
  299. }
  300. }
  301. if (LOWORD(wParam) == IDC_VERTICAL2)
  302. {
  303. g_ConfigThis->mode = getMode(hwndDlg);
  304. if ((g_ConfigThis->mode & VERTICAL1) && (g_ConfigThis->mode & VERTICAL2) && !(g_ConfigThis->onbeat))
  305. {
  306. CheckDlgButton(hwndDlg,IDC_VERTICAL1,BST_UNCHECKED);
  307. g_ConfigThis->mode = getMode(hwndDlg);
  308. }
  309. }
  310. if (LOWORD(wParam) == IDC_STAT || LOWORD(wParam) == IDC_ONBEAT)
  311. {
  312. g_ConfigThis->onbeat=IsDlgButtonChecked(hwndDlg,IDC_ONBEAT)?1:0;
  313. if (!(g_ConfigThis->onbeat))
  314. {
  315. if ((g_ConfigThis->mode & HORIZONTAL1) && (g_ConfigThis->mode & HORIZONTAL2))
  316. CheckDlgButton(hwndDlg,IDC_HORIZONTAL2,BST_UNCHECKED);
  317. if ((g_ConfigThis->mode & VERTICAL1) && (g_ConfigThis->mode & VERTICAL2))
  318. CheckDlgButton(hwndDlg,IDC_VERTICAL2,BST_UNCHECKED);
  319. g_ConfigThis->mode = getMode(hwndDlg);
  320. }
  321. }
  322. if (LOWORD(wParam) == IDC_SMOOTH)
  323. g_ConfigThis->smooth=IsDlgButtonChecked(hwndDlg,IDC_SMOOTH)?1:0;
  324. return 0;
  325. }
  326. return 0;
  327. }
  328. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  329. {
  330. g_ConfigThis = this;
  331. return WASABI_API_CREATEDIALOG(IDD_CFG_MIRROR,hwndParent,g_DlgProc);
  332. }
  333. // export stuff
  334. C_RBASE *R_Mirror(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  335. {
  336. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  337. return (C_RBASE *) new C_THISCLASS();
  338. }
  339. #else
  340. C_RBASE *R_Mirror(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  341. { return NULL; }
  342. #endif