From 6be0a24e07e532270b9d6b1a05dccf487b58cdd0 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 2 Nov 2014 23:29:13 -0700 Subject: [PATCH] Changed entities to bitmaps --- AndroidManifest.xml | 2 +- .../wurbo/ghostcatcher/GameSurfaceView.java | 30 ++++++++++++++----- .../wurbo/ghostcatcher/entities/Entity.java | 15 ++++++---- .../wurbo/ghostcatcher/entities/Ghost.java | 13 ++++++-- .../wurbo/ghostcatcher/entities/Player.java | 4 +-- 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 1e9157f..12b8896 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -4,7 +4,7 @@ android:versionName="3.0.2" > diff --git a/src/com/wurbo/ghostcatcher/GameSurfaceView.java b/src/com/wurbo/ghostcatcher/GameSurfaceView.java index 23cd65d..c40d5bb 100644 --- a/src/com/wurbo/ghostcatcher/GameSurfaceView.java +++ b/src/com/wurbo/ghostcatcher/GameSurfaceView.java @@ -8,8 +8,11 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Typeface; import android.graphics.drawable.PictureDrawable; @@ -33,8 +36,8 @@ public class GameSurfaceView extends SurfaceView implements private MainThread mThread; private long height, width; public static final String TAG = GameSurfaceView.class.getSimpleName(); - PictureDrawable ghostBase; - PictureDrawable playerBase; + Bitmap ghostBase; + Bitmap playerBase; PictureDrawable background; private float cursorX = 0; private float cursorY = 0; @@ -88,9 +91,20 @@ public GameSurfaceView(Context context, String username) { // Load SVGS SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.ghostbasic); - ghostBase = svg.createPictureDrawable(); - svg = SVGParser.getSVGFromResource(getResources(), R.raw.ghostbasic); - playerBase = svg.createPictureDrawable(); + PictureDrawable pd = svg.createPictureDrawable(); + Bitmap bitmap = Bitmap.createBitmap(pd.getIntrinsicWidth(), pd.getIntrinsicHeight(), Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + canvas.drawPicture(pd.getPicture()); + + + Bitmap bitmap2 = Bitmap.createBitmap(pd.getIntrinsicWidth(), pd.getIntrinsicHeight(), Config.ARGB_8888); + Canvas canvas2 = new Canvas(bitmap2); + canvas2.scale(1, -1); + canvas2.translate(0, -pd.getIntrinsicWidth()); + canvas2.drawPicture(pd.getPicture()); + + ghostBase = bitmap2; + playerBase = bitmap; // Get window height/width DisplayMetrics displaymetrics = new DisplayMetrics(); @@ -132,7 +146,7 @@ public GameSurfaceView(Context context, String username) { for (int i = 0; i < numGhosts; i++) { Ghost ghost = new Ghost(50, (int) width, (int) height, ghostBase); ghost.random((int) width, (int) height); - ghost.setFlipped(true); + //ghost.setFlipped(true); ghosts.add(ghost); } @@ -333,8 +347,8 @@ private void changeSize(String string) { } private void drawEntity(Canvas canvas, Entity entity) { - canvas.drawPicture(entity.basePicture(), entity); - canvas.drawCircle(entity.getX(), entity.getY() + entity.getOffset(), entity.getOffset(), entity.getPaint()); + canvas.drawBitmap(entity.basePicture(), null, entity, entity.getPaint()); + //canvas.drawCircle(entity.getX(), entity.getY() + entity.getOffset(), entity.getOffset(), entity.getPaint()); } @Override diff --git a/src/com/wurbo/ghostcatcher/entities/Entity.java b/src/com/wurbo/ghostcatcher/entities/Entity.java index 42467cc..41a78d0 100644 --- a/src/com/wurbo/ghostcatcher/entities/Entity.java +++ b/src/com/wurbo/ghostcatcher/entities/Entity.java @@ -2,10 +2,12 @@ import java.util.Random; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.graphics.ColorFilter; +import android.graphics.LightingColorFilter; import android.graphics.Paint; -import android.graphics.Picture; import android.graphics.RectF; -import android.graphics.drawable.PictureDrawable; public class Entity extends RectF { @@ -19,16 +21,17 @@ public class Entity extends RectF { private boolean flipped = false; protected Random rand; protected Paint paint; - private Picture basePicture; + private Bitmap basePicture; - public Entity(float size, int screenWidth, int screenHeight, PictureDrawable baseImage) { + public Entity(float size, int screenWidth, int screenHeight, Bitmap playerBase) { rand = new Random(); paint = new Paint(); //Create ratio for width for target width of 720px this.widthRatio = (float) (screenWidth / 720.0); this.heightRatio = (float) (screenHeight / 1080.0); this.size = size; - this.basePicture = baseImage.getPicture(); + //playerBase.setColorFilter(cf); + this.basePicture = playerBase; } public float getSize() { @@ -112,7 +115,7 @@ public Paint getPaint() { return paint; } - public Picture basePicture() { + public Bitmap basePicture() { return basePicture; } } diff --git a/src/com/wurbo/ghostcatcher/entities/Ghost.java b/src/com/wurbo/ghostcatcher/entities/Ghost.java index 2d3e801..1e12c76 100644 --- a/src/com/wurbo/ghostcatcher/entities/Ghost.java +++ b/src/com/wurbo/ghostcatcher/entities/Ghost.java @@ -1,6 +1,8 @@ package com.wurbo.ghostcatcher.entities; -import android.graphics.drawable.PictureDrawable; +import android.graphics.Bitmap; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; public class Ghost extends Entity { @@ -10,7 +12,7 @@ public enum GhostType { private GhostType type; - public Ghost(float size, int screenWidth, int screenHeight, PictureDrawable ghostBase) { + public Ghost(float size, int screenWidth, int screenHeight, Bitmap ghostBase) { super(size, screenWidth, screenHeight, ghostBase); paint.setARGB(0, 0, 0, 0); setType(GhostType.WHITE); @@ -20,7 +22,7 @@ public void setType(GhostType type) { this.type = type; switch (type) { case WHITE: - paint.setARGB(0, 0, 0, 0); + paint.setARGB(200, 255, 255, 255); setSize(50); break; case RED: @@ -46,6 +48,11 @@ public void setType(GhostType type) { default: break; } + + //ColorFilter cf = new LightingColorFilter(paint.getColor(), 1); + if (type != GhostType.WHITE) { + paint.setColorFilter(new PorterDuffColorFilter(paint.getColor(), PorterDuff.Mode.ADD)); + } } public void random(int width, int height) { diff --git a/src/com/wurbo/ghostcatcher/entities/Player.java b/src/com/wurbo/ghostcatcher/entities/Player.java index aaece56..3a78ff7 100644 --- a/src/com/wurbo/ghostcatcher/entities/Player.java +++ b/src/com/wurbo/ghostcatcher/entities/Player.java @@ -1,6 +1,6 @@ package com.wurbo.ghostcatcher.entities; -import android.graphics.drawable.PictureDrawable; +import android.graphics.Bitmap; public class Player extends Entity { @@ -9,7 +9,7 @@ public class Player extends Entity { private long shieldTime = 10000; private int score = 0; - public Player(float size, int screenWidth, int screenHeight, PictureDrawable playerBase) { + public Player(float size, int screenWidth, int screenHeight, Bitmap playerBase) { super(size, screenWidth, screenHeight, playerBase); paint.setARGB(0, 0, 0, 0); }