text.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // Portions based on PALx Project by palxex.
  7. // Copyright (c) 2006-2008, Pal Lockheart <palxex@gmail.com>.
  8. //
  9. // This file is part of SDLPAL.
  10. //
  11. // SDLPAL is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  25. //
  26. #include "main.h"
  27. #define FONT_COLOR_DEFAULT 0x4F
  28. #define FONT_COLOR_YELLOW 0x2D
  29. #define FONT_COLOR_RED 0x1A
  30. #define FONT_COLOR_CYAN 0x8D
  31. #define FONT_COLOR_CYAN_ALT 0x8C
  32. BOOL g_fUpdatedInBattle = FALSE;
  33. #define INCLUDE_CODEPAGE_H
  34. #include "codepage.h"
  35. #ifndef PAL_CLASSIC
  36. static const WCHAR* gc_rgszAdditionalWords[CP_MAX][6] = {
  37. { L"\x6230\x9B25\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  38. { L"\x6218\x6597\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  39. { L"\x6226\x95D8\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  40. };
  41. static const WCHAR** g_rgszAdditionalWords;
  42. #endif
  43. typedef struct tagTEXTLIB
  44. {
  45. LPWSTR* lpWordBuf;
  46. LPWSTR* lpMsgBuf;
  47. int nWords;
  48. int nMsgs;
  49. int nCurrentDialogLine;
  50. BYTE bCurrentFontColor;
  51. PAL_POS posIcon;
  52. PAL_POS posDialogTitle;
  53. PAL_POS posDialogText;
  54. BYTE bDialogPosition;
  55. BYTE bIcon;
  56. int iDelayTime;
  57. BOOL fUserSkip;
  58. BOOL fPlayingRNG;
  59. BYTE bufDialogIcons[282];
  60. } TEXTLIB, *LPTEXTLIB;
  61. static TEXTLIB g_TextLib;
  62. INT
  63. PAL_InitText(
  64. VOID
  65. )
  66. /*++
  67. Purpose:
  68. Initialize the in-game texts.
  69. Parameters:
  70. None.
  71. Return value:
  72. 0 = success.
  73. -1 = memory allocation error.
  74. --*/
  75. {
  76. FILE *fpMsg, *fpWord;
  77. DWORD *offsets;
  78. LPBYTE temp;
  79. LPWSTR tmp;
  80. int wlen, wpos;
  81. int i;
  82. //
  83. // Open the message and word data files.
  84. //
  85. fpMsg = UTIL_OpenRequiredFile("m.msg");
  86. fpWord = UTIL_OpenRequiredFile("word.dat");
  87. //
  88. // See how many words we have
  89. //
  90. fseek(fpWord, 0, SEEK_END);
  91. i = ftell(fpWord);
  92. //
  93. // Each word has 10 or 16 bytes
  94. //
  95. g_TextLib.nWords = (i + (gpGlobals->dwWordLength - 1)) / gpGlobals->dwWordLength;
  96. //
  97. // Read the words
  98. //
  99. temp = (LPBYTE)malloc(i);
  100. if (temp == NULL)
  101. {
  102. fclose(fpWord);
  103. fclose(fpMsg);
  104. return -1;
  105. }
  106. fseek(fpWord, 0, SEEK_SET);
  107. fread(temp, i, 1, fpWord);
  108. //
  109. // Close the words file
  110. //
  111. fclose(fpWord);
  112. // Split the words and do code page conversion
  113. for (i = 0, wlen = 0; i < g_TextLib.nWords; i++)
  114. {
  115. int base = i * gpGlobals->dwWordLength;
  116. int pos = base + gpGlobals->dwWordLength - 1;
  117. while (pos >= base && temp[pos] == ' ') temp[pos--] = 0;
  118. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + base, gpGlobals->dwWordLength, NULL, 0) + 1;
  119. }
  120. g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));
  121. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  122. for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)
  123. {
  124. int l;
  125. g_TextLib.lpWordBuf[i] = tmp + wpos;
  126. l = PAL_MultiByteToWideChar((LPCSTR)temp + i * gpGlobals->dwWordLength, gpGlobals->dwWordLength, g_TextLib.lpWordBuf[i], wlen - wpos);
  127. if (l > 0 && g_TextLib.lpWordBuf[i][l - 1] == '1')
  128. g_TextLib.lpWordBuf[i][l - 1] = 0;
  129. g_TextLib.lpWordBuf[i][l] = 0;
  130. wpos += l + 1;
  131. }
  132. free(temp);
  133. //
  134. // Read the message offsets. The message offsets are in SSS.MKF #3
  135. //
  136. i = PAL_MKFGetChunkSize(3, gpGlobals->f.fpSSS) / sizeof(DWORD);
  137. g_TextLib.nMsgs = i - 1;
  138. offsets = (LPDWORD)malloc(i * sizeof(DWORD));
  139. if (offsets == NULL)
  140. {
  141. free(g_TextLib.lpWordBuf);
  142. fclose(fpMsg);
  143. return -1;
  144. }
  145. PAL_MKFReadChunk((LPBYTE)(offsets), i * sizeof(DWORD), 3, gpGlobals->f.fpSSS);
  146. //
  147. // Read the messages.
  148. //
  149. fseek(fpMsg, 0, SEEK_END);
  150. i = ftell(fpMsg);
  151. temp = (LPBYTE)malloc(i);
  152. if (temp == NULL)
  153. {
  154. free(offsets);
  155. free(g_TextLib.lpWordBuf[0]);
  156. free(g_TextLib.lpWordBuf);
  157. fclose(fpMsg);
  158. return -1;
  159. }
  160. fseek(fpMsg, 0, SEEK_SET);
  161. fread(temp, 1, i, fpMsg);
  162. fclose(fpMsg);
  163. // Split messages and do code page conversion here
  164. for (i = 0, wlen = 0; i < g_TextLib.nMsgs; i++)
  165. {
  166. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), NULL, 0) + 1;
  167. }
  168. g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
  169. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  170. for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)
  171. {
  172. int l;
  173. g_TextLib.lpMsgBuf[i] = tmp + wpos;
  174. l = PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), g_TextLib.lpMsgBuf[i], wlen - wpos);
  175. g_TextLib.lpMsgBuf[i][l] = 0;
  176. wpos += l + 1;
  177. }
  178. free(temp);
  179. free(offsets);
  180. #ifndef PAL_CLASSIC
  181. g_rgszAdditionalWords = gc_rgszAdditionalWords[gpGlobals->iCodePage];
  182. #endif
  183. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  184. g_TextLib.bIcon = 0;
  185. g_TextLib.posIcon = 0;
  186. g_TextLib.nCurrentDialogLine = 0;
  187. g_TextLib.iDelayTime = 3;
  188. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  189. g_TextLib.posDialogText = PAL_XY(44, 26);
  190. g_TextLib.bDialogPosition = kDialogUpper;
  191. g_TextLib.fUserSkip = FALSE;
  192. PAL_MKFReadChunk(g_TextLib.bufDialogIcons, 282, 12, gpGlobals->f.fpDATA);
  193. return 0;
  194. }
  195. VOID
  196. PAL_FreeText(
  197. VOID
  198. )
  199. /*++
  200. Purpose:
  201. Free the memory used by the texts.
  202. Parameters:
  203. None.
  204. Return value:
  205. None.
  206. --*/
  207. {
  208. if (g_TextLib.lpMsgBuf != NULL)
  209. {
  210. free(g_TextLib.lpMsgBuf[0]);
  211. free(g_TextLib.lpMsgBuf);
  212. g_TextLib.lpMsgBuf = NULL;
  213. }
  214. if (g_TextLib.lpWordBuf != NULL)
  215. {
  216. free(g_TextLib.lpWordBuf[0]);
  217. free(g_TextLib.lpWordBuf);
  218. g_TextLib.lpWordBuf = NULL;
  219. }
  220. }
  221. LPCWSTR
  222. PAL_GetWord(
  223. WORD wNumWord
  224. )
  225. /*++
  226. Purpose:
  227. Get the specified word.
  228. Parameters:
  229. [IN] wNumWord - the number of the requested word.
  230. Return value:
  231. Pointer to the requested word. NULL if not found.
  232. --*/
  233. {
  234. #ifndef PAL_CLASSIC
  235. if (wNumWord >= PAL_ADDITIONAL_WORD_FIRST)
  236. {
  237. return g_rgszAdditionalWords[wNumWord - PAL_ADDITIONAL_WORD_FIRST];
  238. }
  239. #endif
  240. if (wNumWord >= g_TextLib.nWords)
  241. {
  242. return NULL;
  243. }
  244. return g_TextLib.lpWordBuf[wNumWord];
  245. }
  246. LPCWSTR
  247. PAL_GetMsg(
  248. WORD wNumMsg
  249. )
  250. /*++
  251. Purpose:
  252. Get the specified message.
  253. Parameters:
  254. [IN] wNumMsg - the number of the requested message.
  255. Return value:
  256. Pointer to the requested message. NULL if not found.
  257. --*/
  258. {
  259. return (wNumMsg >= g_TextLib.nMsgs) ? NULL : g_TextLib.lpMsgBuf[wNumMsg];
  260. }
  261. VOID
  262. PAL_DrawText(
  263. LPCWSTR lpszText,
  264. PAL_POS pos,
  265. BYTE bColor,
  266. BOOL fShadow,
  267. BOOL fUpdate
  268. )
  269. /*++
  270. Purpose:
  271. Draw text on the screen.
  272. Parameters:
  273. [IN] lpszText - the text to be drawn.
  274. [IN] pos - Position of the text.
  275. [IN] bColor - Color of the text.
  276. [IN] fShadow - TRUE if the text is shadowed or not.
  277. [IN] fUpdate - TRUE if update the screen area.
  278. Return value:
  279. None.
  280. --*/
  281. {
  282. SDL_Rect rect, urect;
  283. rect.x = PAL_X(pos);
  284. rect.y = PAL_Y(pos);
  285. urect.x = rect.x;
  286. urect.y = rect.y;
  287. urect.h = gpGlobals->fUseEmbeddedFonts ? 16 : 17;
  288. urect.w = 0;
  289. while (*lpszText)
  290. {
  291. //
  292. // Draw the character
  293. //
  294. int char_width = PAL_CharWidth(*lpszText);
  295. if (fShadow)
  296. {
  297. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0);
  298. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0);
  299. }
  300. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x, rect.y), bColor);
  301. lpszText++;
  302. rect.x += char_width;
  303. urect.w += char_width;
  304. }
  305. //
  306. // Update the screen area
  307. //
  308. if (fUpdate && urect.w > 0)
  309. {
  310. if (gpGlobals->fIsWIN95)
  311. {
  312. urect.w++;
  313. if (urect.x + urect.w > 320)
  314. {
  315. urect.w = 320 - urect.x;
  316. }
  317. }
  318. VIDEO_UpdateScreen(&urect);
  319. }
  320. }
  321. VOID
  322. PAL_DialogSetDelayTime(
  323. INT iDelayTime
  324. )
  325. /*++
  326. Purpose:
  327. Set the delay time for dialog.
  328. Parameters:
  329. [IN] iDelayTime - the delay time to be set.
  330. Return value:
  331. None.
  332. --*/
  333. {
  334. g_TextLib.iDelayTime = iDelayTime;
  335. }
  336. VOID
  337. PAL_StartDialog(
  338. BYTE bDialogLocation,
  339. BYTE bFontColor,
  340. INT iNumCharFace,
  341. BOOL fPlayingRNG
  342. )
  343. /*++
  344. Purpose:
  345. Start a new dialog.
  346. Parameters:
  347. [IN] bDialogLocation - the location of the text on the screen.
  348. [IN] bFontColor - the font color of the text.
  349. [IN] iNumCharFace - number of the character face in RGM.MKF.
  350. [IN] fPlayingRNG - whether we are playing a RNG video or not.
  351. Return value:
  352. None.
  353. --*/
  354. {
  355. PAL_LARGE BYTE buf[16384];
  356. SDL_Rect rect;
  357. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  358. {
  359. //
  360. // Update the screen in battle, or the graphics may seem messed up
  361. //
  362. VIDEO_UpdateScreen(NULL);
  363. g_fUpdatedInBattle = TRUE;
  364. }
  365. g_TextLib.bIcon = 0;
  366. g_TextLib.posIcon = 0;
  367. g_TextLib.nCurrentDialogLine = 0;
  368. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  369. g_TextLib.fUserSkip = FALSE;
  370. if (bFontColor != 0)
  371. {
  372. g_TextLib.bCurrentFontColor = bFontColor;
  373. }
  374. if (fPlayingRNG && iNumCharFace)
  375. {
  376. VIDEO_BackupScreen();
  377. g_TextLib.fPlayingRNG = TRUE;
  378. }
  379. switch (bDialogLocation)
  380. {
  381. case kDialogUpper:
  382. if (iNumCharFace > 0)
  383. {
  384. //
  385. // Display the character face at the upper part of the screen
  386. //
  387. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  388. {
  389. rect.w = PAL_RLEGetWidth((LPCBITMAPRLE)buf);
  390. rect.h = PAL_RLEGetHeight((LPCBITMAPRLE)buf);
  391. rect.x = 48 - rect.w / 2;
  392. rect.y = 55 - rect.h / 2;
  393. if (rect.x < 0)
  394. {
  395. rect.x = 0;
  396. }
  397. if (rect.y < 0)
  398. {
  399. rect.y = 0;
  400. }
  401. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  402. if (rect.x < 0)
  403. {
  404. rect.x = 0;
  405. }
  406. if (rect.y < 0)
  407. {
  408. rect.y = 0;
  409. }
  410. VIDEO_UpdateScreen(&rect);
  411. }
  412. }
  413. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 80 : 12, 8);
  414. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 96 : 44, 26);
  415. break;
  416. case kDialogCenter:
  417. g_TextLib.posDialogText = PAL_XY(80, 40);
  418. break;
  419. case kDialogLower:
  420. if (iNumCharFace > 0)
  421. {
  422. //
  423. // Display the character face at the lower part of the screen
  424. //
  425. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  426. {
  427. rect.x = 270 - PAL_RLEGetWidth((LPCBITMAPRLE)buf) / 2;
  428. rect.y = 144 - PAL_RLEGetHeight((LPCBITMAPRLE)buf) / 2;
  429. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  430. VIDEO_UpdateScreen(NULL);
  431. }
  432. }
  433. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 4 : 12, 108);
  434. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 20 : 44, 126);
  435. break;
  436. case kDialogCenterWindow:
  437. g_TextLib.posDialogText = PAL_XY(160, 40);
  438. break;
  439. }
  440. g_TextLib.bDialogPosition = bDialogLocation;
  441. }
  442. static VOID
  443. PAL_DialogWaitForKey(
  444. VOID
  445. )
  446. /*++
  447. Purpose:
  448. Wait for player to press a key after showing a dialog.
  449. Parameters:
  450. None.
  451. Return value:
  452. None.
  453. --*/
  454. {
  455. PAL_LARGE SDL_Color palette[256];
  456. SDL_Color *pCurrentPalette, t;
  457. int i;
  458. //
  459. // get the current palette
  460. //
  461. pCurrentPalette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  462. memcpy(palette, pCurrentPalette, sizeof(palette));
  463. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  464. g_TextLib.bDialogPosition != kDialogCenter)
  465. {
  466. //
  467. // show the icon
  468. //
  469. LPCBITMAPRLE p = PAL_SpriteGetFrame(g_TextLib.bufDialogIcons, g_TextLib.bIcon);
  470. if (p != NULL)
  471. {
  472. SDL_Rect rect;
  473. rect.x = PAL_X(g_TextLib.posIcon);
  474. rect.y = PAL_Y(g_TextLib.posIcon);
  475. rect.w = 16;
  476. rect.h = 16;
  477. PAL_RLEBlitToSurface(p, gpScreen, g_TextLib.posIcon);
  478. VIDEO_UpdateScreen(&rect);
  479. }
  480. }
  481. PAL_ClearKeyState();
  482. while (TRUE)
  483. {
  484. UTIL_Delay(100);
  485. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  486. g_TextLib.bDialogPosition != kDialogCenter)
  487. {
  488. //
  489. // palette shift
  490. //
  491. t = palette[0xF9];
  492. for (i = 0xF9; i < 0xFE; i++)
  493. {
  494. palette[i] = palette[i + 1];
  495. }
  496. palette[0xFE] = t;
  497. VIDEO_SetPalette(palette);
  498. }
  499. if (g_InputState.dwKeyPress != 0)
  500. {
  501. break;
  502. }
  503. }
  504. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  505. g_TextLib.bDialogPosition != kDialogCenter)
  506. {
  507. PAL_SetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  508. }
  509. PAL_ClearKeyState();
  510. g_TextLib.fUserSkip = FALSE;
  511. }
  512. VOID
  513. PAL_ShowDialogText(
  514. LPCWSTR lpszText
  515. )
  516. /*++
  517. Purpose:
  518. Show one line of the dialog text.
  519. Parameters:
  520. [IN] lpszText - the text to be shown.
  521. Return value:
  522. None.
  523. --*/
  524. {
  525. SDL_Rect rect;
  526. int x, y;
  527. PAL_ClearKeyState();
  528. g_TextLib.bIcon = 0;
  529. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  530. {
  531. //
  532. // Update the screen in battle, or the graphics may seem messed up
  533. //
  534. VIDEO_UpdateScreen(NULL);
  535. g_fUpdatedInBattle = TRUE;
  536. }
  537. if (g_TextLib.nCurrentDialogLine > 3)
  538. {
  539. //
  540. // The rest dialogs should be shown in the next page.
  541. //
  542. PAL_DialogWaitForKey();
  543. g_TextLib.nCurrentDialogLine = 0;
  544. VIDEO_RestoreScreen();
  545. VIDEO_UpdateScreen(NULL);
  546. }
  547. x = PAL_X(g_TextLib.posDialogText);
  548. y = PAL_Y(g_TextLib.posDialogText) + g_TextLib.nCurrentDialogLine * 18;
  549. if (g_TextLib.bDialogPosition == kDialogCenterWindow)
  550. {
  551. //
  552. // The text should be shown in a small window at the center of the screen
  553. //
  554. #ifndef PAL_CLASSIC
  555. if (gpGlobals->fInBattle && g_Battle.BattleResult == kBattleResultOnGoing)
  556. {
  557. PAL_BattleUIShowText(lpszText, 1400);
  558. }
  559. else
  560. #endif
  561. {
  562. PAL_POS pos;
  563. LPBOX lpBox;
  564. int i, w = wcslen(lpszText), len = 0;
  565. for (i = 0; i < w; i++)
  566. len += PAL_CharWidth(lpszText[i]) >> 3;
  567. //
  568. // Create the window box
  569. //
  570. pos = PAL_XY(PAL_X(g_TextLib.posDialogText) - len * 4, PAL_Y(g_TextLib.posDialogText));
  571. lpBox = PAL_CreateSingleLineBox(pos, (len + 1) / 2, TRUE);
  572. rect.x = PAL_X(pos);
  573. rect.y = PAL_Y(pos);
  574. rect.w = 320 - rect.x * 2 + 32;
  575. rect.h = 64;
  576. //
  577. // Show the text on the screen
  578. //
  579. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  580. PAL_DrawText(lpszText, pos, 0, FALSE, FALSE);
  581. VIDEO_UpdateScreen(&rect);
  582. PAL_DialogWaitForKey();
  583. //
  584. // Delete the box
  585. //
  586. PAL_DeleteBox(lpBox);
  587. VIDEO_UpdateScreen(&rect);
  588. PAL_EndDialog();
  589. }
  590. }
  591. else
  592. {
  593. int len = wcslen(lpszText);
  594. if (g_TextLib.nCurrentDialogLine == 0 &&
  595. g_TextLib.bDialogPosition != kDialogCenter &&
  596. (lpszText[len - 1] == 0xff1a ||
  597. lpszText[len - 1] == 0x2236 || // Special case for Pal WIN95 Simplified Chinese version
  598. lpszText[len - 1] == ':')
  599. )
  600. {
  601. //
  602. // name of character
  603. //
  604. PAL_DrawText(lpszText, g_TextLib.posDialogTitle, FONT_COLOR_CYAN_ALT, TRUE, TRUE);
  605. }
  606. else
  607. {
  608. //
  609. // normal texts
  610. //
  611. WCHAR text[2];
  612. if (!g_TextLib.fPlayingRNG && g_TextLib.nCurrentDialogLine == 0)
  613. {
  614. //
  615. // Save the screen before we show the first line of dialog
  616. //
  617. VIDEO_BackupScreen();
  618. }
  619. while (lpszText != NULL && *lpszText != '\0')
  620. {
  621. switch (*lpszText)
  622. {
  623. case '-':
  624. //
  625. // Set the font color to Cyan
  626. //
  627. if (g_TextLib.bCurrentFontColor == FONT_COLOR_CYAN)
  628. {
  629. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  630. }
  631. else
  632. {
  633. g_TextLib.bCurrentFontColor = FONT_COLOR_CYAN;
  634. }
  635. lpszText++;
  636. break;
  637. #if 0
  638. /* Not used */
  639. case '\'':
  640. //
  641. // Set the font color to Red
  642. //
  643. if (g_TextLib.bCurrentFontColor == FONT_COLOR_RED)
  644. {
  645. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  646. }
  647. else
  648. {
  649. g_TextLib.bCurrentFontColor = FONT_COLOR_RED;
  650. }
  651. lpszText++;
  652. break;
  653. #endif
  654. case '\"':
  655. //
  656. // Set the font color to Yellow
  657. //
  658. if (g_TextLib.bCurrentFontColor == FONT_COLOR_YELLOW)
  659. {
  660. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  661. }
  662. else
  663. {
  664. g_TextLib.bCurrentFontColor = FONT_COLOR_YELLOW;
  665. }
  666. lpszText++;
  667. break;
  668. case '$':
  669. //
  670. // Set the delay time of text-displaying
  671. //
  672. g_TextLib.iDelayTime = wcstol(lpszText + 1, NULL, 10) * 10 / 7;
  673. lpszText += 3;
  674. break;
  675. case '~':
  676. //
  677. // Delay for a period and quit
  678. //
  679. UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);
  680. g_TextLib.nCurrentDialogLine = 0;
  681. g_TextLib.fUserSkip = FALSE;
  682. return; // don't go further
  683. case ')':
  684. //
  685. // Set the waiting icon
  686. //
  687. g_TextLib.bIcon = 1;
  688. lpszText++;
  689. break;
  690. case '(':
  691. //
  692. // Set the waiting icon
  693. //
  694. g_TextLib.bIcon = 2;
  695. lpszText++;
  696. break;
  697. case '\\':
  698. lpszText++;
  699. default:
  700. text[0] = *lpszText++;
  701. text[1] = 0;
  702. PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, TRUE);
  703. x += PAL_CharWidth(text[0]);
  704. if (!g_TextLib.fUserSkip)
  705. {
  706. PAL_ClearKeyState();
  707. UTIL_Delay(g_TextLib.iDelayTime * 8);
  708. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  709. {
  710. //
  711. // User pressed a key to skip the dialog
  712. //
  713. g_TextLib.fUserSkip = TRUE;
  714. }
  715. }
  716. }
  717. }
  718. g_TextLib.posIcon = PAL_XY(x, y);
  719. g_TextLib.nCurrentDialogLine++;
  720. }
  721. }
  722. }
  723. VOID
  724. PAL_ClearDialog(
  725. BOOL fWaitForKey
  726. )
  727. /*++
  728. Purpose:
  729. Clear the state of the dialog.
  730. Parameters:
  731. [IN] fWaitForKey - whether wait for any key or not.
  732. Return value:
  733. None.
  734. --*/
  735. {
  736. if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)
  737. {
  738. PAL_DialogWaitForKey();
  739. }
  740. g_TextLib.nCurrentDialogLine = 0;
  741. if (g_TextLib.bDialogPosition == kDialogCenter)
  742. {
  743. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  744. g_TextLib.posDialogText = PAL_XY(44, 26);
  745. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  746. g_TextLib.bDialogPosition = kDialogUpper;
  747. }
  748. }
  749. VOID
  750. PAL_EndDialog(
  751. VOID
  752. )
  753. /*++
  754. Purpose:
  755. Ends a dialog.
  756. Parameters:
  757. None.
  758. Return value:
  759. None.
  760. --*/
  761. {
  762. PAL_ClearDialog(TRUE);
  763. //
  764. // Set some default parameters, as there are some parts of script
  765. // which doesn't have a "start dialog" instruction before showing the dialog.
  766. //
  767. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  768. g_TextLib.posDialogText = PAL_XY(44, 26);
  769. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  770. g_TextLib.bDialogPosition = kDialogUpper;
  771. g_TextLib.fUserSkip = FALSE;
  772. g_TextLib.fPlayingRNG = FALSE;
  773. }
  774. BOOL
  775. PAL_IsInDialog(
  776. VOID
  777. )
  778. /*++
  779. Purpose:
  780. Check if there are dialog texts on the screen.
  781. Parameters:
  782. None.
  783. Return value:
  784. TRUE if there are dialog texts on the screen, FALSE if not.
  785. --*/
  786. {
  787. return (g_TextLib.nCurrentDialogLine != 0);
  788. }
  789. BOOL
  790. PAL_DialogIsPlayingRNG(
  791. VOID
  792. )
  793. /*++
  794. Purpose:
  795. Check if the script used the RNG playing parameter when displaying texts.
  796. Parameters:
  797. None.
  798. Return value:
  799. TRUE if the script used the RNG playing parameter, FALSE if not.
  800. --*/
  801. {
  802. return g_TextLib.fPlayingRNG;
  803. }
  804. INT
  805. PAL_MultiByteToWideCharCP(
  806. CODEPAGE cp,
  807. LPCSTR mbs,
  808. int mbslength,
  809. LPWSTR wcs,
  810. int wcslength
  811. )
  812. /*++
  813. Purpose:
  814. Convert multi-byte string into the corresponding unicode string.
  815. Parameters:
  816. [IN] cp - Code page for conversion.
  817. [IN] mbs - Pointer to the multi-byte string.
  818. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  819. [IN] wcs - Pointer to the wide string buffer.
  820. [IN] wcslength - Length of the wide string buffer.
  821. Return value:
  822. The length of converted wide string. If mbslength is set to -1, the returned
  823. value includes the terminal null-char; otherwise, the null-char is not included.
  824. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  825. required length of the wide string buffer.
  826. --*/
  827. {
  828. int i = 0, state = 0, wlen = 0, null = 0;
  829. if (mbslength == -1)
  830. {
  831. mbslength = strlen(mbs);
  832. null = 1;
  833. }
  834. if (!wcs)
  835. {
  836. switch (cp)
  837. {
  838. case CP_SHIFTJIS:
  839. for (i = 0; i < mbslength && mbs[i]; i++)
  840. {
  841. if (state == 0)
  842. {
  843. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  844. wlen++;
  845. else
  846. state = 1;
  847. }
  848. else
  849. {
  850. wlen++;
  851. state = 0;
  852. }
  853. }
  854. return wlen + null + state;
  855. case CP_GBK:
  856. case CP_BIG5:
  857. for (i = 0; i < mbslength && mbs[i]; i++)
  858. {
  859. if (state == 0)
  860. {
  861. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  862. wlen++;
  863. else
  864. state = 1;
  865. }
  866. else
  867. {
  868. wlen++;
  869. state = 0;
  870. }
  871. }
  872. return wlen + null + state;
  873. default:
  874. return -1;
  875. }
  876. }
  877. else
  878. {
  879. WCHAR invalid_char;
  880. switch (cp)
  881. {
  882. case CP_SHIFTJIS:
  883. invalid_char = 0x30fb;
  884. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  885. {
  886. if (state == 0)
  887. {
  888. if ((BYTE)mbs[i] <= 0x80)
  889. wcs[wlen++] = mbs[i];
  890. else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  891. wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  892. else if ((BYTE)mbs[i] == 0xfd)
  893. wcs[wlen++] = 0xf8f1;
  894. else if ((BYTE)mbs[i] == 0xfe)
  895. wcs[wlen++] = 0xf8f2;
  896. else if ((BYTE)mbs[i] == 0xff)
  897. wcs[wlen++] = 0xf8f3;
  898. else
  899. state = 1;
  900. }
  901. else
  902. {
  903. if ((BYTE)mbs[i] < 0x40)
  904. wcs[wlen++] = 0x30fb;
  905. else if ((BYTE)mbs[i - 1] < 0xa0)
  906. wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  907. else
  908. wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  909. state = 0;
  910. }
  911. }
  912. break;
  913. case CP_GBK:
  914. invalid_char = 0x3f;
  915. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  916. {
  917. if (state == 0)
  918. {
  919. if ((BYTE)mbs[i] < 0x80)
  920. wcs[wlen++] = mbs[i];
  921. else if ((BYTE)mbs[i] == 0x80)
  922. wcs[wlen++] = 0x20ac;
  923. else if ((BYTE)mbs[i] == 0xff)
  924. wcs[wlen++] = 0xf8f5;
  925. else
  926. state = 1;
  927. }
  928. else
  929. {
  930. if ((BYTE)mbs[i] < 0x40)
  931. wcs[wlen++] = invalid_char;
  932. else
  933. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  934. state = 0;
  935. }
  936. }
  937. break;
  938. case CP_BIG5:
  939. invalid_char = 0x3f;
  940. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  941. {
  942. if (state == 0)
  943. {
  944. if ((BYTE)mbs[i] <= 0x80)
  945. wcs[wlen++] = mbs[i];
  946. else if ((BYTE)mbs[i] == 0xff)
  947. wcs[wlen++] = 0xf8f8;
  948. else
  949. state = 1;
  950. }
  951. else
  952. {
  953. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  954. wcs[wlen++] = invalid_char;
  955. else if ((BYTE)mbs[i] <= 0x7e)
  956. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  957. else
  958. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  959. state = 0;
  960. }
  961. }
  962. break;
  963. default:
  964. return -1;
  965. }
  966. if (state == 1 && wlen < wcslength)
  967. {
  968. wcs[wlen++] = invalid_char;
  969. }
  970. if (null)
  971. {
  972. if (wlen < wcslength)
  973. wcs[wlen++] = 0;
  974. else
  975. wcs[wlen - 1] = 0;
  976. }
  977. return wlen;
  978. }
  979. }
  980. INT
  981. PAL_MultiByteToWideChar(
  982. LPCSTR mbs,
  983. int mbslength,
  984. LPWSTR wcs,
  985. int wcslength
  986. )
  987. /*++
  988. Purpose:
  989. Convert multi-byte string into the corresponding unicode string.
  990. Parameters:
  991. [IN] mbs - Pointer to the multi-byte string.
  992. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  993. [IN] wcs - Pointer to the wide string buffer.
  994. [IN] wcslength - Length of the wide string buffer.
  995. Return value:
  996. The length of converted wide string. If mbslength is set to -1, the returned
  997. value includes the terminal null-char; otherwise, the null-char is not included.
  998. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  999. required length of the wide string buffer.
  1000. --*/
  1001. {
  1002. return PAL_MultiByteToWideCharCP(gpGlobals->iCodePage, mbs, mbslength, wcs, wcslength);
  1003. }
  1004. WCHAR
  1005. PAL_GetInvalidChar(
  1006. CODEPAGE iCodePage
  1007. )
  1008. {
  1009. switch(iCodePage)
  1010. {
  1011. case CP_BIG5: return 0x3f;
  1012. case CP_GBK: return 0x3f;
  1013. case CP_SHIFTJIS: return 0x30fb;
  1014. default: return 0;
  1015. }
  1016. }