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

             

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

                    1. 標(biāo)量字體, 放大會(huì)失真;

                    2. 字符集有限, 尤其是使用中文時(shí), 改動(dòng)幾率大;

             

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

                下面是FreeTypeFontGenerator對(duì)象的主要函數(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是個(gè)很實(shí)用的對(duì)象, 通過(guò)它可以在游戲初始時(shí)就將資源加載或者初始化好, 使用時(shí)只需要傳遞資源名稱即可獲取相關(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);

                字體也是一種資源, 因此也可以通過(guò)AssetManager來(lái)加載TTF字體. 為了能使得兩者無(wú)縫鏈接, 可以模仿BitmapFont對(duì)象創(chuàng)建個(gè)BitmapTrueFont來(lái)實(shí)現(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 閱讀(1976) 評(píng)論(6)  編輯 收藏 引用 所屬分類: G7I;P

            評(píng)論

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-22 17:55 西米的風(fēng)

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

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

            @西米的風(fēng)
            啊..還有一個(gè)BitmapTrueFontLoader來(lái)負(fù)責(zé)這事情..  回復(fù)  更多評(píng)論   

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 15:34 西米的風(fēng)

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

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

            @西米的風(fēng)
            客氣了..這里想多說(shuō)一句 -- LIBGDX真的還有點(diǎn)小眾...  回復(fù)  更多評(píng)論   

            # re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 16:06 西米的風(fēng)

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

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

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

            公告

            Using C++

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(73)

            隨筆分類(513)

            積分與排名

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久无码人妻精品一区二区三区| 久久国产精品免费一区| 久久这里只有精品首页| 亚洲精品国产自在久久| 久久亚洲春色中文字幕久久久| 国产精品99久久免费观看| 精品国产热久久久福利| 久久人人爽人人爽人人AV | 噜噜噜色噜噜噜久久| 欧美亚洲色综久久精品国产| 国产精品久久久天天影视香蕉| 伊人久久大香线蕉av不卡| 国产巨作麻豆欧美亚洲综合久久 | 久久青青草原精品国产软件| 综合久久给合久久狠狠狠97色| 久久久久久无码Av成人影院 | 亚洲欧美日韩久久精品第一区| 亚洲国产精品热久久| 人妻精品久久久久中文字幕一冢本| 久久国产三级无码一区二区| 青草国产精品久久久久久| 欧美一区二区久久精品| 国产亚洲精久久久久久无码AV| 久久棈精品久久久久久噜噜| 久久综合久久美利坚合众国| 久久久久亚洲av成人无码电影| 精品久久人妻av中文字幕| 99精品国产免费久久久久久下载| 久久久精品久久久久久| 国产成人久久精品麻豆一区| 久久精品午夜一区二区福利 | 国产精品免费久久久久电影网| 国产精品99精品久久免费| 久久久久亚洲av无码专区导航 | 国产综合免费精品久久久| 久久综合狠狠色综合伊人| 久久国产精品-久久精品| 国产成人精品久久一区二区三区| 无码精品久久久天天影视| 亚洲∧v久久久无码精品| 男女久久久国产一区二区三区|