1
0

r_mosaic.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 / Mosaic"
  33. #define C_THISCLASS C_MosaicClass
  34. class C_THISCLASS : public C_RBASE {
  35. protected:
  36. public:
  37. C_THISCLASS();
  38. float GET_FLOAT(unsigned char *data, int pos);
  39. void PUT_FLOAT(float f, unsigned char *data, int pos);
  40. void InitializeStars(int Start);
  41. void CreateStar(int A);
  42. virtual ~C_THISCLASS();
  43. virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
  44. virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_MOSAIC,desc,128):desc); }
  45. virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
  46. virtual void load_config(unsigned char *data, int len);
  47. virtual int save_config(unsigned char *data);
  48. int enabled;
  49. int quality;
  50. int quality2;
  51. int blend;
  52. int blendavg;
  53. int onbeat;
  54. int durFrames;
  55. int nF;
  56. int thisQuality;
  57. };
  58. static C_THISCLASS *g_ConfigThis; // global configuration dialog pointer
  59. static HINSTANCE g_hDllInstance; // global DLL instance pointer (not needed in this example, but could be useful)
  60. C_THISCLASS::~C_THISCLASS()
  61. {
  62. }
  63. // configuration read/write
  64. C_THISCLASS::C_THISCLASS() // set up default configuration
  65. {
  66. enabled=1;
  67. quality = 50;
  68. blend = 0;
  69. blendavg = 0;
  70. onbeat = 0;
  71. durFrames = 15;
  72. nF=0;
  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) { quality=GET_INT(); pos+=4; }
  80. if (len-pos >= 4) { quality2=GET_INT(); pos+=4; }
  81. if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
  82. if (len-pos >= 4) { blendavg=GET_INT(); pos+=4; }
  83. if (len-pos >= 4) { onbeat=GET_INT(); pos+=4; }
  84. if (len-pos >= 4) { durFrames=GET_INT(); pos+=4; }
  85. }
  86. #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
  87. int C_THISCLASS::save_config(unsigned char *data) // write configuration to data, return length. config data should not exceed 64k.
  88. {
  89. int pos=0;
  90. PUT_INT(enabled); pos+=4;
  91. PUT_INT(quality); pos+=4;
  92. PUT_INT(quality2); pos+=4;
  93. PUT_INT(blend); pos+=4;
  94. PUT_INT(blendavg); pos+=4;
  95. PUT_INT(onbeat); pos+=4;
  96. PUT_INT(durFrames); pos+=4;
  97. return pos;
  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 rval=0;
  108. if (isBeat&0x80000000) return 0;
  109. if (!enabled) return 0;
  110. if (onbeat && isBeat)
  111. {
  112. thisQuality=quality2;
  113. nF = durFrames;
  114. }
  115. else if (!nF) thisQuality = quality;
  116. if (thisQuality<100)
  117. {
  118. int y;
  119. int *p = fbout;
  120. int *p2 = framebuffer;
  121. int sXInc = (w*65536) / thisQuality;
  122. int sYInc = (h*65536) / thisQuality;
  123. int ypos=(sYInc>>17);
  124. int dypos=0;
  125. for (y = 0; y < h; y ++)
  126. {
  127. int x=w;
  128. int *fbread=framebuffer+ypos*w;
  129. int dpos=0;
  130. int xpos=(sXInc>>17);
  131. int src=fbread[xpos];
  132. if (blend)
  133. {
  134. while (x--)
  135. {
  136. *p++ = BLEND(*p2++, src);
  137. dpos+=1<<16;
  138. if (dpos>=sXInc)
  139. {
  140. xpos+=dpos>>16;
  141. if (xpos >= w) break;
  142. src=fbread[xpos];
  143. dpos-=sXInc;
  144. }
  145. }
  146. }
  147. else if (blendavg)
  148. {
  149. while (x--)
  150. {
  151. *p++ = BLEND_AVG(*p2++, src);
  152. dpos+=1<<16;
  153. if (dpos>=sXInc)
  154. {
  155. xpos+=dpos>>16;
  156. if (xpos >= w) break;
  157. src=fbread[xpos];
  158. dpos-=sXInc;
  159. }
  160. }
  161. }
  162. else
  163. {
  164. while (x--)
  165. {
  166. *p++ = src;
  167. dpos+=1<<16;
  168. if (dpos>=sXInc)
  169. {
  170. xpos+=dpos>>16;
  171. if (xpos >= w) break;
  172. src=fbread[xpos];
  173. dpos-=sXInc;
  174. }
  175. }
  176. }
  177. dypos+=1<<16;
  178. if (dypos>=sYInc)
  179. {
  180. ypos+=(dypos>>16);
  181. dypos-=sYInc;
  182. if (ypos >= h) break;
  183. }
  184. }
  185. rval=1;
  186. }
  187. if (nF)
  188. {
  189. nF--;
  190. if (nF)
  191. {
  192. int a = abs(quality - quality2) / durFrames;
  193. thisQuality += a * (quality2 > quality ? -1 : 1);
  194. }
  195. }
  196. return rval;
  197. }
  198. // configuration dialog stuff
  199. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  200. {
  201. switch (uMsg)
  202. {
  203. case WM_INITDIALOG:
  204. SendDlgItemMessage(hwndDlg, IDC_QUALITY, TBM_SETTICFREQ, 10, 0);
  205. SendDlgItemMessage(hwndDlg, IDC_QUALITY, TBM_SETRANGE, TRUE, MAKELONG(1, 100));
  206. SendDlgItemMessage(hwndDlg, IDC_QUALITY, TBM_SETPOS, TRUE, g_ConfigThis->quality);
  207. SendDlgItemMessage(hwndDlg, IDC_QUALITY2, TBM_SETTICFREQ, 10, 0);
  208. SendDlgItemMessage(hwndDlg, IDC_QUALITY2, TBM_SETRANGE, TRUE, MAKELONG(1, 100));
  209. SendDlgItemMessage(hwndDlg, IDC_QUALITY2, TBM_SETPOS, TRUE, g_ConfigThis->quality2);
  210. SendDlgItemMessage(hwndDlg, IDC_BEATDUR, TBM_SETTICFREQ, 10, 0);
  211. SendDlgItemMessage(hwndDlg, IDC_BEATDUR, TBM_SETRANGE, TRUE, MAKELONG(1, 100));
  212. SendDlgItemMessage(hwndDlg, IDC_BEATDUR, TBM_SETPOS, TRUE, g_ConfigThis->durFrames);
  213. if (g_ConfigThis->enabled) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
  214. if (g_ConfigThis->onbeat) CheckDlgButton(hwndDlg,IDC_ONBEAT,BST_CHECKED);
  215. if (g_ConfigThis->blend) CheckDlgButton(hwndDlg,IDC_ADDITIVE,BST_CHECKED);
  216. if (g_ConfigThis->blendavg) CheckDlgButton(hwndDlg,IDC_5050,BST_CHECKED);
  217. if (!g_ConfigThis->blend && !g_ConfigThis->blendavg)
  218. CheckDlgButton(hwndDlg,IDC_REPLACE,BST_CHECKED);
  219. return 1;
  220. case WM_NOTIFY:
  221. {
  222. if (LOWORD(wParam) == IDC_QUALITY2)
  223. g_ConfigThis->quality2 = SendDlgItemMessage(hwndDlg, IDC_QUALITY2, TBM_GETPOS, 0, 0);
  224. if (LOWORD(wParam) == IDC_BEATDUR)
  225. g_ConfigThis->durFrames = SendDlgItemMessage(hwndDlg, IDC_BEATDUR, TBM_GETPOS, 0, 0);
  226. if (LOWORD(wParam) == IDC_QUALITY)
  227. g_ConfigThis->quality = SendDlgItemMessage(hwndDlg, IDC_QUALITY, TBM_GETPOS, 0, 0);
  228. }
  229. return 0;
  230. case WM_COMMAND:
  231. if ((LOWORD(wParam) == IDC_CHECK1) ||
  232. (LOWORD(wParam) == IDC_ONBEAT) ||
  233. (LOWORD(wParam) == IDC_ADDITIVE) ||
  234. (LOWORD(wParam) == IDC_REPLACE) ||
  235. (LOWORD(wParam) == IDC_5050) )
  236. {
  237. g_ConfigThis->enabled=IsDlgButtonChecked(hwndDlg,IDC_CHECK1)?1:0;
  238. g_ConfigThis->onbeat=IsDlgButtonChecked(hwndDlg,IDC_ONBEAT)?1:0;
  239. g_ConfigThis->blend=IsDlgButtonChecked(hwndDlg,IDC_ADDITIVE)?1:0;
  240. g_ConfigThis->blendavg=IsDlgButtonChecked(hwndDlg,IDC_5050)?1:0;
  241. }
  242. }
  243. return 0;
  244. }
  245. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
  246. {
  247. g_ConfigThis = this;
  248. return WASABI_API_CREATEDIALOG(IDD_CFG_MOSAIC,hwndParent,g_DlgProc);
  249. }
  250. // export stuff
  251. C_RBASE *R_Mosaic(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  252. {
  253. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  254. return (C_RBASE *) new C_THISCLASS();
  255. }
  256. #else
  257. C_RBASE *R_Mosaic(char *desc) // creates a new effect object if desc is NULL, otherwise fills in desc with description
  258. {return NULL; }
  259. #endif