diff --git a/assets/Times New Roman.ttf b/assets/Times New Roman.ttf new file mode 100644 index 0000000..d67e102 Binary files /dev/null and b/assets/Times New Roman.ttf differ diff --git a/assets/font.ttf b/assets/font.ttf new file mode 100644 index 0000000..9dce970 Binary files /dev/null and b/assets/font.ttf differ diff --git a/src/com/pongo/towerdefense/TowerDefense.java b/src/com/pongo/towerdefense/TowerDefense.java index 374752d..ce827a8 100644 --- a/src/com/pongo/towerdefense/TowerDefense.java +++ b/src/com/pongo/towerdefense/TowerDefense.java @@ -1,5 +1,7 @@ package com.pongo.towerdefense; +import java.util.ArrayList; + import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; @@ -17,7 +19,7 @@ import com.pongo.towerdefense.input.InputManager; import com.pongo.towerdefense.screens.GameLoop; -public class TowerDefense extends Activity implements Renderer{ +public class TowerDefense extends Activity implements Renderer { public GameScreen screen; private int frames = 0; @@ -32,7 +34,8 @@ public class TowerDefense extends Activity implements Renderer{ public InputManager inputManager; private GestureDetector gestureDedector; private GestureListener gestureListener; - + private float time = 0; + public int framesPerSecond = 0; @Override protected void onCreate(Bundle savedInstanceState) { @@ -48,8 +51,8 @@ protected void onCreate(Bundle savedInstanceState) { input = new Input(); gestureListener = new GestureListener(this, 0, 0); gestureDedector = new GestureDetector(this, gestureListener); - - //glSurface.setOnTouchListener(input); + + // glSurface.setOnTouchListener(input); } @@ -60,12 +63,20 @@ public void setup(GL10 gl) { } public void mainLoopItration(GL10 gl) { - //screen.input(this); + // screen.input(this); screen.update(this); screen.render(gl, this); + frames++; + time += deltaTime; + if (time > 1) { + framesPerSecond = frames; + frames = 0; + time = 0; + } } + @Override public void onDrawFrame(GL10 gl) { long currentFrameStart = System.nanoTime(); @@ -118,14 +129,13 @@ public void onResume() { super.onResume(); glSurface.onResume(); } + @Override public boolean onTouchEvent(MotionEvent event) { - if (gestureDedector.onTouchEvent(event)) - return true; - else - return false; + if (gestureDedector.onTouchEvent(event)) + return true; + else + return false; } - - } diff --git a/src/com/pongo/towerdefense/gl/GameScreen.java b/src/com/pongo/towerdefense/gl/GameScreen.java index cadae9c..4f76e0d 100644 --- a/src/com/pongo/towerdefense/gl/GameScreen.java +++ b/src/com/pongo/towerdefense/gl/GameScreen.java @@ -4,7 +4,6 @@ import android.view.MotionEvent; -import com.pongo.towerdefense.Input; import com.pongo.towerdefense.TowerDefense; public interface GameScreen { diff --git a/src/com/pongo/towerdefense/gl/Renderer.java b/src/com/pongo/towerdefense/gl/Renderer.java index b8215cf..36e34d3 100644 --- a/src/com/pongo/towerdefense/gl/Renderer.java +++ b/src/com/pongo/towerdefense/gl/Renderer.java @@ -1,7 +1,6 @@ package com.pongo.towerdefense.gl; import java.util.ArrayList; -import java.util.Vector; import javax.microedition.khronos.opengles.GL10; @@ -14,118 +13,136 @@ import com.pongo.towerdefense.model.GameField; import com.pongo.towerdefense.model.Richtung; import com.pongo.towerdefense.model.Tower; -import com.pongo.towerdefense.tools.Mesh; -import com.pongo.towerdefense.tools.Mesh.PrimitiveType; +import com.pongo.towerdefense.tools.Font; +import com.pongo.towerdefense.tools.Font.FontStyle; +import com.pongo.towerdefense.tools.Font.Text; +import com.pongo.towerdefense.tools.OwnMesh; public class Renderer { - private Mesh enemy; - private Mesh tower; - private ArrayList blocks; - private Mesh linie; + private OwnMesh enemy; + private OwnMesh tower; + private ArrayList blocks; + private Font font; + private Text framesPerSecond; + private InputManager inputManager; + private TowerDefense activity; private GameField field; - public Renderer(GL10 gl, TowerDefense activity, GameField field) { + public Renderer(GL10 gl, TowerDefense activity, GameField field, InputManager manager) { + this.inputManager = manager; + this.activity = activity; this.field = field; - enemy = new Mesh(gl, 3, true, false, false); - enemy.color(0, 1, 0, 1); - enemy.vertex(0, 0, 0); - enemy.color(0, 1, 0, 1); - enemy.vertex(50, 0, 0); - enemy.color(0, 1, 0, 1); - enemy.vertex(25, 50, 0); - - blocks = new ArrayList(); - for (Block actualBlock : field.blocks) { - blocks.add(new Mesh(gl, 4, true, false, false)); - int size = blocks.size(); - blocks.get(size - 1).color(0.5f, 0.2f, 0.05f, 1f); - blocks.get(size - 1).vertex(actualBlock.position.x, - actualBlock.position.y + actualBlock.height, 0); - blocks.get(size - 1).color(0.5f, 0.2f, 0.05f, 1f); - blocks.get(size - 1).vertex( - actualBlock.position.x + actualBlock.width, - actualBlock.position.y + actualBlock.height, 0); - blocks.get(size - 1).color(0.5f, 0.2f, 0.05f, 1f); - blocks.get(size - 1).vertex( - actualBlock.position.x + actualBlock.width, - actualBlock.position.y, 0); - blocks.get(size - 1).color(0.5f, 0.2f, 0.05f, 1f); - blocks.get(size - 1).vertex(actualBlock.position.x, - actualBlock.position.y, 0); - } - tower = new Mesh(gl, 4, true, false, false); - tower.color(1, 0, 0, 1); - tower.vertex(0, 0, 0); - tower.color(1, 0, 0, 1); - tower.vertex(10, 0, 0); - tower.color(1, 0, 0, 1); - tower.vertex(10, 10, 0); - tower.color(1, 0, 0, 1); - tower.vertex(0, 10, 0); - - - - // linie = new Mesh(gl, 2, true, false, false); - // linie.color(255, 140, 0, 1); - // linie.vertex(25, 25, 0); - // linie.color(255, 140, 0, 1); - // linie.vertex(700, 700, 0); - // linie.color(255, 140, 0, 1); - // linie.vertex(400, 400, 0); - // linie.color(255, 140, 0, 1); - // linie.vertex(150, 450, 0); + initializeEnemy(gl); + initializeBlocks(gl); + initializeTower(gl); + initalizeFont(gl); + + } - public void render(GL10 gl, TowerDefense activity, GameField field, - InputManager inputManager) { + public void render(GL10 gl) { + gl.glViewport(0, 0, activity.getWidth(), activity.getHeight()); gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + - gl.glMatrixMode(GL10.GL_PROJECTION); - gl.glLoadIdentity(); - GLU.gluOrtho2D(gl, inputManager.screenX, inputManager.screenX - + activity.getWidth(), inputManager.screenY, - inputManager.screenY + activity.getHeight()); - - // gl.glMatrixMode(GL10.GL_MODELVIEW); - // gl.glLoadIdentity(); - // - // GLU.gluLookAt(gl, inputManager.screenX, inputManager.screenY, 1, - // inputManager.screenX, inputManager.screenY, 0, 0, 1, 0); + set2DProjection(gl); + GLU.gluLookAt(gl, inputManager.screenX, inputManager.screenY, 1, inputManager.screenX, inputManager.screenY, 0, 0, 1, 0); + renderBlocks(gl); renderEnemies(gl, field.getWalkingEnemies()); renderTower(gl, field.getTower()); - // renderLinie(gl); + renderText(gl, activity.framesPerSecond); } - private void renderLinie(GL10 gl) { - gl.glLineWidth(10); - linie.render(PrimitiveType.LineStrip); + private void initalizeFont(GL10 gl) { + font = new Font(gl, activity.getAssets(), "Times New Roman.ttf", 50, FontStyle.Bold); + framesPerSecond = font.newText(gl); + } + + private void initializeTower(GL10 gl) { + tower = new OwnMesh(gl, 4, true, false); + tower.setVertex(0, 0, 0); + tower.setVertex(30, 0, 0); + tower.setVertex(30, 30, 0); + tower.setColor(1, 0, 0, 1); + tower.setVertex(0, 30, 0); + tower.setColor(1, 0, 0, 1); + tower.setColor(1, 0, 0, 1); + tower.setColor(1, 0, 0, 1); + } + + private void initializeBlocks(GL10 gl) { + blocks = new ArrayList(); + for (Block actualBlock : field.blocks) { + blocks.add(new OwnMesh(gl, 4, true, false)); + int size = blocks.size(); + blocks.get(size - 1).setVertex(actualBlock.position.x, actualBlock.position.y + actualBlock.height, 0); + blocks.get(size - 1).setVertex(actualBlock.position.x + actualBlock.width, actualBlock.position.y + actualBlock.height, 0); + blocks.get(size - 1).setVertex(actualBlock.position.x + actualBlock.width, actualBlock.position.y, 0); + blocks.get(size - 1).setVertex(actualBlock.position.x, actualBlock.position.y, 0); + blocks.get(size - 1).setColor(0.5f, 0.2f, 0.05f, 1f); + blocks.get(size - 1).setColor(0.5f, 0.2f, 0.05f, 1f); + blocks.get(size - 1).setColor(0.5f, 0.2f, 0.05f, 1f); + blocks.get(size - 1).setColor(0.5f, 0.2f, 0.05f, 1f); + } + } + + private void initializeEnemy(GL10 gl) { + gl.glClear(GL10.GL_COLOR_BUFFER_BIT); + enemy = new OwnMesh(gl, 3, true, false); + enemy.setVertex(0, 0, 0); + enemy.setVertex(50, 0, 0); + enemy.setVertex(25, 50, 0); + enemy.setColor(1, 0, 0, 1); + enemy.setColor(1, 0, 0, 1); + enemy.setColor(1, 0, 0, 1); + } + + private void set2DProjection(GL10 gl) { + gl.glMatrixMode(GL10.GL_PROJECTION); + gl.glLoadIdentity(); + GLU.gluOrtho2D(gl, 0, activity.getWidth(), 0, +activity.getHeight()); + + gl.glMatrixMode(GL10.GL_MODELVIEW); + gl.glLoadIdentity(); + } + + private void renderText(GL10 gl, int framesPerSecond) { + set2DProjection(gl); + gl.glEnable(GL10.GL_TEXTURE_2D); + gl.glEnable(GL10.GL_BLEND); + gl.glPushMatrix(); + gl.glTranslatef(activity.getWidth() - 100, activity.getHeight() - 30, 0); + + gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); + this.framesPerSecond.setText(String.valueOf(framesPerSecond)); + this.framesPerSecond.render(); + gl.glPopMatrix(); + gl.glDisable(GL10.GL_BLEND); + gl.glDisable(GL10.GL_TEXTURE_2D); } private void renderBlocks(GL10 gl) { - for (Mesh actualBlock : blocks) { - gl.glPushMatrix(); - actualBlock.render(PrimitiveType.TriangleFan); - gl.glPopMatrix(); + for (OwnMesh actualBlock : blocks) { + actualBlock.render(GL10.GL_TRIANGLE_FAN); } - + // blocks.get(0).render(PrimitiveType.TriangleFan); } private void renderEnemies(GL10 gl, ArrayList enemies) { for (Enemy actualEnemy : enemies) { gl.glPushMatrix(); - gl.glTranslatef(actualEnemy.actualPosition.x, - actualEnemy.actualPosition.y, actualEnemy.actualPosition.z); + gl.glTranslatef(actualEnemy.actualPosition.x, actualEnemy.actualPosition.y, actualEnemy.actualPosition.z); if (actualEnemy.richtung == Richtung.Osten) { gl.glRotatef(-90, 0, 0, 1); } else if (actualEnemy.richtung == Richtung.Sueden) { @@ -135,20 +152,18 @@ private void renderEnemies(GL10 gl, ArrayList enemies) { gl.glRotatef(90, 0, 0, 1); } - enemy.render(PrimitiveType.Triangles); + enemy.render(GL10.GL_TRIANGLES); gl.glPopMatrix(); } } - private void renderTower(GL10 gl, Vector towerList) { + private void renderTower(GL10 gl, ArrayList towerList) { - for (Tower actualTower : towerList) { gl.glPushMatrix(); - gl.glTranslatef(actualTower.position.x, actualTower.position.y, - actualTower.position.z); - tower.render(PrimitiveType.TriangleFan); + gl.glTranslatef(actualTower.position.x, actualTower.position.y, actualTower.position.z); + tower.render(GL10.GL_TRIANGLE_FAN); gl.glPopMatrix(); } } @@ -156,5 +171,9 @@ private void renderTower(GL10 gl, Vector towerList) { public void dispose() { enemy.dispose(); tower.dispose(); + for (OwnMesh currenBlock : blocks) { + currenBlock.dispose(); + } + font.dispose(); } } diff --git a/src/com/pongo/towerdefense/model/GameField.java b/src/com/pongo/towerdefense/model/GameField.java index 142a175..6ff2419 100644 --- a/src/com/pongo/towerdefense/model/GameField.java +++ b/src/com/pongo/towerdefense/model/GameField.java @@ -13,7 +13,7 @@ public class GameField { private ArrayList lostEnemies; private ArrayList deadEnemies; private boolean startEnemies; - private Vector tower; + private ArrayList tower; public ArrayList towerToBuild; private float totalTime; private float enemyCounter; @@ -28,7 +28,7 @@ public GameField(ArrayList enemies, int width, int height) { this.waitingEnemies = enemies; this.walkingEnemies = new ArrayList(); this.lostEnemies = new ArrayList(); - this.tower = new Vector(); + this.tower = new ArrayList(); this.deadEnemies = new ArrayList(); this.totalTime = 0; this.enemyCounter = 0; @@ -50,7 +50,7 @@ public void startAction(float deltaTime) { totalTime += deltaTime; if (towerToBuild.size() > 0) { tower.addAll(towerToBuild); - towerToBuild = new ArrayList(); + towerToBuild.clear(); } moveEnemies(deltaTime); @@ -104,7 +104,7 @@ public ArrayList getWalkingEnemies() { return walkingEnemies; } - public Vector getTower() { + public ArrayList getTower() { // TODO Auto-generated method stub return tower; } diff --git a/src/com/pongo/towerdefense/screens/GameLoop.java b/src/com/pongo/towerdefense/screens/GameLoop.java index 0cf5773..c44e5ab 100644 --- a/src/com/pongo/towerdefense/screens/GameLoop.java +++ b/src/com/pongo/towerdefense/screens/GameLoop.java @@ -29,9 +29,9 @@ public GameLoop(GL10 gl, TowerDefense activity) { initalizeField2(); field.startEnemies(); - inputManager = new InputManager(0, 0, field.width, field.height,field); - renderer = new Renderer(gl, activity, field); - + inputManager = new InputManager(0, 0, field.width, field.height, field); + renderer = new Renderer(gl, activity, field, inputManager); + } private void initalizeField() { @@ -53,13 +53,14 @@ private void initalizeField() { for (int i = 0; i < 4; i++) { enemies.add(new Panzer(route, Richtung.Osten)); } - + field = new GameField(enemies, 2000, 1200); field.addBlocks(0, 20, 0, 20); field.addBlocks(30, 100, 30, 100); field.addTower(new Aussichtsturm(new Vector(50, 400, 0))); field.addTower(new Tower1(new Vector(1000, 400, 0))); } + private void initalizeField2() { ArrayList route = new ArrayList(); route.add(new Vector(0, 650, 0)); @@ -79,13 +80,12 @@ private void initalizeField2() { route.add(new Vector(1750, 1600, 0)); route.add(new Vector(1250, 1600, 0)); route.add(new Vector(1250, 2000, 0)); - ArrayList enemies = new ArrayList(); for (int i = 0; i < 50; i++) { enemies.add(new Ninja(route, Richtung.Osten)); } - + field = new GameField(enemies, 2000, 2000); field.addBlocks(0, 600, 0, 200); field.addBlocks(0, 200, 200, 2000); @@ -104,26 +104,24 @@ private void initalizeField2() { field.addBlocks(1350, 1550, 1200, 1700); field.addBlocks(200, 1650, 1800, 2000); field.addBlocks(1650, 2000, 1300, 2000); -// field.addTower(new Aussichtsturm(new Vector(50, 400, 0))); -// field.addTower(new Tower1(new Vector(1000, 400, 0))); } public GameLoop(GL10 gl, TowerDefense activity, GameField field) { this.field = field; - renderer = new Renderer(gl, activity, field); + renderer = new Renderer(gl, activity, field, inputManager); } @Override public void update(TowerDefense activity) { - + field.startAction(activity.getDeltaTime()); } @Override public void render(GL10 gl, TowerDefense activity) { - renderer.render(gl, activity, field, inputManager); + renderer.render(gl); } @@ -139,9 +137,6 @@ public void dispose() { } - - - @Override public boolean inputTip(MotionEvent ev, TowerDefense activity) { return inputManager.tip(ev, activity); @@ -153,6 +148,4 @@ public boolean inputScroll(TowerDefense activity, int distanceX, return inputManager.scroll(activity, distanceX, distanceY); } - - } diff --git a/src/com/pongo/towerdefense/tools/Font.java b/src/com/pongo/towerdefense/tools/Font.java index bf442d4..382d2fb 100644 --- a/src/com/pongo/towerdefense/tools/Font.java +++ b/src/com/pongo/towerdefense/tools/Font.java @@ -5,18 +5,17 @@ import javax.microedition.khronos.opengles.GL10; -import com.pongo.towerdefense.tools.Mesh.PrimitiveType; -import com.pongo.towerdefense.tools.Texture.TextureFilter; -import com.pongo.towerdefense.tools.Texture.TextureWrap; - import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.Rect; -import android.graphics.Typeface; import android.graphics.Paint.FontMetrics; import android.graphics.Paint.Style; +import android.graphics.Rect; +import android.graphics.Typeface; + +import com.pongo.towerdefense.tools.Texture.TextureFilter; +import com.pongo.towerdefense.tools.Texture.TextureWrap; @@ -233,7 +232,7 @@ public Glyph( int advance, int width, int height, float u, float v, float uWidth public class Text { private GL10 gl; - private Mesh mesh; + private OwnMesh mesh; private String text = ""; private int width; private int height; @@ -291,11 +290,11 @@ public void setPosition( float x, float y ) private void rebuild( ) { if( mesh == null ) - mesh = new Mesh( gl, 6 * text.length(), false, true, false ); + mesh = new OwnMesh( gl, 6 * text.length(), false, true); - if( mesh.getMaximumVertices() / 6 < text.length() ) + if( mesh.getAddedVertex() / 6 < text.length() ) { - mesh = new Mesh( gl, 6 * text.length(), false, true, false ); + mesh = new OwnMesh( gl, 6 * text.length(), false, true); } mesh.reset(); @@ -325,18 +324,18 @@ private void rebuild( ) for( int j = 0; j < line.length(); j++ ) { Glyph glyph = getGlyph( line.charAt(j) ); - mesh.texCoord( glyph.u, glyph.v ); - mesh.vertex( posX + x, posY + y, 0 ); - mesh.texCoord( glyph.u + glyph.uWidth, glyph.v ); - mesh.vertex( posX + x + glyph.width, posY + y, 0 ); - mesh.texCoord( glyph.u + glyph.uWidth, glyph.v + glyph.vHeight ); - mesh.vertex( posX + x + glyph.width, posY + y - lineHeight, 0 ); - mesh.texCoord( glyph.u + glyph.uWidth, glyph.v + glyph.vHeight ); - mesh.vertex( posX + x + glyph.width, posY + y - lineHeight, 0 ); - mesh.texCoord( glyph.u, glyph.v + glyph.vHeight ); - mesh.vertex( posX + x, posY + y - lineHeight, 0 ); - mesh.texCoord( glyph.u, glyph.v ); - mesh.vertex( posX + x, y, 0 ); + mesh.setVertex( posX + x, posY + y, 0 ); + mesh.setVertex( posX + x + glyph.width, posY + y, 0 ); + mesh.setVertex( posX + x + glyph.width, posY + y - lineHeight, 0 ); + mesh.setVertex( posX + x + glyph.width, posY + y - lineHeight, 0 ); + mesh.setVertex( posX + x, posY + y - lineHeight, 0 ); + mesh.setVertex( posX + x, y, 0 ); + mesh.setTexture( glyph.u, glyph.v ); + mesh.setTexture( glyph.u + glyph.uWidth, glyph.v ); + mesh.setTexture( glyph.u + glyph.uWidth, glyph.v + glyph.vHeight ); + mesh.setTexture( glyph.u + glyph.uWidth, glyph.v + glyph.vHeight ); + mesh.setTexture( glyph.u, glyph.v + glyph.vHeight ); + mesh.setTexture( glyph.u, glyph.v ); x += glyph.advance; } } @@ -348,7 +347,7 @@ public void render( ) return; texture.bind(); - mesh.render(PrimitiveType.Triangles); + mesh.render(GL10.GL_TRIANGLES); } public void dispose( ) diff --git a/src/com/pongo/towerdefense/tools/Mesh.java b/src/com/pongo/towerdefense/tools/Mesh.java deleted file mode 100644 index 6702077..0000000 --- a/src/com/pongo/towerdefense/tools/Mesh.java +++ /dev/null @@ -1,415 +0,0 @@ -package com.pongo.towerdefense.tools; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.FloatBuffer; - -import javax.microedition.khronos.opengles.GL10; -import javax.microedition.khronos.opengles.GL11; - -/** - * A simple Mesh class that wraps OpenGL ES - * Vertex Arrays. Just instantiate it with - * the proper parameters then fill it with - * the color, texCoord, normal and vertex - * method. - * - * @author mzechner - * - */ -public final class Mesh -{ - public enum PrimitiveType - { - Points, - Lines, - Triangles, - LineStrip, - TriangleStrip, - TriangleFan - } - - /** The gl instance **/ - private GL10 gl; - - /** vertex position buffer and array **/ - private float vertices[]; - private int vertexHandle = -1; - private FloatBuffer vertexBuffer; - - /** color buffer and array **/ - private float colors[]; - private int colorHandle = -1; - private FloatBuffer colorBuffer; - - /** texture coordinate buffer and array **/ - private float texCoords[]; - private int texHandle = -1; - private FloatBuffer texCoordBuffer; - - /** normal buffer and array **/ - private float normals[]; - private int normalHandle = -1; - private FloatBuffer normalBuffer; - - /** vertex index at which the next vertex gets inserted **/ - private int index = 0; - - /** number of vertices defined for the mesh **/ - private int numVertices = 0; - - /** is the mesh dirty? **/ - private boolean dirty = true; - - /** last mesh **/ - private static Mesh lastMesh; - - /** renderer supports vbos **/ - public static boolean globalVBO = true; - - /** mesh count **/ - public static int meshes = 0; - - public Mesh( GL10 gl, int numVertices, boolean hasColors, boolean hasTextureCoordinates, boolean hasNormals ) - { - this.gl = gl; - vertices = new float[numVertices * 3]; - int[] buffer = new int[1]; - - if(!globalVBO ) - vertexBuffer = allocateBuffer( numVertices * 3 ); - else - { - ((GL11)gl).glGenBuffers(1, buffer, 0); - vertexHandle = buffer[0]; - vertexBuffer = FloatBuffer.wrap( vertices ); - } - - if( hasColors ) - { - colors = new float[numVertices * 4]; - if(!globalVBO ) - colorBuffer = allocateBuffer( numVertices * 4 ); - else - { - ((GL11)gl).glGenBuffers(1, buffer, 0); - colorHandle = buffer[0]; - colorBuffer = FloatBuffer.wrap( colors ); - } - } - - if( hasTextureCoordinates ) - { - texCoords = new float[numVertices * 2]; - if(!globalVBO ) - texCoordBuffer = allocateBuffer( numVertices * 2 ); - else - { - ((GL11)gl).glGenBuffers(1, buffer, 0); - texHandle = buffer[0]; - texCoordBuffer = FloatBuffer.wrap( texCoords ); - } - } - - if( hasNormals ) - { - normals = new float[numVertices * 3]; - if(!globalVBO ) - normalBuffer = allocateBuffer( numVertices * 3 ); - else - { - ((GL11)gl).glGenBuffers(1, buffer, 0); - normalHandle = buffer[0]; - normalBuffer = FloatBuffer.wrap( normals ); - } - } - } - - /** - * Allocates a direct FloatBuffer of the given size. - * Sets order to native - * @param size The size in number of floats - * @return The FloatBuffer - */ - private FloatBuffer allocateBuffer( int size ) - { - ByteBuffer buffer = ByteBuffer.allocateDirect( size * 4 ); - buffer.order(ByteOrder.nativeOrder()); - return buffer.asFloatBuffer(); - } - - /** - * updates the direct buffers in case the user - */ - private void update( ) - { - if( !globalVBO ) - { - vertexBuffer.put(vertices); - vertexBuffer.position(0); - - if( colors != null ) - { - colorBuffer.put( colors ); - colorBuffer.position(0); - } - - if( texCoords != null ) - { - texCoordBuffer.put( texCoords ); - texCoordBuffer.position(0); - } - - if( normals != null ) - { - normalBuffer.put( normals ); - normalBuffer.position(0); - } - } - else - { - GL11 gl = (GL11)this.gl; - - gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, vertexHandle ); - gl.glBufferData( GL11.GL_ARRAY_BUFFER, vertices.length * 4, vertexBuffer, GL11.GL_DYNAMIC_DRAW); - - if( colors != null ) - { - gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, colorHandle ); - gl.glBufferData( GL11.GL_ARRAY_BUFFER, colors.length * 4, colorBuffer, GL11.GL_DYNAMIC_DRAW); - } - - if( normals != null ) - { - gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, normalHandle ); - gl.glBufferData( GL11.GL_ARRAY_BUFFER, normals.length * 4, normalBuffer, GL11.GL_DYNAMIC_DRAW); - } - - if( texCoords != null ) - { - gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, texHandle ); - gl.glBufferData( GL11.GL_ARRAY_BUFFER, texCoords.length * 4, texCoordBuffer, GL11.GL_DYNAMIC_DRAW); - } - - gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, 0 ); - } - - numVertices = index; - index = 0; - dirty = false; - } - - /** - * Returns the OpenGL constant for the given - * primitive type - * @param type the type - * @return the OpenGL constant - */ - private int getPrimitiveType( PrimitiveType type ) - { - if( type == PrimitiveType.Lines ) - return GL10.GL_LINES; - else - if( type == PrimitiveType.Triangles ) - return GL10.GL_TRIANGLES; - else - if( type == PrimitiveType.LineStrip) - return GL10.GL_LINE_STRIP; - else - if( type == PrimitiveType.TriangleStrip) - return GL10.GL_TRIANGLE_STRIP; - else - if( type == PrimitiveType.Points ) - return GL10.GL_POINTS; - else - return GL10.GL_TRIANGLE_FAN; - - } - - /** - * Renders the mesh as the given type, starting at offset using - * numVertices vertices. - * @param type the type - * @param offset the offset, in number of vertices - * @param numVertices the number of vertices to use - */ - public void render( PrimitiveType type, int offset, int numVertices ) - { - boolean wasDirty = dirty; - if( dirty ) - update(); - - if( this == lastMesh && !wasDirty ) - { - gl.glDrawArrays( getPrimitiveType( type ), offset, numVertices ); - return; - } - else - { - gl.glDisableClientState(GL10.GL_VERTEX_ARRAY ); - gl.glDisableClientState(GL10.GL_COLOR_ARRAY ); - gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY ); - gl.glDisableClientState(GL10.GL_NORMAL_ARRAY ); - } - - gl.glEnableClientState( GL10.GL_VERTEX_ARRAY ); - if( globalVBO ) - { - ((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, vertexHandle ); - ((GL11)gl).glVertexPointer( 3, GL10.GL_FLOAT, 0, 0 ); - } - else - gl.glVertexPointer( 3, GL10.GL_FLOAT, 0, vertexBuffer ); - - if( colors != null ) - { - gl.glEnableClientState( GL10.GL_COLOR_ARRAY ); - if( globalVBO ) - { - ((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, colorHandle ); - ((GL11)gl).glColorPointer( 4, GL10.GL_FLOAT, 0, 0 ); - } - else - gl.glColorPointer( 4, GL10.GL_FLOAT, 0, colorBuffer ); - } - - if( texCoords != null ) - { - gl.glEnableClientState( GL10.GL_TEXTURE_COORD_ARRAY ); - if( globalVBO ) - { - ((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, texHandle ); - ((GL11)gl).glTexCoordPointer( 2, GL10.GL_FLOAT, 0, 0 ); - } - else - gl.glTexCoordPointer( 2, GL10.GL_FLOAT, 0, texCoordBuffer ); - } - - if( normals != null ) - { - gl.glEnableClientState( GL10.GL_NORMAL_ARRAY ); - if( globalVBO ) - { - ((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, normalHandle ); - ((GL11)gl).glNormalPointer( GL10.GL_FLOAT, 0, 0 ); - } - else - gl.glNormalPointer( GL10.GL_FLOAT, 0, normalBuffer ); - } - - gl.glDrawArrays( getPrimitiveType( type ), offset, numVertices ); - lastMesh = this; - } - - /** - * Renders the mesh as the given type using as many vertices as have - * been defined by calling vertex(). - * @param type the type - */ - public void render( PrimitiveType type ) - { - render( type, 0, numVertices ); - } - - /** - * Defines the position of the current vertex. Before - * you call this you have to call any other method like - * color, normal and texCoord for the current vertex! - * - * @param x the x coordinate - * @param y the y coordinate - * @param z the z coordinate - */ - public void vertex( float x, float y, float z ) - { - dirty = true; - int offset = index * 3; - vertices[offset] = x; - vertices[offset+1] = y; - vertices[offset+2] = z; - index++; - } - - /** - * Sets the color of the current vertex - * - * @param r the red component - * @param g the green component - * @param b the blue component - * @param a the alpha component - */ - public void color( float r, float g, float b, float a ) - { - dirty = true; - int offset = index * 4; - colors[offset] = r; - colors[offset+1] = g; - colors[offset+2] = b; - colors[offset+3] = a; - } - - /** - * Sets the normal of the current vertex - * @param x the x component - * @param y the y component - * @param z the z component - */ - public void normal( float x, float y, float z ) - { - dirty = true; - int offset = index * 3; - normals[offset] = x; - normals[offset+1] = y; - normals[offset+2] = z; - } - - /** - * Sets the texture coordinates of the current vertex - * @param s the s coordinate - * @param t the t coordinate - */ - public void texCoord( float s, float t ) - { - dirty = true; - int offset = index * 2; - texCoords[offset] = s; - texCoords[offset+1] = t; - } - - public int getMaximumVertices() - { - return vertices.length / 3; - } - - public void reset( ) - { - dirty = true; - index = 0; - } - - public void dispose( ) - { - if( globalVBO ) - { - GL11 gl = (GL11)this.gl; - if( vertexHandle != -1 ) - gl.glDeleteBuffers(1, new int[] { vertexHandle }, 0); - if( colorHandle != -1 ) - gl.glDeleteBuffers(1, new int[] { colorHandle }, 0); - if( normalHandle != -1 ) - gl.glDeleteBuffers(1, new int[] { normalHandle }, 0); - if( texHandle != -1 ) - gl.glDeleteBuffers(1, new int[] { texHandle }, 0); - } - - vertices = null; - vertexBuffer = null; - colors = null; - colorBuffer = null; - normals = null; - normalBuffer = null; - texCoords = null; - texCoordBuffer = null; - meshes--; - } -} diff --git a/src/com/pongo/towerdefense/tools/OwnMesh.java b/src/com/pongo/towerdefense/tools/OwnMesh.java new file mode 100644 index 0000000..cfc5202 --- /dev/null +++ b/src/com/pongo/towerdefense/tools/OwnMesh.java @@ -0,0 +1,138 @@ +package com.pongo.towerdefense.tools; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.FloatBuffer; + +import javax.microedition.khronos.opengles.GL10; + +public class OwnMesh { + + private GL10 gl; + private int numberOfVertex; + private ByteBuffer buffer1; + private ByteBuffer buffer2; + private ByteBuffer buffer3; + + private FloatBuffer vertices; + private FloatBuffer colors; + private FloatBuffer textures; + + private boolean color; + private boolean texture; + + private int addedVertex; + private int addedColors; + private int addedTextures; + + private OwnMesh lastMesh; + + private final int SIZE_OF_FLOAT = 4; + + public OwnMesh(GL10 gl, int numberOfVertex, boolean color, boolean texture) { + this.gl = gl; + this.color = color; + this.texture = texture; + this.numberOfVertex = numberOfVertex; + this.addedVertex = this.addedColors = this.addedTextures = 0; + + initializeBuffer(); + + } + + private void initializeBuffer() { + this.buffer1 = ByteBuffer.allocateDirect(numberOfVertex * 3 * SIZE_OF_FLOAT); + buffer1.order(ByteOrder.nativeOrder()); + vertices = buffer1.asFloatBuffer(); + + if (color) { + this.buffer2 = ByteBuffer.allocateDirect(numberOfVertex * 4 * SIZE_OF_FLOAT); + buffer2.order(ByteOrder.nativeOrder()); + + colors = buffer2.asFloatBuffer(); + } + if (texture) { + this.buffer3 = ByteBuffer.allocateDirect(numberOfVertex * 2 * SIZE_OF_FLOAT); + buffer3.order(ByteOrder.nativeOrder()); + textures = buffer3.asFloatBuffer(); + } + } + + public void setVertex(float x, float y, float z) { + vertices.put(x); + vertices.put(y); + vertices.put(z); + addedVertex++; + if (addedVertex >= numberOfVertex) { + vertices.rewind(); + + + } + } + + public void setColor(float r, float g, float b, float alpha) { + colors.put(r); + colors.put(g); + colors.put(b); + colors.put(alpha); + addedColors++; + if (addedColors >= numberOfVertex) { + colors.rewind(); + + } + } + + public void setTexture(float s, float t) { + textures.put(s); + textures.put(t); + addedTextures++; + if (addedTextures >= numberOfVertex) { + textures.rewind(); + + } + } + + public void render(int drawMode) { + if (this == lastMesh) { + gl.glDrawArrays(drawMode, 0, numberOfVertex); + } else { + + gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); + gl.glDisableClientState(GL10.GL_COLOR_ARRAY); + gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); + gl.glDisableClientState(GL10.GL_NORMAL_ARRAY); + } + + gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); + gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices); + if (color) { + gl.glEnableClientState(GL10.GL_COLOR_ARRAY); + gl.glColorPointer(4, GL10.GL_FLOAT, 0, colors); + } + if (texture) { + gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); + gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textures); + } + gl.glDrawArrays(drawMode, 0, numberOfVertex); + } + + public void dispose() { + buffer1 = null; + buffer2 = null; + buffer3 = null; + vertices = null; + colors = null; + textures = null; + + } + + public int getAddedVertex() { + return numberOfVertex; + } + + public void reset() { + addedVertex = addedColors = addedTextures = 0; + initializeBuffer(); + } + +}