LibGdx 游戏引擎 freetype 使用指南

2024-01-24 01:58

本文主要是介绍LibGdx 游戏引擎 freetype 使用指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

网上大多数的Libgdx文档,使用的版本都是0.9X的时候留下的,1.X版本的比较少。freetype在1.X时使用发生发生了变化,写一下变化。


本人使用的是Libgdx 1.6版本。

首先给两个传送门,可以去Git上看官方的wiki。

官方的freetype说明
https://github.com/libgdx/libgdx/wiki/Gdx-freetype
官方的freetype样例
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/extensions/InternationalFontsTest.java

下面是两端代码的比较
0.9X版本

package com.example.mylibgdxfont03;import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData;public class MyGame implements ApplicationListener {private BitmapFont font;private FreeTypeFontGenerator generator;private FreeTypeBitmapFontData fontData;private SpriteBatch batch;@Overridepublic void create() {generator = new FreeTypeFontGenerator(Gdx.files.internal("testfont.ttf"));fontData = generator.generateData(25, generator.DEFAULT_CHARS+ "晚风把荣华吹散,名利是如此浅薄。—我一条寻水的鱼!", false);// 这里需要把你要输出的字,全部写上,前提是不能有重复的字。font = new BitmapFont(fontData, fontData.getTextureRegion(), false);font.setColor(Color.PINK);batch = new SpriteBatch();}@Overridepublic void dispose() {font.dispose();generator.dispose();batch.dispose();}@Overridepublic void render() {Gdx.gl.glClearColor(1, 1, 1, 1);Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);batch.begin();font.drawMultiLine(batch,"Hello Everyone: \n \n晚风把荣华吹散,\n名利是如此浅薄。",50, 220);font.draw(batch, "——我是一条寻水的鱼!", 150, 90);batch.end();}@Overridepublic void resize(int width, int height) {}@Overridepublic void pause() {}@Overridepublic void resume() {}}
摘自 http://m.blog.csdn.net/blog/yangyu20121224/9182243

1.6版本的代码

package com.mygdx.game;import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;public class MyGdxGame extends ApplicationAdapter {SpriteBatch batch;FreeTypeFontGenerator generator;FreeTypeFontParameter parameter;private BitmapFont font;@Overridepublic void create () {batch = new SpriteBatch();generator = new FreeTypeFontGenerator(Gdx.files.internal("SIMKAI.TTF"));parameter = new FreeTypeFontParameter();parameter.characters = "今天气很好,风和日丽!";font = generator.generateFont(parameter);generator.dispose();}@Overridepublic void render () {Gdx.gl.glClearColor(0, 0, 0, 0);Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);batch.begin();font.draw(batch, "今天天气很好,风和日丽!", 100, 100);batch.end();}@Overridepublic void dispose() {batch.dispose();super.dispose();}}

区别在于:
1 、生成FreeType
0.9X 版本的代码

generator.generateData(25, generator.DEFAULT_CHARS+ "晚风把荣华吹散,名利是如此浅薄。—我一条寻水的鱼!", false);// 这里需要把你要输出的字,全部写上,前提是不能有重复的字。

1.6版本 需要使用FreeTypeFontParameter对象

generator = new FreeTypeFontGenerator(Gdx.files.internal("SIMKAI.TTF"));
parameter = new FreeTypeFontParameter();
parameter.characters = "今天气很好,风和日丽!";

2、使用BitmapFont
0.9X版本代码

font = new BitmapFont(fontData, fontData.getTextureRegion(), false);

1.6版本代码

font = generator.generateFont(parameter);

1.6版本和0.9X版本的freetype差别总结就这些。

另外附上freetype 中FreeTypeFontParameter 的默认参数

/** The size in pixels */
public int size = 16;
/** Foreground color (required for non-black borders) */
public Color color = Color.WHITE;
/** Border width in pixels, 0 to disable */
public float borderWidth = 0;
/** Border color; only used if borderWidth > 0 */
public Color borderColor = Color.BLACK;
/** true for straight (mitered), false for rounded borders */
public boolean borderStraight = false;
/** Offset of text shadow on X axis in pixels, 0 to disable */
public int shadowOffsetX = 0;
/** Offset of text shadow on Y axis in pixels, 0 to disable */
public int shadowOffsetY = 0;
/** Shadow color; only used if shadowOffset > 0 */
public Color shadowColor = new Color(0, 0, 0, 0.75f);
/** The characters the font should contain */
public String characters = DEFAULT_CHARS;
/** Whether the font should include kerning */
public boolean kerning = true;
/** The optional PixmapPacker to use */
public PixmapPacker packer = null;
/** Whether to flip the font vertically */
public boolean flip = false;
/** Whether or not to generate mip maps for the resulting texture */
public boolean genMipMaps = false;
/** Minification filter */
public TextureFilter minFilter = TextureFilter.Nearest;
/** Magnification filter */
public TextureFilter magFilter = TextureFilter.Nearest;

这篇关于LibGdx 游戏引擎 freetype 使用指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/638209

相关文章

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

MySQL 存储引擎 MyISAM详解(最新推荐)

《MySQL存储引擎MyISAM详解(最新推荐)》使用MyISAM存储引擎的表占用空间很小,但是由于使用表级锁定,所以限制了读/写操作的性能,通常用于中小型的Web应用和数据仓库配置中的只读或主要... 目录mysql 5.5 之前默认的存储引擎️‍一、MyISAM 存储引擎的特性️‍二、MyISAM 的主

Java JSQLParser解析SQL的使用指南

《JavaJSQLParser解析SQL的使用指南》JSQLParser是一个Java语言的SQL语句解析工具,可以将SQL语句解析成为Java类的层次结构,还支持改写SQL,下面我们就来看看它的具... 目录一、引言二、jsQLParser常见类2.1 Class Diagram2.2 Statement

正则表达式r前缀使用指南及如何避免常见错误

《正则表达式r前缀使用指南及如何避免常见错误》正则表达式是处理字符串的强大工具,但它常常伴随着转义字符的复杂性,本文将简洁地讲解r的作用、基本原理,以及如何在实际代码中避免常见错误,感兴趣的朋友一... 目录1. 字符串的双重翻译困境2. 为什么需要 r?3. 常见错误和正确用法4. Unicode 转换的

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

Spring Validation中9个数据校验工具使用指南

《SpringValidation中9个数据校验工具使用指南》SpringValidation作为Spring生态系统的重要组成部分,提供了一套强大而灵活的数据校验机制,本文给大家介绍了Spring... 目录1. Bean Validation基础注解常用注解示例在控制器中应用2. 自定义约束验证器定义自