[alienbloodbath commit] r69 - trunk/src/android/com/abb

3 views
Skip to first unread message

codesite...@google.com

unread,
Jan 10, 2009, 8:42:50 PM1/10/09
to alien-blood-b...@googlegroups.com
Author: matt.burkhart
Date: Sat Jan 10 17:14:09 2009
New Revision: 69

Modified:
trunk/src/android/com/abb/ArticulatedEntity.java
trunk/src/android/com/abb/Avatar.java
trunk/src/android/com/abb/Enemy.java
trunk/src/android/com/abb/Entity.java
trunk/src/android/com/abb/GameState.java
trunk/src/android/com/abb/GameView.java
trunk/src/android/com/abb/Graphics.java
trunk/src/android/com/abb/Map.java

Log:
Performance improvements. No new feautres.

Modified: trunk/src/android/com/abb/ArticulatedEntity.java
==============================================================================
--- trunk/src/android/com/abb/ArticulatedEntity.java (original)
+++ trunk/src/android/com/abb/ArticulatedEntity.java Sat Jan 10 17:14:09
2009
@@ -83,14 +83,14 @@
}
}

- public void loadAnimationFromUri(Uri uri) {
- Animation animation = mAnimationCache.get(uri);
+ public void loadAnimationFromUri(String uri_string) {
+ Animation animation = mAnimationCache.get(uri_string);
if (animation != null) {
mAnimation = animation;
} else {
mAnimation = new Animation();
- mAnimation.loadFromUri(uri);
- mAnimationCache.put(uri, mAnimation);
+ mAnimation.loadFromUri(uri_string);
+ mAnimationCache.put(uri_string, mAnimation);
}
}

@@ -123,13 +123,13 @@
horizontal_flip = -1.0f;
}

- Matrix root_transformation = new Matrix();
- root_transformation.preTranslate(
+ mRootTransformation.reset();
+ mRootTransformation.preTranslate(
graphics.getWidth() / 2 + (x - center_x) * zoom,
graphics.getHeight() / 2 + (y - center_y) * zoom);
- root_transformation.preScale(
+ mRootTransformation.preScale(
mDrawingScale * zoom * horizontal_flip, mDrawingScale * zoom);
- mRoot.draw(graphics, mImageHandle, root_transformation, mAnimation);
+ mRoot.draw(graphics, mImageHandle, mRootTransformation, mAnimation);
}

/** Return the 3x3 transformation matrix used to draw child parts. In
other
@@ -151,6 +151,11 @@
}
}

+ @Override
+ public Object clone() {
+ return super.clone();
+ }
+
/** The Part class structure represents a single element of the
articulated
* entity. */
private class Part {
@@ -211,7 +216,9 @@
/** The Animation class stores and provides access to a independent, time
* varying set of values called tracks. */
private class Animation {
- public void loadFromUri(Uri uri) {
+ public void loadFromUri(String uri_string) {
+ Uri uri = Uri.parse(uri_string);
+
// The file format is expected to be an ASCII text file laid out
with a
// single key-frame per line. The first two lines must
contain "offset_x
// #" and "offset_y #". Each consecutive line / key frame must have
the
@@ -329,10 +336,11 @@
}

private Animation mAnimation = new Animation();
- private TreeMap<Uri, Animation> mAnimationCache =
- new TreeMap<Uri, Animation>();
+ private TreeMap<String, Animation> mAnimationCache =
+ new TreeMap<String, Animation>();
private float mDrawingScale = 1.0f;
private int mImageHandle = -1;
private Uri mImageUri;
private Part mRoot = new Part();
+ private Matrix mRootTransformation = new Matrix();
}

Modified: trunk/src/android/com/abb/Avatar.java
==============================================================================
--- trunk/src/android/com/abb/Avatar.java (original)
+++ trunk/src/android/com/abb/Avatar.java Sat Jan 10 17:14:09 2009
@@ -62,14 +62,14 @@

if (has_ground_contact) {
if (Math.abs(dx) > kAnimationStopThreshold) {
-
loadAnimationFromUri(Uri.parse("content:///run.humanoid.animation"));
+ loadAnimationFromUri("content:///run.humanoid.animation");
stepAnimation(kGroundAnimationSpeed * Math.abs(dx));
} else {
-
loadAnimationFromUri(Uri.parse("content:///stand.humanoid.animation"));
+ loadAnimationFromUri("content:///stand.humanoid.animation");
stepAnimation(time_step);
}
} else {
-
loadAnimationFromUri(Uri.parse("content:///jump.humanoid.animation"));
+ loadAnimationFromUri("content:///jump.humanoid.animation");
stepAnimation(time_step);
}


Modified: trunk/src/android/com/abb/Enemy.java
==============================================================================
--- trunk/src/android/com/abb/Enemy.java (original)
+++ trunk/src/android/com/abb/Enemy.java Sat Jan 10 17:14:09 2009
@@ -99,7 +99,12 @@
String animation = (String)enemy_parameters.get(kParameterAnimation);
Assert.assertTrue("Enemy animation must be specified.",
!animation.equals("none"));
- super.loadAnimationFromUri(Uri.parse(base_uri_string + "/" +
animation));
+ super.loadAnimationFromUri(base_uri_string + "/" + animation);
+ }
+
+ @Override
+ public Object clone() {
+ return super.clone();
}

private float mAcceleration;

Modified: trunk/src/android/com/abb/Entity.java
==============================================================================
--- trunk/src/android/com/abb/Entity.java (original)
+++ trunk/src/android/com/abb/Entity.java Sat Jan 10 17:14:09 2009
@@ -15,13 +15,14 @@
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.RectF;
+import java.lang.CloneNotSupportedException;
import java.lang.Math;
import java.util.Random;


/** The Entity class is intended to be lowest level, drawable, physical
in-game
* object. */
-public class Entity {
+public class Entity implements Cloneable {
public boolean alive = true; // Should not be deleted from the game.
public boolean has_ground_contact;
public float radius;
@@ -61,22 +62,32 @@
int canvas_width = graphics.getWidth();
int canvas_height = graphics.getHeight();

- RectF sprite_destination = new RectF(
- 0, 0,
- sprite_rect.width() * zoom,
- sprite_rect.height() * zoom);
- sprite_destination.offset(
- (x - center_x) * zoom +
- (canvas_width - sprite_rect.width() * zoom) / 2.0f,
- (y - center_y) * zoom +
- (canvas_height - sprite_rect.height() * zoom) / 2.0f);
+ mRectF.left = (x - center_x) * zoom +
+ (canvas_width - sprite_rect.width() * zoom) / 2.0f;
+ mRectF.top = (y - center_y) * zoom +
+ (canvas_height - sprite_rect.height() * zoom) / 2.0f;
+ mRectF.right = mRectF.left + sprite_rect.width() * zoom;
+ mRectF.bottom = mRectF.top + sprite_rect.height() * zoom;
+
graphics.drawImage(
- sprite_image, sprite_rect, sprite_destination,
+ sprite_image, sprite_rect, mRectF,
sprite_flipped_horizontal, sprite_flipped_vertical);
}
}

+ @Override
+ public Object clone() {
+ try {
+ return super.clone();
+ } catch (CloneNotSupportedException ex) {
+ throw new InternalError(ex.toString());
+ }
+ }
+
+ // The following allocations are made here to avoid allocating anything
during
+ // the game. They are intended to be used by this an any child classe.
protected static Random mRandom = new Random();
+ protected static RectF mRectF = new RectF();

private static final float kMaxVelocity = 200.0f;
}

Modified: trunk/src/android/com/abb/GameState.java
==============================================================================
--- trunk/src/android/com/abb/GameState.java (original)
+++ trunk/src/android/com/abb/GameState.java Sat Jan 10 17:14:09 2009
@@ -24,11 +24,12 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
+import java.util.TreeMap;


public class GameState implements Game {
public Avatar avatar = new Avatar(this);
- public ArrayList<Entity> enemies = new ArrayList<Entity>();
+ public ArrayList<Enemy> enemies = new ArrayList<Enemy>();
public Map map = new Map(this);
public int misc_sprites;
public ArrayList<Entity> particles = new ArrayList<Entity>();
@@ -125,8 +126,8 @@
}

// Step the enemies.
- for (Iterator it = enemies.iterator(); it.hasNext();) {
- Enemy enemy = (Enemy)it.next();
+ for (int index = 0; index < enemies.size(); ++index) {
+ Enemy enemy = enemies.get(index);
enemy.step(time_step);
map.collideEntity(enemy);
if (!enemy.alive) {
@@ -137,28 +138,28 @@
kBloodBathVelocity * (0.5f - mRandom.nextFloat()) + enemy.dx,
kBloodBathVelocity * (0.5f - mRandom.nextFloat()) +
enemy.dy);
}
- it.remove();
+ enemies.remove(index);
}
}

// Step the projectiles and collide them against the enemies.
- for (Iterator it = projectiles.iterator(); it.hasNext();) {
- Fire projectile = (Fire)it.next();
+ for (int index = 0; index < projectiles.size(); ++index) {
+ Fire projectile = (Fire)projectiles.get(index);
projectile.step(time_step);
- for (Iterator enemy_it = enemies.iterator(); enemy_it.hasNext();) {
- projectile.collideEntity((Entity)enemy_it.next());
+ for (int enemy_index = 0; enemy_index < enemies.size();
++enemy_index) {
+ projectile.collideEntity(enemies.get(enemy_index));
}
if (!projectile.alive) {
- it.remove();
+ projectiles.remove(index);
}
}

// Step the particles.
- for (Iterator it = particles.iterator(); it.hasNext();) {
- Entity particle = (Entity)it.next();
+ for (int index = 0; index < particles.size(); ++index) {
+ Entity particle = particles.get(index);
particle.step(time_step);
if (!particle.alive) {
- it.remove();
+ particles.remove(index);
}
}
}
@@ -170,8 +171,8 @@
map.draw(graphics, mViewX, mViewY, mZoom);

// Draw the enemies.
- for (Iterator it = enemies.iterator(); it.hasNext();) {
- ((Entity)it.next()).draw(graphics, mViewX, mViewY, mZoom);
+ for (int index = 0; index < enemies.size(); ++index) {
+ enemies.get(index).draw(graphics, mViewX, mViewY, mZoom);
}

// Draw the avatar and weapon.
@@ -182,19 +183,25 @@
}

// Draw the projectiles.
- for (Iterator it = projectiles.iterator(); it.hasNext();) {
- ((Entity)it.next()).draw(graphics, mViewX, mViewY, mZoom);
+ for (int index = 0; index < projectiles.size(); ++index) {
+ projectiles.get(index).draw(graphics, mViewX, mViewY, mZoom);
}

// Draw the particles.
- for (Iterator it = particles.iterator(); it.hasNext();) {
- ((Entity)it.next()).draw(graphics, mViewX, mViewY, mZoom);
+ for (int index = 0; index < particles.size(); ++index) {
+ particles.get(index).draw(graphics, mViewX, mViewY, mZoom);
}
}

- public Entity createEnemy(Uri enemy_uri, float x, float y) {
- Enemy enemy = new Enemy(avatar);
- enemy.loadFromUri(enemy_uri);
+ public Entity createEnemyFromUri(Uri uri, float x, float y) {
+ Enemy enemy = mEnemyCache.get(uri);
+ if (enemy == null) {
+ enemy = new Enemy(avatar);
+ enemy.loadFromUri(uri);
+ mEnemyCache.put(uri, enemy);
+ }
+
+ enemy = (Enemy)enemy.clone();
enemy.x = x;
enemy.y = y;
enemies.add(enemy);
@@ -272,6 +279,7 @@

private Context mContext;
private float mDeathTimer = kDeathTimer;
+ private TreeMap<Uri, Enemy> mEnemyCache = new TreeMap<Uri, Enemy>();
private Random mRandom = new Random();
private float mTargetViewX = 0.0f;
private float mTargetViewY = 0.0f;

Modified: trunk/src/android/com/abb/GameView.java
==============================================================================
--- trunk/src/android/com/abb/GameView.java (original)
+++ trunk/src/android/com/abb/GameView.java Sat Jan 10 17:14:09 2009
@@ -66,7 +66,7 @@
// possible. Here we define the maximum framerate which needs to
make the
// trade off between graphics fluidity and power savings.
final float kMaxFrameRate = 30.0f; // Frames / second.
- final float kMinFrameRate = 5.0f; // Frames / second.
+ final float kMinFrameRate = 6.0f; // Frames / second.
final float kMinTimeStep = 1.0f / kMaxFrameRate; // Seconds.
final float kMaxTimeStep = 1.0f / kMinFrameRate; // Seconds.


Modified: trunk/src/android/com/abb/Graphics.java
==============================================================================
--- trunk/src/android/com/abb/Graphics.java (original)
+++ trunk/src/android/com/abb/Graphics.java Sat Jan 10 17:14:09 2009
@@ -148,32 +148,109 @@
boolean flipped_horizontal, boolean
flipped_vertical) {
Assert.assertTrue("Invalid image handle in drawImage", image_handle >=
0);

- switch (mBackendType) {
- case ANDROID2D:
- drawImageAndroid2D(image_handle, source_rect, dest_rect,
- flipped_horizontal, flipped_vertical);
- break;
- case OPENGL:
- drawImageOpenGL(image_handle, source_rect, dest_rect,
- flipped_horizontal, flipped_vertical);
- break;
+ // The drawImageOpenGL implementation has been inlined here for
performance
+ // reasons. TODO: Inline the 2D API implementation here.
+ Assert.assertEquals(mBackendType, BackendType.OPENGL);
+
+ if (image_handle != mCurrentTexture) {
+ mCurrentTexture = image_handle;
+ mGl.glBindTexture(GL10.GL_TEXTURE_2D, image_handle);
+ }
+
+ // The vertex and texture coordinate arrays have already been
initialized.
+ // All that is left is to set up the texture and model view
transformation
+ // matrices and render. Note that the OpenGL API expects matrices with
a
+ // column-major layout.
+ float texture_width = mTextureWidths.get(image_handle);
+ float texture_height = mTextureHeights.get(image_handle);
+
+ mMatrix4x4[1] = mMatrix4x4[2] = mMatrix4x4[4] =
+ mMatrix4x4[6] = mMatrix4x4[8] = mMatrix4x4[9] = 0.0f;
+ if (flipped_vertical) {
+ mMatrix4x4[5] = (source_rect.top - source_rect.bottom) /
texture_height;
+ mMatrix4x4[13] = source_rect.bottom / texture_height;
+ } else {
+ mMatrix4x4[5] = (source_rect.bottom - source_rect.top) /
texture_height;
+ mMatrix4x4[13] = source_rect.top / texture_height;
+ }
+ if (flipped_horizontal) {
+ mMatrix4x4[0] = (source_rect.left - source_rect.right) /
texture_width;
+ mMatrix4x4[12] = source_rect.right / texture_width;
+ } else {
+ mMatrix4x4[0] = (source_rect.right - source_rect.left) /
texture_width;
+ mMatrix4x4[12] = source_rect.left / texture_width;
}
+
+ mGl.glMatrixMode(GL10.GL_TEXTURE);
+ mGl.glLoadMatrixf(mMatrix4x4, 0);
+
+ mMatrix4x4[0] = dest_rect.right - dest_rect.left;
+ mMatrix4x4[5] = dest_rect.top - dest_rect.bottom;
+ mMatrix4x4[12] = dest_rect.left;
+ mMatrix4x4[13] = mSurfaceHeight - dest_rect.top;
+ mGl.glMatrixMode(GL10.GL_MODELVIEW);
+ mGl.glLoadMatrixf(mMatrix4x4, 0);
+
+ mGl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
}

public void drawImage(int image_handle, Rect source_rect, Matrix
dest_matrix,
boolean flipped_horizontal, boolean
flipped_vertical) {
Assert.assertTrue("Invalid image handle in drawImage", image_handle >=
0);

- switch (mBackendType) {
- case ANDROID2D:
- drawImageAndroid2D(image_handle, source_rect, dest_matrix,
- flipped_horizontal, flipped_vertical);
- break;
- case OPENGL:
- drawImageOpenGL(image_handle, source_rect, dest_matrix,
- flipped_horizontal, flipped_vertical);
- break;
+ // The drawImageOpenGL implementation has been inlined here for
performance
+ // reasons. TODO: Inline the 2D API implementation here.
+ Assert.assertEquals(mBackendType, BackendType.OPENGL);
+
+ if (image_handle != mCurrentTexture) {
+ mCurrentTexture = image_handle;
+ mGl.glBindTexture(GL10.GL_TEXTURE_2D, image_handle);
+ }
+
+ // The vertex and texture coordinate arrays have already been
initialized.
+ // All that is left is to set up the texture and model view
transformation
+ // matrices and render. Note that the OpenGL API expects matrices with
a
+ // column-major layout.
+ float texture_width = mTextureWidths.get(image_handle);
+ float texture_height = mTextureHeights.get(image_handle);
+
+ mMatrix4x4[1] = mMatrix4x4[2] = mMatrix4x4[4] =
+ mMatrix4x4[6] = mMatrix4x4[8] = mMatrix4x4[9] = 0.0f;
+ if (flipped_vertical) {
+ mMatrix4x4[5] = (source_rect.top - source_rect.bottom) /
texture_height;
+ mMatrix4x4[13] = source_rect.bottom / texture_height;
+ } else {
+ mMatrix4x4[5] = (source_rect.bottom - source_rect.top) /
texture_height;
+ mMatrix4x4[13] = source_rect.top / texture_height;
+ }
+ if (flipped_horizontal) {
+ mMatrix4x4[0] = (source_rect.left - source_rect.right) /
texture_width;
+ mMatrix4x4[12] = source_rect.right / texture_width;
+ } else {
+ mMatrix4x4[0] = (source_rect.right - source_rect.left) /
texture_width;
+ mMatrix4x4[12] = source_rect.left / texture_width;
}
+
+ mGl.glMatrixMode(GL10.GL_TEXTURE);
+ mGl.glLoadMatrixf(mMatrix4x4, 0);
+
+ mScreenMatrix.reset();
+ mScreenMatrix.preTranslate(0.0f, mSurfaceHeight);
+ mScreenMatrix.preScale(1.0f, -1.0f);
+ mScreenMatrix.preConcat(dest_matrix);
+ mScreenMatrix.getValues(mMatrix3x3);
+ mMatrix4x4[0] = mMatrix3x3[0];
+ mMatrix4x4[1] = mMatrix3x3[3];
+ mMatrix4x4[2] = mMatrix3x3[6];
+ mMatrix4x4[4] = mMatrix3x3[1];
+ mMatrix4x4[5] = mMatrix3x3[4];
+ mMatrix4x4[6] = mMatrix3x3[6];
+ mMatrix4x4[12] = mMatrix3x3[2];
+ mMatrix4x4[13] = mMatrix3x3[5];
+ mGl.glMatrixMode(GL10.GL_MODELVIEW);
+ mGl.glLoadMatrixf(mMatrix4x4, 0);
+
+ mGl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
}

public void beginFrame() {
@@ -507,107 +584,6 @@

private int getHeightOpenGL() {
return mSurfaceHeight;
- }
-
- private void drawImageOpenGL(int image_handle,
- Rect source_rect, RectF dest_rect,
- boolean flipped_horizontal,
- boolean flipped_vertical) {
- if (image_handle != mCurrentTexture) {
- mCurrentTexture = image_handle;
- mGl.glBindTexture(GL10.GL_TEXTURE_2D, image_handle);
- }
-
- // The vertex and texture coordinate arrays have already been
initialized.
- // All that is left is to set up the texture and model view
transformation
- // matrices and render. Note that the OpenGL API expects matrices with
a
- // column-major layout.
- float texture_width = mTextureWidths.get(image_handle);
- float texture_height = mTextureHeights.get(image_handle);
-
- mMatrix4x4[1] = mMatrix4x4[2] = mMatrix4x4[4] =
- mMatrix4x4[6] = mMatrix4x4[8] = mMatrix4x4[9] = 0.0f;
- if (flipped_vertical) {
- mMatrix4x4[5] = (source_rect.top - source_rect.bottom) /
texture_height;
- mMatrix4x4[13] = source_rect.bottom / texture_height;
- } else {
- mMatrix4x4[5] = (source_rect.bottom - source_rect.top) /
texture_height;
- mMatrix4x4[13] = source_rect.top / texture_height;
- }
- if (flipped_horizontal) {
- mMatrix4x4[0] = (source_rect.left - source_rect.right) /
texture_width;
- mMatrix4x4[12] = source_rect.right / texture_width;
- } else {
- mMatrix4x4[0] = (source_rect.right - source_rect.left) /
texture_width;
- mMatrix4x4[12] = source_rect.left / texture_width;
- }
-
- mGl.glMatrixMode(GL10.GL_TEXTURE);
- mGl.glLoadMatrixf(mMatrix4x4, 0);
-
- mMatrix4x4[0] = dest_rect.right - dest_rect.left;
- mMatrix4x4[5] = dest_rect.top - dest_rect.bottom;
- mMatrix4x4[12] = dest_rect.left;
- mMatrix4x4[13] = mSurfaceHeight - dest_rect.top;
- mGl.glMatrixMode(GL10.GL_MODELVIEW);
- mGl.glLoadMatrixf(mMatrix4x4, 0);
-
- mGl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
- }
-
- private void drawImageOpenGL(int image_handle,
- Rect source_rect, Matrix dest_matrix,
- boolean flipped_horizontal,
- boolean flipped_vertical) {
- if (image_handle != mCurrentTexture) {
- mCurrentTexture = image_handle;
- mGl.glBindTexture(GL10.GL_TEXTURE_2D, image_handle);
- }
-
- // The vertex and texture coordinate arrays have already been
initialized.
- // All that is left is to set up the texture and model view
transformation
- // matrices and render. Note that the OpenGL API expects matrices with
a
- // column-major layout.
- float texture_width = mTextureWidths.get(image_handle);
- float texture_height = mTextureHeights.get(image_handle);
-
- mMatrix4x4[1] = mMatrix4x4[2] = mMatrix4x4[4] =
- mMatrix4x4[6] = mMatrix4x4[8] = mMatrix4x4[9] = 0.0f;
- if (flipped_vertical) {
- mMatrix4x4[5] = (source_rect.top - source_rect.bottom) /
texture_height;
- mMatrix4x4[13] = source_rect.bottom / texture_height;
- } else {
- mMatrix4x4[5] = (source_rect.bottom - source_rect.top) /
texture_height;
- mMatrix4x4[13] = source_rect.top / texture_height;
- }
- if (flipped_horizontal) {
- mMatrix4x4[0] = (source_rect.left - source_rect.right) /
texture_width;
- mMatrix4x4[12] = source_rect.right / texture_width;
- } else {
- mMatrix4x4[0] = (source_rect.right - source_rect.left) /
texture_width;
- mMatrix4x4[12] = source_rect.left / texture_width;
- }
-
- mGl.glMatrixMode(GL10.GL_TEXTURE);
- mGl.glLoadMatrixf(mMatrix4x4, 0);
-
- mScreenMatrix.reset();
- mScreenMatrix.preTranslate(0.0f, mSurfaceHeight);
- mScreenMatrix.preScale(1.0f, -1.0f);
- mScreenMatrix.preConcat(dest_matrix);
- mScreenMatrix.getValues(mMatrix3x3);
- mMatrix4x4[0] = mMatrix3x3[0];
- mMatrix4x4[1] = mMatrix3x3[3];
- mMatrix4x4[2] = mMatrix3x3[6];
- mMatrix4x4[4] = mMatrix3x3[1];
- mMatrix4x4[5] = mMatrix3x3[4];
- mMatrix4x4[6] = mMatrix3x3[6];
- mMatrix4x4[12] = mMatrix3x3[2];
- mMatrix4x4[13] = mMatrix3x3[5];
- mGl.glMatrixMode(GL10.GL_MODELVIEW);
- mGl.glLoadMatrixf(mMatrix4x4, 0);
-
- mGl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
}

private void beginFrameOpenGL() {

Modified: trunk/src/android/com/abb/Map.java
==============================================================================
--- trunk/src/android/com/abb/Map.java (original)
+++ trunk/src/android/com/abb/Map.java Sat Jan 10 17:14:09 2009
@@ -360,20 +360,24 @@
int canvas_height = graphics.getHeight();
if (graphics.hasHardwareAcceleration() && mBackgroundImage != -1) {
float scaled_background_size = kBackgroundScale * kBackgroundSize;
- Rect background_src = new Rect(0, 0, kBackgroundSize,
kBackgroundSize);
- RectF background_dst = new RectF(
- 0.0f, 0.0f, scaled_background_size, scaled_background_size);
- background_dst.offset(-center_x / (kTileSize * kMapWidth) *
- (scaled_background_size - canvas_width),
- -center_y / (kTileSize * kMapHeight) *
- (scaled_background_size - canvas_height));
+ mRectSource.left = mRectSource.top = 0;
+ mRectSource.right = mRectSource.bottom = kBackgroundSize;
+
+ mRectDest.left = -center_x / (kTileSize * kMapWidth) *
+ (scaled_background_size - canvas_width);
+ mRectDest.top = -center_y / (kTileSize * kMapHeight) *
+ (scaled_background_size - canvas_height);
+ mRectDest.right = mRectDest.left + scaled_background_size;
+ mRectDest.bottom = mRectDest.top + scaled_background_size;
+
graphics.drawImage(
- mBackgroundImage, background_src, background_dst, false, false);
+ mBackgroundImage, mRectSource, mRectDest, false, false);
}

// Draw tiles.
- Rect source_rect = new Rect(0, 0, kTileSize, kTileSize);
- RectF destination_rect = new RectF();
+ mRectSource.top = mRectSource.left = 0;
+ mRectSource.right = mRectSource.bottom = kTileSize;
+
int half_canvas_width = canvas_width / 2;
int half_canvas_height = canvas_height / 2;
float x_min = center_x - half_canvas_width / zoom;
@@ -393,7 +397,7 @@
if (trigger.startsWith("enemy=")) {
Uri enemy_uri =
Uri.withAppendedPath(mBaseUri, trigger.substring(6));
- mGameState.createEnemy(enemy_uri, x, y);
+ mGameState.createEnemyFromUri(enemy_uri, x, y);
mTriggers[tile_index] = null;
}
}
@@ -406,17 +410,16 @@

int index_x = (int)(x / kTileSize + 0.5f);
int index_y = (int)(y / kTileSize + 0.5f);
- source_rect.top = kTileSize * tile_id;
- source_rect.bottom = kTileSize * tile_id + kTileSize;
- destination_rect.left = kTileSize * index_x * zoom;
- destination_rect.top = kTileSize * index_y * zoom;
- destination_rect.right = (kTileSize * index_x + kTileSize) * zoom;
- destination_rect.bottom = (kTileSize * index_y + kTileSize) * zoom;
- destination_rect.offset(
+ mRectSource.top = kTileSize * tile_id;
+ mRectSource.bottom = kTileSize * tile_id + kTileSize;
+ mRectDest.left = kTileSize * index_x * zoom;
+ mRectDest.top = kTileSize * index_y * zoom;
+ mRectDest.right = (kTileSize * index_x + kTileSize) * zoom;
+ mRectDest.bottom = (kTileSize * index_y + kTileSize) * zoom;
+ mRectDest.offset(
-center_x * zoom + half_canvas_width - kTileSize / 2 * zoom,
-center_y * zoom + half_canvas_height - kTileSize / 2 * zoom);
- graphics.drawImage(
- mTilesImage, source_rect, destination_rect, false, false);
+ graphics.drawImage(mTilesImage, mRectSource, mRectDest, false,
false);
}
}
}
@@ -441,8 +444,10 @@
private boolean[] mEffectsExplode;
private boolean[] mEffectsSolid;
private GameState mGameState;
- private int mLevelOffset = 0; // Level within the mBaseUri package.
+ private int mLevelOffset = 0; // Level within the mBaseUri map package.
private Random mRandom = new Random();
+ private Rect mRectSource = new Rect();
+ private RectF mRectDest = new RectF();
private float mStartingX;
private float mStartingY;
private char[] mTiles;

Reply all
Reply to author
Forward
0 new messages