rl_trans.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #ifdef LASER
  25. #include <windows.h>
  26. #include <commctrl.h>
  27. #include "../r_defs.h"
  28. #include "../resource.h"
  29. #include "../evallib/eval.h"
  30. #include "../evallib/compiler.h"
  31. #include <math.h>
  32. #define C_THISCLASS CLASER_Transform
  33. #define MOD_NAME "Misc / Transform"
  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); // returns 1 if fbout has dest
  40. virtual char *get_desc() { return MOD_NAME; }
  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. void load_string(RString &s,unsigned char *data, int &pos, int len);
  45. void save_string(unsigned char *data, int &pos, RString &text);
  46. void dopoint(float &x, float &y, char visdata[2][2][576], int &color);
  47. varType vars[EVAL_MAX_VARS];
  48. NSEEL_CODEHANDLE codehandle,codehandle_b,codehandle_i,codehandle_f;
  49. double *v_d;
  50. double *v_r;
  51. double *v_x;
  52. double *v_y;
  53. double *v_b;
  54. double *v_red;
  55. double *v_green;
  56. double *v_blue;
  57. int rectangular;
  58. RString effect_exp[4];
  59. int effect_exp_ch;
  60. CRITICAL_SECTION rcs;
  61. };
  62. #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
  63. #define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
  64. void C_THISCLASS::load_config(unsigned char *data, int len)
  65. {
  66. int pos=0;
  67. rectangular=GET_INT(); pos+=4;
  68. if (data[pos] == 1)
  69. {
  70. pos++;
  71. load_string(effect_exp[0],data,pos,len);
  72. load_string(effect_exp[1],data,pos,len);
  73. load_string(effect_exp[2],data,pos,len);
  74. load_string(effect_exp[3],data,pos,len);
  75. }
  76. else
  77. {
  78. char buf[1025];
  79. if (len-pos >= 1024)
  80. {
  81. memcpy(buf,data+pos,1024);
  82. pos+=1024;
  83. buf[1024]=0;
  84. effect_exp[3].assign(buf+768);
  85. buf[768]=0;
  86. effect_exp[2].assign(buf+512);
  87. buf[512]=0;
  88. effect_exp[1].assign(buf+256);
  89. buf[256]=0;
  90. effect_exp[0].assign(buf);
  91. }
  92. }
  93. effect_exp_ch=1;
  94. }
  95. void C_THISCLASS::load_string(RString &s,unsigned char *data, int &pos, int len) // read configuration of max length "len" from data.
  96. {
  97. int size=GET_INT(); pos += 4;
  98. if (size > 0 && len-pos >= size)
  99. {
  100. s.resize(size);
  101. memcpy(s.get(), data+pos, size);
  102. pos+=size;
  103. }
  104. else
  105. {
  106. s.resize(1);
  107. s.get()[0]=0;
  108. }
  109. }
  110. int C_THISCLASS::save_config(unsigned char *data)
  111. {
  112. int pos=0;
  113. PUT_INT(rectangular); pos+=4;
  114. data[pos++]=1;
  115. save_string(data,pos,effect_exp[0]);
  116. save_string(data,pos,effect_exp[1]);
  117. save_string(data,pos,effect_exp[2]);
  118. save_string(data,pos,effect_exp[3]);
  119. return pos;
  120. }
  121. // special version that encodes with a 1 at the start.
  122. void C_THISCLASS::save_string(unsigned char *data, int &pos, RString &text)
  123. {
  124. if (text.get() && text.get()[0])
  125. {
  126. int l=(strlen(text.get())+1);
  127. PUT_INT(l); pos+=4;
  128. memcpy(data+pos, text.get(), strlen(text.get())+1);
  129. pos+=strlen(text.get())+1;
  130. }
  131. else
  132. {
  133. PUT_INT(0);
  134. pos+=4;
  135. }
  136. }
  137. C_THISCLASS::C_THISCLASS()
  138. {
  139. rectangular=0;
  140. codehandle=0;
  141. codehandle_b=0;
  142. codehandle_i=0;
  143. codehandle_f=0;
  144. memset(vars,0,sizeof(vars));
  145. resetVars(vars);
  146. v_d = registerVar("d");
  147. v_r = registerVar("r");
  148. v_x = registerVar("x");
  149. v_y = registerVar("y");
  150. v_b = registerVar("b");
  151. v_red = registerVar("red");
  152. v_green = registerVar("green");
  153. v_blue = registerVar("blue");
  154. resetVars(NULL);
  155. effect_exp[0].assign("d=0.5;");
  156. effect_exp[1].assign("");
  157. effect_exp[2].assign("");
  158. effect_exp[3].assign("");
  159. effect_exp_ch=1;
  160. InitializeCriticalSection(&rcs);
  161. }
  162. C_THISCLASS::~C_THISCLASS()
  163. {
  164. freeCode(codehandle);
  165. freeCode(codehandle_b);
  166. freeCode(codehandle_i);
  167. freeCode(codehandle_f);
  168. codehandle=0;
  169. codehandle_b=0;
  170. codehandle_i=0;
  171. codehandle_f=0;
  172. DeleteCriticalSection(&rcs);
  173. }
  174. void C_THISCLASS::dopoint(float &x, float &y, char visdata[2][2][576], int &color)
  175. {
  176. *v_d=sqrt(x*x+y*y);
  177. *v_r=atan2(y,x) + 3.14159*0.5;
  178. *v_x=x;
  179. *v_y=y;
  180. *v_blue=(color&0xff)/255.0;
  181. *v_green=((color>>8)&0xff)/255.0;
  182. *v_red=((color>>16)&0xff)/255.0;
  183. executeCode(codehandle,visdata);
  184. if (rectangular)
  185. {
  186. x=*v_x;
  187. y=*v_y;
  188. }
  189. else
  190. {
  191. *v_r -= 3.14159*0.5;
  192. if (*v_r < 0.0) *v_r += 3.14159*2.0;
  193. if (*v_r >= 3.14159*2.0) *v_r -= 3.14159*2.0;
  194. x=cos(*v_r)* *v_d;
  195. y=sin(*v_r)* *v_d;
  196. }
  197. int a=(int)(*v_blue * 255.0);
  198. if (a>255) a=255;
  199. if (a<0)a=0;
  200. color=a;
  201. a=(int)(*v_green * 255.0);
  202. if (a>255) a=255;
  203. if (a<0)a=0;
  204. color|=a<<8;
  205. a=(int)(*v_red * 255.0);
  206. if (a>255) a=255;
  207. if (a<0)a=0;
  208. color|=a<<16;
  209. }
  210. int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h) // returns 1 if fbout has dest
  211. {
  212. if (isBeat&0x80000000) return 0;
  213. if (effect_exp_ch || !codehandle)
  214. {
  215. EnterCriticalSection(&rcs);
  216. memset(vars,0,sizeof(vars));
  217. resetVars(vars);
  218. if (codehandle) freeCode(codehandle);
  219. if (codehandle_b) freeCode(codehandle_b);
  220. if (codehandle_i) freeCode(codehandle_i);
  221. if (codehandle_f) freeCode(codehandle_f);
  222. codehandle=compileCode(effect_exp[0].get());
  223. codehandle_i=compileCode(effect_exp[1].get());
  224. codehandle_f=compileCode(effect_exp[2].get());
  225. codehandle_b=compileCode(effect_exp[3].get());
  226. effect_exp_ch=0;
  227. resetVars(NULL);
  228. LeaveCriticalSection(&rcs);
  229. executeCode(codehandle_i,visdata);
  230. }
  231. executeCode(codehandle_f,visdata);
  232. if (isBeat)
  233. executeCode(codehandle_b,visdata);
  234. if (codehandle)
  235. {
  236. *v_b=isBeat?1.0:0.0;
  237. int x,num=g_laser_linelist->GetUsedLines();
  238. LineType *l=g_laser_linelist->GetLineList();
  239. for (x = 0; x < num; x ++)
  240. {
  241. dopoint(l->x1,l->y1,visdata,l->color);
  242. if (l->mode == 0) dopoint(l->x2,l->y2,visdata,l->color);
  243. l++;
  244. }
  245. }
  246. return 0;
  247. }
  248. C_RBASE *RLASER_Transform(char *desc)
  249. {
  250. if (desc) { strcpy(desc,MOD_NAME); return NULL; }
  251. return (C_RBASE *) new C_THISCLASS();
  252. }
  253. static C_THISCLASS *g_this;
  254. static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  255. {
  256. static int isstart;
  257. int *a=NULL;
  258. switch (uMsg)
  259. {
  260. case WM_INITDIALOG:
  261. isstart=1;
  262. SetDlgItemText(hwndDlg,IDC_EDIT1,g_this->effect_exp[0].get());
  263. SetDlgItemText(hwndDlg,IDC_EDIT2,g_this->effect_exp[1].get());
  264. SetDlgItemText(hwndDlg,IDC_EDIT3,g_this->effect_exp[2].get());
  265. SetDlgItemText(hwndDlg,IDC_EDIT4,g_this->effect_exp[3].get());
  266. isstart=0;
  267. if (g_this->rectangular)
  268. CheckDlgButton(hwndDlg,IDC_CHECK3,BST_CHECKED);
  269. return 1;
  270. case WM_COMMAND:
  271. if ((
  272. LOWORD(wParam) == IDC_EDIT1 ||
  273. LOWORD(wParam) == IDC_EDIT2 ||
  274. LOWORD(wParam) == IDC_EDIT3 ||
  275. LOWORD(wParam) == IDC_EDIT4
  276. )
  277. && HIWORD(wParam) == EN_CHANGE)
  278. {
  279. if (!isstart)
  280. {
  281. EnterCriticalSection(&g_this->rcs);
  282. g_this->effect_exp[0].get_from_dlgitem(hwndDlg,IDC_EDIT1);
  283. g_this->effect_exp[1].get_from_dlgitem(hwndDlg,IDC_EDIT2);
  284. g_this->effect_exp[2].get_from_dlgitem(hwndDlg,IDC_EDIT3);
  285. g_this->effect_exp[3].get_from_dlgitem(hwndDlg,IDC_EDIT4);
  286. g_this->effect_exp_ch=1;
  287. LeaveCriticalSection(&g_this->rcs);
  288. }
  289. }
  290. if (LOWORD(wParam) == IDC_CHECK3)
  291. {
  292. EnterCriticalSection(&g_this->rcs);
  293. g_this->rectangular=IsDlgButtonChecked(hwndDlg,IDC_CHECK3)?1:0;
  294. g_this->effect_exp_ch=1;
  295. LeaveCriticalSection(&g_this->rcs);
  296. }
  297. return 0;
  298. }
  299. return 0;
  300. }
  301. HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent)
  302. {
  303. g_this = this;
  304. return WASABI_API_CREATEDIALOG(IDD_CFG_LASER_TRANSFORM,hwndParent,g_DlgProc);
  305. }
  306. #endif