Browse Source

Font init optimization

LouYihua 7 years ago
parent
commit
620c93e7a6
6 changed files with 189 additions and 191 deletions
  1. 1 10
      common.h
  2. 33 81
      font.c
  3. 76 19
      font.h
  4. 0 66
      global.h
  5. 3 15
      main.c
  6. 76 0
      palcommon.h

+ 1 - 10
common.h

@@ -38,6 +38,7 @@
 #include <limits.h>
 #include <stdarg.h>
 #include <assert.h>
+#include <stdint.h>
 
 #include "SDL.h"
 #include "SDL_endian.h"
@@ -210,14 +211,4 @@ typedef const WCHAR        *LPCWSTR;
 # define PAL_FATAL_OUTPUT(s)
 #endif
 
-typedef enum tagCODEPAGE {
-	CP_MIN = 0,
-	CP_BIG5 = 0,
-	CP_GBK = 1,
-	CP_SHIFTJIS = 2,
-	CP_JISX0208 = 3,
-	CP_MAX = CP_GBK + 1,
-	CP_UTF_8 = CP_MAX + 1
-} CODEPAGE;
-
 #endif

+ 33 - 81
font.c

@@ -32,7 +32,7 @@
 
 static int _font_height = 16;
 
-uint8_t reverseBits(uint8_t x) {
+static uint8_t reverseBits(uint8_t x) {
     uint8_t y = 0;
     for(int i = 0 ; i < 8; i++){
         y <<= 1;
@@ -41,24 +41,8 @@ uint8_t reverseBits(uint8_t x) {
     }
     return y;
 }
-INT
-PAL_InitEmbeddedFont(
-   VOID
-)
-/*++
-  Purpose:
-
-    None.
 
-  Parameters:
-
-    None.
-
-  Return value:
-
-    Always return 0.
-
---*/
+static void PAL_InitEmbeddedFont(void)
 {
 	FILE *fp;
 	char *char_buf;
@@ -70,7 +54,7 @@ PAL_InitEmbeddedFont(
 	//
 	if (NULL == (fp = UTIL_OpenFile("wor16.asc")))
 	{
-		return 0;
+		return;
 	}
 
 	//
@@ -85,7 +69,7 @@ PAL_InitEmbeddedFont(
 	if (NULL == (char_buf = (char *)malloc(nBytes)))
 	{
 		fclose(fp);
-		return 0;
+		return;
 	}
 	fseek(fp, 0, SEEK_SET);
 	fread(char_buf, 1, nBytes, fp);
@@ -102,7 +86,7 @@ PAL_InitEmbeddedFont(
 	if (NULL == (wchar_buf = (wchar_t *)malloc(nChars * sizeof(wchar_t))))
 	{
 		free(char_buf);
-		return 0;
+		return;
 	}
 	PAL_MultiByteToWideChar(char_buf, nBytes, wchar_buf, nChars);
 	free(char_buf);
@@ -137,8 +121,6 @@ PAL_InitEmbeddedFont(
 		unicode_font[i][15] = 0;
 	}
 	_font_height = 15;
-
-	return 0;
 }
 
 INT
@@ -244,55 +226,39 @@ PAL_LoadBdfFont(
    return 0;
 }
 
-VOID
-PAL_FreeFont(
-   VOID
+int
+PAL_InitFont(
+	const CONFIGURATION* cfg
 )
-/*++
-  Purpose:
-
-    None.
-
-  Parameters:
-
-    None.
+{
+	if (cfg->pszBdfFile)
+	{
+		PAL_LoadBdfFont(cfg->pszBdfFile);
+	}
 
-  Return value:
+	if (!cfg->fIsWIN95 && cfg->fUseEmbeddedFonts)
+	{
+		PAL_InitEmbeddedFont();
+	}
 
-    None.
+	return 0;
+}
 
---*/
+void
+PAL_FreeFont(
+	void
+)
 {
 }
 
-VOID
+void
 PAL_DrawCharOnSurface(
-   WORD                     wChar,
-   SDL_Surface             *lpSurface,
-   PAL_POS                  pos,
-   BYTE                     bColor,
-   BOOL                     fUse8x8Font
+	uint16_t                 wChar,
+	SDL_Surface             *lpSurface,
+	PAL_POS                  pos,
+	uint8_t                  bColor,
+	BOOL                     fUse8x8Font
 )
-/*++
-  Purpose:
-
-    Draw a Unicode character on a surface.
-
-  Parameters:
-
-    [IN]  wChar - the unicode character to be drawn.
-
-    [OUT] lpSurface - the destination surface.
-
-    [IN]  pos - the destination location of the surface.
-
-    [IN]  bColor - the color of the character.
-
-  Return value:
-
-    None.
-
---*/
 {
 	int       i, j;
 	int       x = PAL_X(pos), y = PAL_Y(pos);
@@ -370,24 +336,10 @@ PAL_DrawCharOnSurface(
 	}
 }
 
-INT
+int
 PAL_CharWidth(
-   WORD                     wChar
+	uint16_t                 wChar
 )
-/*++
-  Purpose:
-
-    Get the text width of a character.
-
-  Parameters:
-
-    [IN]  wChar - the unicode character for width calculation.
-
-  Return value:
-
-    The width of the character, 16 for full-width char and 8 for half-width char.
-
---*/
 {
 	if ((wChar >= unicode_lower_top && wChar < unicode_upper_base) || wChar >= unicode_upper_top)
 	{
@@ -405,9 +357,9 @@ PAL_CharWidth(
 	return font_width[wChar] >> 1;
 }
 
-INT
+int
 PAL_FontHeight(
-   VOID
+	void
 )
 {
 	return _font_height;

+ 76 - 19
font.h

@@ -26,41 +26,98 @@
 
 #include "common.h"
 #include "palcommon.h"
+#include "palcfg.h"
 
 PAL_C_LINKAGE_BEGIN
 
-INT
-PAL_InitEmbeddedFont(
-   VOID
-);
+/*++
+  Purpose:
+
+    Initialize the font subsystem.
+
+  Parameters:
+
+    [IN]  cfg - Pointer to the configuration object.
 
-INT
-PAL_LoadBdfFont(
-   LPCSTR      pszBdfFileName
+  Return value:
+
+    0 = success, -1 = failure.
+--*/
+int
+PAL_InitFont(
+	const CONFIGURATION* cfg
 );
 
-VOID
+void
 PAL_FreeFont(
-   VOID
+	void
 );
 
-VOID
+/*++
+  Purpose:
+
+    Draw a Unicode character on a surface.
+
+  Parameters:
+
+    [IN]  wChar - the unicode character to be drawn.
+
+    [OUT] lpSurface - the destination surface.
+
+    [IN]  pos - the destination location of the surface.
+
+    [IN]  bColor - the color of the character.
+
+  Return value:
+
+    None.
+
+--*/
+void
 PAL_DrawCharOnSurface(
-   WORD                     wChar,
-   SDL_Surface             *lpSurface,
-   PAL_POS                  pos,
-   BYTE                     bColor,
-   BOOL                     fUse8x8Font
+	uint16_t                 wChar,
+	SDL_Surface             *lpSurface,
+	PAL_POS                  pos,
+	uint8_t                  bColor,
+	BOOL                     fUse8x8Font
 );
 
-INT
+/*++
+  Purpose:
+
+    Get the text width of a character.
+
+  Parameters:
+
+    [IN]  wChar - the unicode character for width calculation.
+
+  Return value:
+
+    The width of the character in pixels, 16 for full-width char and 8 for half-width char.
+
+--*/
+int
 PAL_CharWidth(
-   WORD                     wChar
+	uint16_t                 wChar
 );
 
-INT
+/*++
+  Purpose:
+
+    Get the height of the currently used font.
+
+  Parameters:
+
+    None.
+
+  Return value:
+
+    The height of the font in pixels.
+
+--*/
+int
 PAL_FontHeight(
-   VOID
+	void
 );
 
 PAL_C_LINKAGE_END

+ 0 - 66
global.h

@@ -38,56 +38,6 @@
 // an event object, player triggered an event script by pressing Spacebar).
 //
 
-// maximum number of players in party
-#define     MAX_PLAYERS_IN_PARTY         3
-
-// total number of possible player roles
-#define     MAX_PLAYER_ROLES             6
-
-// totally number of playable player roles
-#define     MAX_PLAYABLE_PLAYER_ROLES    5
-
-// maximum entries of inventory
-#define     MAX_INVENTORY                256
-
-// maximum items in a store
-#define     MAX_STORE_ITEM               9
-
-// total number of magic attributes
-#define     NUM_MAGIC_ELEMENTAL          5
-
-// maximum number of enemies in a team
-#define     MAX_ENEMIES_IN_TEAM          5
-
-// maximum number of equipments for a player
-#define     MAX_PLAYER_EQUIPMENTS        6
-
-// maximum number of magics for a player
-#define     MAX_PLAYER_MAGICS            32
-
-// maximum number of scenes
-#define     MAX_SCENES                   300
-
-// maximum number of objects
-#define     MAX_OBJECTS                  600
-
-// maximum number of event objects (should be somewhat more than the original,
-// as there are some modified versions which has more)
-#define     MAX_EVENT_OBJECTS            5500
-
-// maximum number of effective poisons to players
-#define     MAX_POISONS                  16
-
-// maximum number of level
-#define     MAX_LEVELS                   99
-
-#define     OBJECT_ITEM_START            0x3D
-#define     OBJECT_ITEM_END              0x126
-#define     OBJECT_MAGIC_START           0x127
-#define     OBJECT_MAGIC_END             0x18D
-
-#define     MINIMAL_WORD_COUNT           (MAX_OBJECTS + 13)
-
 // status of characters
 typedef enum tagSTATUS
 {
@@ -543,22 +493,6 @@ typedef struct tagPOISONSTATUS
    WORD              wPoisonScript;   // script entry
 } POISONSTATUS, *LPPOISONSTATUS;
 
-typedef enum tagMUSICTYPE
-{
-	MUSIC_MIDI,
-	MUSIC_RIX,
-	MUSIC_MP3,
-	MUSIC_OGG,
-	MUSIC_SDLCD
-} MUSICTYPE, *LPMUSICTYPE;
-
-typedef enum tagOPLTYPE
-{
-	OPL_DOSBOX,
-	OPL_MAME,
-	OPL_DOSBOX_NEW,
-} OPLTYPE, *LPOPLTYPE;
-
 typedef struct tagGLOBALVARS
 {
    FILES            f;

+ 3 - 15
main.c

@@ -83,22 +83,10 @@ PAL_Init(
 
    VIDEO_SetWindowTitle("Loading...");
 
-   if (!gConfig.fIsWIN95 && gConfig.fUseEmbeddedFonts)
-   {
-      e = PAL_InitEmbeddedFont();
-      if (e != 0)
-      {
-         TerminateOnError("Could not load fonts: %d.\n", e);
-      }
-   }
-
-   if (gConfig.pszBdfFile != NULL)
+   e = PAL_InitFont(&gConfig);
+   if (e != 0)
    {
-	  e = PAL_LoadBdfFont(gConfig.pszBdfFile);
-      if (e != 0)
-      {
-         TerminateOnError("Could not load BDF fonts: %d.\n", e);
-      }
+      TerminateOnError("Could not load fonts: %d.\n", e);
    }
 
    e = PAL_InitUI();

+ 76 - 0
palcommon.h

@@ -37,6 +37,56 @@ typedef DWORD           PAL_POS;
 #define PAL_Y(xy)       (SHORT)(((xy) >> 16) & 0xFFFF)
 #define PAL_XY_OFFSET(xy, x, y)    (PAL_POS)(((((INT)(y) << 16) & 0xFFFF0000) + ((xy) & 0xFFFF0000)) | (((INT)(x) & 0xFFFF) + ((xy) & 0xFFFF)))
 
+// maximum number of players in party
+#define     MAX_PLAYERS_IN_PARTY         3
+
+// total number of possible player roles
+#define     MAX_PLAYER_ROLES             6
+
+// totally number of playable player roles
+#define     MAX_PLAYABLE_PLAYER_ROLES    5
+
+// maximum entries of inventory
+#define     MAX_INVENTORY                256
+
+// maximum items in a store
+#define     MAX_STORE_ITEM               9
+
+// total number of magic attributes
+#define     NUM_MAGIC_ELEMENTAL          5
+
+// maximum number of enemies in a team
+#define     MAX_ENEMIES_IN_TEAM          5
+
+// maximum number of equipments for a player
+#define     MAX_PLAYER_EQUIPMENTS        6
+
+// maximum number of magics for a player
+#define     MAX_PLAYER_MAGICS            32
+
+// maximum number of scenes
+#define     MAX_SCENES                   300
+
+// maximum number of objects
+#define     MAX_OBJECTS                  600
+
+// maximum number of event objects (should be somewhat more than the original,
+// as there are some modified versions which has more)
+#define     MAX_EVENT_OBJECTS            5500
+
+// maximum number of effective poisons to players
+#define     MAX_POISONS                  16
+
+// maximum number of level
+#define     MAX_LEVELS                   99
+
+#define     OBJECT_ITEM_START            0x3D
+#define     OBJECT_ITEM_END              0x126
+#define     OBJECT_MAGIC_START           0x127
+#define     OBJECT_MAGIC_END             0x18D
+
+#define     MINIMAL_WORD_COUNT           (MAX_OBJECTS + 13)
+
 typedef enum tagPALDIRECTION
 {
    kDirSouth = 0,
@@ -46,6 +96,32 @@ typedef enum tagPALDIRECTION
    kDirUnknown
 } PALDIRECTION, *LPPALDIRECTION;
 
+typedef enum tagMUSICTYPE
+{
+	MUSIC_MIDI,
+	MUSIC_RIX,
+	MUSIC_MP3,
+	MUSIC_OGG,
+	MUSIC_SDLCD
+} MUSICTYPE, *LPMUSICTYPE;
+
+typedef enum tagOPLTYPE
+{
+	OPL_DOSBOX,
+	OPL_MAME,
+	OPL_DOSBOX_NEW,
+} OPLTYPE, *LPOPLTYPE;
+
+typedef enum tagCODEPAGE {
+	CP_MIN = 0,
+	CP_BIG5 = 0,
+	CP_GBK = 1,
+	CP_SHIFTJIS = 2,
+	CP_JISX0208 = 3,
+	CP_MAX = CP_GBK + 1,
+	CP_UTF_8 = CP_MAX + 1
+} CODEPAGE;
+
 PAL_C_LINKAGE_BEGIN
 
 INT