text.c 39 KB

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