text.c 41 KB

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