Android Open Source - aCARdeRUN Text Buffer






From Project

Back to project page aCARdeRUN.

License

The source code is released under:

Apache License

If you think the Android project aCARdeRUN listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright 2012 Dennis Grewe
 */*  ww w.j av  a  2  s . c  om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */
package de.hdm.mib.dg041.graphics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Map;

import javax.microedition.khronos.opengles.GL10;

import de.hdm.mib.dg041.graphics.SpriteFont.CharacterInfo;
import de.hdm.mib.dg041.graphics.VertexElement.VertexSemantic;

/**
 * @author dennis.grewe [dg041@hdm-stuttgart.de]
 * Created on 23.02.2012.
 */
public class TextBuffer
{
    // --------------------------------------------------------
    // PROPERTIES
    // --------------------------------------------------------

    private SpriteFont spriteFont;
    private Mesh mesh;

    // --------------------------------------------------------
    // CONSTRUCTOR
    // --------------------------------------------------------

    TextBuffer(GraphicDevice graphicsDevice, SpriteFont spriteFont, int capacity)
    {
      VertexElement[] elements = new VertexElement[] {
           new VertexElement(0, 16, GL10.GL_FLOAT, 2, VertexSemantic.VERTEX_ELEMENT_POSITION),
           new VertexElement(8, 16, GL10.GL_FLOAT, 2, VertexSemantic.VERTEX_ELEMENT_TEXCOORD)
      };

        // allocate byte buffer
        ByteBuffer data = ByteBuffer.allocateDirect(6 * 16 * capacity);
        data.order(ByteOrder.nativeOrder());

        // create a new buffer and fill up with elements
        VertexBuffer vertexBuffer = new VertexBuffer();
        vertexBuffer.setElements(elements);
        vertexBuffer.setBuffer(data);
        vertexBuffer.setNumVertices(0);

        this.spriteFont = spriteFont;
        this.mesh = new Mesh(vertexBuffer, GL10.GL_TRIANGLES);
    }

    // --------------------------------------------------------
    // METHODS
    // --------------------------------------------------------

    /**
     * @return the sprite font of the text buffer
     */
    public SpriteFont getSpriteFont()
    {
      return spriteFont;
    }

    /*
     * @return the mesh
     */
    public Mesh getMesh()
    {
      return mesh;
    }

    /**
     * @param text set the text to the text buffer
     */
    public void setText(String text)
    {
        Map<Character, SpriteFont.CharacterInfo> characterInfos = spriteFont.getCharacterInfos();
        Texture texture = spriteFont.getMaterial().getTexture();
        ByteBuffer data = mesh.getVertexBuffer().getBuffer();

        data.position(0);

        float x = 0;
        float y = 0;
        for (int index = 0; index < text.length(); ++index)
        {
            char c = text.charAt(index);

            CharacterInfo info = characterInfos.get(c);

            float posLeft =   x + info.offset.x;
            float posRight =   x + info.offset.x + info.area.width();
            float posTop =     y - info.offset.y;
            float posBottom =   y - (info.offset.y + info.area.height());
            float texLeft =   (float) info.area.left / (float) texture.getWidth();
            float texRight =   (float) info.area.right / (float) texture.getWidth();
            float texTop =     1.0f - (float) info.area.top   / (float) texture.getHeight();
            float texBottom =   1.0f - (float) info.area.bottom / (float) texture.getHeight();

            // triangle 1
            data.putFloat(posLeft);   data.putFloat(posTop);     data.putFloat(texLeft);   data.putFloat(texTop);
            data.putFloat(posLeft);   data.putFloat(posBottom);   data.putFloat(texLeft);   data.putFloat(texBottom);
            data.putFloat(posRight);   data.putFloat(posTop);     data.putFloat(texRight);   data.putFloat(texTop);

            // triangle 2
            data.putFloat(posRight);   data.putFloat(posTop);     data.putFloat(texRight);   data.putFloat(texTop);
            data.putFloat(posLeft);   data.putFloat(posBottom);   data.putFloat(texLeft);   data.putFloat(texBottom);
            data.putFloat(posRight);   data.putFloat(posBottom);   data.putFloat(texRight);   data.putFloat(texBottom);

            x += info.width;
        }

        data.position(0);
        mesh.getVertexBuffer().setNumVertices(6 * text.length());
    }
}




Java Source Code List

de.hdm.mib.dg041.acarderun.aCARdeRunActivity.java
de.hdm.mib.dg041.acarderun.aCARdeRunView.java
de.hdm.mib.dg041.collision.AxisAlignedBoundingBox.java
de.hdm.mib.dg041.collision.Circle.java
de.hdm.mib.dg041.collision.Point.java
de.hdm.mib.dg041.collision.Shape2D.java
de.hdm.mib.dg041.game.GameScreen.java
de.hdm.mib.dg041.game.Game.java
de.hdm.mib.dg041.game.HUD.java
de.hdm.mib.dg041.game.Hero.java
de.hdm.mib.dg041.game.InGameScreen.java
de.hdm.mib.dg041.game.MenuScreen.java
de.hdm.mib.dg041.game.Obstacle.java
de.hdm.mib.dg041.game.Ranking.java
de.hdm.mib.dg041.game.World.java
de.hdm.mib.dg041.game.aCARdeRunGame.java
de.hdm.mib.dg041.graphics.BlendFactor.java
de.hdm.mib.dg041.graphics.Camera.java
de.hdm.mib.dg041.graphics.CompareFunction.java
de.hdm.mib.dg041.graphics.GraphicDevice.java
de.hdm.mib.dg041.graphics.Material.java
de.hdm.mib.dg041.graphics.Mesh.java
de.hdm.mib.dg041.graphics.Renderer.java
de.hdm.mib.dg041.graphics.Side.java
de.hdm.mib.dg041.graphics.SpriteFont.java
de.hdm.mib.dg041.graphics.TextBuffer.java
de.hdm.mib.dg041.graphics.TextureBlendMode.java
de.hdm.mib.dg041.graphics.TextureFilter.java
de.hdm.mib.dg041.graphics.TextureWrapMode.java
de.hdm.mib.dg041.graphics.Texture.java
de.hdm.mib.dg041.graphics.VertexBuffer.java
de.hdm.mib.dg041.graphics.VertexElement.java
de.hdm.mib.dg041.input.InputEvent.java
de.hdm.mib.dg041.input.InputSystem.java
de.hdm.mib.dg041.math.MathHelper.java
de.hdm.mib.dg041.math.Matrix4x4.java
de.hdm.mib.dg041.math.Vector2.java
de.hdm.mib.dg041.math.Vector3.java
de.hdm.mib.dg041.math.Vector4.java