text.c 25 KB

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