Code Monkey home page Code Monkey logo

gdx-lazy-font's Introduction

GDX-LAZY-FONT

智能且懒加载的BitmapFont模块。
Auto generate & manage your bitmapfont without pre-generate.

Introduce & demo in English ,click me.

下载地址-Download

点我下载最新版,click me to download last release

注意:1.9的libgdx在文字绘制部分的API有些小改动,如果你需要针对1.9版本的GDX,请在下载页面专门选择1.9那个版本,否则就使用他下面那个正常的版本!

NOTICE<< GDX1.9 has some API change for render text,please notice the version on download page


介绍 & 使用方法

这是LibGDX的一个第三方小组件,他轻巧的只有140行不到的代码,但或许能帮助你大忙( •́ .̫ •̀ )

介绍

众所周知,LibGDX的文字模块对非英语地区支持的并不是很好(虽然现在已经好多了)。

为什么说对非英语地区支持不好呢?首先英语就那几个字母,加上标点符号之类,总共的字符也就100个左右,而我们可怜的libgdx并没有文字纹理的管理方案,我们使用libgdx绘制文字时,要先声明且创建文字的“纹理”,然后才能画到屏幕上,麻烦的一逼好么?!而前面说过,英语就100多个字符,可以在程序加载时候都先声明好纹理然后随便用,但我们汉语乱七八糟的字符要成千上万个,不可能在程序载入时候都加载进来,否则别人的手机或者显卡就要爆掉了= = 所以我们要每次使用文字的时候,先创建个什么鬼generator,然后生成所要文字的纹理,然后在画上去,这非常的麻烦而且精神污染,于是这个项目就产生了( ̄∀ ̄)

这个项目主要是支持我正在开发的一款角色扮演游戏(见我仓库里的“GDX-RPG”项目),他的作用是完全的字体纹理自我加载和管理,不用generator去一遍遍生成纹理,想在屏幕画什么字就画什么字,爽到爆~

这个LazyFont项目以前就有了,只不过以前是针对LibGDX1.5版本以下的,因为我自己写的项目也只是用1.4.1版本的,所以这个git项目创建了之后就没太更新过。 前两天痛下狠心把1.4.1版本升级到了最新的1.7.0(当然来自未来的你那时候的GDX版本可能更高了)

然后你懂的各种报错红叉叉让我痛不欲生(╯ ̄Д ̄)╯
其中最大的问题就是文字模块,1.4和1.7的GDX中,文字核心绘制方法已经变得很多了,我以前写的一些中文绘制辅助类也早就用不了了,于是打算重新研究下这个项目。
(不要问我为什么不更新这个项目了,因为这项目已经写完了啊,除非libgdx有什么大变动不然可以随便用,我会持续关注并且更新的)

废话少说,本项目的作用& 好处有什么?

1.全自动的生成BitmapFont,无需手动generate,适合大量文字应用的场景(服务器公告、文字冒险游戏等)。
(那啥,总有人问我,这个东西影不影响性能,我只想说,可以告诉你,用了这个组件,只会多出连0.1%的CPU占用都不到而已,请不要成天跟我性能性能的了,你特么做个消灭星星也要性能,我也是笑的前列腺液都出来了)
2.支持BitmapFont.draw(),也支持所有需要调用font的地方(如Scene2d.ui.Label这个类)。
3.不污染代码,即插即用,理论来说以后的GDX只要更新不是太猛,基本都能兼容(不兼容1.4以下版本,如果你想要1.4以下的,看一下本项目的历史记录自己找吧)。

使用方法

你可以直接下载jar包,然后导入到你的项目里,或者拷贝源代码。

要创建一个LazyBitmapFont,你需要一个FreeTypeFontGenerator,且只需要一个,你可以创建一个FreeTypeFontGenerator变量用到游戏结束。

lazyBitmapFont = new LazyBitmapFont(FreeTypeFontGenerator generator, int fontSize);

其中,generator就是上面说的变量,你只需要全局new一个这个变量,就可以给所有的LazyBitmapFont或者你自己的需求使用了。 然后,fontSize顾名思义,就是文字的大小了。遗憾的是,本组件不能动态的更改文字大小,也就是说一旦new了一个本组件,就只能使用一种文字大小了,如果你需要好几种字号,那么就分别多new几个不同字号的本组件吧(每个组件都很轻量,不会占资源的~

接着,没了,是的,没了( •́ .̫ •̀ )

你可以直接调用下面代码试一下:

lazyBitmapFont.draw(batch, "我随便说一句话就能够组成十五字", 100, 200);

看看,屏幕上有没有字出来呢?

同理,你也可以弄一个label试试:

//创建...
LabelStyle style = new LabelStyle();
style.font = lazyBitmapFont;

Label label = new Label("我就随便打句话",style);

Stage stage = new Stage();
stage.addActor(label);

// 渲染...
stage.act();
stage.draw();

//或者试试在运行时候把label的文字改掉...
label.setText("在运行时候随便的更改下文字!");

好了,就是这么简单,除了new的时候和原版不同以外,其他都是一模一样的,而且再也不用换一句文本就重新generate一下啦,就是这么酸爽!

当然,最后用完了也别忘了dispose()掉哦。

lazyBitmapFont.dispose();

下面是一模一样的英文版介绍,不用看了,赶紧去试试吧。

what & how to

what's this?

LazyBitmapFont is an auto and self-texture-gengrate bitmapFont extend, you can draw any text without generator. And it's helpful for no English-base language to manage BitmapFont.

how to use it

First, download jar file and import to your project, or copy source.

You need a FreeTypeFontGenerator to create this extend, like:

lazyBitmapFont = new LazyBitmapFont(FreeTypeFontGenerator generator, int fontSize);

the fontSize is final variable that you can't modify at runtime. Of course you can create some different size object.

Then ,this API is same as BitmapFont.

lazyBitmapFont.draw(batch, "我随便说一句话就能够组成十五字", 100, 200);

or create a Label as usual as before:

//create...
LabelStyle style = new LabelStyle();
style.font = lazyBitmapFont;

Label label = new Label("我就随便打句话",style);

Stage stage = new Stage();
stage.addActor(label);

// render...
stage.act();
stage.draw();

//or change text at runtime...
label.setText("在运行时候随便的更改下文字!");

final, use dispose() to dispose this.

 lazyBitmapFont.dispose();

gdx-lazy-font's People

Contributors

dingjibang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gdx-lazy-font's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.