青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

月下的博客

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  34 Posts :: 0 Stories :: 59 Comments :: 0 Trackbacks

常用鏈接

留言簿(5)

我參與的團隊

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

  上個月開始和同學一塊在做類似CS的射擊游戲demo,要自己實現游戲里的簡單2DOverlay和文本顯示(不用具體GUI了,菜單什么的再用CEGUI),由于我這人做事慢,所以就去實現字體這種小模塊~0~(學末總是很難靜下心編程唉~~),本來以為網上這類資源挺多的,搜到的無非是NeHe的openGL+ft2,還有一位仁兄模仿Ogre寫的一個代碼(里面有些問題),多不是很系統,自己認真花了3,4天模仿OgreFont實現了一個簡單的英文字體在dx 9下使用ID3DXSprite接口進行渲染,然后照著clayman和hyzboy的提示修改成了支持中文的動態調頻寫入紋理那樣(不過沒做測試哦~~下下周得去上海2K面試,我就將就用了~)問題應該還有很多,而且應該做成考慮時間那樣(LRU),以后再說吧。。。
整個流程:

  最后把代碼貼下面吧,希望對大家有幫助。

/************************************************************************\
  This is a fucking
  ______   ___   _      _       
  |  ___| / _ \ | |    | |      
  | |_   / /_\ \| |__  | |  ___ 
  |  _|  |  _  || '_ \ | | / _ \
  | |    | | | || |_) || ||  __/
  \_|    \_| |_/|_.__/ |_| \___| 's free file

  filename: Font.h 
  created:  2010/07/30
  creator:    承天一
   
  purpose:  Freetype字體類
************************************************************************
*/

#ifndef __FONT_H__
#define __FONT_H__

#include 
<map>
#include 
<ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

using namespace std;

namespace Fable
{
    
class Font : public NedAlloc
    {
    
public:
        typedef unsigned 
int CodePoint;
        typedef FloatRect UVRect;
        
/// Information about the position and size of a glyph in a texture
        struct GlyphInfo
        {
        
public:
            CodePoint codePoint;        
//字符的unicode碼
            UVRect uvRect;                //紋理區域
            float aspectRatio;
            USHORT    useCount;            
//字符的使用次數
            UINT    l;
            UINT    m;

        
public:
            GlyphInfo(CodePoint _code, 
const UVRect& _rect, float _aspect, UINT _l, UINT _m) 
                : codePoint(_code), uvRect(_rect), aspectRatio(_aspect), useCount(
0), l(_l), m(_m)
            {
            }
        };
        
/// A range of code points, inclusive on both ends
        typedef std::pair<CodePoint, CodePoint> CodePointRange;
        typedef vector
<CodePointRange> CodePointRangeList;
    
protected:
        
/// Map from unicode code point to texture coordinates
        typedef map<CodePoint, GlyphInfo> CodePointMap;
        CodePointMap mCodePointMap;

        
/// Range of code points to generate glyphs for (truetype only)
        CodePointRangeList mCodePointRangeList;
    
public:

        Font();

        
~Font();

        
void        load(const std::string& name);

        
void        unLoad();

        
void        addCodePointRange(const CodePointRange& range)
        {
            mCodePointRangeList.push_back(range);
        }

        
/** Clear the list of code point ranges.
        
*/
        
void        clearCodePointRanges()
        {
            mCodePointRangeList.clear();
        }
        
/** Get a const reference to the list of code point ranges to be used to
            generate glyphs from a truetype font.
        
*/
        
const CodePointRangeList& getCodePointRangeList() const
        {
            
return mCodePointRangeList;
        }

    
protected:
        
/// Size of the truetype font, in points
        float            mTtfSize;
        
/// Resolution (dpi) of truetype font
        unsigned int    mTtfResolution;
        
/// Max distance to baseline of this (truetype) font
        int                mTtfMaxBearingY;
        
/// for TRUE_TYPE font only
        bool            mAntialiasColour;

        IDirect3DTexture9
*    mTexture;

        UINT            mWidth;
        UINT            mHeight;
        std::
string        mFontName;

        
/// 紋理上使用區域還剩的個數
        UINT            mLeftBlankNum;
        UINT            mMaxCharNum;
        uchar
*            mImageData;
        FT_Library        mFtLibrary;
        FT_Face            mFtFace;

        UINT            mPixelBytes;
        UINT            mCharDataWidth;
        UINT            mMaxCharSize;
        UINT            mDataSize;

        
int                mMaxHeight;
        
int                mMaxWidth;
        
float            mTextureAspect;
        UINT            mCharSpacer;

        UINT            mImage_m;
        UINT            mImage_l;

    
public:

        UINT    getTextureWidth() 
const { return mWidth;}

        UINT    getTextureHeight() 
const { return mHeight;}


         inline 
const UVRect& getGlyphTexCoords(CodePoint id) const
        {
            CodePointMap::const_iterator i 
= mCodePointMap.find(id);
            
if (i != mCodePointMap.end())
            {
                
return i->second.uvRect;
            }
            
else
            {
                
static UVRect nullRect(0.00.00.00.0);
                
return nullRect;
            }
        }

        
/** Sets the texture coordinates of a glyph.
        @remarks
            You only need to call this if you're setting up a font loaded from a texture manually.
        @note
            Also sets the aspect ratio (width / height) of this character. textureAspect
            is the width/height of the texture (may be non-square)
        
*/
        inline 
void setGlyphTexCoords(CodePoint id, UINT u1Pixel, UINT v1Pixel, UINT u2Pixel, UINT v2Pixel, float textureAspect)
        {
            
float u1 = (float)u1Pixel / (float)mWidth, v1 = (float)v1Pixel / (float)mHeight, u2 = (float)u2Pixel / (float)mWidth, v2 = (float)v2Pixel / (float)mWidth;
            CodePointMap::iterator i 
= mCodePointMap.find(id);
            
if (i != mCodePointMap.end())
            {
                i
->second.uvRect.left = u1;
                i
->second.uvRect.top = v1;
                i
->second.uvRect.right = u2;
                i
->second.uvRect.bottom = v2;
                i
->second.aspectRatio = textureAspect * (u2 - u1)  / (v2 - v1);
                i
->second.l = u1Pixel;
                i
->second.m = v1Pixel;
            }
            
else
            {
                mCodePointMap.insert(
                    CodePointMap::value_type(id, 
                        GlyphInfo(id, UVRect(u1, v1, u2, v2), 
                            textureAspect 
* (u2 - u1)  / (v2 - v1), u1Pixel, v1Pixel)));
            }

        }

        
/** Gets the aspect ratio (width / height) of this character. */
        inline 
float getGlyphAspectRatio(CodePoint id) const
        {
            CodePointMap::const_iterator i 
= mCodePointMap.find(id);
            
if (i != mCodePointMap.end())
            {
                
return i->second.aspectRatio;
            }
            
else
            {
                
return 1.0;
            }
        }
        
/** Sets the aspect ratio (width / height) of this character.
        @remarks
            You only need to call this if you're setting up a font loaded from a 
            texture manually.
        
*/
        inline 
void setGlyphAspectRatio(CodePoint id, float ratio)
        {
            CodePointMap::iterator i 
= mCodePointMap.find(id);
            
if (i != mCodePointMap.end())
            {
                i
->second.aspectRatio = ratio;
            }
        }

        
/** Gets the information available for a glyph corresponding to a
            given code point, or throws an exception if it doesn't exist;
        
*/
        
const GlyphInfo* getGlyphInfo(CodePoint id) const;


        LPDIRECT3DTEXTURE9    getFontTexture() 
const { return mTexture; }

        
void    insertGlyphInfo(CodePoint id);

        
bool    hasBlankInTexture() const 
        { 
            
return mLeftBlankNum > 0
        }

        
void    renderGlyphIntoTexture(CodePoint id);

        CodePoint    getLessUseChar();

        
void    removeGlyph(CodePoint id);
    
    };
}
#endif

/************************************************************************\
This is a fucking
______   ___   _      _       
|  ___| / _ \ | |    | |      
| |_   / /_\ \| |__  | |  ___ 
|  _|  |  _  || '_ \ | | / _ \
| |    | | | || |_) || ||  __/
\_|    \_| |_/|_.__/ |_| \___| 's free file

filename: Font.cpp 
created:  2010/07/30
creator:    承天一

purpose:  Freetype字體類
************************************************************************
*/

#include 
"stdafx.h"
#include 
"RenderCore.h"
#include 
"Font.h"
#include 
"TextureManager.h"
#include 
<d3dx9tex.h>


#undef max
#undef min

namespace Fable
{

    Font::Font()
        :mTtfMaxBearingY(
0), mTtfResolution(0), mAntialiasColour(false),
        mTexture(
0), mLeftBlankNum(0),
        mImageData(nullptr), mImage_m(
0),mImage_l(0)
    {
        mWidth 
= 1024;
        mHeight 
= 1024;
        mTtfSize 
= 20;
        mTtfResolution 
= 96;
    }

    Font::
~Font()
    {
        unLoad();
    }

    
void Font::load(const std::string& name)
    {
        mFontName 
= name;

        FT_Library ftLibrary;

        
//初始化庫 
        if(FT_Init_FreeType(&ftLibrary))
            FA_EXCEPT(ERR_FONT, 
"FreeType初始化失敗");

        mCharSpacer 
= 5;
        
if(FT_New_Face(ftLibrary, name.c_str(), 0&mFtFace)) 
            FA_EXCEPT(ERR_FONT, 
"FreeType無法打開ttf文件");

        UINT maxFaceNum 
= mFtFace->num_faces;

        FT_F26Dot6 ftSize 
= (FT_F26Dot6)(mTtfSize * (1 << 6));

        
if(FT_Set_Char_Size( mFtFace, ftSize, 0, mTtfResolution, mTtfResolution))
            FA_EXCEPT(ERR_FONT, 
"Could not set char size!");

        mMaxHeight 
= 0, mMaxWidth = 0;

        
if(mCodePointRangeList.empty())
        {
            mCodePointRangeList.push_back(CodePointRange(
33166));
            mCodePointRangeList.push_back(CodePointRange(
1996840869));
        }

        
// Calculate maximum width, height and bearing
        for (CodePointRangeList::const_iterator r = mCodePointRangeList.begin();
            r 
!= mCodePointRangeList.end(); ++r)
        {
            
const CodePointRange& range = *r;
            
for(CodePoint cp = range.first; cp <= range.second; ++cp)
            {
                FT_Load_Char( mFtFace, cp, FT_LOAD_RENDER );

                
if( ( 2 * ( mFtFace->glyph->bitmap.rows << 6 ) - mFtFace->glyph->metrics.horiBearingY ) > mMaxHeight )
                    mMaxHeight 
= ( 2 * ( mFtFace->glyph->bitmap.rows << 6 ) - mFtFace->glyph->metrics.horiBearingY );
                
if( mFtFace->glyph->metrics.horiBearingY > mTtfMaxBearingY )
                    mTtfMaxBearingY 
= mFtFace->glyph->metrics.horiBearingY;

                
if( (mFtFace->glyph->advance.x >> 6 ) + ( mFtFace->glyph->metrics.horiBearingX >> 6 ) > mMaxWidth)
                    mMaxWidth 
= (mFtFace->glyph->advance.x >> 6 ) + ( mFtFace->glyph->metrics.horiBearingX >> 6 );
            }
        }

        
// We just make a 1024 * 1024 texture, it's enough
        mTextureAspect = 1.0f;

        mPixelBytes 
= 2;
        mCharDataWidth 
= (mMaxWidth + mCharSpacer) * mPixelBytes;
        mDataSize
= mWidth * mHeight * mPixelBytes;
        mMaxCharSize 
= ((mMaxHeight >> 6+ mCharSpacer) * mCharDataWidth;
        mMaxCharNum 
= mDataSize / mMaxCharSize;
        mLeftBlankNum 
= mMaxCharNum;
        CON_INFO(
"Font texture size %d * %d", mWidth, mHeight);

        mImageData
= FA_NEW_ARRAY_T(uchar, mDataSize);
        
// Reset content (transparent, white)
        for (size_t i = 0; i < mDataSize; i += mPixelBytes)
        {
            mImageData[i 
+ 0= 0xFF// luminance
            mImageData[i + 1= 0x00;    // alpha
        }

        HRESULT hr 
= 0;
        hr 
= D3DXCreateTexture(
            RenderCore::getInstancePtr()
->getDevice(),
            mWidth,
            mHeight,
            
1,
            
0,
            D3DFMT_A8L8,
            D3DPOOL_MANAGED,
            
&mTexture);

        
if(FAILED(hr))
        {
            
string msg = DXGetErrorDescriptionA(hr);
            FA_EXCEPT(ERR_FONT, 
"Create font Texture failed: " + msg);
        }

    }

    
void Font::unLoad()
    {
        FA_DELETE_ARRAY_T(mImageData, uchar, mDataSize);
        SAFE_RELEASE(mTexture);
        FT_Done_FreeType(mFtLibrary);
    }

    
const Font::GlyphInfo* Font::getGlyphInfo(CodePoint id) const
    {
        CodePointMap::const_iterator i 
= mCodePointMap.find(id);
        
if (i == mCodePointMap.end())
        {
            
return nullptr;
        }
        
return &i->second;
    }

    
void Font::renderGlyphIntoTexture(CodePoint id)
    {
        FT_Error ftResult;

        
// Load & render glyph
        ftResult = FT_Load_Char( mFtFace, id, FT_LOAD_RENDER );
        
if (ftResult)
        {
            
// problem loading this glyph, continue
            CON_INFO("Info: cannot load CodePoint %d", id);
        }

        FT_Int advance 
= mFtFace->glyph->advance.x >> 6;
        unsigned 
char* buffer = mFtFace->glyph->bitmap.buffer;

        
if (!buffer)
        {
            
// Yuck, FT didn't detect this but generated a null pointer!
            CON_INFO("Info: Freetype returned null for character %d", id);
        }

        
int y_bearnig = ( mTtfMaxBearingY >> 6 ) - ( mFtFace->glyph->metrics.horiBearingY >> 6 );
        
int x_bearing = mFtFace->glyph->metrics.horiBearingX >> 6;

        
for(int j = 0; j < mFtFace->glyph->bitmap.rows; ++j )
        {
            size_t row 
= j + mImage_m + y_bearnig;
            UCHAR
* pDest = &mImageData[(row * mWidth * mPixelBytes) + (mImage_l + x_bearing) * mPixelBytes];
            
for(int k = 0; k < mFtFace->glyph->bitmap.width; ++k )
            {
                
if (mAntialiasColour)
                        {
                            
// Use the same greyscale pixel for all components RGBA
                            *pDest++= *buffer;
                        }
                        
else
                        {
                            
// Always white whether 'on' or 'off' pixel, since alpha
                            
// will turn off
                            *pDest++= 0xFF;
                        }
                        
// Always use the greyscale value for alpha
                        *pDest++= *buffer++
            }
        }

        
this->setGlyphTexCoords(id,
            mImage_l,  
// u1
            mImage_m,  // v1
            mImage_l + ( mFtFace->glyph->advance.x >> 6 ), // u2
            mImage_m + ( mMaxHeight >> 6 ), // v2
            mTextureAspect
            );

        
// Advance a column
        mImage_l += (advance + mCharSpacer);

        
// If at end of row
        if( mWidth - 1 < mImage_l + ( advance ) )
        {
            mImage_m 
+= ( mMaxHeight >> 6 ) + mCharSpacer;
            mImage_l 
= 0;
        }
        
--mLeftBlankNum;

        D3DLOCKED_RECT lockedRect;
        mTexture
->LockRect(0&lockedRect,00);         

        
//使用類型注意
        uchar* TexData = (uchar*)lockedRect.pBits;

        
for(UINT i = 0; i < mHeight; ++i)
        {
            
for(UINT j = 0; j < mWidth; ++j)
            {
                
//Pitch數據的總長度
                int index = i * lockedRect.Pitch / mPixelBytes + j;
                TexData[index] 
= mImageData[index];
            }
        }
        mTexture
->UnlockRect(0);

        
// for test
//#ifdef    _DEBUG
//        D3DXSaveTextureToFileA("..//media//test.png",D3DXIFF_PNG, mTexture, 0);
//#endif
    }

    
void Font::insertGlyphInfo(CodePoint id)
    {
        
if(!hasBlankInTexture())    //has no space left in texture    
        {
            removeGlyph(getLessUseChar());
        }
        renderGlyphIntoTexture(id);

    }

    Font::CodePoint Font::getLessUseChar()
    {
        CodePointMap::iterator i 
= mCodePointMap.begin(), iend = mCodePointMap.end(), iless = mCodePointMap.begin();
        
while(i != iend)
        {
            
if(i->second.useCount < iless->second.useCount)
                iless 
= i;
            
++i;
        }
        
return iless->second.codePoint;   
    }

    
void Font::removeGlyph(CodePoint id)
    {
        CodePointMap::iterator it 
= mCodePointMap.find(id);
        
if(it != mCodePointMap.end())
        {
            mImage_l 
= it->second.l;
            mImage_m 
= it->second.m;
            mCodePointMap.erase(it);
            
++mLeftBlankNum;
        }
        
else
        {
            FA_EXCEPT(ERR_FONT, 
"Can not find CodePoint to remove in void Font::removeGlyph(CodePoint id)");
        }
    }

}

posted on 2010-08-09 19:02 月下圓舞曲 閱讀(4731) 評論(1)  編輯 收藏 引用 所屬分類: 開發

Feedback

# re: 實現了簡陋的Freetype2在DirectX下顯示字體 2010-11-02 14:41 放放風
很大部分是 ogre 的代碼  回復  更多評論
  

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美黄色一区二区| av成人免费| 亚洲主播在线播放| 亚洲午夜免费视频| 国产精品每日更新| 久久蜜桃香蕉精品一区二区三区| 亚洲一区二区三区在线观看视频| 欧美日韩一区二区高清| 亚洲一二三区视频在线观看| 久久久91精品国产一区二区精品| 亚洲欧洲一区二区三区在线观看| 99视频日韩| 亚洲电影免费在线观看| 亚洲最新视频在线播放| 在线观看91久久久久久| 亚洲视频图片小说| 亚洲欧洲一区二区三区久久| 午夜在线成人av| 一片黄亚洲嫩模| 久久天天躁狠狠躁夜夜av| 一区二区三区不卡视频在线观看| 久久久精品日韩欧美| 亚洲欧美视频一区| 美女久久网站| 久久精品道一区二区三区| 欧美日韩久久| 亚洲第一天堂av| 国产一区亚洲| 在线视频精品| 亚洲伦伦在线| 久久综合久久88| 久久九九全国免费精品观看| 国产精品theporn| 亚洲人成毛片在线播放| 亚洲第一页在线| 久久精品一本| 久久一区二区三区四区五区| 亚洲国产精品悠悠久久琪琪| 国产亚洲一本大道中文在线| 在线综合亚洲| 在线亚洲一区| 欧美三级视频在线观看| 亚洲精品视频二区| 99天天综合性| 欧美日韩一区二区三区视频| 亚洲国产片色| 日韩一二三区视频| 一本色道久久99精品综合| 免费短视频成人日韩| 欧美一区二区三区视频在线| 国产日韩一区在线| 国产精品看片资源| 免费视频一区二区三区在线观看| 亚洲毛片在线免费观看| 欧美日韩免费观看一区二区三区| 鲁大师成人一区二区三区| 欧美日韩成人免费| 一区二区欧美在线观看| 亚洲精品国精品久久99热一| 久热精品视频在线观看一区| 免费看精品久久片| 在线播放中文一区| 免费在线看成人av| 亚洲国产色一区| 亚洲免费观看在线视频| 欧美精品乱人伦久久久久久| 在线日韩欧美| 欧美在线观看网址综合| 亚洲永久字幕| 欧美在线精品免播放器视频| 国产精品成人一区| 一区二区成人精品| 欧美一区二区性| 一区二区在线看| 欧美激情黄色片| 亚洲欧洲综合另类| 亚洲一区二区三区高清 | 欧美日韩国内自拍| 日韩一级免费观看| 欧美一区二区三区免费观看视频| 国内成人自拍视频| 另类欧美日韩国产在线| 亚洲视频一二| 国产精品入口日韩视频大尺度| 性18欧美另类| 欧美成在线观看| 亚洲天堂男人| 国内精品久久久久国产盗摄免费观看完整版 | 亚洲大片免费看| 一本色道88久久加勒比精品 | 久久综合一区| 日韩亚洲精品视频| 国产欧美日韩精品a在线观看| 快射av在线播放一区| 亚洲天堂免费在线观看视频| 蜜桃av噜噜一区二区三区| 99精品国产在热久久下载| 国产一区二区成人久久免费影院| 欧美成在线观看| 亚洲欧美日韩国产精品 | 国产精品豆花视频| 久久亚洲春色中文字幕久久久| 亚洲精品免费在线播放| 久久香蕉国产线看观看网| 一区电影在线观看| 狠狠色狠色综合曰曰| 欧美激情视频一区二区三区不卡| 午夜精品一区二区三区四区| 亚洲精品视频在线观看免费| 免费观看一区| 亚洲精品综合| 91久久久在线| 麻豆成人精品| 欧美一进一出视频| 在线亚洲精品福利网址导航| 国产日产欧产精品推荐色 | 久久精品一区二区三区中文字幕| 日韩一区二区电影网| 在线成人亚洲| 国产在线不卡精品| 国产免费观看久久黄| 国产精品国产三级国产普通话三级| 欧美成人精品三级在线观看| 久久精品免费| 欧美在线播放| 香蕉成人啪国产精品视频综合网| 99精品国产热久久91蜜凸| 亚洲国产精品123| 美女精品在线| 久久综合久色欧美综合狠狠| 久久狠狠婷婷| 久久大逼视频| 欧美在线观看视频一区二区| 午夜精品视频| 欧美诱惑福利视频| 欧美一区二区三区喷汁尤物| 午夜宅男欧美| 欧美在线免费观看视频| 欧美一级片一区| 午夜精品久久久久久99热| 亚洲男女毛片无遮挡| 亚洲欧美日韩国产综合精品二区| 亚洲一区二区三区欧美| 亚洲一区二区免费看| 亚洲欧美日韩国产综合精品二区| 午夜精品久久久久久| 欧美在线视频在线播放完整版免费观看 | 亚洲一区二区三区四区视频| 亚洲伊人色欲综合网| 亚洲欧美日韩网| 欧美自拍偷拍午夜视频| 久久免费午夜影院| 牛夜精品久久久久久久99黑人| 欧美国产成人在线| 亚洲免费观看在线观看| 亚洲一区二区三区四区五区黄| 亚洲欧美在线一区| 久久精品青青大伊人av| 巨乳诱惑日韩免费av| 欧美激情一区二区| 国产精品成人久久久久| 国产伦一区二区三区色一情| 韩国久久久久| 99re66热这里只有精品4 | 欧美日韩不卡合集视频| 国产精品激情电影| 国产专区欧美专区| 亚洲激情第一区| 亚洲影视中文字幕| 久久夜色精品国产| 亚洲经典在线| 西西人体一区二区| 久久亚洲欧洲| 国产精品xxxav免费视频| 国产亚洲欧美中文| 日韩午夜视频在线观看| 欧美一区在线视频| 亚洲国产网站| 性欧美激情精品| 欧美韩日精品| 国产日韩精品一区二区三区在线| 伊人夜夜躁av伊人久久| 一区二区三区视频在线| 欧美在线观看天堂一区二区三区| 午夜天堂精品久久久久| 亚洲国产高清视频| 亚洲女女女同性video| 麻豆精品传媒视频| 一区二区免费看| 久久看片网站| 国产精品免费一区二区三区在线观看| 精品1区2区3区4区| 亚洲欧美成人| 欧美激情视频一区二区三区在线播放| 亚洲色在线视频| 欧美成人午夜激情在线| 国产午夜精品麻豆| 一本高清dvd不卡在线观看| 久久久人成影片一区二区三区| 日韩视频免费大全中文字幕|