Skip to content

Commit

Permalink
OpenGL Optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
gamblore committed Dec 2, 2012
1 parent b53d688 commit a60f255
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/net/androidpunk/Engine.java
Expand Up @@ -368,7 +368,7 @@ public void render() {

// render loop
//FP.screen.swap();
Draw.resetTarget();
//Draw.resetTarget();
//FP.screen.refresh();
World world = FP.getWorld();
if (world.visible)
Expand Down
8 changes: 4 additions & 4 deletions src/net/androidpunk/Entity.java
Expand Up @@ -220,8 +220,8 @@ public Entity collideTypes(Vector<String> types, int x, int y) {
if (mWorld == null)
return null;
Entity e;
for (String type : types) {
if ((e = collide(type, x, y)) != null)
for (int i = 0; i < types.size(); i++) {
if ((e = collide(types.get(i), x, y)) != null)
return e;
}
return null;
Expand Down Expand Up @@ -385,8 +385,8 @@ public void collideInto(String type, int x, int y, Vector<Entity> array) {
public void collideTypesInto(Vector<String> types, int x, int y, Vector<Entity> array) {
if (mWorld == null)
return;
for (String type : types) {
collideInto(type, x, y, array);
for (int i = 0; i < types.size(); i++) {
collideInto(types.get(i), x, y, array);
}
}

Expand Down
76 changes: 70 additions & 6 deletions src/net/androidpunk/Mask.java
Expand Up @@ -56,20 +56,84 @@ protected boolean hitTest(Bitmap bm, Point firstPoint, int alphaThreshold, Rect
checkRect.top = Math.max(0, r.top);
checkRect.right = Math.min(checkRect.left + r.width(), bm.getWidth());
checkRect.bottom = Math.min(checkRect.top + r.height(), bm.getHeight());
int maxPixel = checkRect.width() * checkRect.height();

int rectWidth = checkRect.width();
int rectHeight = checkRect.height();

/*
// Box first
int maxPixel = rectWidth * rectHeight;
if (mTempPixels == null || (mTempPixels != null && mTempPixels.length < maxPixel) ) {
mTempPixels = new int[checkRect.width() * checkRect.height()];
mTempPixels = new int[rectWidth * rectHeight];
}
int alpha;
bm.getPixels(mTempPixels, 0, checkRect.width(), checkRect.left, checkRect.top, checkRect.width(), checkRect.height());
for (int i = 0; i < maxPixel; i++) {
alpha = Color.alpha(mTempPixels[i]);
bm.getPixels(mTempPixels, 0, rectWidth, checkRect.left, checkRect.top, rectWidth, 1);
for (int i = 0; i < rectWidth; i++) {
if (Color.alpha(mTempPixels[i]) > alphaThreshold) {
return true;
}
}
bm.getPixels(mTempPixels, 0, rectWidth, checkRect.left, checkRect.bottom-1, rectWidth, 1);
for (int i = 0; i < rectWidth; i++) {
if (Color.alpha(mTempPixels[i]) > alphaThreshold) {
return true;
}
}
bm.getPixels(mTempPixels, 0, rectWidth, checkRect.left, checkRect.top, 1, rectHeight);
for (int i = 0; i < rectHeight; i++) {
if (Color.alpha(mTempPixels[i]) > alphaThreshold) {
return true;
}
}
bm.getPixels(mTempPixels, 0, rectWidth, checkRect.right-1, checkRect.top, 1, rectHeight);
for (int i = 0; i < rectHeight; i++) {
if (Color.alpha(mTempPixels[i]) > alphaThreshold) {
return true;
}
}
return false;
*/

//all at once

int maxPixel = rectWidth * rectHeight;
if (mTempPixels == null || (mTempPixels != null && mTempPixels.length < maxPixel) ) {
mTempPixels = new int[rectWidth * rectHeight];
}
int alpha;
bm.getPixels(mTempPixels, 0, rectWidth, checkRect.left, checkRect.top, rectWidth, rectHeight);
for (int i = 0; i < maxPixel; i++) {
alpha = Color.alpha(mTempPixels[i]);
if (alpha > alphaThreshold) {
return true;
}
}
return false;

/*
//check line by line
if (mTempPixels == null || (mTempPixels != null && mTempPixels.length < rectWidth) ) {
mTempPixels = new int[rectWidth];
}
int alpha;
for (int y = 0; y < rectHeight; y++) {
bm.getPixels(mTempPixels, 0, rectWidth, checkRect.left, checkRect.top+y, rectWidth, 1);
for (int i = 0; i < rectWidth; i++) {
alpha = Color.alpha(mTempPixels[i]);
if (alpha > alphaThreshold) {
return true;
}
}
}
return false;
*/
}

protected boolean hitTest(Bitmap bm, Point firstPoint, int alphaThreshold, Bitmap bm2, Point secondPoint, int secondAlphaThreshold) {
Expand Down
15 changes: 10 additions & 5 deletions src/net/androidpunk/graphics/atlas/Emitter.java
Expand Up @@ -164,14 +164,18 @@ public void render(GL10 gl, Point point, Point camera) {

gl.glPushMatrix();
{
setGeometryBuffer(QUAD_FLOAT_BUFFER_1, mP.x, mP.y, rect.width(), rect.height());
// setGeometryBuffer(QUAD_FLOAT_BUFFER_1, mP.x, mP.y, rect.width(), rect.height());
// if (type.mFrames != null) {
// setTextureBuffer(QUAD_FLOAT_BUFFER_2, mSubTexture, type.mFrames[(int)(td * type.mFrameCount)], rect.width(), rect.height());
// } else {
// setTextureBuffer(QUAD_FLOAT_BUFFER_2, mSubTexture, 0, rect.width(), rect.height());
// }
if (type.mFrames != null) {
setTextureBuffer(QUAD_FLOAT_BUFFER_2, mSubTexture, type.mFrames[(int)(td * type.mFrameCount)], rect.width(), rect.height());
type.mTextureBuffer.position(8 * type.mFrames[(int)(td * type.mFrameCount)]);
} else {
setTextureBuffer(QUAD_FLOAT_BUFFER_2, mSubTexture, 0, rect.width(), rect.height());
type.mTextureBuffer.position(0);
}

setBuffers(gl, QUAD_FLOAT_BUFFER_1, QUAD_FLOAT_BUFFER_2);
setBuffers(gl, type.mVertexBuffer, type.mTextureBuffer);


// draw particle
Expand All @@ -184,6 +188,7 @@ public void render(GL10 gl, Point point, Point camera) {
float blue = (type.mBlue + type.mBlueRange * td) / 255.0f;
float alpha = (type.mAlpha + type.mAlphaRange * ((type.mAlphaEase == null) ? t : type.mAlphaEase.ease(t))) / 255.0f;
gl.glColor4f(red, green, blue, alpha);
gl.glTranslatef(mP.x, mP.y, 0.0f);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
gl.glPopMatrix();
Expand Down
24 changes: 24 additions & 0 deletions src/net/androidpunk/graphics/atlas/ParticleType.java
@@ -1,8 +1,12 @@
package net.androidpunk.graphics.atlas;

import java.nio.FloatBuffer;

import net.androidpunk.FP;
import net.androidpunk.flashcompat.OnEaseCallback;
import net.androidpunk.graphics.opengl.GLGraphic;
import net.androidpunk.graphics.opengl.SubTexture;
import net.androidpunk.graphics.opengl.Texture;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
Expand Down Expand Up @@ -51,6 +55,10 @@ public class ParticleType {

protected SubTexture mSubTexture;


protected FloatBuffer mVertexBuffer = GLGraphic.getDirectFloatBuffer(8);
protected FloatBuffer mTextureBuffer;

/**
* Constructor.
* @param name Name of the particle type.
Expand All @@ -71,6 +79,22 @@ public ParticleType(String name, int[] frames, SubTexture source, int frameWidth
} else {
mFrameCount = 0;
}

GLGraphic.setGeometryBuffer(mVertexBuffer,0, 0, frameWidth, frameHeight);
mTextureBuffer = GLGraphic.getDirectFloatBuffer(8*frames.length);

Rect r = FP.rect;

mTextureBuffer.position(0);
Texture t = source.getTexture();
for( int i = 0; i < mFrameCount; i++) {
source.getFrame(r, i, frameWidth, frameHeight);

mTextureBuffer.put((float)r.left/t.getWidth()).put((float)r.top/t.getHeight());
mTextureBuffer.put((float)(r.left + r.width())/t.getWidth()).put((float)r.top/t.getHeight());
mTextureBuffer.put((float)r.left/t.getWidth()).put((float)(r.top + r.height())/t.getHeight());
mTextureBuffer.put((float)(r.left + r.width())/t.getWidth()).put((float)(r.top + r.height())/t.getHeight());
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/net/androidpunk/graphics/atlas/SpriteMap.java
Expand Up @@ -100,7 +100,7 @@ public SpriteMap(SubTexture subTexture, int frameWidth, int frameHeight, OnAnima

setGeometryBuffer(mVertexBuffer, 0, 0, mFrameWidth, mFrameHeight);

Rect r = new Rect();
Rect r = FP.rect;

mTextureBuffer.position(0);
Texture t = subTexture.getTexture();
Expand Down
17 changes: 13 additions & 4 deletions src/net/androidpunk/graphics/atlas/TileMap.java
Expand Up @@ -91,11 +91,11 @@ public TileMap(SubTexture tileset, int width, int height, int tileWidth, int til

Log.d(TAG, String.format("%d vertices", mVerticesCount));

mIndexCount = mColumns * mRows * 6;
//mIndexCount = mColumns * mRows * 6;
mIndexBuffer = getDirectCharBuffer(mColumns * mRows * 6);

setTileVerticesBuffer();
setTileIndexBuffer();
mIndexCount = setTileIndexBuffer();

// load the tileset graphic
mSet = tileset;
Expand Down Expand Up @@ -180,13 +180,18 @@ private void setTileTextureBuffer() {
mTextureBuffer.put(f).position(0);
}

private void setTileIndexBuffer() {
char indices[] = new char[mIndexCount];
private int setTileIndexBuffer() {
char indices[] = new char[mColumns * mRows * 6];
mIndexBuffer.position(0);
int i = 0;
for (int y = 0; y < mRows; y++) {
final int indexY = y * 2;
for (int x = 0; x < mColumns; x++) {
int tile = getTile(x, y);
if (tile == 0x00ffffff) {
//Log.d(TAG, String.format("Skipping (%d, %d)", x, y));
continue;
}
final int indexX = x * 2;
char a = (char) (indexY * mVerticiesAcross + indexX);
char b = (char) (indexY * mVerticiesAcross + indexX + 1);
Expand All @@ -211,6 +216,8 @@ private void setTileIndexBuffer() {
}
}
mIndexBuffer.put(indices).position(0);
Log.d(TAG, String.format("Tilemap is %d indicies down from %d", i, mColumns * mRows * 6));
return i;
}

@Override
Expand Down Expand Up @@ -433,7 +440,9 @@ public void loadFromString(String str, String columnSep, String rowSep) {
setTile(x+xp, y, Integer.parseInt(col[x]));
}
}
setTileVerticesBuffer();
setTileTextureBuffer();
mIndexCount = setTileIndexBuffer();
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/net/androidpunk/graphics/opengl/TextAtlas.java
Expand Up @@ -185,8 +185,12 @@ public int getHeight(String s) {
return (int)(-mPaint.ascent() + mPaint.descent()) * split.length;
}

public static final Typeface getFontFromRes(int resource)
{
public static final Typeface getFontFromAssets(String path) {
Typeface tf = Typeface.createFromAsset(FP.context.getAssets(), path);
return tf;
}

public static final Typeface getFontFromRes(int resource) {
Typeface tf = null;
InputStream is = null;
try {
Expand Down
62 changes: 31 additions & 31 deletions src/net/androidpunk/utils/Draw.java
Expand Up @@ -17,44 +17,44 @@ public class Draw {
* The blending mode used by Draw functions. This will not
* apply to Draw.line(), but will apply to Draw.linePlus().
*/
public static final Paint blend = new Paint();
//public static final Paint blend = new Paint();
private static final Paint resetPaint = new Paint();

private static Bitmap mTarget;
private static Point mCamera;
private static Canvas mCanvas = FP.canvas;
private static Rect mRect = FP.rect;

public static void setTarget(Bitmap target) {
setTarget(target, null, null);
}

public static void setTarget(Bitmap target, Point camera) {
setTarget(target, camera, null);
}

/**
* Sets the drawing target for Draw functions.
* @param target The buffer to draw to.
* @param camera The camera offset (use null for none).
* @param blend The blend mode to use.
*/
public static void setTarget(Bitmap target, Point camera, Paint blend) {
mTarget = target;
mCamera = (camera != null) ? camera : FP.zero;
if (blend != null) {
Draw.blend.set(blend);
}
}

/**
* Resets the drawing target to the default. The same as calling Draw.setTarget(FP.buffer, FP.camera).
*/
public static void resetTarget() {
mTarget = FP.buffer;
mCamera = FP.camera;
Draw.blend.set(resetPaint);
}
// public static void setTarget(Bitmap target) {
// setTarget(target, null, null);
// }
//
// public static void setTarget(Bitmap target, Point camera) {
// setTarget(target, camera, null);
// }
//
// /**
// * Sets the drawing target for Draw functions.
// * @param target The buffer to draw to.
// * @param camera The camera offset (use null for none).
// * @param blend The blend mode to use.
// */
// public static void setTarget(Bitmap target, Point camera, Paint blend) {
// mTarget = target;
// mCamera = (camera != null) ? camera : FP.zero;
// if (blend != null) {
// Draw.blend.set(blend);
// }
// }
//
// /**
// * Resets the drawing target to the default. The same as calling Draw.setTarget(FP.buffer, FP.camera).
// */
// public static void resetTarget() {
// mTarget = FP.buffer;
// mCamera = FP.camera;
// Draw.blend.set(resetPaint);
// }


public static void line(int x1, int y1, int x2, int y2) {
Expand Down

0 comments on commit a60f255

Please sign in to comment.