Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: PandaMoniumHUN (posted 2014-01-14 12:18:28, viewed 46 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106  
package com.pandadev.ogame.fonts;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector2f;

import com.pandadev.ogame.MatrixStack;
import com.pandadev.ogame.textures.Texture;

public class FontText {
   public FontText(Font font, String text, Vector2f position){
      this.fontTexture = font.getTexture();
      //Generate text using the font information
      Vector2f cursor = new Vector2f(position);
      FontCharacter lastCharacter = null;
      char[] textCharacters = text.toCharArray();
      FloatBuffer vertexData = BufferUtils.createFloatBuffer(textCharacters.length*16);
      for(char textCharacter : textCharacters){
         //Convert the character from ASCII value to FontCharacter object
         FontCharacter character = font.getCharacter(Character.toString(textCharacter));
         if(character == null){
            character = font.getCharacter(Character.toString('?'));
         }
         Vector2f charPos = new Vector2f(cursor.x+character.getOffsetX(), cursor.y+character.getOffsetY());
         //Check and apply kerning if possible
         if(lastCharacter != null){
            FontKerning kerning = font.getKerning(lastCharacter.getId(), character.getId());
            if(kerning != null)
               charPos.x += kerning.getValue();
         }
         //Calculating vertices and uploading to the buffer
         float[] vertices = {
            charPos.x, charPos.y, //Vertex #0
            character.getTextureX(), character.getTextureEndY(), //TexCoord #0
            charPos.x+character.getWidth(), charPos.y, //Vertex #1
            character.getTextureEndX(), character.getTextureEndY(), //TexCoord #1
            charPos.x+character.getWidth(), charPos.y+character.getHeight(), //Vertex #2
            character.getTextureEndX(), character.getTextureY(), //TexCoord #2
            charPos.x, charPos.y+character.getHeight(),  //Vertex #3
            character.getTextureX(), character.getTextureY() //TexCoord #3
         };
         vertexData.put(vertices);
         cursor.x += character.getAdvanceX();
         lastCharacter = character;
      }
      //Generate indices
      this.index_count = textCharacters.length*6;
      ShortBuffer indexData = BufferUtils.createShortBuffer(index_count);
      short counter = 0;
      for(int i = 0; i < textCharacters.length; i++){
         short[] indices = {
            counter++, counter++, counter++,
            ((short)(counter-3)), ((short)(counter-1)), counter++
         };
         indexData.put(indices);
      }
      vertexData.flip();
      indexData.flip();
      //Upload data to the GPU
      this.vao = GL30.glGenVertexArrays();
      this.vbo = GL15.glGenBuffers();
      this.ibo = GL15.glGenBuffers();
      GL30.glBindVertexArray(vao);
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexData, GL15.GL_STATIC_DRAW);
         GL20.glEnableVertexAttribArray(0);
         GL20.glEnableVertexAttribArray(1);
         GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 16, 0L);
         GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 16, 8L);
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
         GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indexData, GL15.GL_STATIC_DRAW);
      GL30.glBindVertexArray(0);
   }
   private final int vao;
   private final int vbo, ibo;
   private final int index_count;
   private final Matrix4f textureMatrix = new Matrix4f();
   private final Texture fontTexture;
   
   public Texture getFontTexture(){
      return fontTexture;
   }
   
   public void render(){
      MatrixStack.getTextureMatrix().load(textureMatrix);
      fontTexture.bind();
      GL30.glBindVertexArray(vao);
         GL11.glDrawElements(GL11.GL_TRIANGLES, index_count, GL11.GL_UNSIGNED_SHORT, 0L);
      GL30.glBindVertexArray(0);
      fontTexture.unbind();
   }
   
   public void destroy(){
      GL30.glDeleteVertexArrays(vao);
      GL15.glDeleteBuffers(vbo);
      GL15.glDeleteBuffers(ibo);
   }
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (26 views)
2014-04-15 18:08:23

BurntPizza (21 views)
2014-04-15 03:46:01

UprightPath (37 views)
2014-04-14 17:39:50

UprightPath (19 views)
2014-04-14 17:35:47

Porlus (35 views)
2014-04-14 15:48:38

tom_mai78101 (61 views)
2014-04-10 04:04:31

BurntPizza (119 views)
2014-04-08 23:06:04

tom_mai78101 (219 views)
2014-04-05 13:34:39

trollwarrior1 (186 views)
2014-04-04 12:06:45

CJLetsGame (193 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!