生成在运行时的纹理使用libgdx文本 [英] Generating textures at runtime with text using libgdx

查看:293
本文介绍了生成在运行时的纹理使用libgdx文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作电话的文字游戏。昨天,我决定用libgtx尝试和改进的图形性能和电池使用+瞄准更多的平台,切换到OpenGL。

方式字母画瓷砖是工作在2D画布是每个字母的瓷砖将创建一个位图本身。我想:


  1. 创建从背景位图可变的新位图。

  2. 画上的新位图的信。

  3. 申请其他瓷砖的具体影响。

  4. 绘制新位图的每一帧

什么是我实现我想用什么libgdx最好的方式?


  1. 我应该采取类似的做法?如果是这样,怎么样?我试着用像素图,但无法工作,如何绘制字体。

  2. 我应该建立从A-Z所需的所有瓷砖的spritesheet,只使用了吗?繁琐的有点,当我捏捏图块背景的设计。

  3. 我应该做的完全不同的东西?

显然,最终code不应该从文件每次加载,由于这是一个巨大的性能损失,但这里的工作样本code为别人谁是试图达到同样的效果。

  //后台加载到一个象素图
    像素图瓦=新的像素图(Gdx.files.getFileHandle(
            someFile.png,FileType.Internal));    //加载字体
    手柄的FileHandle = Gdx.files.getFileHandle(someFont.fnt
            FileType.Internal);
    BitmapFont字体=新BitmapFont(句柄);    //获取glypth信息
    BitmapFontData数据= font.getData();
    像素图fontPixmap =新的像素图(Gdx.files.internal(data.imagePaths [0]));
    字形字形= data.getGlyph(getLetterToDraw()的charAt(0));    //绘制字符到我们的基地像素图
    tile.drawPixmap(fontPixmap,(TILE_WIDTH - glyph.width)/ 2,(TILE_HEIGHT - glyph.height)/ 2,
            glyph.srcX,glyph.srcY,glyph.width,glyph.height);    //这个保存为一个新的纹理
    纹理=新的纹理(瓦);


解决方案

报价:您可以创建一个 BitmapFontData 实例,并使用 BitmapFontData.getImagePath( )来加载像素图包含字型。然后,您可以使用 BitmapFontData.getGlyph 来获取特定字形的位置像素图,您可以与使用 Pixmap.drawPixmap()做你想做的事情。来源: libgdx问题691:绘制BitmapFont至像素图/纹理


您获得路径 BitmapFontData 并创建文件句柄一个新的像素图与此路径:


  

像素图(的FileHandle文件)


  
  

创建从给定文件新的点阵图实例。


来源: PixmapAPI

使用 Gdx.files.internal(pathfromBitmapFontData)作为文件句柄,你得到了与内部的所有Glyps的像素图。

像素图P =新的像素图(Gdx.files.internal(myBitmapFontData.getImagePath()))

尝试一下如果一个内部! Meight不内部文件,我不知道它


像素图P =新的像素图(myFont.getData()getFontFile());

这也应该工作

I'm working on a phone word game. Yesterday I decided to switch to OpenGL using libgtx to try and improve graphics performance and battery usage + to target more platforms.

The way letter tile drawing was working on a 2D canvas was each letter tile would create a bitmap for itself. I would:

  1. Create a new mutable bitmap from the background bitmap.
  2. Draw on the letter on the new bitmap.
  3. Apply other tile specific effects.
  4. Draw the new bitmap for each frame

What's the best way for me to achieve what I want using libgdx?

  1. Should I adopt a similar approach? If so, how? I tried using pixmaps but couldn't work out how to draw the font.
  2. Should I create a spritesheet with all the required tiles from A-Z and just use that? A bit tedious when I tweak the design of the tile background.
  3. Should I do something totally different?

Answer

Obviously the final code shouldn't load from the files every time, as this is a massive performance hit, but here's working sample code for anyone else who is trying to achieve the same result.

    // load the background into a pixmap
    Pixmap tile = new Pixmap(Gdx.files.getFileHandle(
            "someFile.png", FileType.Internal));

    // load the font
    FileHandle handle = Gdx.files.getFileHandle("someFont.fnt",
            FileType.Internal);
    BitmapFont font = new BitmapFont(handle);

    // get the glypth info
    BitmapFontData data = font.getData();
    Pixmap fontPixmap = new Pixmap(Gdx.files.internal(data.imagePaths[0]));
    Glyph glyph = data.getGlyph(getLetterToDraw().charAt(0));

    // draw the character onto our base pixmap
    tile.drawPixmap(fontPixmap, (TILE_WIDTH - glyph.width) / 2, (TILE_HEIGHT - glyph.height) / 2,
            glyph.srcX, glyph.srcY, glyph.width, glyph.height);

    // save this as a new texture
    texture = new Texture(tile);

解决方案

Quote: You can create a BitmapFontData instance and use BitmapFontData.getImagePath() to load a Pixmap containing the glyphs. You can then use BitmapFontData.getGlyph to get the location of a specific glyph in the Pixmap, which you can use with Pixmap.drawPixmap() to do what you want to do. Source: libgdx Issue 691: Draw BitmapFont to Pixmap/Texture


You get the path to the BitmapFontData and create a new Pixmap with the Filehandle to the path with this:

Pixmap(FileHandle file)

Creates a new Pixmap instance from the given file.

Source: PixmapAPI

Use Gdx.files.internal(pathfromBitmapFontData)as filehandle and you got the pixmap with all Glyps inside.

Pixmap p = new Pixmap(Gdx.files.internal(myBitmapFontData.getImagePath()))

try out if its an internal! Meight be not an internal file i dont know it


Pixmap p = new Pixmap(myFont.getData().getFontFile());

this should also work

这篇关于生成在运行时的纹理使用libgdx文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆