| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386 | /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- *///// Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.// All rights reserved.//// Portions based on PALx Project by palxex.// Copyright (c) 2006-2008, Pal Lockheart <palxex@gmail.com>.//// This file is part of SDLPAL.//// SDLPAL is free software: you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation, either version 3 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program.  If not, see <http://www.gnu.org/licenses/>.//// Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015//#include "main.h"#ifndef PAL_UNICODE#define WORD_LENGTH      10#endif#define   FONT_COLOR_DEFAULT        0x4F#define   FONT_COLOR_YELLOW         0x2D#define   FONT_COLOR_RED            0x1A#define   FONT_COLOR_CYAN           0x8D#define   FONT_COLOR_CYAN_ALT       0x8CBOOL      g_fUpdatedInBattle      = FALSE;#ifdef PAL_UNICODE#define INCLUDE_CODEPAGE_H#include "codepage.h"static const WCHAR* gc_rgszAdditionalWords[CP_MAX][6] = {   { L"\x6230\x9B25\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },   { L"\x6218\x6597\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },   { L"\x6226\x95D8\x901F\x5EA6", L"\x4E00", L"\x4E8C", L"\x4E09", L"\x56DB", L"\x4E94" },};static const WCHAR** g_rgszAdditionalWords;#elsestatic const char g_rgszAdditionalWords[][WORD_LENGTH + 1] = {   {0xBE, 0xD4, 0xB0, 0xAB, 0xB3, 0x74, 0xAB, 0xD7, 0x00, 0x00, 0x00}, // Battle Speed   {0xA4, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 1   {0xA4, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 2   {0xA4, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 3   {0xA5, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 4   {0xA4, 0xAD, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 5};#endiftypedef struct tagTEXTLIB{#ifdef PAL_UNICODE   LPWSTR*         lpWordBuf;   LPWSTR*         lpMsgBuf;#else   LPBYTE          lpWordBuf;   LPBYTE          lpMsgBuf;   LPDWORD         lpMsgOffset;#endif   int             nWords;   int             nMsgs;   int             nCurrentDialogLine;   BYTE            bCurrentFontColor;   PAL_POS         posIcon;   PAL_POS         posDialogTitle;   PAL_POS         posDialogText;   BYTE            bDialogPosition;   BYTE            bIcon;   int             iDelayTime;   BOOL            fUserSkip;   BOOL            fPlayingRNG;   BYTE            bufDialogIcons[282];} TEXTLIB, *LPTEXTLIB;static TEXTLIB         g_TextLib;INTPAL_InitText(   VOID)/*++  Purpose:    Initialize the in-game texts.  Parameters:    None.  Return value:    0 = success.    -1 = memory allocation error.--*/{   FILE       *fpMsg, *fpWord;#ifdef PAL_UNICODE   DWORD      *offsets;   LPBYTE      temp;   LPWSTR      tmp;   int         wlen, wpos;#endif   int         i;   //   // Open the message and word data files.   //   fpMsg = UTIL_OpenRequiredFile("m.msg");   fpWord = UTIL_OpenRequiredFile("word.dat");   //   // See how many words we have   //   fseek(fpWord, 0, SEEK_END);   i = ftell(fpWord);#ifdef PAL_UNICODE   //   // Each word has 10 or 16 bytes   //   g_TextLib.nWords = (i + (gpGlobals->dwWordLength - 1)) / gpGlobals->dwWordLength;#else   //   // Each word has 10 bytes   //   g_TextLib.nWords = (i + (WORD_LENGTH - 1)) / WORD_LENGTH;#endif   //   // Read the words   //#ifdef PAL_UNICODE   temp = (LPBYTE)malloc(i);   if (temp == NULL)#else   g_TextLib.lpWordBuf = (LPBYTE)malloc(i);   if (g_TextLib.lpWordBuf == NULL)#endif   {      fclose(fpWord);      fclose(fpMsg);      return -1;   }   fseek(fpWord, 0, SEEK_SET);#ifdef PAL_UNICODE   fread(temp, i, 1, fpWord);#else   fread(g_TextLib.lpWordBuf, i, 1, fpWord);#endif   //   // Close the words file   //   fclose(fpWord);#ifdef PAL_UNICODE   // Split the words and do code page conversion   for (i = 0, wlen = 0; i < g_TextLib.nWords; i++)   {	   int base = i * gpGlobals->dwWordLength;	   int pos = base + gpGlobals->dwWordLength - 1;	   while (pos >= base && temp[pos] == ' ') temp[pos--] = 0;	   wlen += PAL_MultiByteToWideChar(temp + base, gpGlobals->dwWordLength, NULL, 0) + 1;   }   g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));   tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));   for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)   {	   int l;	   g_TextLib.lpWordBuf[i] = tmp + wpos;	   l = PAL_MultiByteToWideChar(temp + i * gpGlobals->dwWordLength, gpGlobals->dwWordLength, g_TextLib.lpWordBuf[i], wlen - wpos);	   if (l > 0 && g_TextLib.lpWordBuf[i][l - 1] == '1')		   g_TextLib.lpWordBuf[i][l - 1] = 0;	   g_TextLib.lpWordBuf[i][l] = 0;	   wpos += l + 1;   }   free(temp);#endif   //   // Read the message offsets. The message offsets are in SSS.MKF #3   //   i = PAL_MKFGetChunkSize(3, gpGlobals->f.fpSSS) / sizeof(DWORD);   g_TextLib.nMsgs = i - 1;#ifdef PAL_UNICODE   offsets = (LPDWORD)malloc(i * sizeof(DWORD));   if (offsets == NULL)#else   g_TextLib.lpMsgOffset = (LPDWORD)malloc(i * sizeof(DWORD));   if (g_TextLib.lpMsgOffset == NULL)#endif   {      free(g_TextLib.lpWordBuf);      fclose(fpMsg);      return -1;   }#ifdef PAL_UNICODE   PAL_MKFReadChunk((LPBYTE)(offsets), i * sizeof(DWORD), 3, gpGlobals->f.fpSSS);#else   PAL_MKFReadChunk((LPBYTE)(g_TextLib.lpMsgOffset), i * sizeof(DWORD), 3,      gpGlobals->f.fpSSS);#endif   //   // Read the messages.   //   fseek(fpMsg, 0, SEEK_END);   i = ftell(fpMsg);#ifdef PAL_UNICODE   temp = (LPBYTE)malloc(i);   if (temp == NULL)#else   g_TextLib.lpMsgBuf = (LPBYTE)malloc(i);   if (g_TextLib.lpMsgBuf == NULL)#endif   {#ifdef PAL_UNICODE      free(offsets);	  free(g_TextLib.lpWordBuf[0]);#else      free(g_TextLib.lpMsgOffset);#endif      free(g_TextLib.lpWordBuf);      fclose(fpMsg);      return -1;   }   fseek(fpMsg, 0, SEEK_SET);#ifdef PAL_UNICODE   fread(temp, 1, i, fpMsg);#else   fread(g_TextLib.lpMsgBuf, 1, i, fpMsg);#endif   fclose(fpMsg);#ifdef PAL_UNICODE   // Split messages and do code page conversion here   for (i = 0, wlen = 0; i < g_TextLib.nMsgs; i++)   {	   wlen += PAL_MultiByteToWideChar(temp + SWAP32(offsets[i]), SWAP32(offsets[i + 1]) - SWAP32(offsets[i]), NULL, 0) + 1;   }   g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));   tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));   for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)   {	   int l;	   g_TextLib.lpMsgBuf[i] = tmp + wpos;	   l = PAL_MultiByteToWideChar(temp + SWAP32(offsets[i]), SWAP32(offsets[i + 1]) - SWAP32(offsets[i]), g_TextLib.lpMsgBuf[i], wlen - wpos);	   g_TextLib.lpMsgBuf[i][l] = 0;	   wpos += l + 1;   }   free(temp);   free(offsets);   g_rgszAdditionalWords = gc_rgszAdditionalWords[gpGlobals->iCodePage];#endif   g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;   g_TextLib.bIcon = 0;   g_TextLib.posIcon = 0;   g_TextLib.nCurrentDialogLine = 0;   g_TextLib.iDelayTime = 3;   g_TextLib.posDialogTitle = PAL_XY(12, 8);   g_TextLib.posDialogText = PAL_XY(44, 26);   g_TextLib.bDialogPosition = kDialogUpper;   g_TextLib.fUserSkip = FALSE;   PAL_MKFReadChunk(g_TextLib.bufDialogIcons, 282, 12, gpGlobals->f.fpDATA);   return 0;}VOIDPAL_FreeText(   VOID)/*++  Purpose:    Free the memory used by the texts.  Parameters:    None.  Return value:    None.--*/{   if (g_TextLib.lpMsgBuf != NULL)   {#  ifdef PAL_UNICODE      free(g_TextLib.lpMsgBuf[0]);#  endif      free(g_TextLib.lpMsgBuf);      g_TextLib.lpMsgBuf = NULL;   }#ifndef PAL_UNICODE   if (g_TextLib.lpMsgOffset != NULL)   {      free(g_TextLib.lpMsgOffset);      g_TextLib.lpMsgOffset = NULL;   }#endif   if (g_TextLib.lpWordBuf != NULL)   {#  ifdef PAL_UNICODE      free(g_TextLib.lpWordBuf[0]);#  endif      free(g_TextLib.lpWordBuf);      g_TextLib.lpWordBuf = NULL;   }}#ifdef PAL_UNICODELPCWSTR#elseLPCSTR#endifPAL_GetWord(   WORD       wNumWord)/*++  Purpose:    Get the specified word.  Parameters:    [IN]  wNumWord - the number of the requested word.  Return value:    Pointer to the requested word. NULL if not found.--*/{#ifndef PAL_UNICODE   static char buf[WORD_LENGTH + 1];#endif   if (wNumWord >= PAL_ADDITIONAL_WORD_FIRST)   {      return g_rgszAdditionalWords[wNumWord - PAL_ADDITIONAL_WORD_FIRST];   }   if (wNumWord >= g_TextLib.nWords)   {      return NULL;   }#ifdef PAL_UNICODE   return g_TextLib.lpWordBuf[wNumWord];#else   memcpy(buf, &g_TextLib.lpWordBuf[wNumWord * WORD_LENGTH], WORD_LENGTH);   buf[WORD_LENGTH] = '\0';   //   // Remove the trailing spaces   //   trim(buf);   if ((strlen(buf) & 1) != 0 && buf[strlen(buf) - 1] == '1')   {      buf[strlen(buf) - 1] = '\0';   }   return buf;#endif}#ifdef PAL_UNICODELPCWSTR#elseLPCSTR#endifPAL_GetMsg(   WORD       wNumMsg)/*++  Purpose:    Get the specified message.  Parameters:    [IN]  wNumMsg - the number of the requested message.  Return value:    Pointer to the requested message. NULL if not found.--*/{#ifdef PAL_UNICODE	return (wNumMsg >= g_TextLib.nMsgs) ? NULL : g_TextLib.lpMsgBuf[wNumMsg];#else   static char    buf[256];   DWORD          dwOffset, dwSize;   if (wNumMsg >= g_TextLib.nMsgs)   {      return NULL;   }   dwOffset = SWAP32(g_TextLib.lpMsgOffset[wNumMsg]);   dwSize = SWAP32(g_TextLib.lpMsgOffset[wNumMsg + 1]) - dwOffset;   assert(dwSize < 255);   memcpy(buf, &g_TextLib.lpMsgBuf[dwOffset], dwSize);   buf[dwSize] = '\0';   return buf;#endif}VOIDPAL_DrawText(#ifdef PAL_UNICODE   LPCWSTR    lpszText,#else   LPCSTR     lpszText,#endif   PAL_POS    pos,   BYTE       bColor,   BOOL       fShadow,   BOOL       fUpdate)/*++  Purpose:    Draw text on the screen.  Parameters:    [IN]  lpszText - the text to be drawn.    [IN]  pos - Position of the text.    [IN]  bColor - Color of the text.    [IN]  fShadow - TRUE if the text is shadowed or not.    [IN]  fUpdate - TRUE if update the screen area.  Return value:    None.--*/{   SDL_Rect   rect, urect;#ifndef PAL_UNICODE   WORD       wChar;#endif   rect.x = PAL_X(pos);   rect.y = PAL_Y(pos);   urect.x = rect.x;   urect.y = rect.y;#if defined(PAL_WIN95) || defined(PAL_UNICODE)   urect.h = 17;#else   urect.h = 16;#endif   urect.w = 0;   while (*lpszText)   {      //      // Draw the character      //#  ifdef PAL_UNICODE	  int char_width = PAL_CharWidth(*lpszText);      if (fShadow)      {		  PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0);		  PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0);      }	  PAL_DrawCharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x, rect.y), bColor);      lpszText++;	  rect.x += char_width;	  urect.w += char_width;#  else      if (*lpszText & 0x80)      {         //         // Chinese Character         //         wChar = SWAP16(((LPBYTE)lpszText)[0] | (((LPBYTE)lpszText)[1] << 8));         if (fShadow)         {            PAL_DrawCharOnSurface(wChar, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0);            PAL_DrawCharOnSurface(wChar, gpScreen, PAL_XY(rect.x + 1, rect.y), 0);         }         PAL_DrawCharOnSurface(wChar, gpScreen, PAL_XY(rect.x, rect.y), bColor);         lpszText += 2;         rect.x += 16;         urect.w += 16;      }      else      {         //         // ASCII character         //         if (fShadow)         {            PAL_DrawASCIICharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y + 1), 0);            PAL_DrawASCIICharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x + 1, rect.y), 0);         }         PAL_DrawASCIICharOnSurface(*lpszText, gpScreen, PAL_XY(rect.x, rect.y), bColor);         lpszText++;         rect.x += 8;         urect.w += 8;      }#  endif   }   //   // Update the screen area   //   if (fUpdate && urect.w > 0)   {#ifdef PAL_WIN95      urect.w++;      if (urect.x + urect.w > 320)      {         urect.w = 320 - urect.x;      }#endif      VIDEO_UpdateScreen(&urect);   }}VOIDPAL_DialogSetDelayTime(   INT          iDelayTime)/*++  Purpose:    Set the delay time for dialog.  Parameters:    [IN]  iDelayTime - the delay time to be set.  Return value:    None.--*/{   g_TextLib.iDelayTime = iDelayTime;}VOIDPAL_StartDialog(   BYTE         bDialogLocation,   BYTE         bFontColor,   INT          iNumCharFace,   BOOL         fPlayingRNG)/*++  Purpose:    Start a new dialog.  Parameters:    [IN]  bDialogLocation - the location of the text on the screen.    [IN]  bFontColor - the font color of the text.    [IN]  iNumCharFace - number of the character face in RGM.MKF.    [IN]  fPlayingRNG - whether we are playing a RNG video or not.  Return value:    None.--*/{   PAL_LARGE BYTE buf[16384];   SDL_Rect       rect;   if (gpGlobals->fInBattle && !g_fUpdatedInBattle)   {      //      // Update the screen in battle, or the graphics may seem messed up      //      VIDEO_UpdateScreen(NULL);      g_fUpdatedInBattle = TRUE;   }   g_TextLib.bIcon = 0;   g_TextLib.posIcon = 0;   g_TextLib.nCurrentDialogLine = 0;   g_TextLib.posDialogTitle = PAL_XY(12, 8);   g_TextLib.fUserSkip = FALSE;   if (bFontColor != 0)   {      g_TextLib.bCurrentFontColor = bFontColor;   }   if (fPlayingRNG && iNumCharFace)   {      VIDEO_BackupScreen();      g_TextLib.fPlayingRNG = TRUE;   }   switch (bDialogLocation)   {   case kDialogUpper:      if (iNumCharFace > 0)      {         //         // Display the character face at the upper part of the screen         //         if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)         {            rect.w = PAL_RLEGetWidth((LPCBITMAPRLE)buf);            rect.h = PAL_RLEGetHeight((LPCBITMAPRLE)buf);            rect.x = 48 - rect.w / 2;            rect.y = 55 - rect.h / 2;            if (rect.x < 0)            {               rect.x = 0;            }            if (rect.y < 0)            {               rect.y = 0;            }            PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));            if (rect.x < 0)            {               rect.x = 0;            }            if (rect.y < 0)            {               rect.y = 0;            }            VIDEO_UpdateScreen(&rect);         }      }      g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 80 : 12, 8);      g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 96 : 44, 26);      break;   case kDialogCenter:      g_TextLib.posDialogText = PAL_XY(80, 40);      break;   case kDialogLower:      if (iNumCharFace > 0)      {         //         // Display the character face at the lower part of the screen         //         if (PAL_MKFReadChunk(buf, 16384, iNumCharFace, gpGlobals->f.fpRGM) > 0)         {            rect.x = 270 - PAL_RLEGetWidth((LPCBITMAPRLE)buf) / 2;            rect.y = 144 - PAL_RLEGetHeight((LPCBITMAPRLE)buf) / 2;            PAL_RLEBlitToSurface((LPCBITMAPRLE)buf, gpScreen, PAL_XY(rect.x, rect.y));            VIDEO_UpdateScreen(NULL);         }      }      g_TextLib.posDialogTitle = PAL_XY(iNumCharFace > 0 ? 4 : 12, 108);      g_TextLib.posDialogText = PAL_XY(iNumCharFace > 0 ? 20 : 44, 126);      break;   case kDialogCenterWindow:      g_TextLib.posDialogText = PAL_XY(160, 40);      break;   }   g_TextLib.bDialogPosition = bDialogLocation;}static VOIDPAL_DialogWaitForKey(   VOID)/*++  Purpose:    Wait for player to press a key after showing a dialog.  Parameters:    None.  Return value:    None.--*/{   PAL_LARGE SDL_Color   palette[256];   SDL_Color   *pCurrentPalette, t;   int         i;   //   // get the current palette   //   pCurrentPalette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);   memcpy(palette, pCurrentPalette, sizeof(palette));   if (g_TextLib.bDialogPosition != kDialogCenterWindow &&      g_TextLib.bDialogPosition != kDialogCenter)   {      //      // show the icon      //      LPCBITMAPRLE p = PAL_SpriteGetFrame(g_TextLib.bufDialogIcons, g_TextLib.bIcon);      if (p != NULL)      {         SDL_Rect rect;         rect.x = PAL_X(g_TextLib.posIcon);         rect.y = PAL_Y(g_TextLib.posIcon);         rect.w = 16;         rect.h = 16;         PAL_RLEBlitToSurface(p, gpScreen, g_TextLib.posIcon);         VIDEO_UpdateScreen(&rect);      }   }   PAL_ClearKeyState();   while (TRUE)   {      UTIL_Delay(100);      if (g_TextLib.bDialogPosition != kDialogCenterWindow &&         g_TextLib.bDialogPosition != kDialogCenter)      {         //         // palette shift         //         t = palette[0xF9];         for (i = 0xF9; i < 0xFE; i++)         {            palette[i] = palette[i + 1];         }         palette[0xFE] = t;         VIDEO_SetPalette(palette);      }      if (g_InputState.dwKeyPress != 0)      {         break;      }   }   if (g_TextLib.bDialogPosition != kDialogCenterWindow &&      g_TextLib.bDialogPosition != kDialogCenter)   {      PAL_SetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);   }   PAL_ClearKeyState();   g_TextLib.fUserSkip = FALSE;}VOIDPAL_ShowDialogText(#ifdef PAL_UNICODE   LPCWSTR      lpszText#else   LPCSTR       lpszText#endif)/*++  Purpose:    Show one line of the dialog text.  Parameters:    [IN]  lpszText - the text to be shown.  Return value:    None.--*/{   SDL_Rect        rect;#ifdef PAL_UNICODE   int             x, y;#else   int             x, y, len = strlen(lpszText);#endif   PAL_ClearKeyState();   g_TextLib.bIcon = 0;   if (gpGlobals->fInBattle && !g_fUpdatedInBattle)   {      //      // Update the screen in battle, or the graphics may seem messed up      //      VIDEO_UpdateScreen(NULL);      g_fUpdatedInBattle = TRUE;   }   if (g_TextLib.nCurrentDialogLine > 3)   {      //      // The rest dialogs should be shown in the next page.      //      PAL_DialogWaitForKey();      g_TextLib.nCurrentDialogLine = 0;      VIDEO_RestoreScreen();      VIDEO_UpdateScreen(NULL);   }   x = PAL_X(g_TextLib.posDialogText);   y = PAL_Y(g_TextLib.posDialogText) + g_TextLib.nCurrentDialogLine * 18;   if (g_TextLib.bDialogPosition == kDialogCenterWindow)   {      //      // The text should be shown in a small window at the center of the screen      //#ifndef PAL_CLASSIC      if (gpGlobals->fInBattle && g_Battle.BattleResult == kBattleResultOnGoing)      {         PAL_BattleUIShowText(lpszText, 1400);      }      else#endif      {         PAL_POS    pos;         LPBOX      lpBox;#     ifdef PAL_UNICODE		 int        i, w = wcslen(lpszText), len = 0;		 for (i = 0; i < w; i++)            len += PAL_CharWidth(lpszText[i]) >> 3;#     endif         //         // Create the window box         //         pos = PAL_XY(PAL_X(g_TextLib.posDialogText) - len * 4, PAL_Y(g_TextLib.posDialogText));         lpBox = PAL_CreateSingleLineBox(pos, (len + 1) / 2, TRUE);         rect.x = PAL_X(pos);         rect.y = PAL_Y(pos);         rect.w = 320 - rect.x * 2 + 32;         rect.h = 64;         //         // Show the text on the screen         //         pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);         PAL_DrawText(lpszText, pos, 0, FALSE, FALSE);         VIDEO_UpdateScreen(&rect);         PAL_DialogWaitForKey();         //         // Delete the box         //         PAL_DeleteBox(lpBox);         VIDEO_UpdateScreen(&rect);         PAL_EndDialog();      }   }   else   {#  ifdef PAL_UNICODE      int len = wcslen(lpszText);#  endif      if (g_TextLib.nCurrentDialogLine == 0 &&         g_TextLib.bDialogPosition != kDialogCenter &&#  ifdef PAL_UNICODE		 (lpszText[len - 1] == 0xff1a ||#  else         (((BYTE)lpszText[len - 1] == 0x47 && (BYTE)lpszText[len - 2] == 0xA1) ||          ((BYTE)lpszText[len - 1] == 0xBA && (BYTE)lpszText[len - 2] == 0xA3) ||          ((BYTE)lpszText[len - 1] == 0xC3 && (BYTE)lpszText[len - 2] == 0xA1) ||#  endif		  lpszText[len - 1] == ':'))      {         //         // name of character         //         PAL_DrawText(lpszText, g_TextLib.posDialogTitle, FONT_COLOR_CYAN_ALT, TRUE, TRUE);      }      else      {         //         // normal texts         //#     ifdef PAL_UNICODE         WCHAR text[2];#     else         char text[3];#     endif         if (!g_TextLib.fPlayingRNG && g_TextLib.nCurrentDialogLine == 0)         {            //            // Save the screen before we show the first line of dialog            //            VIDEO_BackupScreen();         }         while (lpszText != NULL && *lpszText != '\0')         {            switch (*lpszText)            {            case '-':               //               // Set the font color to Cyan               //               if (g_TextLib.bCurrentFontColor == FONT_COLOR_CYAN)               {                  g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;               }               else               {                  g_TextLib.bCurrentFontColor = FONT_COLOR_CYAN;               }               lpszText++;               break;#ifndef PAL_WIN95            case '\'':               //               // Set the font color to Red               //               if (g_TextLib.bCurrentFontColor == FONT_COLOR_RED)               {                  g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;               }               else               {                  g_TextLib.bCurrentFontColor = FONT_COLOR_RED;               }               lpszText++;               break;#endif            case '\"':               //               // Set the font color to Yellow               //               if (g_TextLib.bCurrentFontColor == FONT_COLOR_YELLOW)               {                  g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;               }               else               {                  g_TextLib.bCurrentFontColor = FONT_COLOR_YELLOW;               }               lpszText++;               break;            case '$':               //               // Set the delay time of text-displaying               //#           ifdef PAL_UNICODE               g_TextLib.iDelayTime = wcstol(lpszText + 1, NULL, 10) * 10 / 7;#           else               g_TextLib.iDelayTime = atoi(lpszText + 1) * 10 / 7;#           endif               lpszText += 3;               break;            case '~':               //               // Delay for a period and quit               //#           ifdef PAL_UNICODE               UTIL_Delay(wcstol(lpszText + 1, NULL, 10) * 80 / 7);#           else               UTIL_Delay(atoi(lpszText + 1) * 80 / 7);#           endif			   g_TextLib.nCurrentDialogLine = 0;               g_TextLib.fUserSkip = FALSE;               return; // don't go further            case ')':               //               // Set the waiting icon               //               g_TextLib.bIcon = 1;               lpszText++;               break;            case '(':               //               // Set the waiting icon               //               g_TextLib.bIcon = 2;               lpszText++;               break;            case '\\':               lpszText++;            default:#           ifdef PAL_UNICODE               text[0] = *lpszText++;			   text[1] = 0;#           else               if (*lpszText & 0x80)               {                  text[0] = lpszText[0];                  text[1] = lpszText[1];                  text[2] = '\0';                  lpszText += 2;               }               else               {                  text[0] = *lpszText;                  text[1] = '\0';                  lpszText++;               }#           endif               PAL_DrawText(text, PAL_XY(x, y), g_TextLib.bCurrentFontColor, TRUE, TRUE);#           ifdef PAL_UNICODE			   x += PAL_CharWidth(text[0]);#           else               x += ((text[0] & 0x80) ? 16 : 8);#           endif               if (!g_TextLib.fUserSkip)               {                  PAL_ClearKeyState();                  UTIL_Delay(g_TextLib.iDelayTime * 8);                  if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))                  {                     //                     // User pressed a key to skip the dialog                     //                     g_TextLib.fUserSkip = TRUE;                  }               }            }         }         g_TextLib.posIcon = PAL_XY(x, y);         g_TextLib.nCurrentDialogLine++;      }   }}VOIDPAL_ClearDialog(   BOOL       fWaitForKey)/*++  Purpose:    Clear the state of the dialog.  Parameters:    [IN]  fWaitForKey - whether wait for any key or not.  Return value:    None.--*/{   if (g_TextLib.nCurrentDialogLine > 0 && fWaitForKey)   {      PAL_DialogWaitForKey();   }   g_TextLib.nCurrentDialogLine = 0;   if (g_TextLib.bDialogPosition == kDialogCenter)   {      g_TextLib.posDialogTitle = PAL_XY(12, 8);      g_TextLib.posDialogText = PAL_XY(44, 26);      g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;      g_TextLib.bDialogPosition = kDialogUpper;   }}VOIDPAL_EndDialog(   VOID)/*++  Purpose:    Ends a dialog.  Parameters:    None.  Return value:    None.--*/{   PAL_ClearDialog(TRUE);   //   // Set some default parameters, as there are some parts of script   // which doesn't have a "start dialog" instruction before showing the dialog.   //   g_TextLib.posDialogTitle = PAL_XY(12, 8);   g_TextLib.posDialogText = PAL_XY(44, 26);   g_TextLib.bCurrentFontColor = FONT_COLOR_DEFAULT;   g_TextLib.bDialogPosition = kDialogUpper;   g_TextLib.fUserSkip = FALSE;   g_TextLib.fPlayingRNG = FALSE;}BOOLPAL_IsInDialog(   VOID)/*++  Purpose:    Check if there are dialog texts on the screen.  Parameters:    None.  Return value:    TRUE if there are dialog texts on the screen, FALSE if not.--*/{   return (g_TextLib.nCurrentDialogLine != 0);}BOOLPAL_DialogIsPlayingRNG(   VOID)/*++  Purpose:    Check if the script used the RNG playing parameter when displaying texts.  Parameters:    None.  Return value:    TRUE if the script used the RNG playing parameter, FALSE if not.--*/{   return g_TextLib.fPlayingRNG;}#ifdef PAL_UNICODEINTPAL_MultiByteToWideChar(   LPCSTR        mbs,   int           mbslength,   LPWSTR        wcs,   int           wcslength)/*++  Purpose:    Convert multi-byte string into the corresponding unicode string.  Parameters:    [IN]  mbs - Pointer to the multi-byte string.	[IN]  mbslength - Length of the multi-byte string, or -1 for auto-detect.	[IN]  wcs - Pointer to the wide string buffer.	[IN]  wcslength - Length of the wide string buffer.  Return value:    The length of converted wide string. If mbslength is set to -1, the returned	value includes the terminal null-char; otherwise, the null-char is not included.	If wcslength is set to 0, wcs can be set to NULL and the return value is the	required length of the wide string buffer.--*/{	int i = 0, j = 0, state = 0, wlen = 0, null = 0;	if (mbslength == -1)	{		mbslength = strlen(mbs);		null = 1;	}	if (!wcs)	{		switch (gpGlobals->iCodePage)		{		case CP_SHIFTJIS:			for (i = 0; i < mbslength && mbs[i]; i++)			{				if (state == 0)				{					if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] >= 0xfd || ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf))						wlen++;					else						state = 1;				}				else				{					wlen++;					state = 0;				}			}			return wlen + null + state;		case CP_GBK:		case CP_BIG5:			for (i = 0; i < mbslength && mbs[i]; i++)			{				if (state == 0)				{					if ((BYTE)mbs[i] <= 0x80 || (BYTE)mbs[i] == 0xff)						wlen++;					else						state = 1;				}				else				{					wlen++;					state = 0;				}			}			return wlen + null + state;		default:			return -1;		}	}	else	{		WCHAR invalid_char;		switch (gpGlobals->iCodePage)		{		case CP_SHIFTJIS:			invalid_char = 0x30fb;			for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)			{				if (state == 0)				{					if ((BYTE)mbs[i] <= 0x80)						wcs[wlen++] = mbs[i];					else if ((BYTE)mbs[i] >= 0xa0 && (BYTE)mbs[i] <= 0xdf)						wcs[wlen++] = cptbl_jis_half[(BYTE)mbs[i] - 0xa0];					else if ((BYTE)mbs[i] == 0xfd)						wcs[wlen++] = 0xf8f1;					else if ((BYTE)mbs[i] == 0xfe)						wcs[wlen++] = 0xf8f2;					else if ((BYTE)mbs[i] == 0xff)						wcs[wlen++] = 0xf8f3;					else						state = 1;				}				else				{					if ((BYTE)mbs[i] < 0x40)						wcs[wlen++] = 0x30fb;					else if ((BYTE)mbs[i - 1] < 0xa0)						wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];					else						wcs[wlen++] = cptbl_jis[(BYTE)mbs[i - 1] - 0xc1][(BYTE)mbs[i] - 0x40];					state = 0;				}			}			break;		case CP_GBK:			invalid_char = 0x3f;			for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)			{				if (state == 0)				{					if ((BYTE)mbs[i] < 0x80)						wcs[wlen++] = mbs[i];					else if ((BYTE)mbs[i] == 0x80)						wcs[wlen++] = 0x20ac;					else if ((BYTE)mbs[i] == 0xff)						wcs[wlen++] = 0xf8f5;					else						state = 1;				}				else				{					if ((BYTE)mbs[i] < 0x40)						wcs[wlen++] = invalid_char;					else						wcs[wlen++] = cptbl_gbk[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];					state = 0;				}			}			break;		case CP_BIG5:			invalid_char = 0x3f;			for (i = 0; i < mbslength && wlen < wcslength && mbs[i]; i++)			{				if (state == 0)				{					if ((BYTE)mbs[i] <= 0x80)						wcs[wlen++] = mbs[i];					else if ((BYTE)mbs[i] == 0xff)						wcs[wlen++] = 0xf8f8;					else						state = 1;				}				else				{					if ((BYTE)mbs[i] < 0x40 || ((BYTE)mbs[i] >= 0x7f && (BYTE)mbs[i] <= 0xa0))						wcs[wlen++] = invalid_char;					else if (mbs[i] <= 0x7e)						wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x40];					else						wcs[wlen++] = cptbl_big5[(BYTE)mbs[i - 1] - 0x81][(BYTE)mbs[i] - 0x60];					state = 0;				}			}			break;		default:			return -1;		}		if (state == 1 && wlen < wcslength)		{			wcs[wlen++] = invalid_char;		}		if (null)		{			if (wlen < wcslength)				wcs[wlen++] = 0;			else				wcs[wlen - 1] = 0;		}		return wlen;	}}#endif
 |