Search in sources :

Example 41 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class TideMapDirectLoaderTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, (w / h) * 10, 10);
    camera.update();
    cameraController = new OrthoCamController(camera);
    Gdx.input.setInputProcessor(cameraController);
    font = new BitmapFont();
    batch = new SpriteBatch();
    map = new TideMapLoader().load("data/maps/tide/Map01.tide");
    renderer = new OrthogonalTiledMapRenderer(map, 1f / 32f);
}
Also used : OrthogonalTiledMapRenderer(com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) OrthoCamController(com.badlogic.gdx.tests.utils.OrthoCamController) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) TideMapLoader(com.badlogic.gdx.maps.tiled.TideMapLoader) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 42 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class Box2DTest method create.

@Override
public void create() {
    // setup the camera. In Box2D we operate on a
    // meter scale, pixels won't do it. So we use
    // an orthographic camera with a viewport of
    // 48 meters in width and 32 meters in height.
    // We also position the camera so that it
    // looks at (0,16) (that's where the middle of the
    // screen will be located).
    camera = new OrthographicCamera(48, 32);
    camera.position.set(0, 15, 0);
    // create the debug renderer
    renderer = new Box2DDebugRenderer();
    // create the world
    world = new World(new Vector2(0, -10), true);
    // we also need an invisible zero size ground body
    // to which we can connect the mouse joint
    BodyDef bodyDef = new BodyDef();
    groundBody = world.createBody(bodyDef);
    // call abstract method to populate the world
    createWorld(world);
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
}
Also used : Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 43 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class VBOWithVAOPerformanceTest method create.

@Override
public void create() {
    if (Gdx.gl30 == null) {
        throw new GdxRuntimeException("GLES 3.0 profile required for this test");
    }
    String vertexShader = "attribute vec4 a_position;    \n" + "attribute vec4 a_color;\n" + "attribute vec2 a_texCoord0;\n" + "uniform mat4 u_worldView;\n" + "varying vec4 v_color;" + "varying vec2 v_texCoords;" + "void main()                  \n" + "{                            \n" + "   v_color = a_color; \n" + "   v_texCoords = a_texCoord0; \n" + "   gl_Position =  u_worldView * a_position;  \n" + "}                            \n";
    String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec4 v_color;\n" + "varying vec2 v_texCoords;\n" + "uniform sampler2D u_texture;\n" + "void main()                                  \n" + "{                                            \n" + "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" + "}";
    shader = new ShaderProgram(vertexShader, fragmentShader);
    if (shader.isCompiled() == false) {
        Gdx.app.log("ShaderTest", shader.getLog());
        Gdx.app.exit();
    }
    int numSprites = 1000;
    int maxIndices = numSprites * 6;
    int maxVertices = numSprites * 6;
    VertexAttribute[] vertexAttributes = new VertexAttribute[] { VertexAttribute.Position(), VertexAttribute.ColorUnpacked(), VertexAttribute.TexCoords(0) };
    VertexBufferObjectWithVAO newVBOWithVAO = new VertexBufferObjectWithVAO(false, maxVertices, vertexAttributes);
    OldVertexBufferObjectWithVAO oldVBOWithVAO = new OldVertexBufferObjectWithVAO(false, maxVertices, vertexAttributes);
    IndexBufferObjectSubData newIndices = new IndexBufferObjectSubData(false, maxIndices);
    IndexBufferObjectSubData oldIndices = new IndexBufferObjectSubData(false, maxIndices);
    newVBOWithVAOMesh = new Mesh(newVBOWithVAO, newIndices, false) {
    };
    oldVBOWithVAOMesh = new Mesh(oldVBOWithVAO, oldIndices, false) {
    };
    float[] vertexArray = new float[maxVertices * 9];
    int index = 0;
    int stride = 9 * 6;
    for (int i = 0; i < numSprites; i++) {
        addRandomSprite(vertexArray, index);
        index += stride;
    }
    short[] indexArray = new short[maxIndices];
    for (short i = 0; i < maxIndices; i++) {
        indexArray[i] = i;
    }
    newVBOWithVAOMesh.setVertices(vertexArray);
    newVBOWithVAOMesh.setIndices(indexArray);
    oldVBOWithVAOMesh.setVertices(vertexArray);
    oldVBOWithVAOMesh.setIndices(indexArray);
    texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
    batch = new SpriteBatch();
    bitmapFont = new BitmapFont();
    stringBuilder = new StringBuilder();
}
Also used : IndexBufferObjectSubData(com.badlogic.gdx.graphics.glutils.IndexBufferObjectSubData) StringBuilder(com.badlogic.gdx.utils.StringBuilder) Mesh(com.badlogic.gdx.graphics.Mesh) Texture(com.badlogic.gdx.graphics.Texture) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) ShaderProgram(com.badlogic.gdx.graphics.glutils.ShaderProgram) VertexBufferObjectWithVAO(com.badlogic.gdx.graphics.glutils.VertexBufferObjectWithVAO) VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 44 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class FreeTypeAtlasTest method render.

@Override
public void render() {
    Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    float x = 10;
    float y = Gdx.graphics.getHeight() - 10;
    int renderCalls = 0;
    // inside the Enum in order to reduce allocations in the render loop.
    for (FontStyle style : FontStyle.values()) {
        for (FontSize size : FontSize.values()) {
            BitmapFont fnt = getFont(style, size);
            fnt.draw(batch, style.name() + " " + size.size + "pt: The quick brown fox jumps over the lazy dog", x, y);
            y -= fnt.getLineHeight() + 10;
        }
        y -= 20;
    }
    BitmapFont font = getFont(FontStyle.Regular, FontSize.Medium);
    font.draw(batch, text, 10, font.getCapHeight() + 10);
    // draw all glyphs in background
    batch.setColor(1f, 1f, 1f, 0.15f);
    batch.draw(packer.getPages().first().getTexture(), 0, 0);
    batch.setColor(1f, 1f, 1f, 1f);
    batch.end();
}
Also used : BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 45 with BitmapFont

use of com.badlogic.gdx.graphics.g2d.BitmapFont in project libgdx by libgdx.

the class FreeTypePackTest method createFonts.

protected int createFonts() {
    // //////////////////////////////////////////////////////////////////////////////////////////////////////
    // //////Steps to use multiple FreeTypeFontGenerators with a single texture atlas://////////////////////
    // 1. Create a new PixmapPacker big enough to fit all your desired glyphs
    // 2. Create a new FreeTypeFontGenerator for each file (i.e. font styles/families)
    // 3. Pack the data by specifying the PixmapPacker parameter to generateData
    // Keep hold of the returned BitmapFontData for later
    // 4. Repeat for other sizes.
    // 5. Dispose the generator and repeat for other font styles/families
    // 6. Get the TextureRegion(s) from the packer using packer.updateTextureRegions()
    // 7. Dispose the PixmapPacker
    // 8. Use each BitmapFontData to construct a new BitmapFont, and specify your TextureRegion(s) to the font constructor
    // 9. Dispose of the Texture upon application exit or when you are done using the font atlas
    // //////////////////////////////////////////////////////////////////////////////////////////////////////
    // create the pixmap packer
    PixmapPacker packer = new PixmapPacker(FONT_ATLAS_WIDTH, FONT_ATLAS_HEIGHT, Format.RGBA8888, 2, false);
    // we need to load all the BitmapFontDatas before we can start loading BitmapFonts
    FontMap<BitmapFontData> dataMap = new FontMap<BitmapFontData>();
    // for each style...
    for (FontStyle style : FontStyle.values()) {
        // get the file for this style
        FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal(style.path));
        // For each size...
        for (FontSize size : FontSize.values()) {
            // pack the glyphs into the atlas using the default chars
            FreeTypeFontGenerator.FreeTypeFontParameter fontParameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
            fontParameter.size = size.size;
            fontParameter.packer = packer;
            fontParameter.characters = CHARACTERS;
            BitmapFontData data = gen.generateData(fontParameter);
            // store the info for later, when we generate the texture
            dataMap.get(style).put(size, data);
        }
        // dispose of the generator once we're finished with this family
        gen.dispose();
    }
    // Get regions from our packer
    regions = new Array<TextureRegion>();
    packer.updateTextureRegions(regions, TextureFilter.Nearest, TextureFilter.Nearest, false);
    // No more need for our CPU-based pixmap packer, as our textures are now on GPU
    packer.dispose();
    // Now we can create our fonts...
    fontMap = new FontMap<BitmapFont>();
    int fontCount = 0;
    // for each style...
    for (FontStyle style : FontStyle.values()) {
        // For each size...
        for (FontSize size : FontSize.values()) {
            // get the data for this style/size pair
            BitmapFontData data = dataMap.get(style).get(size);
            // create a BitmapFont from the data and shared texture
            BitmapFont bmFont = new BitmapFont(data, regions, INTEGER);
            // place the font into our map of loaded fonts
            fontMap.get(style).put(size, bmFont);
            fontCount++;
        }
    }
    // for the demo, show how many glyphs we loaded
    return fontCount * CHARACTERS.length();
}
Also used : BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) FreeTypeFontGenerator(com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator) PixmapPacker(com.badlogic.gdx.graphics.g2d.PixmapPacker) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Aggregations

BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)104 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)64 Texture (com.badlogic.gdx.graphics.Texture)31 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)21 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)18 Stage (com.badlogic.gdx.scenes.scene2d.Stage)16 OrthoCamController (com.badlogic.gdx.tests.utils.OrthoCamController)13 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)12 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)11 Label (com.badlogic.gdx.scenes.scene2d.ui.Label)10 Drawable (com.badlogic.gdx.scenes.scene2d.utils.Drawable)10 AssetManager (com.badlogic.gdx.assets.AssetManager)9 Color (com.badlogic.gdx.graphics.Color)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 InputAdapter (com.badlogic.gdx.InputAdapter)7 Actor (com.badlogic.gdx.scenes.scene2d.Actor)7 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)7 FileHandle (com.badlogic.gdx.files.FileHandle)6 Pixmap (com.badlogic.gdx.graphics.Pixmap)6 GlyphLayout (com.badlogic.gdx.graphics.g2d.GlyphLayout)6