text.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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 MESSAGE_MAX_BUFFER_SIZE 512
  34. #define INCLUDE_CODEPAGE_H
  35. #include "codepage.h"
  36. #ifndef PAL_CLASSIC
  37. # define ATB_WORD_COUNT 6
  38. static LPWSTR gc_rgszAdditionalWords[CP_MAX][ATB_WORD_COUNT] = {
  39. { L"\x6230\x9B25\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  40. { L"\x6218\x6597\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  41. //{ L"\x6226\x95D8\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },
  42. };
  43. static LPWSTR gc_rgszDefaultAdditionalWords[ATB_WORD_COUNT] = { NULL, L"\xFF11", L"\xFF12", L"\xFF13", L"\xFF14", L"\xFF15" };
  44. #endif
  45. #define SDLPAL_EXTRA_WORD_COUNT 1
  46. static LPWSTR gc_rgszSDLPalWords[CP_MAX][SDLPAL_EXTRA_WORD_COUNT] = {
  47. { L"\x8FD4\x56DE\x8A2D\x5B9A" },
  48. { L"\x8FD4\x56DE\x8BBE\x7F6E" },
  49. };
  50. LPWSTR g_rcCredits[12];
  51. typedef struct tagTEXTLIB
  52. {
  53. LPWSTR *lpWordBuf;
  54. LPWSTR *lpMsgBuf;
  55. int **lpIndexBuf;
  56. int nWords;
  57. int nMsgs;
  58. int nIndices;
  59. int nCurrentDialogLine;
  60. BYTE bCurrentFontColor;
  61. PAL_POS posIcon;
  62. PAL_POS posDialogTitle;
  63. PAL_POS posDialogText;
  64. BYTE bDialogPosition;
  65. BYTE bIcon;
  66. int iDelayTime;
  67. BOOL fUserSkip;
  68. BOOL fPlayingRNG;
  69. BYTE bufDialogIcons[282];
  70. } TEXTLIB, *LPTEXTLIB;
  71. static TEXTLIB g_TextLib;
  72. PAL_FORCE_INLINE int
  73. PAL_ParseLine(
  74. char *line,
  75. char **value,
  76. int *length,
  77. int deltrail
  78. )
  79. {
  80. //
  81. // Remove the leading spaces
  82. //
  83. while (*line && iswspace(*line)) line++;
  84. //
  85. // Skip comments starting with '#'
  86. //
  87. if (*line && *line != '#')
  88. {
  89. //
  90. // Split the index and value
  91. //
  92. LPSTR val = strchr(line, '=');
  93. if (val)
  94. {
  95. //
  96. // Remove the trailing spaces
  97. //
  98. LPSTR end = line + strlen(line);
  99. int index;
  100. if (end > line && end[-1] == '\n') *(--end) = 0;
  101. if (deltrail) while (end > line && iswspace(end[-1])) *(--end) = 0;
  102. //
  103. // Parse the index and pass out value
  104. //
  105. if (sscanf(line, "%d", &index) == 1)
  106. {
  107. *value = val + 1;
  108. *length = end - *value;
  109. return index;
  110. }
  111. }
  112. }
  113. return 0;
  114. }
  115. PAL_FORCE_INLINE char *
  116. PAL_ReadOneLine(
  117. char *temp,
  118. int limit,
  119. FILE *fp
  120. )
  121. {
  122. if (fgets(temp, limit, fp))
  123. {
  124. int n = strlen(temp);
  125. if (n == limit - 1 && temp[n - 1] != '\n' && !feof(fp))
  126. {
  127. // Line too long, try to read it as a whole
  128. int nn = 2;
  129. char *tmp = strdup(temp);
  130. while (!feof(fp))
  131. {
  132. if (!(tmp = (char *)realloc(tmp, nn * limit)))
  133. {
  134. TerminateOnError("PAL_ReadOneLine(): failed to allocate memory for long line!");
  135. }
  136. if (fgets(tmp + n, limit + 1, fp))
  137. {
  138. n += strlen(tmp + n);
  139. if (n < limit - 1 || temp[n - 1] == '\n')
  140. break;
  141. else
  142. nn++;
  143. }
  144. }
  145. if (tmp[n - 1] == '\n') tmp[n - 1] = 0;
  146. return tmp;
  147. }
  148. else
  149. {
  150. while (n > 0 && (temp[n - 1] == '\n' || temp[n - 1] == '\r')) temp[--n] = 0;
  151. return temp;
  152. }
  153. }
  154. else
  155. return NULL;
  156. }
  157. static int
  158. PAL_ReadMessageFile(
  159. FILE *fp
  160. )
  161. {
  162. char temp[MESSAGE_MAX_BUFFER_SIZE];
  163. struct _msg_entry
  164. {
  165. struct _msg_entry *next;
  166. wchar_t *value;
  167. } *cur_val = NULL;
  168. struct _msg_list_entry
  169. {
  170. struct _msg_list_entry *next;
  171. struct _msg_entry *value;
  172. int index;
  173. int count;
  174. } *head = NULL, *item = NULL;
  175. struct _word_list_entry
  176. {
  177. struct _word_list_entry *next;
  178. wchar_t *value;
  179. int index;
  180. } whead = { NULL, NULL }, *witem = NULL;
  181. enum _message_state
  182. {
  183. ST_OUTSIDE,
  184. ST_DIALOG,
  185. ST_WORD,
  186. ST_CREDIT,
  187. ST_LAYOUT
  188. } state = ST_OUTSIDE;
  189. int idx_cnt = 0, msg_cnt = 0, word_cnt = 0, sid, eid = -1;
  190. while (!feof(fp))
  191. {
  192. char *buffer;
  193. if (buffer = PAL_ReadOneLine(temp, MESSAGE_MAX_BUFFER_SIZE, fp))
  194. {
  195. switch(state)
  196. {
  197. case ST_OUTSIDE:
  198. //
  199. // Skip comments starting with '#'
  200. //
  201. if (*buffer && *buffer != '#')
  202. {
  203. if (strncmp(buffer, "[BEGIN MESSAGE]", 15) == 0 &&
  204. sscanf(buffer + 15, "%d", &sid) == 1 && sid > eid)
  205. {
  206. state = ST_DIALOG;
  207. //
  208. // First save values (converted wide string) into a linked list
  209. //
  210. if (head)
  211. {
  212. item->next = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  213. item = item->next;
  214. }
  215. else
  216. {
  217. head = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
  218. item = head;
  219. }
  220. item->value = NULL; item->index = sid;
  221. item->count = 0; item->next = NULL; cur_val = NULL;
  222. if (idx_cnt < item->index) idx_cnt = item->index;
  223. }
  224. else if (strncmp(buffer, "[BEGIN WORDS]", 13) == 0 && !witem)
  225. {
  226. state = ST_WORD;
  227. //
  228. // First save values (converted wide string) into a linked list
  229. //
  230. witem = &whead;
  231. }
  232. else if (strncmp(buffer, "[BEGIN CREDITS]", 15) == 0 && !witem)
  233. {
  234. state = ST_CREDIT;
  235. }
  236. else if (strncmp(buffer, "[BEGIN LAYOUT]", 14) == 0 && !witem)
  237. {
  238. state = ST_LAYOUT;
  239. gConfig.fUseCustomScreenLayout = TRUE;
  240. }
  241. else
  242. {
  243. // Just ignore invalid lines
  244. #ifdef ENABLE_LOG
  245. UTIL_WriteLog(LOG_ERR, "PAL_ReadMessageFile(): encounter invalid line '%s'!\n", line);
  246. #endif
  247. }
  248. }
  249. break;
  250. case ST_DIALOG:
  251. //
  252. // Check if to end one dialog
  253. //
  254. if (strncmp(buffer, "[END MESSAGE]", 13) == 0 &&
  255. sscanf(buffer + 13, "%d", &eid) == 1 && eid >= sid)
  256. {
  257. // End dialog
  258. state = ST_OUTSIDE;
  259. }
  260. else
  261. {
  262. if (cur_val)
  263. {
  264. cur_val->next = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  265. cur_val = cur_val->next;
  266. }
  267. else
  268. cur_val = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
  269. if (strncmp(buffer, "[CLEAR MESSAGE]", 15) == 0)
  270. {
  271. cur_val->value = NULL;
  272. }
  273. else
  274. {
  275. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, NULL, 0);
  276. cur_val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  277. PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, cur_val->value, len);
  278. msg_cnt++;
  279. }
  280. if (!item->value) item->value = cur_val;
  281. cur_val->next = NULL; item->count++;
  282. }
  283. break;
  284. case ST_WORD:
  285. //
  286. // Check if to end word list
  287. //
  288. if (strncmp(buffer, "[END WORDS]", 11) == 0)
  289. {
  290. // End word list
  291. state = ST_OUTSIDE;
  292. }
  293. else
  294. {
  295. char *v;
  296. int l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  297. if (i > 0)
  298. {
  299. int len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  300. struct _word_list_entry *val = (struct _word_list_entry *)UTIL_malloc(sizeof(struct _word_list_entry));
  301. val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  302. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, val->value, len);
  303. val->index = i; val->next = NULL;
  304. witem->next = val; witem = witem->next;
  305. if (word_cnt < i) word_cnt = i;
  306. }
  307. }
  308. break;
  309. case ST_CREDIT:
  310. //
  311. // Check if to end credit list
  312. //
  313. if (strncmp(buffer, "[END CREDITS]", 13) == 0)
  314. {
  315. // End credit list
  316. state = ST_OUTSIDE;
  317. }
  318. else
  319. {
  320. char *v;
  321. int l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  322. if ((i == 1 || (i >= 6 && i <= 11)) && !g_rcCredits[i])
  323. {
  324. int limit = (i == 1) ? 24 * 8 : 40 * 8, w = 0, j = 0, len;
  325. if (i == 6 || i == 7)
  326. {
  327. if (PAL_PLATFORM && PAL_CREDIT && PAL_PORTYEAR)
  328. {
  329. const char *templates[] = { "${platform}", "${author}", "${year}" };
  330. const char *values[] = { PAL_PLATFORM, PAL_CREDIT, PAL_PORTYEAR };
  331. const int matchlen[] = { 11, 9, 7 };
  332. const int valuelen[] = { sizeof(PAL_PLATFORM) - 1, sizeof(PAL_CREDIT) - 1, sizeof(PAL_PORTYEAR) - 1 };
  333. char *tmp = (char *)alloca(valuelen[0] + valuelen[1] + valuelen[2] + l + 1);
  334. char *dst = tmp, *src = v;
  335. while (*src)
  336. {
  337. if (*src == '$')
  338. {
  339. int k;
  340. for (k = 0; k < 3 && strncmp(src, templates[k], matchlen[k]); k++);
  341. if (k < 3)
  342. {
  343. strcpy(dst, values[k]);
  344. dst += valuelen[k];
  345. src += matchlen[k];
  346. continue;
  347. }
  348. }
  349. *dst++ = *src++;
  350. }
  351. *dst = 0;
  352. len = PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, NULL, 0);
  353. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  354. PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, g_rcCredits[i], len);
  355. }
  356. }
  357. else
  358. {
  359. len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
  360. g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
  361. PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, g_rcCredits[i], len);
  362. }
  363. if (g_rcCredits[i])
  364. {
  365. // Limit the length of texts
  366. while (w < limit && j < len - 1) w += PAL_CharWidth(g_rcCredits[i][j++]);
  367. if (w >= limit) g_rcCredits[i][w > limit ? j - 1 : j] = 0;
  368. }
  369. }
  370. }
  371. break;
  372. case ST_LAYOUT:
  373. if (strncmp(buffer, "[END LAYOUT]", 12) == 0)
  374. {
  375. // End layout
  376. state = ST_OUTSIDE;
  377. }
  378. else
  379. {
  380. char *v;
  381. int x, y, f, n, l, i = PAL_ParseLine(buffer, &v, &l, FALSE);
  382. if (i >= 1 && i <= (sizeof(SCREENLAYOUT) / sizeof(PAL_POS)))
  383. {
  384. if ((n = sscanf(v, "%d,%d,%d", &x, &y, &f)) >= 2 && x < 320 && y < 200)
  385. {
  386. gConfig.ScreenLayoutArray[i - 1] = PAL_XY(x, y);
  387. if (n == 3) gConfig.ScreenLayoutFlag[i - 1] = f;
  388. }
  389. }
  390. }
  391. break;
  392. default:
  393. TerminateOnError("PAL_ReadMessageFile(): Reached an unknown state. Something really wrong may have happened!");
  394. break;
  395. }
  396. if (buffer != temp) free(buffer);
  397. }
  398. }
  399. if (msg_cnt > 0)
  400. {
  401. //
  402. // Move values from linked list to array
  403. //
  404. int idx_msg = 1;
  405. g_TextLib.nIndices = (idx_cnt += 1);
  406. g_TextLib.nMsgs = (msg_cnt += 1);
  407. g_TextLib.lpIndexBuf = (int **)UTIL_calloc(idx_cnt, sizeof(int *));
  408. g_TextLib.lpMsgBuf = (LPWSTR *)UTIL_calloc(msg_cnt, sizeof(LPWSTR));
  409. for (item = head; item; )
  410. {
  411. struct _msg_list_entry *temp = item->next;
  412. struct _msg_entry *msg = item->value;
  413. int index = 0;
  414. g_TextLib.lpIndexBuf[item->index] = (int *)UTIL_calloc(item->count + 1, sizeof(int));
  415. while (msg)
  416. {
  417. struct _msg_entry *tmp = msg->next;
  418. if (msg->value)
  419. {
  420. g_TextLib.lpIndexBuf[item->index][index++] = idx_msg;
  421. g_TextLib.lpMsgBuf[idx_msg++] = msg->value;
  422. }
  423. else
  424. g_TextLib.lpIndexBuf[item->index][index++] = 0;
  425. free(msg); msg = tmp;
  426. }
  427. g_TextLib.lpIndexBuf[item->index][item->count] = -1;
  428. free(item); item = temp;
  429. }
  430. }
  431. if (word_cnt > 0)
  432. {
  433. //
  434. // Move values from linked list to array
  435. //
  436. #ifndef PAL_CLASSIC
  437. int i;
  438. #endif
  439. if (word_cnt < MINIMAL_WORD_COUNT - 1) word_cnt = MINIMAL_WORD_COUNT - 1;
  440. g_TextLib.nWords = (word_cnt += 1);
  441. g_TextLib.lpWordBuf = (LPWSTR *)UTIL_calloc(word_cnt, sizeof(LPWSTR));
  442. for (witem = whead.next; witem; )
  443. {
  444. struct _word_list_entry *temp = witem->next;
  445. g_TextLib.lpWordBuf[witem->index] = witem->value;
  446. free(witem); witem = temp;
  447. }
  448. #ifndef PAL_CLASSIC
  449. for (i = 1; i < ATB_WORD_COUNT; i++)
  450. if (!g_TextLib.lpWordBuf[i + SYSMENU_LABEL_BATTLEMODE])
  451. g_TextLib.lpWordBuf[i + SYSMENU_LABEL_BATTLEMODE] = gc_rgszDefaultAdditionalWords[i];
  452. #endif
  453. }
  454. fclose(fp);
  455. return (msg_cnt > 0 && word_cnt > 0) ? 1 : 0;
  456. }
  457. INT
  458. PAL_InitText(
  459. VOID
  460. )
  461. /*++
  462. Purpose:
  463. Initialize the in-game texts.
  464. Parameters:
  465. None.
  466. Return value:
  467. 0 = success.
  468. -1 = memory allocation error.
  469. --*/
  470. {
  471. if (gConfig.pszMsgFile)
  472. {
  473. //
  474. // Open the message, index and word data files.
  475. //
  476. FILE *fp = UTIL_OpenRequiredFileForMode(gConfig.pszMsgFile, "r");
  477. //
  478. // Read the contents of the message, index and word data files.
  479. //
  480. if (!PAL_ReadMessageFile(fp))
  481. {
  482. return -1;
  483. }
  484. else
  485. {
  486. DWORD dwWordLength = 0;
  487. int i;
  488. for (i = 1; i < g_TextLib.nWords; i++)
  489. {
  490. if (g_TextLib.lpWordBuf[i])
  491. {
  492. LPWSTR ptr = g_TextLib.lpWordBuf[i];
  493. DWORD n = 0;
  494. while (*ptr) n += PAL_CharWidth(*ptr++) >> 3;
  495. if (dwWordLength < n) dwWordLength = n;
  496. }
  497. }
  498. gConfig.dwWordLength = dwWordLength;
  499. for (i = 0; i < 12; i++)
  500. {
  501. if (!g_rcCredits[i])
  502. g_rcCredits[i] = L"";
  503. }
  504. }
  505. }
  506. else
  507. {
  508. FILE *fpMsg, *fpWord;
  509. DWORD *offsets;
  510. LPWSTR tmp;
  511. LPBYTE temp;
  512. int wpos, wlen, i;
  513. //
  514. // Open the message and word data files.
  515. //
  516. fpMsg = UTIL_OpenRequiredFile("m.msg");
  517. fpWord = UTIL_OpenRequiredFile("word.dat");
  518. //
  519. // See how many words we have
  520. //
  521. fseek(fpWord, 0, SEEK_END);
  522. i = ftell(fpWord);
  523. //
  524. // Each word has 10 bytes
  525. //
  526. g_TextLib.nWords = (i + (gConfig.dwWordLength - 1)) / gConfig.dwWordLength;
  527. if (g_TextLib.nWords < MINIMAL_WORD_COUNT) g_TextLib.nWords = MINIMAL_WORD_COUNT;
  528. //
  529. // Read the words
  530. //
  531. temp = (LPBYTE)malloc(gConfig.dwWordLength * g_TextLib.nWords);
  532. if (temp == NULL)
  533. {
  534. fclose(fpWord);
  535. fclose(fpMsg);
  536. return -1;
  537. }
  538. fseek(fpWord, 0, SEEK_SET);
  539. fread(temp, i, 1, fpWord);
  540. memset(temp + i, 0, gConfig.dwWordLength * g_TextLib.nWords - i);
  541. //
  542. // Close the words file
  543. //
  544. fclose(fpWord);
  545. // Split the words and do code page conversion
  546. for (i = 0, wlen = 0; i < g_TextLib.nWords; i++)
  547. {
  548. int base = i * gConfig.dwWordLength;
  549. int pos = base + gConfig.dwWordLength - 1;
  550. while (pos >= base && temp[pos] == ' ') temp[pos--] = 0;
  551. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + base, gConfig.dwWordLength, NULL, 0) + 1;
  552. }
  553. g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));
  554. if (g_TextLib.lpWordBuf == NULL)
  555. {
  556. free(temp);
  557. fclose(fpWord);
  558. fclose(fpMsg);
  559. return -1;
  560. }
  561. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  562. if (tmp == NULL)
  563. {
  564. free(g_TextLib.lpWordBuf);
  565. free(temp);
  566. fclose(fpWord);
  567. fclose(fpMsg);
  568. return -1;
  569. }
  570. for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)
  571. {
  572. int l;
  573. g_TextLib.lpWordBuf[i] = tmp + wpos;
  574. l = PAL_MultiByteToWideChar((LPCSTR)temp + i * gConfig.dwWordLength, gConfig.dwWordLength, g_TextLib.lpWordBuf[i], wlen - wpos);
  575. if (l > 0 && g_TextLib.lpWordBuf[i][l - 1] == '1')
  576. g_TextLib.lpWordBuf[i][l - 1] = 0;
  577. g_TextLib.lpWordBuf[i][l] = 0;
  578. wpos += l + 1;
  579. }
  580. free(temp);
  581. //
  582. // Read the message offsets. The message offsets are in SSS.MKF #3
  583. //
  584. i = PAL_MKFGetChunkSize(3, gpGlobals->f.fpSSS) / sizeof(DWORD);
  585. g_TextLib.nMsgs = i - 1;
  586. offsets = (LPDWORD)malloc(i * sizeof(DWORD));
  587. if (offsets == NULL)
  588. {
  589. free(g_TextLib.lpWordBuf);
  590. fclose(fpMsg);
  591. return -1;
  592. }
  593. PAL_MKFReadChunk((LPBYTE)(offsets), i * sizeof(DWORD), 3, gpGlobals->f.fpSSS);
  594. //
  595. // Read the messages.
  596. //
  597. fseek(fpMsg, 0, SEEK_END);
  598. i = ftell(fpMsg);
  599. temp = (LPBYTE)malloc(i);
  600. if (temp == NULL)
  601. {
  602. free(offsets);
  603. free(g_TextLib.lpWordBuf[0]);
  604. free(g_TextLib.lpWordBuf);
  605. fclose(fpMsg);
  606. return -1;
  607. }
  608. fseek(fpMsg, 0, SEEK_SET);
  609. fread(temp, 1, i, fpMsg);
  610. fclose(fpMsg);
  611. // Split messages and do code page conversion here
  612. for (i = 0, wlen = 0; i < g_TextLib.nMsgs; i++)
  613. {
  614. wlen += PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), NULL, 0) + 1;
  615. }
  616. g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
  617. if (g_TextLib.lpMsgBuf == NULL)
  618. {
  619. free(g_TextLib.lpWordBuf);
  620. free(offsets);
  621. return -1;
  622. }
  623. tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
  624. if (tmp == NULL)
  625. {
  626. free(g_TextLib.lpMsgBuf);
  627. free(g_TextLib.lpWordBuf);
  628. free(offsets);
  629. return -1;
  630. }
  631. for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)
  632. {
  633. int l;
  634. g_TextLib.lpMsgBuf[i] = tmp + wpos;
  635. l = PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), g_TextLib.lpMsgBuf[i], wlen - wpos);
  636. g_TextLib.lpMsgBuf[i][l] = 0;
  637. wpos += l + 1;
  638. }
  639. free(temp);
  640. free(offsets);
  641. g_TextLib.lpIndexBuf = NULL;
  642. memcpy(g_TextLib.lpWordBuf + SYSMENU_LABEL_LAUNCHSETTING, gc_rgszSDLPalWords[gConfig.uCodePage], SDLPAL_EXTRA_WORD_COUNT * sizeof(LPCWSTR));
  643. #ifndef PAL_CLASSIC
  644. memcpy(g_TextLib.lpWordBuf + SYSMENU_LABEL_BATTLEMODE, gc_rgszAdditionalWords[gConfig.uCodePage], ATB_WORD_COUNT * sizeof(LPCWSTR));
  645. #endif
  646. }
  647. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  648. g_TextLib.bIcon = 0;
  649. g_TextLib.posIcon = 0;
  650. g_TextLib.nCurrentDialogLine = 0;
  651. g_TextLib.iDelayTime = 3;
  652. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  653. g_TextLib.posDialogText = PAL_XY(44, 26);
  654. g_TextLib.bDialogPosition = kDialogUpper;
  655. g_TextLib.fUserSkip = FALSE;
  656. PAL_MKFReadChunk(g_TextLib.bufDialogIcons, 282, 12, gpGlobals->f.fpDATA);
  657. return 0;
  658. }
  659. VOID
  660. PAL_FreeText(
  661. VOID
  662. )
  663. /*++
  664. Purpose:
  665. Free the memory used by the texts.
  666. Parameters:
  667. None.
  668. Return value:
  669. None.
  670. --*/
  671. {
  672. int i;
  673. if (g_TextLib.lpMsgBuf != NULL)
  674. {
  675. if (gConfig.pszMsgFile)
  676. for(i = 0; i < g_TextLib.nMsgs; i++) free(g_TextLib.lpMsgBuf[i]);
  677. else
  678. free(g_TextLib.lpMsgBuf[0]);
  679. free(g_TextLib.lpMsgBuf);
  680. g_TextLib.lpMsgBuf = NULL;
  681. }
  682. if (g_TextLib.lpWordBuf != NULL)
  683. {
  684. if (gConfig.pszMsgFile)
  685. for(i = 0; i < g_TextLib.nWords; i++) free(g_TextLib.lpWordBuf[i]);
  686. else
  687. free(g_TextLib.lpWordBuf[0]);
  688. free(g_TextLib.lpWordBuf);
  689. g_TextLib.lpWordBuf = NULL;
  690. }
  691. if (g_TextLib.lpIndexBuf != NULL)
  692. {
  693. if (gConfig.pszMsgFile)
  694. for(i = 0; i < g_TextLib.nIndices; i++) free(g_TextLib.lpIndexBuf[i]);
  695. else
  696. free(g_TextLib.lpIndexBuf[0]);
  697. free(g_TextLib.lpIndexBuf);
  698. g_TextLib.lpIndexBuf = NULL;
  699. }
  700. }
  701. LPCWSTR
  702. PAL_GetWord(
  703. int iNumWord
  704. )
  705. /*++
  706. Purpose:
  707. Get the specified word.
  708. Parameters:
  709. [IN] wNumWord - the number of the requested word.
  710. Return value:
  711. Pointer to the requested word. NULL if not found.
  712. --*/
  713. {
  714. return (iNumWord >= g_TextLib.nWords || !g_TextLib.lpWordBuf[iNumWord]) ? L"" : g_TextLib.lpWordBuf[iNumWord];
  715. }
  716. LPCWSTR
  717. PAL_GetMsg(
  718. int iNumMsg
  719. )
  720. /*++
  721. Purpose:
  722. Get the specified message.
  723. Parameters:
  724. [IN] wNumMsg - the number of the requested message.
  725. Return value:
  726. Pointer to the requested message. NULL if not found.
  727. --*/
  728. {
  729. return (iNumMsg >= g_TextLib.nMsgs || !g_TextLib.lpMsgBuf[iNumMsg]) ? L"" : g_TextLib.lpMsgBuf[iNumMsg];
  730. }
  731. int
  732. PAL_GetMsgNum(
  733. int iIndex,
  734. int iOrder
  735. )
  736. /*++
  737. Purpose:
  738. Get the number of specified message from index & order.
  739. Parameters:
  740. [IN] iMsgIndex - index.
  741. [IN] iOrder - order inside the index.
  742. Return value:
  743. The number of message. Zero means pausing for key, and -1 means end.
  744. --*/
  745. {
  746. return (iIndex >= g_TextLib.nMsgs || !g_TextLib.lpIndexBuf[iIndex]) ? -1 : g_TextLib.lpIndexBuf[iIndex][iOrder];
  747. }
  748. VOID
  749. PAL_DrawText(
  750. LPCWSTR lpszText,
  751. PAL_POS pos,
  752. BYTE bColor,
  753. BOOL fShadow,
  754. BOOL fUpdate,
  755. BOOL fUse8x8Font
  756. )
  757. /*++
  758. Purpose:
  759. Draw text on the screen.
  760. Parameters:
  761. [IN] lpszText - the text to be drawn.
  762. [IN] pos - Position of the text.
  763. [IN] bColor - Color of the text.
  764. [IN] fShadow - TRUE if the text is shadowed or not.
  765. [IN] fUpdate - TRUE if update the screen area.
  766. [IN] fUse8x8Font - TRUE if use 8x8 font.
  767. Return value:
  768. None.
  769. --*/
  770. {
  771. SDL_Rect rect, urect;
  772. urect.x = rect.x = PAL_X(pos);
  773. urect.y = rect.y = PAL_Y(pos);
  774. urect.h = (fUse8x8Font ? 8 : PAL_FontHeight()) + (fShadow ? 1 : 0);
  775. urect.w = 0;
  776. while (*lpszText)
  777. {
  778. //
  779. // Draw the character
  780. //
  781. int char_width = fUse8x8Font ? 8 : PAL_CharWidth(*lpszText);
  782. if (fShadow)
  783. {
  784. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0, fUse8x8Font);
  785. PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0, fUse8x8Font);
  786. }
  787. PAL_DrawCharOnSurface(*lpszText++, gpScreen, PAL_XY(rect.x, rect.y), bColor, fUse8x8Font);
  788. rect.x += char_width; urect.w += char_width;
  789. }
  790. //
  791. // Update the screen area
  792. //
  793. if (fUpdate && urect.w > 0)
  794. {
  795. if (fShadow) urect.w++;
  796. if (urect.x + urect.w > 320)
  797. {
  798. urect.w = 320 - urect.x;
  799. }
  800. VIDEO_UpdateScreen(&urect);
  801. }
  802. }
  803. VOID
  804. PAL_DialogSetDelayTime(
  805. INT iDelayTime
  806. )
  807. /*++
  808. Purpose:
  809. Set the delay time for dialog.
  810. Parameters:
  811. [IN] iDelayTime - the delay time to be set.
  812. Return value:
  813. None.
  814. --*/
  815. {
  816. g_TextLib.iDelayTime = iDelayTime;
  817. }
  818. VOID
  819. PAL_StartDialog(
  820. BYTE bDialogLocation,
  821. BYTE bFontColor,
  822. INT iNumCharFace,
  823. BOOL fPlayingRNG
  824. )
  825. /*++
  826. Purpose:
  827. Start a new dialog.
  828. Parameters:
  829. [IN] bDialogLocation - the location of the text on the screen.
  830. [IN] bFontColor - the font color of the text.
  831. [IN] iNumCharFace - number of the character face in RGM.MKF.
  832. [IN] fPlayingRNG - whether we are playing a RNG video or not.
  833. Return value:
  834. None.
  835. --*/
  836. {
  837. PAL_LARGE BYTE buf[16384];
  838. SDL_Rect rect;
  839. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  840. {
  841. //
  842. // Update the screen in battle, or the graphics may seem messed up
  843. //
  844. VIDEO_UpdateScreen(NULL);
  845. g_fUpdatedInBattle = TRUE;
  846. }
  847. g_TextLib.bIcon = 0;
  848. g_TextLib.posIcon = 0;
  849. g_TextLib.nCurrentDialogLine = 0;
  850. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  851. g_TextLib.fUserSkip = FALSE;
  852. if (bFontColor != 0)
  853. {
  854. g_TextLib.bCurrentFontColor = bFontColor;
  855. }
  856. if (fPlayingRNG && iNumCharFace)
  857. {
  858. VIDEO_BackupScreen();
  859. g_TextLib.fPlayingRNG = TRUE;
  860. }
  861. switch (bDialogLocation)
  862. {
  863. case kDialogUpper:
  864. if (iNumCharFace > 0)
  865. {
  866. //
  867. // Display the character face at the upper part of the screen
  868. //
  869. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  870. {
  871. rect.w = PAL_RLEGetWidth((LPCBITMAPRLE)buf);
  872. rect.h = PAL_RLEGetHeight((LPCBITMAPRLE)buf);
  873. rect.x = 48 - rect.w / 2;
  874. rect.y = 55 - rect.h / 2;
  875. if (rect.x < 0)
  876. {
  877. rect.x = 0;
  878. }
  879. if (rect.y < 0)
  880. {
  881. rect.y = 0;
  882. }
  883. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  884. if (rect.x < 0)
  885. {
  886. rect.x = 0;
  887. }
  888. if (rect.y < 0)
  889. {
  890. rect.y = 0;
  891. }
  892. VIDEO_UpdateScreen(&rect);
  893. }
  894. }
  895. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 80 : 12, 8);
  896. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 96 : 44, 26);
  897. break;
  898. case kDialogCenter:
  899. g_TextLib.posDialogText = PAL_XY(80, 40);
  900. break;
  901. case kDialogLower:
  902. if (iNumCharFace > 0)
  903. {
  904. //
  905. // Display the character face at the lower part of the screen
  906. //
  907. if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)
  908. {
  909. rect.x = 270 - PAL_RLEGetWidth((LPCBITMAPRLE)buf) / 2;
  910. rect.y = 144 - PAL_RLEGetHeight((LPCBITMAPRLE)buf) / 2;
  911. PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));
  912. VIDEO_UpdateScreen(NULL);
  913. }
  914. }
  915. g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 4 : 12, 108);
  916. g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 20 : 44, 126);
  917. break;
  918. case kDialogCenterWindow:
  919. g_TextLib.posDialogText = PAL_XY(160, 40);
  920. break;
  921. }
  922. g_TextLib.bDialogPosition = bDialogLocation;
  923. }
  924. static VOID
  925. PAL_DialogWaitForKey(
  926. VOID
  927. )
  928. /*++
  929. Purpose:
  930. Wait for player to press a key after showing a dialog.
  931. Parameters:
  932. None.
  933. Return value:
  934. None.
  935. --*/
  936. {
  937. PAL_LARGE SDL_Color palette[256];
  938. SDL_Color *pCurrentPalette, t;
  939. int i;
  940. //
  941. // get the current palette
  942. //
  943. pCurrentPalette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  944. memcpy(palette, pCurrentPalette, sizeof(palette));
  945. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  946. g_TextLib.bDialogPosition != kDialogCenter)
  947. {
  948. //
  949. // show the icon
  950. //
  951. LPCBITMAPRLE p = PAL_SpriteGetFrame(g_TextLib.bufDialogIcons, g_TextLib.bIcon);
  952. if (p != NULL)
  953. {
  954. SDL_Rect rect;
  955. rect.x = PAL_X(g_TextLib.posIcon);
  956. rect.y = PAL_Y(g_TextLib.posIcon);
  957. rect.w = 16;
  958. rect.h = 16;
  959. PAL_RLEBlitToSurface(p, gpScreen, g_TextLib.posIcon);
  960. VIDEO_UpdateScreen(&rect);
  961. }
  962. }
  963. PAL_ClearKeyState();
  964. while (TRUE)
  965. {
  966. UTIL_Delay(100);
  967. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  968. g_TextLib.bDialogPosition != kDialogCenter)
  969. {
  970. //
  971. // palette shift
  972. //
  973. t = palette[0xF9];
  974. for (i = 0xF9; i < 0xFE; i++)
  975. {
  976. palette[i] = palette[i + 1];
  977. }
  978. palette[0xFE] = t;
  979. VIDEO_SetPalette(palette);
  980. }
  981. if (g_InputState.dwKeyPress != 0)
  982. {
  983. break;
  984. }
  985. }
  986. if (g_TextLib.bDialogPosition != kDialogCenterWindow &&
  987. g_TextLib.bDialogPosition != kDialogCenter)
  988. {
  989. PAL_SetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  990. }
  991. PAL_ClearKeyState();
  992. g_TextLib.fUserSkip = FALSE;
  993. }
  994. VOID
  995. PAL_ShowDialogText(
  996. LPCWSTR lpszText
  997. )
  998. /*++
  999. Purpose:
  1000. Show one line of the dialog text.
  1001. Parameters:
  1002. [IN] lpszText - the text to be shown.
  1003. Return value:
  1004. None.
  1005. --*/
  1006. {
  1007. SDL_Rect rect;
  1008. int x, y;
  1009. PAL_ClearKeyState();
  1010. g_TextLib.bIcon = 0;
  1011. if (gpGlobals->fInBattle && !g_fUpdatedInBattle)
  1012. {
  1013. //
  1014. // Update the screen in battle, or the graphics may seem messed up
  1015. //
  1016. VIDEO_UpdateScreen(NULL);
  1017. g_fUpdatedInBattle = TRUE;
  1018. }
  1019. if (g_TextLib.nCurrentDialogLine > 3)
  1020. {
  1021. //
  1022. // The rest dialogs should be shown in the next page.
  1023. //
  1024. PAL_DialogWaitForKey();
  1025. g_TextLib.nCurrentDialogLine = 0;
  1026. VIDEO_RestoreScreen();
  1027. VIDEO_UpdateScreen(NULL);
  1028. }
  1029. x = PAL_X(g_TextLib.posDialogText);
  1030. y = PAL_Y(g_TextLib.posDialogText) + g_TextLib.nCurrentDialogLine * 18;
  1031. if (g_TextLib.bDialogPosition == kDialogCenterWindow)
  1032. {
  1033. //
  1034. // The text should be shown in a small window at the center of the screen
  1035. //
  1036. #ifndef PAL_CLASSIC
  1037. if (gpGlobals->fInBattle && g_Battle.BattleResult == kBattleResultOnGoing)
  1038. {
  1039. PAL_BattleUIShowText(lpszText, 1400);
  1040. }
  1041. else
  1042. #endif
  1043. {
  1044. PAL_POS pos;
  1045. LPBOX lpBox;
  1046. int i, w = wcslen(lpszText), len = 0;
  1047. for (i = 0; i < w; i++)
  1048. len += PAL_CharWidth(lpszText[i]) >> 3;
  1049. //
  1050. // Create the window box
  1051. //
  1052. pos = PAL_XY(PAL_X(g_TextLib.posDialogText) - len * 4, PAL_Y(g_TextLib.posDialogText));
  1053. lpBox = PAL_CreateSingleLineBox(pos, (len + 1) / 2, TRUE);
  1054. rect.x = PAL_X(pos);
  1055. rect.y = PAL_Y(pos);
  1056. rect.w = 320 - rect.x * 2 + 32;
  1057. rect.h = 64;
  1058. //
  1059. // Show the text on the screen
  1060. //
  1061. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  1062. PAL_DrawText(lpszText, pos, 0, FALSE, FALSE, FALSE);
  1063. VIDEO_UpdateScreen(&rect);
  1064. PAL_DialogWaitForKey();
  1065. //
  1066. // Delete the box
  1067. //
  1068. PAL_DeleteBox(lpBox);
  1069. VIDEO_UpdateScreen(&rect);
  1070. PAL_EndDialog();
  1071. }
  1072. }
  1073. else
  1074. {
  1075. int len = wcslen(lpszText);
  1076. if (g_TextLib.nCurrentDialogLine == 0 &&
  1077. g_TextLib.bDialogPosition != kDialogCenter &&
  1078. (lpszText[len - 1] == 0xff1a ||
  1079. lpszText[len - 1] == 0x2236 || // Special case for Pal WIN95 Simplified Chinese version
  1080. lpszText[len - 1] == ':')
  1081. )
  1082. {
  1083. //
  1084. // name of character
  1085. //
  1086. PAL_DrawText(lpszText, g_TextLib.posDialogTitle, FONT_COLOR_CYAN_ALT, TRUE, TRUE, FALSE);
  1087. }
  1088. else
  1089. {
  1090. //
  1091. // normal texts
  1092. //
  1093. WCHAR text[2];
  1094. if (!g_TextLib.fPlayingRNG && g_TextLib.nCurrentDialogLine == 0)
  1095. {
  1096. //
  1097. // Save the screen before we show the first line of dialog
  1098. //
  1099. VIDEO_BackupScreen();
  1100. }
  1101. while (lpszText != NULL && *lpszText != '\0')
  1102. {
  1103. switch (*lpszText)
  1104. {
  1105. case '-':
  1106. //
  1107. // Set the font color to Cyan
  1108. //
  1109. if (g_TextLib.bCurrentFontColor == FONT_COLOR_CYAN)
  1110. {
  1111. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1112. }
  1113. else
  1114. {
  1115. g_TextLib.bCurrentFontColor = FONT_COLOR_CYAN;
  1116. }
  1117. lpszText++;
  1118. break;
  1119. #if 0
  1120. /* Not used */
  1121. case '\'':
  1122. //
  1123. // Set the font color to Red
  1124. //
  1125. if (g_TextLib.bCurrentFontColor == FONT_COLOR_RED)
  1126. {
  1127. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1128. }
  1129. else
  1130. {
  1131. g_TextLib.bCurrentFontColor = FONT_COLOR_RED;
  1132. }
  1133. lpszText++;
  1134. break;
  1135. #endif
  1136. case '\"':
  1137. //
  1138. // Set the font color to Yellow
  1139. //
  1140. if (g_TextLib.bCurrentFontColor == FONT_COLOR_YELLOW)
  1141. {
  1142. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1143. }
  1144. else
  1145. {
  1146. g_TextLib.bCurrentFontColor = FONT_COLOR_YELLOW;
  1147. }
  1148. lpszText++;
  1149. break;
  1150. case '$':
  1151. //
  1152. // Set the delay time of text-displaying
  1153. //
  1154. g_TextLib.iDelayTime = wcstol(lpszText + 1, NULL, 10) * 10 / 7;
  1155. lpszText += 3;
  1156. break;
  1157. case '~':
  1158. //
  1159. // Delay for a period and quit
  1160. //
  1161. UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);
  1162. g_TextLib.nCurrentDialogLine = 0;
  1163. g_TextLib.fUserSkip = FALSE;
  1164. return; // don't go further
  1165. case ')':
  1166. //
  1167. // Set the waiting icon
  1168. //
  1169. g_TextLib.bIcon = 1;
  1170. lpszText++;
  1171. break;
  1172. case '(':
  1173. //
  1174. // Set the waiting icon
  1175. //
  1176. g_TextLib.bIcon = 2;
  1177. lpszText++;
  1178. break;
  1179. case '\\':
  1180. lpszText++;
  1181. default:
  1182. text[0] = *lpszText++;
  1183. text[1] = 0;
  1184. PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, TRUE, FALSE);
  1185. x += PAL_CharWidth(text[0]);
  1186. if (!g_TextLib.fUserSkip)
  1187. {
  1188. PAL_ClearKeyState();
  1189. UTIL_Delay(g_TextLib.iDelayTime * 8);
  1190. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  1191. {
  1192. //
  1193. // User pressed a key to skip the dialog
  1194. //
  1195. g_TextLib.fUserSkip = TRUE;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. g_TextLib.posIcon = PAL_XY(x, y);
  1201. g_TextLib.nCurrentDialogLine++;
  1202. }
  1203. }
  1204. }
  1205. VOID
  1206. PAL_ClearDialog(
  1207. BOOL fWaitForKey
  1208. )
  1209. /*++
  1210. Purpose:
  1211. Clear the state of the dialog.
  1212. Parameters:
  1213. [IN] fWaitForKey - whether wait for any key or not.
  1214. Return value:
  1215. None.
  1216. --*/
  1217. {
  1218. if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)
  1219. {
  1220. PAL_DialogWaitForKey();
  1221. }
  1222. g_TextLib.nCurrentDialogLine = 0;
  1223. if (g_TextLib.bDialogPosition == kDialogCenter)
  1224. {
  1225. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1226. g_TextLib.posDialogText = PAL_XY(44, 26);
  1227. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1228. g_TextLib.bDialogPosition = kDialogUpper;
  1229. }
  1230. }
  1231. VOID
  1232. PAL_EndDialog(
  1233. VOID
  1234. )
  1235. /*++
  1236. Purpose:
  1237. Ends a dialog.
  1238. Parameters:
  1239. None.
  1240. Return value:
  1241. None.
  1242. --*/
  1243. {
  1244. PAL_ClearDialog(TRUE);
  1245. //
  1246. // Set some default parameters, as there are some parts of script
  1247. // which doesn't have a "start dialog" instruction before showing the dialog.
  1248. //
  1249. g_TextLib.posDialogTitle = PAL_XY(12, 8);
  1250. g_TextLib.posDialogText = PAL_XY(44, 26);
  1251. g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;
  1252. g_TextLib.bDialogPosition = kDialogUpper;
  1253. g_TextLib.fUserSkip = FALSE;
  1254. g_TextLib.fPlayingRNG = FALSE;
  1255. }
  1256. BOOL
  1257. PAL_IsInDialog(
  1258. VOID
  1259. )
  1260. /*++
  1261. Purpose:
  1262. Check if there are dialog texts on the screen.
  1263. Parameters:
  1264. None.
  1265. Return value:
  1266. TRUE if there are dialog texts on the screen, FALSE if not.
  1267. --*/
  1268. {
  1269. return (g_TextLib.nCurrentDialogLine != 0);
  1270. }
  1271. BOOL
  1272. PAL_DialogIsPlayingRNG(
  1273. VOID
  1274. )
  1275. /*++
  1276. Purpose:
  1277. Check if the script used the RNG playing parameter when displaying texts.
  1278. Parameters:
  1279. None.
  1280. Return value:
  1281. TRUE if the script used the RNG playing parameter, FALSE if not.
  1282. --*/
  1283. {
  1284. return g_TextLib.fPlayingRNG;
  1285. }
  1286. INT
  1287. PAL_MultiByteToWideCharCP(
  1288. CODEPAGE cp,
  1289. LPCSTR mbs,
  1290. int mbslength,
  1291. LPWSTR wcs,
  1292. int wcslength
  1293. )
  1294. /*++
  1295. Purpose:
  1296. Convert multi-byte string into the corresponding unicode string.
  1297. Parameters:
  1298. [IN] cp - Code page for conversion.
  1299. [IN] mbs - Pointer to the multi-byte string.
  1300. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1301. [IN] wcs - Pointer to the wide string buffer.
  1302. [IN] wcslength - Length of the wide string buffer.
  1303. Return value:
  1304. The length of converted wide string. If mbslength is set to -1, the returned
  1305. value includes the terminal null-char; otherwise, the null-char is not included.
  1306. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1307. required length of the wide string buffer.
  1308. --*/
  1309. {
  1310. int i = 0, state = 0, wlen = 0, null = 0;
  1311. if (mbslength == -1)
  1312. {
  1313. mbslength = strlen(mbs);
  1314. null = 1;
  1315. }
  1316. if (!wcs)
  1317. {
  1318. switch (cp)
  1319. {
  1320. //case CP_SHIFTJIS:
  1321. // for (i = 0; i < mbslength && mbs[i]; i++)
  1322. // {
  1323. // if (state == 0)
  1324. // {
  1325. // if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))
  1326. // wlen++;
  1327. // else
  1328. // state = 1;
  1329. // }
  1330. // else
  1331. // {
  1332. // wlen++;
  1333. // state = 0;
  1334. // }
  1335. // }
  1336. // break;
  1337. case CP_GBK:
  1338. case CP_BIG5:
  1339. for (i = 0; i < mbslength && mbs[i]; i++)
  1340. {
  1341. if (state == 0)
  1342. {
  1343. if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)
  1344. wlen++;
  1345. else
  1346. state = 1;
  1347. }
  1348. else
  1349. {
  1350. wlen++;
  1351. state = 0;
  1352. }
  1353. }
  1354. break;
  1355. case CP_UTF_8:
  1356. for (i = 0; i < mbslength && mbs[i]; i++)
  1357. {
  1358. if (state == 0)
  1359. {
  1360. if ((BYTE)mbs[i] >= 0x80)
  1361. {
  1362. BYTE s = (BYTE)mbs[i] << 1;
  1363. while (s >= 0x80) { state++; s <<= 1; }
  1364. if (state < 1 || state > 3)
  1365. {
  1366. state = 0;
  1367. wlen++;
  1368. }
  1369. }
  1370. else
  1371. wlen++;
  1372. }
  1373. else
  1374. {
  1375. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1376. {
  1377. if (--state == 0) wlen++;
  1378. }
  1379. else
  1380. {
  1381. state = 0; wlen++;
  1382. }
  1383. }
  1384. }
  1385. break;
  1386. default:
  1387. return -1;
  1388. }
  1389. if (i < mbslength && !mbs[i]) null = 1;
  1390. return wlen + null + (state != 0);
  1391. }
  1392. else
  1393. {
  1394. WCHAR invalid_char;
  1395. switch (cp)
  1396. {
  1397. //case CP_SHIFTJIS:
  1398. // invalid_char = 0x30fb;
  1399. // for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1400. // {
  1401. // if (state == 0)
  1402. // {
  1403. // if ((BYTE)mbs[i] <= 0x80)
  1404. // wcs[wlen++] = mbs[i];
  1405. // else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)
  1406. // wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];
  1407. // else if ((BYTE)mbs[i] == 0xfd)
  1408. // wcs[wlen++] = 0xf8f1;
  1409. // else if ((BYTE)mbs[i] == 0xfe)
  1410. // wcs[wlen++] = 0xf8f2;
  1411. // else if ((BYTE)mbs[i] == 0xff)
  1412. // wcs[wlen++] = 0xf8f3;
  1413. // else
  1414. // state = 1;
  1415. // }
  1416. // else
  1417. // {
  1418. // if ((BYTE)mbs[i] < 0x40)
  1419. // wcs[wlen++] = 0x30fb;
  1420. // else if ((BYTE)mbs[i - 1] < 0xa0)
  1421. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1422. // else
  1423. // wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];
  1424. // state = 0;
  1425. // }
  1426. // }
  1427. // break;
  1428. case CP_GBK:
  1429. invalid_char = 0x3f;
  1430. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1431. {
  1432. if (state == 0)
  1433. {
  1434. if ((BYTE)mbs[i] < 0x80)
  1435. wcs[wlen++] = mbs[i];
  1436. else if ((BYTE)mbs[i] == 0x80)
  1437. wcs[wlen++] = 0x20ac;
  1438. else if ((BYTE)mbs[i] == 0xff)
  1439. wcs[wlen++] = 0xf8f5;
  1440. else
  1441. state = 1;
  1442. }
  1443. else
  1444. {
  1445. if ((BYTE)mbs[i] < 0x40)
  1446. wcs[wlen++] = invalid_char;
  1447. else
  1448. wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1449. state = 0;
  1450. }
  1451. }
  1452. break;
  1453. case CP_BIG5:
  1454. invalid_char = 0x3f;
  1455. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1456. {
  1457. if (state == 0)
  1458. {
  1459. if ((BYTE)mbs[i] <= 0x80)
  1460. wcs[wlen++] = mbs[i];
  1461. else if ((BYTE)mbs[i] == 0xff)
  1462. wcs[wlen++] = 0xf8f8;
  1463. else
  1464. state = 1;
  1465. }
  1466. else
  1467. {
  1468. if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))
  1469. wcs[wlen++] = invalid_char;
  1470. else if ((BYTE)mbs[i] <= 0x7e)
  1471. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];
  1472. else
  1473. wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];
  1474. state = 0;
  1475. }
  1476. }
  1477. break;
  1478. case CP_UTF_8:
  1479. invalid_char = 0x3f;
  1480. for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)
  1481. {
  1482. if (state == 0)
  1483. {
  1484. if ((BYTE)mbs[i] >= 0x80)
  1485. {
  1486. BYTE s = (BYTE)mbs[i] << 1;
  1487. while (s >= 0x80) { state++; s <<= 1; }
  1488. if (state < 1 || state > 3)
  1489. {
  1490. state = 0;
  1491. wcs[wlen++] = invalid_char;
  1492. }
  1493. else
  1494. {
  1495. wcs[wlen] = s >> (state + 1);
  1496. }
  1497. }
  1498. else
  1499. wcs[wlen++] = mbs[i];
  1500. }
  1501. else
  1502. {
  1503. if ((BYTE)mbs[i] >= 0x80 && (BYTE)mbs[i] < 0xc0)
  1504. {
  1505. wcs[wlen] <<= 6;
  1506. wcs[wlen] |= (BYTE)mbs[i] & 0x3f;
  1507. if (--state == 0) wlen++;
  1508. }
  1509. else
  1510. {
  1511. state = 0;
  1512. wcs[wlen++] = invalid_char;
  1513. }
  1514. }
  1515. }
  1516. break;
  1517. default:
  1518. return -1;
  1519. }
  1520. if (state != 0 && wlen < wcslength)
  1521. {
  1522. wcs[wlen++] = invalid_char;
  1523. }
  1524. if (null || (i < mbslength && !mbs[i]))
  1525. {
  1526. if (wlen < wcslength)
  1527. wcs[wlen++] = 0;
  1528. else
  1529. wcs[wlen - 1] = 0;
  1530. }
  1531. return wlen;
  1532. }
  1533. }
  1534. INT
  1535. PAL_MultiByteToWideChar(
  1536. LPCSTR mbs,
  1537. int mbslength,
  1538. LPWSTR wcs,
  1539. int wcslength
  1540. )
  1541. /*++
  1542. Purpose:
  1543. Convert multi-byte string into the corresponding unicode string.
  1544. Parameters:
  1545. [IN] mbs - Pointer to the multi-byte string.
  1546. [IN] mbslength - Length of the multi-byte string, or -1 for auto-detect.
  1547. [IN] wcs - Pointer to the wide string buffer.
  1548. [IN] wcslength - Length of the wide string buffer.
  1549. Return value:
  1550. The length of converted wide string. If mbslength is set to -1, the returned
  1551. value includes the terminal null-char; otherwise, the null-char is not included.
  1552. If wcslength is set to 0, wcs can be set to NULL and the return value is the
  1553. required length of the wide string buffer.
  1554. --*/
  1555. {
  1556. return PAL_MultiByteToWideCharCP(gConfig.uCodePage, mbs, mbslength, wcs, wcslength);
  1557. }
  1558. WCHAR
  1559. PAL_GetInvalidChar(
  1560. CODEPAGE uCodePage
  1561. )
  1562. {
  1563. switch(uCodePage)
  1564. {
  1565. case CP_BIG5: return 0x3f;
  1566. case CP_GBK: return 0x3f;
  1567. //case CP_SHIFTJIS: return 0x30fb;
  1568. case CP_UTF_8: return 0x3f;
  1569. default: return 0;
  1570. }
  1571. }