Skip to content

Commit

Permalink
Added BufferUtils.
Browse files Browse the repository at this point in the history
Removed FastFloatBuffer.
Added Color object.
Color data in all default Meshes now submitted as 4bytes packed as 1 float. (Saves lots of bandwidth)
Refactoring in util package. (Added subpackages)
Refactoring of ShaderPrograms/Constants.
Other minor changes.
  • Loading branch information
Nicolas Gramlich committed Aug 14, 2011
1 parent 20b07c9 commit 09b35f1
Show file tree
Hide file tree
Showing 66 changed files with 844 additions and 1,897 deletions.
68 changes: 56 additions & 12 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
TODOS:
- http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html
- Test performance of colors as bytes.
- See if it makes sense to overall store color as bytes.
- Try to minimize usage Float.floatToIntRawBits by smartly.
- Keep in mind: I.e. more benefits in SpriteGroup as it writes the colors even if they didn't change.
- Find higher performance Matrix4f class?
- Test if only unbinding a ShaderProgram if different one comes in improves performance.
- Think about a way to update the Mesh of a Sprite more efficient.
- Currently when updating the TextureCoordinates, the whole data array is sent to the buffer.
- This could be optimized by not storing the data alternating (X0,Y0,U0,V0,X1,Y1,...) but in (X0,Y0,X1,Y1,...,U0,V0,U1,V1,...)
- This could be optimized by not storing the data alternating (X0,Y0,U0,V0,X1,Y1,...) but in (X0,Y0,X1,Y1,...,U0,V0,U1,V1,...)
- Think about the default blendfunction (for sprites, but also for primitives).
- For Sprites, keep premultiplyalpha in mind..
- Eventually separate a TextureRegion from knowing what Texture it is on.
Expand All @@ -17,21 +13,69 @@ TODOS:
- Drawbacks?
- All custom Exceptions extend from AndEngineException
- Convert "IllegalArgumentException("Unexpected..." to "UnexpectedArgumentException extends AndEngineException
- Replace:
- mEngine.getTextureManager() -> getTextureManager()
- mEngine.getFontManager() -> getFontManager()
- Remove POT texture restriction
- Maybe introduce Color3/4 objects
- Add Color3/4Pool
- According to the Performance myths, pulling class attributes to local fields is not necessary anymore with the JIT.
- As GLES2 is only available on JIT devices, this might be something to consider
- I think I once wrote a commment to remove some legacy code when > 1.6.
- FIND IT!!!
- FIND MORE LEGACY CODE AND GET RID OF IT =) *YAY*
- TODO See if mediump precision and lowp precision make a difference. (cocos2d-gles2 seems to use lowp precision mostly).
- Shadercache, as with the color in the VBO, shaders can be reused A LOT more easily !!!!!
- Instead of directly updating the buffers in onUpdatePosition/onUpdateColor, they could be only marked as dirty and actually written when needed.

EXAMPLES:
- Replace:
- mEngine.getTextureManager() -> getTextureManager()
- mEngine.getFontManager() -> getFontManager()
- Blur/Bloom Shader?
- http://stackoverflow.com/questions/4712042/iphone-post-processing-with-a-single-fbo-with-opengl-es-2-0
- Custom Text Mesh that shows how to color letters individually and make those colors change over time using a custom shader.
- Custom Text Mesh that shows how to color letters individually and make those colors change over time using a custom shader.


21:46:55.724: WARN/AndEngine(7595): Writing Texture to hardware...
21:46:55.724: INFO/AndEngine(7595): Texture 6
21:46:55.724: WARN/AndEngine(7595): Loading PVR data...
21:46:55.764: DEBUG/dalvikvm(7595): GC_FOR_MALLOC freed 4571K, 82% free 3430K/18375K, external 2197K/2744K, paused 21ms
21:46:55.764: INFO/dalvikvm-heap(7595): Grow heap (frag case) to 8.889MB for 1073844-byte allocation
21:46:55.844: DEBUG/dalvikvm(7595): GC_CONCURRENT freed 520K, 79% free 3959K/18375K, external 2197K/2744K, paused 2ms+4ms
21:46:55.864: INFO/dalvikvm-heap(7595): Grow heap (frag case) to 10.444MB for 2163192-byte allocation
21:46:55.954: DEBUG/dalvikvm(7595): GC_CONCURRENT freed 1048K, 73% free 5022K/18375K, external 2197K/2744K, paused 2ms+3ms
21:46:55.974: INFO/dalvikvm-heap(7595): Grow heap (frag case) to 13.560MB for 4341946-byte allocation
21:46:56.074: DEBUG/dalvikvm(7595): GC_CONCURRENT freed 2112K, 62% free 7150K/18375K, external 2197K/2744K, paused 2ms+4ms
21:46:56.094: INFO/dalvikvm-heap(7595): Grow heap (frag case) to 15.490MB for 4194372-byte allocation
21:46:56.144: WARN/AndEngine(7595): done loading PVR data.
21:46:56.164: WARN/AndEngine(7595): Allocating pixelData Buffer...
21:46:56.194: DEBUG/dalvikvm(7595): GC_CONCURRENT freed 4240K, 62% free 6998K/18375K, external 2197K/2744K, paused 2ms+3ms
21:46:56.214: INFO/dalvikvm-heap(7595): Grow heap (frag case) to 15.349MB for 4194320-byte allocation
21:46:56.244: WARN/AndEngine(7595): done allocating pixelData Buffer.
21:46:56.364: DEBUG/dalvikvm(7595): GC_EXPLICIT freed 4096K, 62% free 6998K/18375K, external 2197K/2744K, paused 31ms
21:46:56.364: WARN/AndEngine(7595): done writing Texture to hardware!

20:47:14.102: WARN/AndEngine(761): Writing Texture to hardware...
20:47:14.102: WARN/AndEngine(761): Loading PVR data...
20:47:14.102: INFO/AndEngine(761): Texture 6
20:47:14.122: DEBUG/dalvikvm(761): GC_FOR_MALLOC freed 4104K, 80% free 2919K/13959K, external 2185K/2729K, paused 19ms
20:47:14.122: INFO/dalvikvm-heap(761): Grow heap (frag case) to 11.354MB for 4194372-byte allocation
20:47:14.322: INFO/dalvikvm-heap(761): Grow heap (frag case) to 15.354MB for 4194372-byte allocation
20:47:14.372: WARN/AndEngine(761): done loading PVR data.
20:47:14.382: WARN/AndEngine(761): Allocating pixelData Buffer...
20:47:14.432: DEBUG/dalvikvm(761): GC_CONCURRENT freed 4096K, 50% free 7015K/13959K, external 2185K/2729K, paused 3ms+5ms
20:47:14.462: INFO/dalvikvm-heap(761): Grow heap (frag case) to 15.353MB for 4194320-byte allocation
20:47:14.492: WARN/AndEngine(761): done allocating pixelData Buffer.
20:47:14.602: DEBUG/dalvikvm(761): GC_EXPLICIT freed 4096K, 50% free 7015K/13959K, external 2185K/2729K, paused 27ms
20:47:14.602: WARN/AndEngine(761): done writing Texture to hardware!


21:12:17.650: WARN/AndEngine(1452): Writing Texture to hardware...
21:12:17.650: WARN/AndEngine(1452): Loading PVR data...
21:12:17.660: INFO/AndEngine(1452): Texture 6
21:12:17.670: DEBUG/dalvikvm(1452): GC_FOR_MALLOC freed 4104K, 80% free 2905K/13895K, external 2197K/2744K, paused 17ms
21:12:17.680: INFO/dalvikvm-heap(1452): Grow heap (frag case) to 11.352MB for 4194372-byte allocation
21:12:17.800: WARN/AndEngine(1452): done loading PVR data.
21:12:17.800: WARN/AndEngine(1452): Allocating pixelData Buffer...
21:12:17.830: INFO/dalvikvm-heap(1452): Grow heap (frag case) to 15.351MB for 4194320-byte allocation
21:12:17.850: WARN/AndEngine(1452): done allocating pixelData Buffer.
21:12:17.890: WARN/AndEngine(1452): Calling glTexImage2D...
21:12:17.930: WARN/AndEngine(1452): done calling glTexImage2D.
21:12:17.930: WARN/AndEngine(1452): System.gc
21:12:17.970: DEBUG/dalvikvm(1452): GC_EXPLICIT freed 4096K, 50% free 7001K/13895K, external 2197K/2744K, paused 31ms
21:12:17.970: WARN/AndEngine(1452): done writing Texture to hardware!
5 changes: 3 additions & 2 deletions jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := GLES20Fix
LOCAL_MODULE := andengine
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := src/GLES20Fix.c
LOCAL_SRC_FILES := src/GLES20Fix.c \
src/BufferUtils.cpp
LOCAL_LDLIBS := -lGLESv2

include $(BUILD_SHARED_LIBRARY)
15 changes: 15 additions & 0 deletions jni/src/BufferUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "BufferUtils.h"
#include <stdio.h>
#include <stdlib.h>

// ===========================================================
// org.anddev.andengine.opengl.util.BufferUtils
// ===========================================================

JNIEXPORT void JNICALL Java_org_anddev_andengine_opengl_util_BufferUtils_jniPut(JNIEnv *env, jclass, jobject pBuffer, jfloatArray pData, jint pLength, jint pOffset) {
float* dataAddress = (float*)env->GetPrimitiveArrayCritical(pData, 0);
float* bufferAddress = (float*)env->GetDirectBufferAddress(pBuffer);

memcpy(bufferAddress, dataAddress + pOffset, pLength << 2);
env->ReleasePrimitiveArrayCritical(pData, dataAddress, 0);
}
10 changes: 10 additions & 0 deletions jni/src/BufferUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <jni.h>

extern "C" {

// ===========================================================
// org.anddev.andengine.opengl.util.BufferUtils
// ===========================================================

JNIEXPORT void JNICALL Java_org_anddev_andengine_opengl_util_BufferUtils_jniPut(JNIEnv *, jclass, jobject, jfloatArray, jint, jint);
}
5 changes: 4 additions & 1 deletion jni/src/GLES20Fix.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <jni.h>
#include <GLES2/gl2.h>
//#include <GLES2/gl2ext.h>

// ===========================================================
// org.anddev.andengine.opengl.GLES20Fix
// ===========================================================

void Java_org_anddev_andengine_opengl_GLES20Fix_glVertexAttribPointer (JNIEnv *env, jclass c, jint index, jint size, jint type, jboolean normalized, jint stride, jint offset) {
glVertexAttribPointer(index, size, type, normalized, stride, (void*) offset);
Expand Down
Binary file removed libs/armeabi-v7a/libGLES20Fix.so
Binary file not shown.
Binary file added libs/armeabi-v7a/libandengine.so
Binary file not shown.
Binary file removed libs/armeabi/libGLES20Fix.so
Binary file not shown.
Binary file added libs/armeabi/libandengine.so
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.shape.RectangularShape;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.math.MathUtils;

/**
* (c) 2010 Nicolas Gramlich
Expand Down
2 changes: 1 addition & 1 deletion src/org/anddev/andengine/engine/camera/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.anddev.andengine.entity.shape.RectangularShape;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.util.GLHelper;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.math.MathUtils;

/**
* (c) 2010 Nicolas Gramlich
Expand Down
2 changes: 1 addition & 1 deletion src/org/anddev/andengine/engine/camera/ZoomCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.anddev.andengine.util.constants.Constants.VERTEX_INDEX_Y;

import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.math.MathUtils;



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.anddev.andengine.input.touch.detector.ClickDetector;
import org.anddev.andengine.input.touch.detector.ClickDetector.IClickDetectorListener;
import org.anddev.andengine.opengl.texture.region.ITextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.constants.TimeConstants;
import org.anddev.andengine.util.math.MathUtils;

import android.util.FloatMath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.ITextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.math.MathUtils;

import android.view.MotionEvent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.opengl.texture.region.ITextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.math.MathUtils;

/**
* (c) 2010 Nicolas Gramlich
Expand Down
50 changes: 22 additions & 28 deletions src/org/anddev/andengine/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import org.anddev.andengine.opengl.util.GLHelper;
import org.anddev.andengine.util.ParameterCallable;
import org.anddev.andengine.util.SmartList;
import org.anddev.andengine.util.Transformation;
import org.anddev.andengine.util.color.Color;
import org.anddev.andengine.util.constants.Constants;
import org.anddev.andengine.util.transformation.Transformation;


/**
Expand Down Expand Up @@ -61,10 +62,7 @@ public void call(final IEntity pEntity) {
private EntityModifierList mEntityModifiers;
private UpdateHandlerList mUpdateHandlers;

protected float mRed = 1f;
protected float mGreen = 1f;
protected float mBlue = 1f;
protected float mAlpha = 1f;
protected Color mColor = new Color(1, 1, 1, 1);

protected float mX;
protected float mY;
Expand Down Expand Up @@ -356,33 +354,41 @@ public void setScaleCenter(final float pScaleCenterX, final float pScaleCenterY)

@Override
public float getRed() {
return this.mRed;
return this.mColor.getRed();
}

@Override
public float getGreen() {
return this.mGreen;
return this.mColor.getGreen();
}

@Override
public float getBlue() {
return this.mBlue;
return this.mColor.getBlue();
}

@Override
public float getAlpha() {
return this.mAlpha;
return this.mColor.getAlpha();
}

@Override
public Color getColor() {
return this.mColor;
}

@Override
public void setColor(Color pColor) {
this.mColor.set(pColor);
}

/**
* @param pAlpha from <code>0.0f</code> (transparent) to <code>1.0f</code> (opaque)
*/
@Override
public void setAlpha(final float pAlpha) {
if(this.mAlpha != pAlpha ) {
this.mAlpha = pAlpha;

this.onUpdateColor(); // TODO Potentially could be only updating the alpha
if(this.mColor.setAlphaChecking(pAlpha)) {
this.onUpdateColor();
}
}

Expand All @@ -393,11 +399,7 @@ public void setAlpha(final float pAlpha) {
*/
@Override
public void setColor(final float pRed, final float pGreen, final float pBlue) {
if(this.mRed != pRed || this.mGreen != pGreen || this.mBlue != pBlue) { // TODO Is this check worth it?
this.mRed = pRed;
this.mGreen = pGreen;
this.mBlue = pBlue;

if(this.mColor.setChanging(pRed, pGreen, pBlue)) { // TODO Is this check worth it?
this.onUpdateColor();
}
}
Expand All @@ -410,12 +412,7 @@ public void setColor(final float pRed, final float pGreen, final float pBlue) {
*/
@Override
public void setColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha) {
if(this.mAlpha != pAlpha || this.mRed != pRed || this.mGreen != pGreen || this.mBlue != pBlue) { // TODO Is this check worth it?
this.mRed = pRed;
this.mGreen = pGreen;
this.mBlue = pBlue;
this.mAlpha = pAlpha;

if(this.mColor.setChanging(pRed, pGreen, pBlue, pAlpha)) { // TODO Is this check worth it?
this.onUpdateColor();
}
}
Expand Down Expand Up @@ -911,10 +908,7 @@ public void reset() {
this.mScaleX = 1;
this.mScaleY = 1;

this.mRed = 1.0f;
this.mGreen = 1.0f;
this.mBlue = 1.0f;
this.mAlpha = 1.0f;
this.mColor.reset();

if(this.mEntityModifiers != null) {
this.mEntityModifiers.reset();
Expand Down
7 changes: 5 additions & 2 deletions src/org/anddev/andengine/entity/IEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import org.anddev.andengine.opengl.IDrawable;
import org.anddev.andengine.util.IMatcher;
import org.anddev.andengine.util.ParameterCallable;
import org.anddev.andengine.util.Transformation;
import org.anddev.andengine.util.color.Color;
import org.anddev.andengine.util.transformation.Transformation;


/**
Expand Down Expand Up @@ -87,8 +88,10 @@ public interface IEntity extends IDrawable, IUpdateHandler {
public float getGreen();
public float getBlue();
public float getAlpha();
public Color getColor();

public void setAlpha(final float pAlpha);

public void setColor(final Color pColor);
public void setColor(final float pRed, final float pGreen, final float pBlue);
public void setColor(final float pRed, final float pGreen, final float pBlue, final float pAlpha);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,24 +109,4 @@ protected void onManagedInitialize(final IEntity pEntity) {
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

public void updatePosition(final float pPercentageDone, final IEntity pEntity) {
final float u = 1 - pPercentageDone;
final float tt = pPercentageDone * pPercentageDone;
final float uu = u * u;
final float uuu = uu * u;
final float ttt = tt * pPercentageDone;

final float ut3 = 3 * uu * pPercentageDone;
final float utt3 = 3 * u * tt;

/*
* Formula: ((1-t)^3 * p1) + (3*(t)*(1-t)^2 * p2) + (3*(t^2)*(1-t) * p3)
* + (t^3 * p4)
*/
final float x = (uuu * this.mX1) + (ut3 * this.mX2) + (utt3 * this.mX3) + (ttt * this.mX4);
final float y = (uuu * this.mY1) + (ut3 * this.mY2) + (utt3 * this.mY3) + (ttt * this.mY4);

pEntity.setPosition(x, y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,4 @@ protected void onManagedInitialize(final IEntity pEntity) {
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

public void updatePosition(final float pPercentageDone, final IEntity pEntity) {
final float u = 1 - pPercentageDone;
final float tt = pPercentageDone*pPercentageDone;
final float uu = u*u;

final float ut2 = 2 * u * pPercentageDone;

/* Formula:
* ((1-t)^2 * p1) + (2*(t)*(1-t) * p2) + ((t^2) * p3) */
final float x = (uu * this.mX1) + (ut2 * this.mX2) + (tt * this.mX3);
final float y = (uu * this.mY1) + (ut2 * this.mY2) + (tt * this.mY3);

pEntity.setPosition(x, y);
}
}
Loading

0 comments on commit 09b35f1

Please sign in to comment.