texmgr.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005-2013 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 "texmgr.h"
  25. //#include "jpegstuff.h"
  26. //#include "evallib/compiler.h"
  27. #include "ns-eel2/ns-eel.h"
  28. #include "support.h"
  29. #include "plugin.h"
  30. #include "utility.h"
  31. texmgr::texmgr()
  32. {
  33. }
  34. texmgr::~texmgr()
  35. {
  36. //Finish();
  37. // DO NOT RELEASE OR DELETE m_lpDD; CLIENT SHOULD DO THIS!
  38. }
  39. void texmgr::Finish()
  40. {
  41. for (int i=0; i<NUM_TEX; i++)
  42. {
  43. KillTex(i);
  44. /*
  45. if (m_tex[i].pSurface)
  46. {
  47. m_tex[i].pSurface->Release();
  48. m_tex[i].pSurface = NULL;
  49. }
  50. FreeCode(i);
  51. FreeVars(i);
  52. */
  53. NSEEL_VM_free(m_tex[i].tex_eel_ctx);
  54. }
  55. // DO NOT RELEASE OR DELETE m_lpDD; CLIENT SHOULD DO THIS!
  56. }
  57. void texmgr::Init(LPDIRECT3DDEVICE9 lpDD)
  58. {
  59. m_lpDD = lpDD;
  60. for (int i=0; i<NUM_TEX; i++)
  61. {
  62. m_tex[i].pSurface = NULL;
  63. m_tex[i].szFileName[0] = 0;
  64. m_tex[i].m_codehandle = NULL;
  65. m_tex[i].m_szExpr[0] = 0;
  66. m_tex[i].tex_eel_ctx = NSEEL_VM_alloc();
  67. }
  68. }
  69. /*
  70. bool texmgr::TryCreateDDrawSurface(int iSlot, int w, int h)
  71. {
  72. int loop = 1;
  73. int done = 0;
  74. int scaling = false;
  75. do
  76. {
  77. //TEMP!!!
  78. //w = 2048;
  79. //h = 2048;
  80. switch(loop)
  81. {
  82. case 0:
  83. // first, try requesting surface w/original dimensions of image
  84. break;
  85. case 1:
  86. // then, try next-highest-power-of-two for w,h
  87. w = (int)powf(2.0f, ceilf(logf((float)w)/logf(2.0f)));
  88. h = (int)powf(2.0f, ceilf(logf((float)h)/logf(2.0f)));
  89. break;
  90. case 2:
  91. // then, try making it square
  92. if (w < h) w = h;
  93. if (h < w) h = w;
  94. break;
  95. default:
  96. if (!scaling)
  97. {
  98. if (w<256 || h<256)
  99. {
  100. w *= 2;
  101. h *= 2;
  102. }
  103. else
  104. {
  105. scaling = true;
  106. }
  107. }
  108. else
  109. {
  110. if (w>16 && h>16)
  111. {
  112. w /= 2;
  113. h /= 2;
  114. }
  115. else
  116. {
  117. done = 1;
  118. }
  119. }
  120. break;
  121. }
  122. if (done)
  123. break;
  124. // TRY TO CREATE THE SURFACE IN SYSTEM MEMORY.
  125. ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
  126. ddsd.dwSize = sizeof( ddsd );
  127. ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
  128. ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;//DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;// | DDSCAPS_3DDEVICE;// | DDSCAPS_LOCALVIDMEM | DDSCAPS_VIDEOMEMORY;
  129. ddsd.dwWidth = w;
  130. ddsd.dwHeight = h;
  131. m_tex[iSlot].pSurface = NULL;
  132. //if (w<256 && h<256)
  133. if (m_lpDD->CreateSurface( &ddsd, &m_tex[iSlot].pSurface, NULL ) == DD_OK)
  134. break;
  135. m_tex[iSlot].pSurface = NULL;
  136. loop++;
  137. }
  138. while (!done);
  139. if (m_tex[iSlot].pSurface == NULL)
  140. return false;
  141. // find out (& remember) actual size created:
  142. ZeroMemory(&ddsd, sizeof(ddsd));
  143. ddsd.dwSize = sizeof(ddsd);
  144. m_tex[iSlot].pSurface->GetSurfaceDesc(&ddsd);
  145. m_tex[iSlot].tex_w = ddsd.dwWidth;
  146. m_tex[iSlot].tex_h = ddsd.dwHeight;
  147. m_tex[iSlot].scale_x = 1.0f;
  148. m_tex[iSlot].scale_y = 1.0f;
  149. memcpy(&m_tex[iSlot].ddpf, &ddsd.ddpfPixelFormat, sizeof(DDPIXELFORMAT));
  150. return true;
  151. }
  152. */
  153. int texmgr::LoadTex(wchar_t *szFilename, int iSlot, char *szInitCode, char *szCode, float time, int frame, unsigned int ck)
  154. {
  155. if (iSlot < 0) return TEXMGR_ERR_BAD_INDEX;
  156. if (iSlot >= NUM_TEX) return TEXMGR_ERR_BAD_INDEX;
  157. // first, if this texture is already loaded, just add another instance.
  158. bool bTextureInstanced = false;
  159. {
  160. for (int x=0; x<NUM_TEX; x++)
  161. if (m_tex[x].pSurface && _wcsicmp(m_tex[x].szFileName, szFilename)==0)
  162. {
  163. memcpy(&m_tex[iSlot], &m_tex[x], sizeof(td_tex));
  164. m_tex[iSlot].m_szExpr[0] = 0;
  165. m_tex[iSlot].m_codehandle = 0;
  166. bTextureInstanced = true;
  167. break;
  168. }
  169. }
  170. if (!bTextureInstanced)
  171. {
  172. // Free old resources:
  173. /*
  174. if (m_tex[iSlot].pSurface)
  175. {
  176. m_tex[iSlot].pSurface->Release();
  177. m_tex[iSlot].pSurface = NULL;
  178. }*/
  179. KillTex(iSlot);
  180. wcscpy(m_tex[iSlot].szFileName, szFilename);
  181. D3DXIMAGE_INFO info;
  182. HRESULT hr = pCreateTextureFromFileExW(
  183. m_lpDD,
  184. szFilename,
  185. D3DX_DEFAULT,
  186. D3DX_DEFAULT,
  187. D3DX_DEFAULT, // create a mip chain
  188. 0,
  189. D3DFMT_UNKNOWN,
  190. D3DPOOL_DEFAULT,
  191. D3DX_DEFAULT,
  192. D3DX_DEFAULT,
  193. 0xFF000000 | ck,
  194. &info,
  195. NULL,
  196. &m_tex[iSlot].pSurface
  197. );
  198. if (hr != D3D_OK)
  199. {
  200. switch(hr)
  201. {
  202. case E_OUTOFMEMORY:
  203. case D3DERR_OUTOFVIDEOMEMORY:
  204. return TEXMGR_ERR_OUTOFMEM;
  205. default:
  206. return TEXMGR_ERR_BADFILE;
  207. }
  208. }
  209. m_tex[iSlot].img_w = info.Width;
  210. m_tex[iSlot].img_h = info.Height;
  211. /*
  212. unsigned int w_img;
  213. unsigned int h_img;
  214. unsigned int img_color_channels;
  215. int ret = Begin_Jpeg_Read(szFilename, &w_img, &h_img, &img_color_channels);
  216. switch(ret)
  217. {
  218. case JPEGSTUFF_ERR_SUCCESS:
  219. break;
  220. case JPEGSTUFF_ERR_OPENING:
  221. return TEXMGR_ERR_OPENING;
  222. break;
  223. case JPEGSTUFF_ERR_MISC:
  224. return TEXMGR_ERR_FORMAT;
  225. break;
  226. }
  227. sprintf(buf, "texmgr: w=%d, h=%d, channels=%d", w_img, h_img, img_color_channels);
  228. //g_dumpmsg(buf);
  229. m_tex[iSlot].img_w = w_img;
  230. m_tex[iSlot].img_h = h_img;
  231. if (img_color_channels != 3)
  232. {
  233. // error: not 24-bit!
  234. //g_dumpmsg("texmgr: image not 24-bit");
  235. End_Jpeg_Read();
  236. return TEXMGR_ERR_IMAGE_NOT_24_BIT;
  237. }
  238. if ((w_img > 2048) ||
  239. (h_img > 2048)) // RG
  240. {
  241. // error: too large!
  242. //g_dumpmsg("texmgr: image too large");
  243. End_Jpeg_Read();
  244. return TEXMGR_ERR_IMAGE_TOO_LARGE;
  245. }
  246. if (!TryCreateDDrawSurface(iSlot, w_img, h_img))
  247. {
  248. //g_dumpmsg("texmgr: unable to create ddraw surface");
  249. End_Jpeg_Read();
  250. return TEXMGR_ERR_CREATESURFACE_FAILED;
  251. }
  252. unsigned int w_tex = m_tex[iSlot].tex_w;
  253. unsigned int h_tex = m_tex[iSlot].tex_h;
  254. unsigned int bpp_tex = m_tex[iSlot].ddpf.dwRGBBitCount;
  255. sprintf(buf, "texmgr: created ddraw surface; %d x %d x %d", w_tex, h_tex, bpp_tex);
  256. //g_dumpmsg(buf);
  257. DDSURFACEDESC2 ddsd;
  258. ZeroMemory(&ddsd, sizeof(DDSURFACEDESC2));
  259. ddsd.dwSize = sizeof( ddsd );
  260. if (m_tex[iSlot].pSurface->Lock(0, &ddsd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT|DDLOCK_NOSYSLOCK, 0) != DD_OK)
  261. {
  262. //g_dumpmsg("texmgr: unable to lock ddraw surface");
  263. End_Jpeg_Read();
  264. m_tex[iSlot].pSurface->Release();
  265. m_tex[iSlot].pSurface = NULL;
  266. return TEXMGR_ERR_LOCKSURFACE_FAILED;
  267. }
  268. // analyze surface pixel format
  269. unsigned int zeroBits[3] = { 0, 0, 0 };
  270. unsigned int chopBits[3] = { 8, 8, 8 };
  271. unsigned int mask[3] = { ddsd.ddpfPixelFormat.dwRBitMask, ddsd.ddpfPixelFormat.dwGBitMask, ddsd.ddpfPixelFormat.dwBBitMask };
  272. {
  273. int x,y;
  274. for (x=0; x<3; x++)
  275. {
  276. for (y=0; y<32; y++)
  277. {
  278. if ((mask[x] & (1<<y)) == 0)
  279. zeroBits[x]++;
  280. else
  281. break;
  282. }
  283. mask[x] >>= zeroBits[x];
  284. for (y=0; y<32; y++)
  285. {
  286. if ((mask[x] & (1<<y)) != 0)
  287. chopBits[x]--;
  288. }
  289. }
  290. }
  291. unsigned int ck_r1 = (ck_lo >> 16) & 0xFF;
  292. unsigned int ck_g1 = (ck_lo >> 8 ) & 0xFF;
  293. unsigned int ck_b1 = (ck_lo ) & 0xFF;
  294. unsigned int ck_r2 = (ck_hi >> 16) & 0xFF;
  295. unsigned int ck_g2 = (ck_hi >> 8 ) & 0xFF;
  296. unsigned int ck_b2 = (ck_hi ) & 0xFF;
  297. // read jpeg in & store in directdraw surface
  298. // 2. read image into texture
  299. if (w_img > w_tex || h_img > h_tex)
  300. {
  301. // DOWNSAMPLING VERSION
  302. unsigned int new_w_img = min(w_tex, w_img);
  303. unsigned int new_h_img = min(h_tex, h_img);
  304. {
  305. char buf[256];
  306. sprintf(buf, "texmgr: downsampling image from %dx%d to %dx%d and storing in %dx%d texture.", w_img,h_img, new_w_img,new_h_img, w_tex,h_tex);
  307. //g_dumpmsg(buf);
  308. }
  309. int downsample_buf[2048*3];
  310. memset(downsample_buf, 0, sizeof(downsample_buf));
  311. float input_lines_per_output_line = h_img/(float)new_h_img;
  312. float lines = 0.0f;
  313. unsigned int out_y = 0;
  314. for (int y=0; y<(int)h_img; y++)
  315. {
  316. unsigned int x;
  317. unsigned char *buf = Jpeg_Read_Next_Line();
  318. if (!buf)
  319. {
  320. End_Jpeg_Read();
  321. m_tex[iSlot].pSurface->Release();
  322. m_tex[iSlot].pSurface = NULL;
  323. return TEXMGR_ERR_CORRUPT_JPEG;
  324. }
  325. lines += 1.0f;
  326. int portion = (int)(min(256, max(0, (input_lines_per_output_line - lines)*256)));
  327. for (x=0; x<w_img*3; x++)
  328. downsample_buf[x] += ((int)buf[x] * portion) >> 4;
  329. if (portion < 256)
  330. {
  331. // commit this line (out_y) & start a new one
  332. if (out_y < h_tex)
  333. {
  334. float input_cols_per_output_col = w_img/(float)new_w_img;
  335. float cols = 0.0f;
  336. int out_x = 0;
  337. int buf2[2048*3];
  338. memset(buf2, 0, new_w_img*3);
  339. for (x=0; x<w_img; x++)
  340. {
  341. cols += 1.0f;
  342. int portion = (int)(min(256, max(0, (input_cols_per_output_col - cols)*256)));
  343. buf2[out_x*3 ] += (downsample_buf[x*3 ] * portion) >> 4;
  344. buf2[out_x*3+1] += (downsample_buf[x*3+1] * portion) >> 4;
  345. buf2[out_x*3+2] += (downsample_buf[x*3+2] * portion) >> 4;
  346. if (portion < 256)
  347. {
  348. cols -= input_cols_per_output_col;
  349. portion = 256 - portion;
  350. out_x++;
  351. buf2[out_x*3 ] = (downsample_buf[x*3 ] * portion) >> 4;
  352. buf2[out_x*3+1] = (downsample_buf[x*3+1] * portion) >> 4;
  353. buf2[out_x*3+2] = (downsample_buf[x*3+2] * portion) >> 4;
  354. }
  355. }
  356. // now buf2[0..w_tex] has r,g,b colors in it (but scaled) -> SAVE TO TEXTURE.
  357. float scale_factor = 1.0f / (float)(16 * 16 * input_cols_per_output_col * input_lines_per_output_line);
  358. if (bpp_tex == 16)
  359. {
  360. unsigned __int16 *dest16 = (unsigned __int16 *)ddsd.lpSurface;
  361. unsigned int tex_offset = (ddsd.lPitch/2) * out_y;
  362. for (x=0; x<new_w_img; x++)
  363. {
  364. unsigned int cr = (unsigned int)(buf2[x*3 ]*scale_factor);
  365. unsigned int cg = (unsigned int)(buf2[x*3+1]*scale_factor);
  366. unsigned int cb = (unsigned int)(buf2[x*3+2]*scale_factor);
  367. unsigned int color = (((cr >> chopBits[0]) & mask[0]) << zeroBits[0]) |
  368. (((cg >> chopBits[1]) & mask[1]) << zeroBits[1]) |
  369. (((cb >> chopBits[2]) & mask[2]) << zeroBits[2]);
  370. if (!(cr >= ck_r1 && cr <= ck_r2 &&
  371. cg >= ck_g1 && cg <= ck_g2 &&
  372. cb >= ck_b1 && cb <= ck_b2))
  373. color |= ddsd.ddpfPixelFormat.dwRGBAlphaBitMask;
  374. dest16[tex_offset++] = color;
  375. }
  376. }
  377. else if (bpp_tex == 32)
  378. {
  379. unsigned __int32 *dest32 = (unsigned __int32 *)ddsd.lpSurface;
  380. unsigned int tex_offset = (ddsd.lPitch/4) * out_y;
  381. for (x=0; x<new_w_img; x++)
  382. {
  383. unsigned int cr = (unsigned int)(buf2[x*3 ]*scale_factor);
  384. unsigned int cg = (unsigned int)(buf2[x*3+1]*scale_factor);
  385. unsigned int cb = (unsigned int)(buf2[x*3+2]*scale_factor);
  386. unsigned int color = (cr << zeroBits[0]) | (cg << zeroBits[1]) | (cb << zeroBits[2]);
  387. if (!(cr >= ck_r1 && cr <= ck_r2 &&
  388. cg >= ck_g1 && cg <= ck_g2 &&
  389. cb >= ck_b1 && cb <= ck_b2))
  390. color |= ddsd.ddpfPixelFormat.dwRGBAlphaBitMask;
  391. dest32[tex_offset++] = color;
  392. }
  393. }
  394. out_y++;
  395. }
  396. // start next line:
  397. lines -= input_lines_per_output_line;
  398. portion = 256 - portion;
  399. for (x=0; x<w_img*3; x++)
  400. downsample_buf[x] = ((int)buf[x] * portion) >> 4;
  401. }
  402. }
  403. m_tex[iSlot].scale_x = new_w_img/(float)w_img;
  404. m_tex[iSlot].scale_y = new_h_img/(float)h_img;
  405. m_tex[iSlot].img_w = new_w_img;
  406. m_tex[iSlot].img_h = new_h_img;
  407. w_img = new_w_img;
  408. h_img = new_h_img;
  409. }
  410. else
  411. {
  412. // 1:1 VERSION
  413. if (bpp_tex == 16)
  414. {
  415. unsigned __int16 *dest16 = (unsigned __int16 *)ddsd.lpSurface;
  416. for (int y=0; y<(int)h_img; y++)
  417. {
  418. unsigned char *buf = Jpeg_Read_Next_Line();
  419. if (!buf)
  420. {
  421. End_Jpeg_Read();
  422. m_tex[iSlot].pSurface->Release();
  423. m_tex[iSlot].pSurface = NULL;
  424. return TEXMGR_ERR_CORRUPT_JPEG;
  425. }
  426. unsigned int tex_offset = (ddsd.lPitch/2) * y;
  427. for (unsigned int x=0; x<w_img; x++)
  428. {
  429. unsigned int cr = buf[x*3 ];
  430. unsigned int cg = buf[x*3+1];
  431. unsigned int cb = buf[x*3+2];
  432. unsigned int color = (((cr >> chopBits[0]) & mask[0]) << zeroBits[0]) |
  433. (((cg >> chopBits[1]) & mask[1]) << zeroBits[1]) |
  434. (((cb >> chopBits[2]) & mask[2]) << zeroBits[2]);
  435. if (!(cr >= ck_r1 && cr <= ck_r2 &&
  436. cg >= ck_g1 && cg <= ck_g2 &&
  437. cb >= ck_b1 && cb <= ck_b2))
  438. color |= ddsd.ddpfPixelFormat.dwRGBAlphaBitMask;
  439. dest16[tex_offset++] = color;
  440. }
  441. }
  442. }
  443. else if (bpp_tex == 32)
  444. {
  445. unsigned __int32 *dest32 = (unsigned __int32 *)ddsd.lpSurface;
  446. for (int y=0; y<(int)h_img; y++)
  447. {
  448. unsigned char *buf = Jpeg_Read_Next_Line();
  449. if (!buf)
  450. {
  451. End_Jpeg_Read();
  452. m_tex[iSlot].pSurface->Release();
  453. m_tex[iSlot].pSurface = NULL;
  454. return TEXMGR_ERR_CORRUPT_JPEG;
  455. }
  456. unsigned int tex_offset = (ddsd.lPitch/4) * y;
  457. for (unsigned int x=0; x<w_img; x++)
  458. {
  459. unsigned int cr = buf[x*3 ];
  460. unsigned int cg = buf[x*3+1];
  461. unsigned int cb = buf[x*3+2];
  462. unsigned int color = (cr << zeroBits[0]) | (cg << zeroBits[1]) | (cb << zeroBits[2]);
  463. if (!(cr >= ck_r1 && cr <= ck_r2 &&
  464. cg >= ck_g1 && cg <= ck_g2 &&
  465. cb >= ck_b1 && cb <= ck_b2))
  466. color |= ddsd.ddpfPixelFormat.dwRGBAlphaBitMask;
  467. dest32[tex_offset++] = color;
  468. }
  469. }
  470. }
  471. }
  472. m_tex[iSlot].pSurface->Unlock(0);
  473. End_Jpeg_Read();
  474. */
  475. }
  476. m_tex[iSlot].fStartTime = time;
  477. m_tex[iSlot].nStartFrame = frame;
  478. int ret = TEXMGR_ERR_SUCCESS;
  479. // compile & run init. code:
  480. if (!RunInitCode(iSlot, szInitCode))
  481. ret |= TEXMGR_WARN_ERROR_IN_INIT_CODE;
  482. // compile & save per-frame code:
  483. strcpy(m_tex[iSlot].m_szExpr, szCode);
  484. FreeCode(iSlot);
  485. if (!RecompileExpressions(iSlot))
  486. ret |= TEXMGR_WARN_ERROR_IN_REG_CODE;
  487. //g_dumpmsg("texmgr: success");
  488. return ret;
  489. }
  490. void texmgr::KillTex(int iSlot)
  491. {
  492. if (iSlot < 0) return;
  493. if (iSlot >= NUM_TEX) return;
  494. // Free old resources:
  495. if (m_tex[iSlot].pSurface)
  496. {
  497. // first, make sure no other sprites reference this texture!
  498. int refcount = 0;
  499. for (int x=0; x<NUM_TEX; x++)
  500. if (m_tex[x].pSurface == m_tex[iSlot].pSurface)
  501. refcount++;
  502. if (refcount==1)
  503. m_tex[iSlot].pSurface->Release();
  504. m_tex[iSlot].pSurface = NULL;
  505. }
  506. m_tex[iSlot].szFileName[0] = 0;
  507. FreeCode(iSlot);
  508. }
  509. void texmgr::StripLinefeedCharsAndComments(char *src, char *dest)
  510. {
  511. // replaces all LINEFEED_CONTROL_CHAR characters in src with a space in dest;
  512. // also strips out all comments (beginning with '//' and going til end of line).
  513. // Restriction: sizeof(dest) must be >= sizeof(src).
  514. int i2 = 0;
  515. int len = strlen(src);
  516. int bComment = false;
  517. for (int i=0; i<len; i++)
  518. {
  519. if (bComment)
  520. {
  521. if (src[i] == LINEFEED_CONTROL_CHAR)
  522. bComment = false;
  523. }
  524. else
  525. {
  526. if ((src[i] =='\\' && src[i+1] =='\\') || (src[i] =='/' && src[i+1] =='/'))
  527. bComment = true;
  528. else if (src[i] != LINEFEED_CONTROL_CHAR)
  529. dest[i2++] = src[i];
  530. }
  531. }
  532. dest[i2] = 0;
  533. }
  534. bool texmgr::RunInitCode(int iSlot, char *szInitCode)
  535. {
  536. // warning: destroys contents of m_tex[iSlot].m_szExpr,
  537. // so be sure to call RunInitCode before writing or
  538. // compiling that string!
  539. FreeCode(iSlot);
  540. FreeVars(iSlot);
  541. RegisterBuiltInVariables(iSlot);
  542. strcpy(m_tex[iSlot].m_szExpr, szInitCode);
  543. bool ret = RecompileExpressions(iSlot);
  544. // set default values of output variables:
  545. // (by not setting these every frame, we allow the values to persist from frame-to-frame.)
  546. *(m_tex[iSlot].var_x) = 0.5;
  547. *(m_tex[iSlot].var_y) = 0.5;
  548. *(m_tex[iSlot].var_sx) = 1.0;
  549. *(m_tex[iSlot].var_sy) = 1.0;
  550. *(m_tex[iSlot].var_repeatx) = 1.0;
  551. *(m_tex[iSlot].var_repeaty) = 1.0;
  552. *(m_tex[iSlot].var_rot) = 0.0;
  553. *(m_tex[iSlot].var_flipx) = 0.0;
  554. *(m_tex[iSlot].var_flipy) = 0.0;
  555. *(m_tex[iSlot].var_r) = 1.0;
  556. *(m_tex[iSlot].var_g) = 1.0;
  557. *(m_tex[iSlot].var_b) = 1.0;
  558. *(m_tex[iSlot].var_a) = 1.0;
  559. *(m_tex[iSlot].var_blendmode)= 0.0;
  560. *(m_tex[iSlot].var_done) = 0.0;
  561. *(m_tex[iSlot].var_burn) = 1.0;
  562. #ifndef _NO_EXPR_
  563. if (m_tex[iSlot].m_codehandle)
  564. NSEEL_code_execute(m_tex[iSlot].m_codehandle);
  565. #endif
  566. return ret;
  567. }
  568. bool texmgr::RecompileExpressions(int iSlot)
  569. {
  570. char *expr = m_tex[iSlot].m_szExpr;
  571. // QUICK FIX: if the string ONLY has spaces and linefeeds, erase it,
  572. // because for some strange reason this would cause an error in compileCode().
  573. {
  574. char *p = expr;
  575. while (*p==' ' || *p==LINEFEED_CONTROL_CHAR) p++;
  576. if (*p == 0) expr[0] = 0;
  577. }
  578. // replace linefeed control characters with spaces, so they don't mess up the code compiler,
  579. // and strip out any comments ('//') before sending to CompileCode().
  580. char buf[sizeof(m_tex[iSlot].m_szExpr)];
  581. StripLinefeedCharsAndComments(expr, buf);
  582. if (buf[0])
  583. {
  584. #ifndef _NO_EXPR_
  585. //resetVars(m_tex[iSlot].m_vars);
  586. //g_dumpmsg("texmgr: compiling string: ");
  587. //g_dumpmsg(buf);
  588. if ( ! (m_tex[iSlot].m_codehandle = NSEEL_code_compile(m_tex[iSlot].tex_eel_ctx, buf)))
  589. {
  590. //g_dumpmsg(" -error!");
  591. //MessageBox( NULL, "error in per-frame code", "MILKDROP ERROR", MB_OK|MB_SETFOREGROUND|MB_TOPMOST );
  592. //sprintf(pg->m_szUserMessage, "warning: preset \"%s\": error in 'per_frame' code", m_szDesc);
  593. //pg->m_fShowUserMessageUntilThisTime = pg->m_fAnimTime + 6.0f;
  594. }
  595. else
  596. {
  597. //g_dumpmsg(" -ok!");
  598. //pg->m_fShowUserMessageUntilThisTime = pg->m_fAnimTime; // clear any old error msg.
  599. }
  600. //resetVars(NULL);
  601. return (m_tex[iSlot].m_codehandle != 0);
  602. #endif
  603. }
  604. return true;
  605. }
  606. void texmgr::FreeVars(int iSlot)
  607. {
  608. // free the built-in variables AND any user variables
  609. }
  610. void texmgr::FreeCode(int iSlot)
  611. {
  612. // free the compiled expressions
  613. if (m_tex[iSlot].m_codehandle)
  614. {
  615. NSEEL_code_free(m_tex[iSlot].m_codehandle);
  616. m_tex[iSlot].m_codehandle = NULL;
  617. }
  618. }
  619. void texmgr::RegisterBuiltInVariables(int iSlot)
  620. {
  621. NSEEL_VMCTX eel_ctx = m_tex[iSlot].tex_eel_ctx;
  622. NSEEL_VM_resetvars(eel_ctx);
  623. // input variables
  624. m_tex[iSlot].var_time = NSEEL_VM_regvar(eel_ctx, "time");
  625. m_tex[iSlot].var_frame = NSEEL_VM_regvar(eel_ctx, "frame");
  626. m_tex[iSlot].var_fps = NSEEL_VM_regvar(eel_ctx, "fps");
  627. m_tex[iSlot].var_progress = NSEEL_VM_regvar(eel_ctx, "progress");
  628. m_tex[iSlot].var_bass = NSEEL_VM_regvar(eel_ctx, "bass");
  629. m_tex[iSlot].var_bass_att = NSEEL_VM_regvar(eel_ctx, "bass_att");
  630. m_tex[iSlot].var_mid = NSEEL_VM_regvar(eel_ctx, "mid");
  631. m_tex[iSlot].var_mid_att = NSEEL_VM_regvar(eel_ctx, "mid_att");
  632. m_tex[iSlot].var_treb = NSEEL_VM_regvar(eel_ctx, "treb");
  633. m_tex[iSlot].var_treb_att = NSEEL_VM_regvar(eel_ctx, "treb_att");
  634. // output variables
  635. m_tex[iSlot].var_x = NSEEL_VM_regvar(eel_ctx, "x");
  636. m_tex[iSlot].var_y = NSEEL_VM_regvar(eel_ctx, "y");
  637. m_tex[iSlot].var_sx = NSEEL_VM_regvar(eel_ctx, "sx");
  638. m_tex[iSlot].var_sy = NSEEL_VM_regvar(eel_ctx, "sy");
  639. m_tex[iSlot].var_repeatx = NSEEL_VM_regvar(eel_ctx, "repeatx");
  640. m_tex[iSlot].var_repeaty = NSEEL_VM_regvar(eel_ctx, "repeaty");
  641. m_tex[iSlot].var_rot = NSEEL_VM_regvar(eel_ctx, "rot");
  642. m_tex[iSlot].var_flipx = NSEEL_VM_regvar(eel_ctx, "flipx");
  643. m_tex[iSlot].var_flipy = NSEEL_VM_regvar(eel_ctx, "flipy");
  644. m_tex[iSlot].var_r = NSEEL_VM_regvar(eel_ctx, "r");
  645. m_tex[iSlot].var_g = NSEEL_VM_regvar(eel_ctx, "g");
  646. m_tex[iSlot].var_b = NSEEL_VM_regvar(eel_ctx, "b");
  647. m_tex[iSlot].var_a = NSEEL_VM_regvar(eel_ctx, "a");
  648. m_tex[iSlot].var_blendmode = NSEEL_VM_regvar(eel_ctx, "blendmode");
  649. m_tex[iSlot].var_done = NSEEL_VM_regvar(eel_ctx, "done");
  650. m_tex[iSlot].var_burn = NSEEL_VM_regvar(eel_ctx, "burn");
  651. // resetVars(NULL);
  652. }