1
0

r_interleave.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 "resource.h"
  29. #include "r_defs.h"
  30. #include "../Agave/Language/api_language.h"
  31. #ifndef LASER
  32. #define MOD_NAME "Trans / Interleave"
  33. #define C_THISCLASS C_InterleaveClass
  34. class C_THISCLASS : public C_RBASE {
  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_INTERLEAVE,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. int x, y;
  45. int enabled;
  46. int color;
  47. int blend;
  48. int blendavg;
  49. int onbeat, x2,y2,beatdur;
  50. double cur_x,cur_y;
  51. };
  52. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  53. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  54. C_THISCLASS::~C_THISCLASS() // set up default configuration
  55. {
  56. }
  57. // configuration read/write
  58. C_THISCLASS::C_THISCLASS() // set up default configuration
  59. {
  60. onbeat=0;
  61. beatdur=4;
  62. color = 0;
  63. x2=x=1;
  64. y2=y=1;
  65. cur_x=cur_y=1.0;
  66. enabled=1;
  67. blend=0;
  68. blendavg=0;
  69. }
  70. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  71. void C_THISCLASS::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
  72. {
  73. int pos=0;
  74. if (len-pos >= 4) { enabled=GET_INT(); pos+=4; }
  75. if (len-pos >= 4) { x=GET_INT(); pos+=4; }
  76. if (len-pos >= 4) { y=GET_INT(); pos+=4; }
  77. if (len-pos >= 4) { color=GET_INT(); pos+=4; }
  78. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  79. if (len-pos >= 4) { blendavg=GET_INT(); pos+=4; }
  80. if (len-pos >= 4) { onbeat=GET_INT(); pos+=4; }
  81. if (len-pos >= 4) { x2=GET_INT(); pos+=4; }
  82. if (len-pos >= 4) { y2=GET_INT(); pos+=4; }
  83. if (len-pos >= 4) { beatdur=GET_INT(); pos+=4; }
  84. cur_x=x;
  85. cur_y=y;
  86. }
  87. #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
  88. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  89. {
  90. int pos=0;
  91. PUT_INT(enabled); pos+=4;
  92. PUT_INT(x); pos+=4;
  93. PUT_INT(y); pos+=4;
  94. PUT_INT(color); pos+=4;
  95. PUT_INT(blend); pos+=4;
  96. PUT_INT(blendavg); pos+=4;
  97. PUT_INT(onbeat); pos+=4;
  98. PUT_INT(x2); pos+=4;
  99. PUT_INT(y2); pos+=4;
  100. PUT_INT(beatdur); pos+=4;
  101. return pos;
  102. }
  103. // render function
  104. // render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout. this is
  105. // used when you want to do something that you'd otherwise need to make a copy of the framebuffer.
  106. // w and h are the width and height of the screen, in pixels.
  107. // isBeat is 1 if a beat has been detected.
  108. // visdata is in the format of [spectrum:0,wave:1][channel][band].
  109. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
  110. {
  111. if (isBeat&0x80000000) return 0;
  112. if (!enabled) return 0;
  113. int ystat=0;
  114. int yp=0;
  115. double sc1=(beatdur+512.0-64.0)/512.0;
  116. cur_x=(cur_x*sc1+x*(1.0-sc1));
  117. cur_y=(cur_y*sc1+y*(1.0-sc1));
  118. if (isBeat && onbeat)
  119. {
  120. cur_x=x2;
  121. cur_y=y2;
  122. }
  123. int tx=(int)cur_x;
  124. int ty=(int)cur_y;
  125. int xos=0;
  126. int *p=framebuffer;
  127. int j;
  128. if (!ty)
  129. {
  130. ystat=1;
  131. }
  132. if (tx > 0)
  133. {
  134. xos=(w%tx)/2;
  135. }
  136. if (ty > 0) yp=(h%ty)/2;
  137. if (tx>=0 && ty >=0) for (j=0;j<h;j++)
  138. {
  139. int xstat=0;
  140. if (ty && ++yp>=ty)
  141. {
  142. ystat=!ystat;
  143. yp=0;
  144. }
  145. int l=w;
  146. if (!ystat) // this line is pure color
  147. {
  148. if (blend)
  149. {
  150. while (l--)
  151. {
  152. *p=BLEND(*p, color);
  153. p++;
  154. }
  155. }
  156. else if (blendavg)
  157. {
  158. while (l--)
  159. {
  160. *p=BLEND_AVG(*p, color);
  161. p++;
  162. }
  163. }
  164. else
  165. {
  166. while (l--)
  167. {
  168. *p++=color;
  169. }
  170. }
  171. }
  172. else if (tx)
  173. {
  174. if (blend)
  175. {
  176. int xo=xos;
  177. while (l>0)
  178. {
  179. int l2=min(l,tx-xo);
  180. xo=0;
  181. l-=l2;
  182. if (xstat) p+=l2;
  183. else while (l2--) { *p=BLEND(*p,color); p++; }
  184. xstat=!xstat;
  185. }
  186. }
  187. else if (blendavg)
  188. {
  189. int xo=xos;
  190. while (l>0)
  191. {
  192. int l2=min(l,tx-xo);
  193. xo=0;
  194. l-=l2;
  195. if (xstat) p+=l2;
  196. else while (l2--) { *p=BLEND_AVG(*p,color); p++; }
  197. xstat=!xstat;
  198. }
  199. }
  200. else
  201. {
  202. int xo=xos;
  203. while (l>0)
  204. {
  205. int l2=min(l,tx-xo);
  206. xo=0;
  207. l-=l2;
  208. if (xstat) p+=l2;
  209. else while (l2--) { *p++=color; }
  210. xstat=!xstat;
  211. }
  212. }
  213. }
  214. else p+=w;
  215. }
  216. return 0;
  217. }
  218. // configuration dialog stuff
  219. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  220. {
  221. switch (uMsg)
  222. {
  223. case WM_INITDIALOG:
  224. SendDlgItemMessage(hwndDlg, IDC_X, TBM_SETTICFREQ, 1, 0);
  225. SendDlgItemMessage(hwndDlg, IDC_X, TBM_SETRANGE, TRUE, MAKELONG(0, 64));
  226. SendDlgItemMessage(hwndDlg, IDC_X, TBM_SETPOS, TRUE, g_ConfigThis->x);
  227. SendDlgItemMessage(hwndDlg, IDC_Y, TBM_SETTICFREQ, 1, 0);
  228. SendDlgItemMessage(hwndDlg, IDC_Y, TBM_SETRANGE, TRUE, MAKELONG(0, 64));
  229. SendDlgItemMessage(hwndDlg, IDC_Y, TBM_SETPOS, TRUE, g_ConfigThis->y);
  230. SendDlgItemMessage(hwndDlg, IDC_X2, TBM_SETTICFREQ, 1, 0);
  231. SendDlgItemMessage(hwndDlg, IDC_X2, TBM_SETRANGE, TRUE, MAKELONG(0, 64));
  232. SendDlgItemMessage(hwndDlg, IDC_X2, TBM_SETPOS, TRUE, g_ConfigThis->x2);
  233. SendDlgItemMessage(hwndDlg, IDC_Y2, TBM_SETTICFREQ, 1, 0);
  234. SendDlgItemMessage(hwndDlg, IDC_Y2, TBM_SETRANGE, TRUE, MAKELONG(0, 64));
  235. SendDlgItemMessage(hwndDlg, IDC_Y2, TBM_SETPOS, TRUE, g_ConfigThis->y2);
  236. SendDlgItemMessage(hwndDlg, IDC_X3, TBM_SETTICFREQ, 1, 0);
  237. SendDlgItemMessage(hwndDlg, IDC_X3, TBM_SETRANGE, TRUE, MAKELONG(1, 64));
  238. SendDlgItemMessage(hwndDlg, IDC_X3, TBM_SETPOS, TRUE, g_ConfigThis->beatdur);
  239. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  240. if (g_ConfigThis->onbeat) CheckDlgButton(hwndDlg,IDC_CHECK8,BST_CHECKED);
  241. if (g_ConfigThis->blend) CheckDlgButton(hwndDlg,IDC_ADDITIVE,BST_CHECKED);
  242. if (g_ConfigThis->blendavg) CheckDlgButton(hwndDlg,IDC_5050,BST_CHECKED);
  243. if (!g_ConfigThis->blend && !g_ConfigThis->blendavg)
  244. CheckDlgButton(hwndDlg,IDC_REPLACE,BST_CHECKED);
  245. return 1;
  246. case WM_HSCROLL:
  247. {
  248. HWND swnd = (HWND) lParam;
  249. int t = (int) SendMessage(swnd,TBM_GETPOS,0,0);
  250. if (swnd == GetDlgItem(hwndDlg,IDC_X))
  251. {
  252. g_ConfigThis->cur_x=(double)(g_ConfigThis->x = t);
  253. }
  254. else if (swnd == GetDlgItem(hwndDlg,IDC_Y))
  255. {
  256. g_ConfigThis->cur_y=(double)(g_ConfigThis->y = t);
  257. }
  258. else if (swnd == GetDlgItem(hwndDlg,IDC_X2))
  259. {
  260. g_ConfigThis->cur_x=(double)(g_ConfigThis->x2 = t);
  261. }
  262. else if (swnd == GetDlgItem(hwndDlg,IDC_Y2))
  263. {
  264. g_ConfigThis->cur_y=(double)(g_ConfigThis->y2 = t);
  265. }
  266. else if (swnd == GetDlgItem(hwndDlg,IDC_X3))
  267. {
  268. g_ConfigThis->beatdur=t;
  269. }
  270. }
  271. return 0;
  272. case WM_DRAWITEM:
  273. {
  274. DRAWITEMSTRUCT *di=(DRAWITEMSTRUCT *)lParam;
  275. if (di->CtlID == IDC_DEFCOL) // paint nifty color button
  276. {
  277. int w=di->rcItem.right-di->rcItem.left;
  278. int _color=g_ConfigThis->color;
  279. _color = ((_color>>16)&0xff)|(_color&0xff00)|((_color<<16)&0xff0000);
  280. HPEN hPen,hOldPen;
  281. HBRUSH hBrush,hOldBrush;
  282. LOGBRUSH lb={ (COLORREF)BS_SOLID,(COLORREF)_color,(COLORREF)0};
  283. hPen = (HPEN)CreatePen(PS_SOLID,0,_color);
  284. hBrush = CreateBrushIndirect(&lb);
  285. hOldPen=(HPEN)SelectObject(di->hDC,hPen);
  286. hOldBrush=(HBRUSH)SelectObject(di->hDC,hBrush);
  287. Rectangle(di->hDC,di->rcItem.left,di->rcItem.top,di->rcItem.right,di->rcItem.bottom);
  288. SelectObject(di->hDC,hOldPen);
  289. SelectObject(di->hDC,hOldBrush);
  290. DeleteObject(hBrush);
  291. DeleteObject(hPen);
  292. }
  293. }
  294. return 0;
  295. case WM_COMMAND:
  296. if ((LOWORD(wParam) == IDC_CHECK1) ||
  297. (LOWORD(wParam) == IDC_CHECK8) ||
  298. (LOWORD(wParam) == IDC_ADDITIVE) ||
  299. (LOWORD(wParam) == IDC_REPLACE) ||
  300. (LOWORD(wParam) == IDC_5050) )
  301. {
  302. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  303. g_ConfigThis->onbeat=IsDlgButtonChecked(hwndDlg,IDC_CHECK8)?1:0;
  304. g_ConfigThis->blend=IsDlgButtonChecked(hwndDlg,IDC_ADDITIVE)?1:0;
  305. g_ConfigThis->blendavg=IsDlgButtonChecked(hwndDlg,IDC_5050)?1:0;
  306. }
  307. if (LOWORD(wParam) == IDC_DEFCOL) // handle clicks to nifty color button
  308. {
  309. int *a=&(g_ConfigThis->color);
  310. static COLORREF custcolors[16];
  311. CHOOSECOLOR cs;
  312. cs.lStructSize = sizeof(cs);
  313. cs.hwndOwner = hwndDlg;
  314. cs.hInstance = 0;
  315. cs.rgbResult=((*a>>16)&0xff)|(*a&0xff00)|((*a<<16)&0xff0000);
  316. cs.lpCustColors = custcolors;
  317. cs.Flags = CC_RGBINIT|CC_FULLOPEN;
  318. if (ChooseColor(&cs))
  319. {
  320. *a = ((cs.rgbResult>>16)&0xff)|(cs.rgbResult&0xff00)|((cs.rgbResult<<16)&0xff0000);
  321. g_ConfigThis->color = *a;
  322. }
  323. InvalidateRect(GetDlgItem(hwndDlg,IDC_DEFCOL),NULL,TRUE);
  324. }
  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_INTERLEAVE,hwndParent,g_DlgProc);
  332. }
  333. // export stuff
  334. C_RBASE *R_Interleave(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_Interleave(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  341. {
  342. return NULL;
  343. }
  344. #endif