daku
Частый гость
Дата регистрации : Oct 2011
Проживает : (void)
Сообщений : 93
Рейтинг
[кратко]
Пользователи:
-9.0 (1 )
Нарушения:
0.0 (? )
Сообщения:
0.0 (Метки )
Регистрация:
8.2
bitmap fonts ?
Hi i have trouble with bitmap fonts. i have fonts in .fnt format and CFont loader class.
Font file is loaded and with it i have no errors but if i want to write something with that font then on
C++ Source Code:
4 glBindTexture(GL_TEXTURE_2D, pages[page]); //TGA
cpp file
C++ Source Code:
9 //#include "bsprenderer.h"
10 #include "tex_header.h"
13 typedef unsigned int UINT;
16 // Implement private helper classes for loading the bitmap font files
21 CFontLoader(FILE *f, CFont *font, const char *fontFile);
23 virtual int Load() = 0; // Must be implemented by derived class
26 void LoadPage(int id, const char *pageFile, const char *fontFile);
27 void SetFontInfo(int outlineThickness);
28 void SetCommonInfo(int fontHeight, int base, int scaleW, int scaleH, int pages, bool isPacked);
29 void AddChar(int id, int x, int y, int w, int h, int xoffset, int yoffset, int xadvance, int page, int chnl);
30 void AddKerningPair(int first, int second, int amount);
39 class CFontLoaderTextFormat : public CFontLoader
42 CFontLoaderTextFormat(FILE *f, CFont *font, const char *fontFile);
46 int SkipWhiteSpace(std::string &str, int start);
47 int FindEndOfToken(std::string &str, int start);
49 void InterpretInfo(std::string &str, int start);
50 void InterpretCommon(std::string &str, int start);
51 void InterpretChar(std::string &str, int start);
52 void InterpretSpacing(std::string &str, int start);
53 void InterpretKerning(std::string &str, int start);
54 void InterpretPage(std::string &str, int start, const char *fontFile);
58 int CFont::Init(const char *fontFile)
61 char fontFilewithmod[64];
62 sprintf(fontFilewithmod,"cszd/%s" ,fontFile);
65 FILE *f = fopen(fontFilewithmod, "rb" );
66 if (f==NULL) return -1; //фUйл н: оHqлDЩ!!!
68 // Determine format by reading the first bytes of the file
71 fseek(f, 0, SEEK_SET);
73 CFontLoader *loader = 0;
75 loader = new CFontLoaderTextFormat(f, this , fontFile);
78 int r = loader->Load();
98 std::map<int , SCharDescr*>::iterator it = chars.begin();
99 while ( it != chars.end() )
108 SCharDescr *CFont::GetChar(int id)
110 std::map<int , SCharDescr*>::iterator it = chars.find(id);
111 if ( it == chars.end() ) return 0;
117 float CFont::AdjustForKerningPairs(int first, int second)
119 SCharDescr *ch = GetChar(first);
120 if ( ch == 0 ) return 0;
121 for ( UINT n = 0; n < ch->kerningPairs.size(); n += 2 )
123 if ( ch->kerningPairs[n] == second )
124 return ch->kerningPairs[n+1] * scale;
130 float CFont::GetTextWidth(const char *text, int count)
133 count = GetTextLength(text);
137 for ( int n = 0; n < count; )
139 int charId = GetTextChar(text,n,&n);
141 SCharDescr *ch = GetChar(charId);
142 if ( ch == 0 ) ch = &defChar;
144 x += scale * (ch->xAdv);
147 x += AdjustForKerningPairs(charId, GetTextChar(text,n));
154 // Returns the number of bytes in the string until the null char
155 int CFont::GetTextLength(const char *text)
157 return (int )strlen(text);
161 int CFont::GetTextChar(const char *text, int pos, int *nextPos)
167 ch = (unsigned char )text[pos];
169 if ( nextPos ) *nextPos = pos + len;
173 void CFont::InternalWrite(float x, float y, float z, const char *text,int _r, int _g,int _b,int _a, int count, float spacing,float newscale)
178 glEnable(GL_TEXTURE_2D);
180 // y -= (newscale * fontHeight)/2;
182 for ( int n = 0; n < count; )
184 int charId = GetTextChar(text, n, &n);
185 SCharDescr *ch = GetChar(charId);
186 if ( ch == 0 ) ch = &defChar;
190 // Map the center of the texel to the corners
191 // in order to get pixel perfect mapping
192 float u = (float (ch->srcX)+0.5f) / scaleW;
193 float v = (float (ch->srcY)+0.5f) / scaleH;
194 float u2 = u + float (ch->srcW) / scaleW;
195 float v2 = v + float (ch->srcH) / scaleH;
197 float a = newscale * float (ch->xAdv);
198 float w = newscale * float (ch->srcW);
199 float h = newscale * float (ch->srcH);
200 float ox = newscale * float (ch->xOff);
201 float oy = newscale * float (ch->yOff);
203 //C:нU DHUн
цq :Dл
Y
фH нU дуой H:DHу:
204 glEnable(GL_TEXTURE_2D);
206 if ( ch->page != page )
209 // glBindTexture(GL_TEXTURE_2D, pages[page]); //TGA
211 gEngfuncs.Con_Printf( "%d\n" ,&defChar );
214 glColor4ub(_r,_g,_b,_a);
216 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); //
н
Dу: Uльфу в HU
221 glVertex3f(x+ox, y+oy, z);
224 glVertex3f(x+w+ox, y+oy, z);
227 glVertex3f(x+w+ox, y+h+oy, z);
230 glVertex3f(x+ox, y+h+oy, z);
240 x += AdjustForKerningPairs(charId, GetTextChar(text,n));
245 int CFont::Write(float x, float y, float z, const char *text,int r, int g,int b,int a, int count, unsigned int mode,float newscale,bool shadow)
248 count = GetTextLength(text);
250 if ( mode == FONT_ALIGN_CENTER )
252 float w = GetTextWidth(text, count);
253 x -= w/2*newscale; // mcc fix auto scale
255 else if ( mode == FONT_ALIGN_RIGHT )
257 float w = GetTextWidth(text, count);
258 x -= w*newscale;// mcc fix auto scale
261 if (shadow) InternalWrite(x+1, y+1, z, text,0,0,0,255, count,0,newscale);
262 InternalWrite(x, y, z, text,r,g,b,a, count,0,newscale);
264 return GetTextWidth(text, count);
268 //=============================================================================
271 // This is the base class for all loader classes. This is the only class
272 // that has access to and knows how to set the CFont members.
273 //=============================================================================
275 CFontLoader::CFontLoader(FILE *f, CFont *font, const char *fontFile)
279 this ->fontFile = fontFile;
281 outlineThickness = 0;
284 void CFontLoader::LoadPage(int id, const char *pageFile, const char *fontFile)
289 // Load the texture from the same directory as the font descriptor file
291 // Find the directory
293 for ( size_t n = 0; (n = str.find('/', n)) != string::npos; ) str.replace(n, 1, "\\" );
294 size_t i = str.rfind('\\');
295 if ( i != string::npos )
296 str = str.substr(0, i+1);
300 // Load the font textures
303 //font->pages[id] = gEngfuncs.pfnSPR_Load(str.c_str()); //SPR
304 font->pages[id] = glLoadTgaFromPak((char *)str.c_str());// TGA (???)
307 void CFontLoader::SetFontInfo(int outlineThickness)
309 this ->outlineThickness = outlineThickness;
312 void CFontLoader::SetCommonInfo(int fontHeight, int base, int scaleW, int scaleH, int pages, bool isPacked)
314 font->fontHeight = fontHeight;
316 font->scaleW = scaleW;
317 font->scaleH = scaleH;
318 // вqд:лЩ: Uз: од DHUн
цq Y
фHU
319 font->pages.resize(pages);
320 for ( int n = 0; n < pages; n++ )
324 if ( isPacked && outlineThickness )
325 font->hasOutline = true ;
328 void CFontLoader::AddChar(int id, int x, int y, int w, int h, int xoffset, int yoffset, int xadvance, int page, int chnl)
330 // Convert to a 4 element vector
331 // TODO: Does this depend on hardware? It probably does
332 if ( chnl == 1 ) chnl = 0x00010000; // Blue channel
333 else if ( chnl == 2 ) chnl = 0x00000100; // Green channel
334 else if ( chnl == 4 ) chnl = 0x00000001; // Red channel
335 else if ( chnl == 8 ) chnl = 0x01000000; // Alpha channel
340 SCharDescr *ch = new SCharDescr;
351 font->chars.insert(std::map<int , SCharDescr*>::value_type(id, ch));
356 font->defChar.srcX = x;
357 font->defChar.srcY = y;
358 font->defChar.srcW = w;
359 font->defChar.srcH = h;
360 font->defChar.xOff = xoffset;
361 font->defChar.yOff = yoffset;
362 font->defChar.xAdv = xadvance;
363 font->defChar.page = page;
364 font->defChar.chnl = chnl;
368 void CFontLoader::AddKerningPair(int first, int second, int amount)
370 if ( first >= 0 && first < 256 && font->chars[first] )
372 font->chars[first]->kerningPairs.push_back(second);
373 font->chars[first]->kerningPairs.push_back(amount);
377 //=============================================================================
378 // CFontLoaderTextFormat
380 // This class implements the logic for loading a BMFont file in text format
381 //=============================================================================
383 CFontLoaderTextFormat::CFontLoaderTextFormat(FILE *f, CFont *font, const char *fontFile) : CFontLoader(f, font, fontFile)
387 int CFontLoaderTextFormat::Load()
393 // Read until line feed (or EOF)
399 if ( fread(&ch, 1, 1, f) )
409 int pos = SkipWhiteSpace(line, 0);
412 int pos2 = FindEndOfToken(line, pos);
413 string token = line.substr(pos, pos2-pos);
416 if ( token == "info" )
417 InterpretInfo(line, pos2);
418 else if ( token == "common" )
419 InterpretCommon(line, pos2);
420 else if ( token == "char " )
421 InterpretChar(line, pos2);
422 else if ( token == "kerning" )
423 InterpretKerning(line, pos2);
424 else if ( token == "page" )
425 InterpretPage(line, pos2, fontFile);
434 int CFontLoaderTextFormat::SkipWhiteSpace(string &str, int start)
437 while ( n < str.size() )
452 int CFontLoaderTextFormat::FindEndOfToken(string &str, int start)
458 while ( n < str.size() )
463 // Include the last quote char in the token
472 while ( n < str.size() )
489 void CFontLoaderTextFormat::InterpretKerning(string &str, int start)
491 // Read the attributes
496 int pos, pos2 = start;
499 pos = SkipWhiteSpace(str, pos2);
500 pos2 = FindEndOfToken(str, pos);
502 string token = str.substr(pos, pos2-pos);
504 pos = SkipWhiteSpace(str, pos2);
505 if ( pos == str.size() || str[pos] != '=' ) break ;
507 pos = SkipWhiteSpace(str, pos+1);
508 pos2 = FindEndOfToken(str, pos);
510 string value = str.substr(pos, pos2-pos);
512 if ( token == "first" )
513 first = strtol(value.c_str(), 0, 10);
514 else if ( token == "second" )
515 second = strtol(value.c_str(), 0, 10);
516 else if ( token == "amount" )
517 amount = strtol(value.c_str(), 0, 10);
519 if ( pos == str.size() ) break ;
522 // Store the attributes
523 AddKerningPair(first, second, amount);
526 void CFontLoaderTextFormat::InterpretChar(string &str, int start)
528 // Read all attributes
540 int pos, pos2 = start;
543 pos = SkipWhiteSpace(str, pos2);
544 pos2 = FindEndOfToken(str, pos);
546 string token = str.substr(pos, pos2-pos);
548 pos = SkipWhiteSpace(str, pos2);
549 if ( pos == str.size() || str[pos] != '=' ) break ;
551 pos = SkipWhiteSpace(str, pos+1);
552 pos2 = FindEndOfToken(str, pos);
554 string value = str.substr(pos, pos2-pos);
557 id = strtol(value.c_str(), 0, 10);
558 else if ( token == "x" )
559 x = strtol(value.c_str(), 0, 10);
560 else if ( token == "y" )
561 y = strtol(value.c_str(), 0, 10);
562 else if ( token == "width" )
563 width = strtol(value.c_str(), 0, 10);
564 else if ( token == "height" )
565 height = strtol(value.c_str(), 0, 10);
566 else if ( token == "xoffset" )
567 xoffset = strtol(value.c_str(), 0, 10);
568 else if ( token == "yoffset" )
569 yoffset = strtol(value.c_str(), 0, 10);
570 else if ( token == "xadvance" )
571 xadvance = strtol(value.c_str(), 0, 10);
572 else if ( token == "page" )
573 page = strtol(value.c_str(), 0, 10);
574 else if ( token == "chnl" )
575 chnl = strtol(value.c_str(), 0, 10);
577 if ( pos == str.size() ) break ;
580 // Store the attributes
581 AddChar(id, x, y, width, height, xoffset, yoffset, xadvance, page, chnl);
584 void CFontLoaderTextFormat::InterpretCommon(string &str, int start)
593 // Read all attributes
594 int pos, pos2 = start;
597 pos = SkipWhiteSpace(str, pos2);
598 pos2 = FindEndOfToken(str, pos);
600 string token = str.substr(pos, pos2-pos);
602 pos = SkipWhiteSpace(str, pos2);
603 if ( pos == str.size() || str[pos] != '=' ) break ;
605 pos = SkipWhiteSpace(str, pos+1);
606 pos2 = FindEndOfToken(str, pos);
608 string value = str.substr(pos, pos2-pos);
610 if ( token == "lineHeight" )
611 fontHeight = (short )strtol(value.c_str(), 0, 10);
612 else if ( token == "base" )
613 base = (short )strtol(value.c_str(), 0, 10);
614 else if ( token == "scaleW" )
615 scaleW = (short )strtol(value.c_str(), 0, 10);
616 else if ( token == "scaleH" )
617 scaleH = (short )strtol(value.c_str(), 0, 10);
618 else if ( token == "pages" )
619 pages = strtol(value.c_str(), 0, 10);
620 else if ( token == "packed" )
621 packed = strtol(value.c_str(), 0, 10);
623 if ( pos == str.size() ) break ;
626 SetCommonInfo(fontHeight, base, scaleW, scaleH, pages, packed ? true : false );
629 void CFontLoaderTextFormat::InterpretInfo(string &str, int start)
631 int outlineThickness;
633 // Read all attributes
634 int pos, pos2 = start;
637 pos = SkipWhiteSpace(str, pos2);
638 pos2 = FindEndOfToken(str, pos);
640 string token = str.substr(pos, pos2-pos);
642 pos = SkipWhiteSpace(str, pos2);
643 if ( pos == str.size() || str[pos] != '=' ) break ;
645 pos = SkipWhiteSpace(str, pos+1);
646 pos2 = FindEndOfToken(str, pos);
648 string value = str.substr(pos, pos2-pos);
650 if ( token == "outline" )
651 outlineThickness = (short )strtol(value.c_str(), 0, 10);
653 if ( pos == str.size() ) break ;
656 SetFontInfo(outlineThickness);
659 void CFontLoaderTextFormat::InterpretPage(string &str, int start, const char *fontFile)
664 // Read all attributes
665 int pos, pos2 = start;
668 pos = SkipWhiteSpace(str, pos2);
669 pos2 = FindEndOfToken(str, pos);
671 string token = str.substr(pos, pos2-pos);
673 pos = SkipWhiteSpace(str, pos2);
674 if ( pos == str.size() || str[pos] != '=' ) break ;
676 pos = SkipWhiteSpace(str, pos+1);
677 pos2 = FindEndOfToken(str, pos);
679 string value = str.substr(pos, pos2-pos);
682 id = strtol(value.c_str(), 0, 10);
683 else if ( token == "file" )
684 file = value.substr(1, value.length()-2);
686 if ( pos == str.size() ) break ;
689 LoadPage(id, file.c_str(), fontFile);
692 //=============================================================================
693 // CFontLoaderBinaryFormat
695 // This class implements the logic for loading a BMFont file in binary format
696 //=============================================================================
698 CFontLoaderBinaryFormat::CFontLoaderBinaryFormat(FILE *f, CFont *font, const char *fontFile) : CFontLoader(f, font, fontFile)
702 int CFontLoaderBinaryFormat::Load()
704 // Read and validate the tag. It should be 66, 77, 70, 2,
705 // or 'BMF' and 2 where the number is the file version.
707 fread(magicString, 4, 1, f);
708 if( strncmp(magicString, "BMF\003", 4) != 0 )
710 LOG(("Unrecognized format for '%s'", fontFile));
718 while( fread(&blockType, 1, 1, f) )
720 // Read the blockSize
721 fread(&blockSize, 4, 1, f);
726 ReadInfoBlock(blockSize);
729 ReadCommonBlock(blockSize);
732 ReadPagesBlock(blockSize);
735 ReadCharsBlock(blockSize);
737 case 5: // kerning pairs
738 ReadKerningPairsBlock(blockSize);
741 LOG(("Unexpected block type (%d)", blockType));
753 void CFontLoaderBinaryFormat::ReadInfoBlock(int size)
774 BYTE outline; // Added with version 2
779 char *buffer = new char[size];
780 fread(buffer, size, 1, f);
782 // We're only interested in the outline thickness
783 infoBlock *blk = (infoBlock*)buffer;
784 SetFontInfo(blk->outline);
789 void CFontLoaderBinaryFormat::ReadCommonBlock(int size)
809 char *buffer = new char[size];
810 fread(buffer, size, 1, f);
812 commonBlock *blk = (commonBlock*)buffer;
814 SetCommonInfo(blk->lineHeight, blk->base, blk->scaleW, blk->scaleH, blk->pages, blk->packed ? true : false);
819 void CFontLoaderBinaryFormat::ReadPagesBlock(int size)
829 char *buffer = new char[size];
830 fread(buffer, size, 1, f);
832 pagesBlock *blk = (pagesBlock*)buffer;
834 for( int id = 0, pos = 0; pos < size; id++ )
836 LoadPage(id, &blk->pageNames[pos], fontFile);
837 pos += 1 + (int)strlen(&blk->pageNames[pos]);
843 void CFontLoaderBinaryFormat::ReadCharsBlock(int size)
865 char *buffer = new char[size];
866 fread(buffer, size, 1, f);
868 charsBlock *blk = (charsBlock*)buffer;
870 for( int n = 0; int(n*sizeof(charsBlock::charInfo)) < size; n++ )
872 AddChar(blk->chars[n].id,
876 blk->chars[n].height,
877 blk->chars[n].xoffset,
878 blk->chars[n].yoffset,
879 blk->chars[n].xadvance,
887 void CFontLoaderBinaryFormat::ReadKerningPairsBlock(int size)
891 struct kerningPairsBlock
902 char *buffer = new char[size];
903 fread(buffer, size, 1, f);
905 kerningPairsBlock *blk = (kerningPairsBlock*)buffer;
907 for( int n = 0; int(n*sizeof(kerningPairsBlock::kerningPair)) < size; n++ )
909 AddKerningPair(blk->kerningPairs[n].first,
910 blk->kerningPairs[n].second,
911 blk->kerningPairs[n].amount);
header file
C++ Source Code:
1 #pragma warning(disable:4786)
14 typedef int HSPRITE; // handle to a graphic
16 const int FONT_ALIGN_LEFT = 0;
17 const int FONT_ALIGN_CENTER = 1;
18 const int FONT_ALIGN_RIGHT = 2;
19 const int FONT_ALIGN_JUSTIFY = 3;
21 // олнUЩ DHуHуU був, уз
Hь буд:
з nft фUйлU
24 SCharDescr() : srcX(0), srcY(0), srcW(0), srcH(0), xOff(0), yOff(0), xAdv(0), page(0) {}
36 std::vector<int > kerningPairs; //D:ц
нH:вUлq :ду бувU
52 int Init(const char *fontFile);
54 //void SetTextEncoding(EFontTextEncoding encoding);
56 float GetTextWidth(const char *text, int count);
57 int Write(float x, float y, float z, const char *text, int r, int g,int b,int a,int count, unsigned int mode,float newscale, bool shadow);
58 int Draw(float x, float y, float z, const char *text, int r, int g,int b,int a,int count, unsigned int mode, float newscale=1.0f, bool shadow=false )
60 return Write(x,y,z,text,r,g,b,a,count,mode,newscale,shadow);
62 //void WriteML(float x, float y, float z, const char *text, int count, unsigned int mode);
63 //void WriteBox(float x, float y, float z, float width, const char *text, int count, unsigned mode);
65 //void SetHeight(float h);
68 //float GetBottomOffset();
69 //float GetTopOffset();
71 //void PrepareEffect();
72 //void PreparePixelPerfectOutput();
74 friend class CFontLoader; //ф:нд лUDD озовлЩ:H оH:H:д обUoUHьDЩ U Uбл
76 void InternalWrite(float x, float y, float z, const char *text, int _r, int _g,int _b,int _a,int count, float spacing = 0,float newscale=1.0f);
78 float AdjustForKerningPairs(int first, int second);
79 SCharDescr *GetChar(int id);
81 int GetTextLength(const char *text);
82 int GetTextChar(const char *text, int pos, int *nextPos = 0);
83 //int FindTextChar(const char *text, int start, int length, int ch);
85 // --- обo
: нUDHой
fnt
86 short fontHeight; // total height of the font
87 short base; // y of base line
90 SCharDescr defChar; //д:фUлHнqй D
вол
id бувq -1 , нUф
он ну:н?
93 EFontTextEncoding encoding;
97 std::map<int , SCharDescr*> chars; //C++. Уо 10. зучU: map (UDDоц
UH
внqй UDD
â<img src="images/smilies/wink.gif" border="0" alt="">.
98 //std::vector<HSPRITE> pages; //SPR
99 std::vector<int > pages;
100 //std::string fxFile;
106 //GLuint glLoadTgaFromPak( char* name, int index = 0 );
maybe someone can help me
Отредактировано daku 15-05-2013 в 11:49
Сообщить модератору | | IP: Записан
Сообщение: 119905