Skip to content

Commit

Permalink
Implemented the view of Frames per Seconds, Changed the Mesh Class to…
Browse files Browse the repository at this point in the history
… an own class
  • Loading branch information
djschilling committed Feb 23, 2013
1 parent dc44212 commit 716ce2e
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 556 deletions.
Binary file added assets/Times New Roman.ttf
Binary file not shown.
Binary file added assets/font.ttf
Binary file not shown.
32 changes: 21 additions & 11 deletions src/com/pongo/towerdefense/TowerDefense.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.pongo.towerdefense;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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);

}

Expand All @@ -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();
Expand Down Expand Up @@ -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;
}



}
1 change: 0 additions & 1 deletion src/com/pongo/towerdefense/gl/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.view.MotionEvent;

import com.pongo.towerdefense.Input;
import com.pongo.towerdefense.TowerDefense;

public interface GameScreen {
Expand Down
191 changes: 105 additions & 86 deletions src/com/pongo/towerdefense/gl/Renderer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.pongo.towerdefense.gl;

import java.util.ArrayList;
import java.util.Vector;

import javax.microedition.khronos.opengles.GL10;

Expand All @@ -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<Mesh> blocks;
private Mesh linie;
private OwnMesh enemy;
private OwnMesh tower;
private ArrayList<OwnMesh> 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<Mesh>();
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<OwnMesh>();
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<Enemy> 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) {
Expand All @@ -135,26 +152,28 @@ private void renderEnemies(GL10 gl, ArrayList<Enemy> enemies) {

gl.glRotatef(90, 0, 0, 1);
}
enemy.render(PrimitiveType.Triangles);
enemy.render(GL10.GL_TRIANGLES);
gl.glPopMatrix();
}

}

private void renderTower(GL10 gl, Vector<Tower> towerList) {
private void renderTower(GL10 gl, ArrayList<Tower> 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();
}
}

public void dispose() {
enemy.dispose();
tower.dispose();
for (OwnMesh currenBlock : blocks) {
currenBlock.dispose();
}
font.dispose();
}
}
8 changes: 4 additions & 4 deletions src/com/pongo/towerdefense/model/GameField.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class GameField {
private ArrayList<Enemy> lostEnemies;
private ArrayList<Enemy> deadEnemies;
private boolean startEnemies;
private Vector<Tower> tower;
private ArrayList<Tower> tower;
public ArrayList<Tower> towerToBuild;
private float totalTime;
private float enemyCounter;
Expand All @@ -28,7 +28,7 @@ public GameField(ArrayList<Enemy> enemies, int width, int height) {
this.waitingEnemies = enemies;
this.walkingEnemies = new ArrayList<Enemy>();
this.lostEnemies = new ArrayList<Enemy>();
this.tower = new Vector<Tower>();
this.tower = new ArrayList<Tower>();
this.deadEnemies = new ArrayList<Enemy>();
this.totalTime = 0;
this.enemyCounter = 0;
Expand All @@ -50,7 +50,7 @@ public void startAction(float deltaTime) {
totalTime += deltaTime;
if (towerToBuild.size() > 0) {
tower.addAll(towerToBuild);
towerToBuild = new ArrayList<Tower>();
towerToBuild.clear();
}

moveEnemies(deltaTime);
Expand Down Expand Up @@ -104,7 +104,7 @@ public ArrayList<Enemy> getWalkingEnemies() {
return walkingEnemies;
}

public Vector<Tower> getTower() {
public ArrayList<Tower> getTower() {
// TODO Auto-generated method stub
return tower;
}
Expand Down
Loading

0 comments on commit 716ce2e

Please sign in to comment.