Android Open Source - clash_of_balls Font2 D






From Project

Back to project page clash_of_balls.

License

The source code is released under:

GNU General Public License

If you think the Android project clash_of_balls 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 (C) 2012-2013 Hans Hardmeier <hanshardmeier@gmail.com>
 * Copyright (C) 2012-2013 Andrin Jenal/*w  w  w . j a  v  a2s .c om*/
 * Copyright (C) 2012-2013 Beat Kng <beat-kueng@gmx.net>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
package com.sapos_aplastados.game.clash_of_balls;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import com.sapos_aplastados.game.clash_of_balls.game.IDrawable;
import com.sapos_aplastados.game.clash_of_balls.game.RenderHelper;
import com.sapos_aplastados.game.clash_of_balls.game.Vector;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.opengl.GLES20;
import android.util.Log;

/**
 * Font2D
 * 2D OpenGL font
 *
 */
public class Font2D implements IDrawable {

  private static final String LOG_TAG = "Font2D";
  
  private Texture m_texture;
  private Vector m_text_field_size;
  
  private Typeface m_typeface;
  private String m_string;
  private int m_font_size;
  private TextAlign m_align;
  private float m_color_arr[] = new float[4]; //RGBA
  private int m_color; //ARGB
  
  private float m_font_height;
  private float m_x_offset;
  private float m_y_offset;

  private TextureManager m_texture_manager;
  private VertexBufferFloat m_position_data;
  
  private static ArrayList<WeakReference<Font2D>> m_weakFont2D
    = new ArrayList<WeakReference<Font2D>>();
  private WeakReference<Font2D> m_reference;
  
  public static void resetFonts() {
    m_weakFont2D = new ArrayList<WeakReference<Font2D>>();
  }
  
  /**
   * Creates 2D font
   * 
   * font_size:  Size of the font
   * typeface:   Typeface
   * 
   */
  public enum TextAlign {
    LEFT, RIGHT, CENTER
  }
  
  public Font2D(TextureManager texture_manager, Vector text_field_size, 
      Font2DSettings font_settings, int font_size) {
    
    m_texture_manager = texture_manager;
    m_position_data = new VertexBufferFloat(VertexBufferFloat.sprite_position_data, 3);
    
    m_text_field_size = text_field_size;
    m_font_size = font_size;
    m_typeface = font_settings.m_typeface;
    m_align = font_settings.m_align;
    m_color = font_settings.m_color;
    RenderHelper.initColorArray(0xffffffff, m_color_arr);

    m_reference = new WeakReference<Font2D>(this);
    m_weakFont2D.add(m_reference);
    
    Log.v(LOG_TAG, "Font succesfully created");
  }
  
  private void doInit(String string) {
    
    m_string = string;
    m_font_height = 0;
    m_x_offset = 0;
    m_y_offset = 0;
    
    if(m_texture != null) {
      int[] textureHandle = new int[1];
      textureHandle[0] = m_texture.textureHandle();

      GLES20.glDeleteTextures(1, textureHandle, 0);
    }
    
    Bitmap bitmap = createFontBitmap();
    m_texture = m_texture_manager.get(bitmap, false);
    bitmap.recycle();
    
  }
  
  public void setColor(float[] color) {
    for(int i=0; i<4; ++i) m_color_arr[i] = color[i];
  }
  public float[] getColor() { return m_color_arr; }
  
  private Paint.Align getAlignment(TextAlign align) {
    switch(align) {
      case LEFT: 
        return Paint.Align.LEFT;
      
      case CENTER: 
        m_x_offset = m_text_field_size.x / 2;
        return Paint.Align.CENTER;
      
      case RIGHT:
        m_x_offset = m_text_field_size.x;
        return Paint.Align.RIGHT;
        
      default:
        return Paint.Align.LEFT;
    }
  }
  
  private Bitmap createFontBitmap() {

    // Create text paint to customize font
    Paint textPaint = new Paint();

    textPaint.setTypeface(m_typeface);
    textPaint.setTextSize(m_font_size);
    textPaint.setAntiAlias(true);
    textPaint.setColor(m_color);
    
    // Alignment
    textPaint.setTextAlign(getAlignment(m_align));
      
    // Get font metrics
      Paint.FontMetrics fm = textPaint.getFontMetrics();
      m_font_height = Math.round( Math.abs(fm.bottom) + Math.abs(fm.top));
    m_y_offset = (m_text_field_size.y - m_font_height) / 2 + Math.abs(fm.top);

    
    // Create an empty, mutable bitmap
    Bitmap bitmap = Bitmap.createBitmap((int)Math.round(m_text_field_size.x), (int)Math.abs(m_text_field_size.y), Bitmap.Config.ARGB_8888);
    // Get a canvas to paint over the bitmap
    Canvas canvas = new Canvas(bitmap);
    bitmap.eraseColor(0);
    
    // Draw the text centered
    if(m_string!=null)
      canvas.drawText(m_string, m_x_offset, m_y_offset, textPaint);
        
    return bitmap;
  }
  
  public void setString(String string) {
    doInit(string);
  }
  public String getString() { return m_string; }
  
  public static void reloadFonts() {
    Font2D tmpFont2D;
    for (WeakReference<Font2D> wr : m_weakFont2D) {
      tmpFont2D = wr.get();
      if(tmpFont2D != null) {
        Bitmap b = tmpFont2D.createFontBitmap();
        tmpFont2D.m_texture = tmpFont2D.m_texture_manager.get(b, false);
        b.recycle();
      }
    }
  }

  public void draw(RenderHelper renderer) {
    
    if(m_texture == null) return; //this happens if string is not set
    
    renderer.shaderManager().activateTexture(0);
    m_texture.useTexture(renderer);
    
    // position
    int position_handle = renderer.shaderManager().a_Position_handle;
    if(position_handle != -1)
      m_position_data.apply(position_handle);
    
    // color
    int color_handle = renderer.shaderManager().u_Color_handle;
    if(color_handle != -1)
      GLES20.glUniform4fv(color_handle, 1, m_color_arr, 0);
    
    renderer.apply();
    
        // Draw
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
  }
  
  protected void finalize() {
    //remove this object from the object list
    for(int i=0; i<m_weakFont2D.size(); ++i) {
      if(m_weakFont2D.get(i) == m_reference) {
        m_weakFont2D.remove(i);
        return;
      }
    }
    if(m_texture != null) {
      int[] textureHandle = new int[1];
      textureHandle[0] = m_texture.textureHandle();

      GLES20.glDeleteTextures(1, textureHandle, 0);
    }
  }
  
  
  
  /**
   * Font2DSetting
   * Settings for Font2D
   *
   */
  public static class Font2DSettings {
    
    public Typeface m_typeface;
    public int m_color;
    public TextAlign m_align;
    
    public Font2DSettings(Typeface typeface, TextAlign align, int color) {
      
      m_typeface = typeface;
      m_align = align;
      m_color = color;
    }
  }
}




Java Source Code List

au.com.bytecode.opencsv.CSVParser.java
au.com.bytecode.opencsv.CSVReader.java
au.com.bytecode.opencsv.CSVWriter.java
au.com.bytecode.opencsv.ResultSetHelperService.java
au.com.bytecode.opencsv.ResultSetHelper.java
com.sapos_aplastados.game.clash_of_balls.FPSCounter.java
com.sapos_aplastados.game.clash_of_balls.Font2D.java
com.sapos_aplastados.game.clash_of_balls.FontNumbers.java
com.sapos_aplastados.game.clash_of_balls.GameLevel.java
com.sapos_aplastados.game.clash_of_balls.GameRenderer.java
com.sapos_aplastados.game.clash_of_balls.GameSettings.java
com.sapos_aplastados.game.clash_of_balls.ITouchInput.java
com.sapos_aplastados.game.clash_of_balls.LevelManager.java
com.sapos_aplastados.game.clash_of_balls.MainActivity.java
com.sapos_aplastados.game.clash_of_balls.Maths.java
com.sapos_aplastados.game.clash_of_balls.NetworkService.java
com.sapos_aplastados.game.clash_of_balls.ShaderManager.java
com.sapos_aplastados.game.clash_of_balls.TextureBase.java
com.sapos_aplastados.game.clash_of_balls.TextureManager.java
com.sapos_aplastados.game.clash_of_balls.Texture.java
com.sapos_aplastados.game.clash_of_balls.UIBase.java
com.sapos_aplastados.game.clash_of_balls.UIHandler.java
com.sapos_aplastados.game.clash_of_balls.VertexBufferFloat.java
com.sapos_aplastados.game.clash_of_balls.game.DynamicGameObject.java
com.sapos_aplastados.game.clash_of_balls.game.GameBase.java
com.sapos_aplastados.game.clash_of_balls.game.GameField.java
com.sapos_aplastados.game.clash_of_balls.game.GameHole.java
com.sapos_aplastados.game.clash_of_balls.game.GameItem.java
com.sapos_aplastados.game.clash_of_balls.game.GameMenuInGame.java
com.sapos_aplastados.game.clash_of_balls.game.GameObject.java
com.sapos_aplastados.game.clash_of_balls.game.GamePlayer.java
com.sapos_aplastados.game.clash_of_balls.game.GameServer.java
com.sapos_aplastados.game.clash_of_balls.game.GameStatistics.java
com.sapos_aplastados.game.clash_of_balls.game.GameView.java
com.sapos_aplastados.game.clash_of_balls.game.GameWall.java
com.sapos_aplastados.game.clash_of_balls.game.Game.java
com.sapos_aplastados.game.clash_of_balls.game.IDrawable.java
com.sapos_aplastados.game.clash_of_balls.game.IMoveable.java
com.sapos_aplastados.game.clash_of_balls.game.RenderHelper.java
com.sapos_aplastados.game.clash_of_balls.game.SensorThread.java
com.sapos_aplastados.game.clash_of_balls.game.StaticGameObjectNoInteraction.java
com.sapos_aplastados.game.clash_of_balls.game.StaticGameObject.java
com.sapos_aplastados.game.clash_of_balls.game.Vector3D.java
com.sapos_aplastados.game.clash_of_balls.game.Vector.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameEnd.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameInfo.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameStartNow.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventImpact.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemAdded.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemRemoved.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemUpdate.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventPool.java
com.sapos_aplastados.game.clash_of_balls.game.event.Event.java
com.sapos_aplastados.game.clash_of_balls.helper.RawResourceReader.java
com.sapos_aplastados.game.clash_of_balls.helper.ShaderHelper.java
com.sapos_aplastados.game.clash_of_balls.helper.TextureHelper.java
com.sapos_aplastados.game.clash_of_balls.menu.CreationMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.GameMenuBase.java
com.sapos_aplastados.game.clash_of_balls.menu.HelpMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.JoinMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.MainMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuBackground.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemArrow.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemButton.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemGreyButton.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemImg.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemKeyboard.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemLevel.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemList.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemResultEntry.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemStringMultiline.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemString.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItem.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupBase.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupCredit.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupGameStart.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupMsg.java
com.sapos_aplastados.game.clash_of_balls.menu.ResultsMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.WaitMenu.java
com.sapos_aplastados.game.clash_of_balls.network.AlljoynInterface.java
com.sapos_aplastados.game.clash_of_balls.network.NetworkClient.java
com.sapos_aplastados.game.clash_of_balls.network.NetworkServer.java
com.sapos_aplastados.game.clash_of_balls.network.Networking.java