• <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>

            Codejie's C++ Space

            Using C++

            LIBGDX: FreeTypeFontGenerator and BitmapTrueFont

             

                在剛開始敲I;P游戲的時候, 字體使用的是BitmapFont, 只要有一套字體的PNG文件就可以顯示字體數(shù)據(jù)了. 并且通過BitmapFont對象可以很好的跟Label結(jié)合在一起使用. BitmapFont中的字體從PNG文件中截取出來有很多方便的地方, 比如, 一來只需要準備使用的字符即可, 這樣字體文件比較小; 二來可以定義任意的圖案來代替字符, 靈活性定制方便. 但反過來, 由于基于PNG文件, 也導致了BitmapFont有個兩個主要的缺陷 --

                    1. 標量字體, 放大會失真;

                    2. 字符集有限, 尤其是使用中文時, 改動幾率大;

             

                好在LIBGDX還在其擴展庫(gdx-freetype)中提供了另外一個好用的對象 --- FreeTypeFontGenerator. 通過這個對象可以加載TTF (TrueType Font), 然后產(chǎn)生BitmapFont對象就可以使用了.

                下面是FreeTypeFontGenerator對象的主要函數(shù)原型.

            /** Generates a new {@link BitmapFont}, containing glyphs for the given characters. The size is expressed in pixels. Throws a
             * GdxRuntimeException in case the font could not be generated. Using big sizes might cause such an exception. All characters
             * need to fit onto a single texture.
             * @param size the size in pixels
             * @param characters the characters the font should contain
             * @param flip whether to flip the font horizontally, see {@link BitmapFont#BitmapFont(FileHandle, TextureRegion, boolean)} */
            public BitmapFont generateFont (int size, String characters, boolean flip) {}
            
            /** Generates a new {@link BitmapFont}. The size is expressed in pixels. Throws a GdxRuntimeException in case the font could not
             * be generated. Using big sizes might cause such an exception. All characters need to fit onto a single texture.
             * 
             * @param size the size of the font in pixels */
            public BitmapFont generateFont (int size) {}

             

                在LIBGDX中, AssetManager是個很實用的對象, 通過它可以在游戲初始時就將資源加載或者初始化好, 使用時只需要傳遞資源名稱即可獲取相關(guān)資源. 比如, 下面代碼加載了TextureAtlas和Sound的資源.

            assetManager.load(PackConfig.SCREEN_PLAY, TextureAtlas.class);
            assetManager.load(PackConfig.SCREEN_MENU, TextureAtlas.class);
            
            assetManager.load(AudioConfig.MENU_CLICK, Sound.class);
            assetManager.load(AudioConfig.TRAY_CATCH, Sound.class);

                字體也是一種資源, 因此也可以通過AssetManager來加載TTF字體. 為了能使得兩者無縫鏈接, 可以模仿BitmapFont對象創(chuàng)建個BitmapTrueFont來實現(xiàn).

             

            package jie.android.ip.common.ttf;
            
            import java.util.HashMap;
            
            import com.badlogic.gdx.assets.AssetLoaderParameters;
            import com.badlogic.gdx.graphics.g2d.BitmapFont;
            import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
            import com.badlogic.gdx.utils.Disposable;
            
            public class BitmapTrueFont implements Disposable {
            
            	private final HashMap<Integer, BitmapFont> fontMap = new HashMap<Integer, BitmapFont>();
            	
            	private final FreeTypeFontGenerator generator;
            	private final String chars;
            	
            	public BitmapTrueFont(final FreeTypeFontGenerator generator, final BitmapTrueFontParameter parameter) {
            		this.generator = generator;
            		this.chars = parameter.getChars();
            	}
            	
            	@Override
            	public void dispose() {
            		for (final BitmapFont font : fontMap.values()) {
            			if (font != null) {
            				font.dispose();
            			}
            		}
            
            		if (generator != null) {
            			generator.dispose();
            		}
            	}
            	
            	public final BitmapFont getBitmapFont(int size) {
            		if (generator == null) {
            			return null;
            		}
            		
            		BitmapFont font = fontMap.get(Integer.valueOf(size));
            		if (font == null) {
            			if (chars == null) {
            				font = generator.generateFont(size);
            			} else {
            				font = generator.generateFont(size, chars, false);
            			}
            			fontMap.put(Integer.valueOf(size), font);
            		}
            		return font;
            	}
            	
            	static public class BitmapTrueFontParameter extends AssetLoaderParameters<BitmapTrueFont> {
            		private String chars = null;
            		
            		public BitmapTrueFontParameter() {			
            		}
            		
            		public BitmapTrueFontParameter(final String chars) {
            			this.chars = chars;
            		}
            		
            		public final String getChars() {
            			return chars;
            		}
            	}	
            }

                OK, 現(xiàn)在可以使用下面代碼加載TTF資源了.

            assetManager.load("example.ttf", BitmapTrueFont.class, new BitmapTrueFont.BitmapTrueFontParameter(null));

            posted on 2014-03-09 21:03 codejie 閱讀(1986) 評論(6)  編輯 收藏 引用 所屬分類: G7I;P

            評論

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-22 17:55 西米的風

            那get 怎么做 你這個構(gòu)造貌似不能調(diào)用get 得不到值啊 因為 一直generator 是null吧!! 在構(gòu)造函數(shù)里面應該要new一個generator 把  回復  更多評論   

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-22 21:05 codejie

            @西米的風
            啊..還有一個BitmapTrueFontLoader來負責這事情..  回復  更多評論   

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 15:34 西米的風

            恩 我也是剛接觸這個,我昨天看了下源代碼 的確還要寫一個loader 來處理,謝謝了,膜拜大神,中文游戲就是麻煩ttf資源 如果不放在assetManager里面 游戲如果轉(zhuǎn)入后臺資源就被釋放了,哎這個問題 糾結(jié)好久 再次謝謝了,看了您的文章受益匪淺!@codejie  回復  更多評論   

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 15:56 codejie

            @西米的風
            客氣了..這里想多說一句 -- LIBGDX真的還有點小眾...  回復  更多評論   

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 16:06 西米的風

            @codejie
            我想先拿來學習 ,學習寫法思路,我開始的想法是先把一個搞精通,搞其他的引擎也就水到渠成,libgdx現(xiàn)在的網(wǎng)上學習資源還是很多的,哈哈,天資愚鈍只能慢慢來啦!  回復  更多評論   

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 17:26 codejie

            @西米的風
            哈, 如此'執(zhí)迷不悟'...那很高興這些對你有用.  回復  更多評論   

            公告

            Using C++

            導航

            統(tǒng)計

            留言簿(73)

            隨筆分類(513)

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品久久婷婷六月丁香| 国产L精品国产亚洲区久久| 久久激情五月丁香伊人| 久久婷婷人人澡人人爽人人爱 | 中文精品久久久久人妻不卡| 久久国产乱子伦精品免费强| 亚洲欧美精品一区久久中文字幕 | 久久久久久久91精品免费观看| 亚洲精品无码久久一线| 日本一区精品久久久久影院| 亚洲精品国产字幕久久不卡| 久久精品无码一区二区三区| 一97日本道伊人久久综合影院| 久久青青草原精品国产| 亚洲精品无码久久久久sm| 一本大道久久a久久精品综合| 久久天堂AV综合合色蜜桃网| 日产精品久久久久久久| 一本大道加勒比久久综合| 亚洲色欲久久久综合网东京热| 国产激情久久久久影院小草| 久久久一本精品99久久精品66| 亚洲欧洲精品成人久久奇米网| 久久精品草草草| 久久午夜伦鲁片免费无码| 亚洲人成无码久久电影网站| 国产午夜电影久久| 品成人欧美大片久久国产欧美...| 18岁日韩内射颜射午夜久久成人| 久久久99精品成人片中文字幕| 99久久成人国产精品免费| 精品久久久久久亚洲| 亚洲精品无码久久久久去q| 久久久久久久久66精品片| 欧美激情精品久久久久久| 亚洲人成无码网站久久99热国产| 91精品观看91久久久久久| 国产成人精品久久亚洲高清不卡| 久久免费美女视频| 精品久久久久久久久久久久久久久| 久久久久久久综合日本|