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

月下的博客

  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 月下圓舞曲 閱讀(4721) 評論(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>
            亚洲尤物在线| 国产精品久久9| 亚洲午夜一区二区| 日韩视频―中文字幕| 日韩视频第一页| 99香蕉国产精品偷在线观看| 亚洲人成在线播放| 一区二区欧美视频| 羞羞色国产精品| 久久国内精品视频| 欧美大片在线观看一区二区| 欧美天堂在线观看| 国内精品免费午夜毛片| 亚洲精品乱码视频| 午夜精品久久久久久久久久久久| 亚洲欧美激情在线视频| 麻豆freexxxx性91精品| 亚洲国产经典视频| 亚洲一区二区在线观看视频| 久久爱www久久做| 你懂的亚洲视频| 国产精品色婷婷| 亚洲美女性视频| 欧美在线三级| 亚洲精品网址在线观看| 欧美在线啊v| 欧美日韩国产综合一区二区 | 欧美风情在线| 国产精品男人爽免费视频1 | 国产日韩一区二区| 亚洲人成网站999久久久综合| 一区二区三区高清视频在线观看 | 伊人精品视频| 亚洲综合精品四区| 亚洲动漫精品| 欧美专区亚洲专区| 国产精品久久久91| 日韩亚洲欧美一区| 免费观看亚洲视频大全| 亚洲综合色激情五月| 欧美精品导航| 亚洲国产精品va在线观看黑人| 欧美一区免费视频| 国产日韩视频一区二区三区| 久久婷婷国产麻豆91天堂| 欧美日韩一级片在线观看| 尤物九九久久国产精品的分类| 午夜精品在线| 国产精品99久久久久久宅男| 欧美片网站免费| 日韩小视频在线观看专区| 欧美黄色一区| 免费日本视频一区| 亚洲大黄网站| 欧美国产专区| 免费在线观看日韩欧美| 亚洲国产精品精华液网站| 免费日韩精品中文字幕视频在线| 欧美一区三区二区在线观看| 国产久一道中文一区| 欧美与黑人午夜性猛交久久久| 亚洲中字在线| 国产一区二区三区久久| 久久久精品国产免大香伊| 久久av一区二区三区| 揄拍成人国产精品视频| 猛男gaygay欧美视频| 狂野欧美激情性xxxx欧美| 亚洲成色www久久网站| 欧美国产日韩a欧美在线观看| 久热国产精品视频| 亚洲免费观看高清在线观看 | 尤物九九久久国产精品的分类| 久久国产一区二区| 久久久精品免费视频| 亚洲欧洲日产国码二区| 亚洲九九精品| 国产精品中文字幕欧美| 久久久综合香蕉尹人综合网| 久久综合色天天久久综合图片| 亚洲福利视频一区| 99在线观看免费视频精品观看| 国产精品日韩欧美一区二区| 久久久久久久久久看片| 男女激情视频一区| 亚洲欧美一区二区三区在线| 久久精品国产亚洲高清剧情介绍| 亚洲黄色在线| 亚洲一区亚洲| 亚洲欧洲另类国产综合| 亚洲一区二区三区精品在线观看| 国产一区二区三区电影在线观看| 欧美国产高清| 国产精品一级二级三级| 欧美成人午夜激情在线| 欧美午夜理伦三级在线观看| 久久天天躁夜夜躁狠狠躁2022| 欧美成人久久| 欧美在线欧美在线| 欧美日本免费一区二区三区| 国产欧美日韩中文字幕在线| 亚洲国产精品悠悠久久琪琪| 99在线观看免费视频精品观看| 国产日韩亚洲| 亚洲精选一区| 亚洲成色精品| 午夜免费电影一区在线观看| 亚洲六月丁香色婷婷综合久久| 亚洲一区二区三区中文字幕在线| 亚洲福利视频一区| 性视频1819p久久| 亚洲男女自偷自拍图片另类| 久热精品视频在线| 久久婷婷丁香| 国产亚洲成人一区| 亚洲一二三区在线观看| 99精品黄色片免费大全| 麻豆九一精品爱看视频在线观看免费| 亚洲欧美国产精品专区久久| 欧美久久视频| 亚洲丁香婷深爱综合| 亚洲成人在线网| 久久久免费精品| 美女脱光内衣内裤视频久久网站| 国产精品区一区二区三| 一本一道久久综合狠狠老精东影业| 亚洲激情黄色| 免费在线国产精品| 欧美激情第三页| 亚洲人成77777在线观看网| 久久久999精品| 免费黄网站欧美| 激情综合色丁香一区二区| 性欧美xxxx大乳国产app| 亚洲欧美一区二区精品久久久| 欧美视频一二三区| 9人人澡人人爽人人精品| 夜夜嗨网站十八久久| 欧美日本韩国一区二区三区| 亚洲三级免费| 亚洲一区二区三区高清| 国产精品vvv| 午夜精品99久久免费| 欧美一区国产在线| 精品91免费| 免费久久99精品国产自在现线| 蜜桃av噜噜一区| 亚洲人成在线观看| 欧美日韩亚洲高清一区二区| 一区二区三区 在线观看视频| 亚洲综合精品自拍| 国产手机视频一区二区| 久久久国产亚洲精品| 欧美国产日韩a欧美在线观看| 亚洲精品乱码久久久久久按摩观 | 亚洲色诱最新| 久久国产色av| 亚洲黄色在线| 欧美新色视频| 久久国产精品黑丝| 欧美激情视频网站| 亚洲私人黄色宅男| 狠狠狠色丁香婷婷综合激情| 欧美www视频| 亚洲一区精品视频| 欧美国产国产综合| 亚洲欧美日韩国产一区二区三区| 国产一区二区按摩在线观看| 亚洲日本激情| 国产精品久久国产精品99gif | 另类尿喷潮videofree| 99在线热播精品免费| 久久精品成人一区二区三区| 亚洲国产精品女人久久久| 欧美性做爰猛烈叫床潮| 久久精品国产久精国产爱| 亚洲激情偷拍| 久久精品国产清自在天天线| 亚洲毛片在线看| 国产亚洲高清视频| 欧美日韩亚洲在线| 久久久水蜜桃av免费网站| 在线亚洲成人| 亚洲国产一区二区三区在线播| 欧美中文在线免费| 中文亚洲欧美| 亚洲激情在线播放| 一区二区亚洲| 国产永久精品大片wwwapp| 欧美体内she精视频在线观看| 久久久另类综合| 亚洲欧美日韩成人高清在线一区| 亚洲青色在线| 欧美夫妇交换俱乐部在线观看| 欧美亚洲免费高清在线观看| 99亚洲一区二区| 亚洲福利国产精品| 狠狠色狠色综合曰曰| 国产麻豆日韩欧美久久| 欧美天堂亚洲电影院在线播放 |