1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
class CharDescriptor
{
public:
int x, y, width, height, xoffset, yoffset, xadvance;
char c;
};
CharDescriptor g_AllChars[256];
unsigned char g_uFontHeight = 0;
int g_iNbDescriptor = 0;
// -----------------------------------------------------------------
// Name : storeData_
// -----------------------------------------------------------------
void storeData_(char * sTitle, char * sKey, char * sValue)
{
int iVal;
if (strcmp(sTitle, "common") == 0 && strcmp(sKey, "lineHeight") == 0)
{
sscanf(sValue, "%d", &iVal);
g_uFontHeight = (unsigned char) iVal;
}
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "id") == 0)
{
sscanf(sValue, "%d", &iVal);
g_AllChars[g_iNbDescriptor++].c = (wint_t)iVal;
}
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "x") == 0)
sscanf(sValue, "%d", &(g_AllChars[g_iNbDescriptor-1].x));
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "y") == 0)
sscanf(sValue, "%d", &(g_AllChars[g_iNbDescriptor-1].y));
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "width") == 0)
sscanf(sValue, "%d", &(g_AllChars[g_iNbDescriptor-1].width));
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "height") == 0)
sscanf(sValue, "%d", &(g_AllChars[g_iNbDescriptor-1].height));
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "xoffset") == 0)
sscanf(sValue, "%d", &(g_AllChars[g_iNbDescriptor-1].xoffset));
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "yoffset") == 0)
sscanf(sValue, "%d", &(g_AllChars[g_iNbDescriptor-1].yoffset));
else if (strcmp(sTitle, "char") == 0 && strcmp(sKey, "xadvance") == 0)
sscanf(sValue, "%d", &(g_AllChars[g_iNbDescriptor-1].xadvance));
}
// -----------------------------------------------------------------
// Name : loadMyFont
// -----------------------------------------------------------------
short loadMyFont(char * sFontName, TextureEngine * pTexEngine)
{
char descFile[MAX_PATH];
sprintf(descFile, "%s.fnt", sFontName);
FILE * f = NULL;
errno_t err = fopen_s(&f, descFile, "r");
if (err != 0)
{
// TODO : handle error
return -1;
}
char sTitle[64];
char sKey[64];
char sValue[64];
bool bGuillemets;
int curchar = 0;
while (!feof(f))
{
wint_t c;
do {
c = fgetc(f);
} while (c == ' ' || c == '\t' || c == '\n' || c == '\r');
do {
sTitle[curchar++] = (char)c;
c = fgetc(f);
} while (c != ' ' && c != '\t' && c != '\n' && c != '\r' && !feof(f));
sTitle[curchar] = '\0';
curchar = 0;
if (feof(f))
break;
do {
c = fgetc(f);
} while (c == ' ' || c == '\t');
while (c != '\n' && c != '\r')
{
do {
sKey[curchar++] = (char)c;
c = fgetc(f);
} while (c != '=');
sKey[curchar] = '\0';
curchar = 0;
c = fgetc(f);
if (c == '\"')
{
bGuillemets = true;
c = fgetc(f);
}
else
bGuillemets = false;
while ((bGuillemets && c != '\"') || (!bGuillemets && c != ' ' && c != '\t' && c != '\n' && c != '\r'))
{
sValue[curchar++] = (char)c;
c = fgetc(f);
}
sValue[curchar] = '\0';
curchar = 0;
storeData_(sTitle, sKey, sValue);
if (bGuillemets)
c = fgetc(f);
while (c == ' ' || c == '\t')
c = fgetc(f);
}
}
fclose(f);
sprintf(descFile, "%s.png", sFontName);
// TODO : load png file "descFile"
return 1;
} |
Partager