Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
Reducing CPU load by removing stupid stuff.
Browse files Browse the repository at this point in the history
Just preloading bitmaps, and other easy things.
  • Loading branch information
darnuria committed Mar 18, 2016
1 parent 0efad6c commit 889520f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
7 changes: 3 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="android.rushdroid">



<application
android:name=".GameApplication"
android:allowBackup="true"
Expand All @@ -20,6 +17,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".GameActivity"></activity>
<activity android:name=".GameActivity"
android:hardwareAccelerated="true">
</activity>
</application>
</manifest>
16 changes: 1 addition & 15 deletions app/src/main/java/android/rushdroid/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,7 @@ public class GameActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

Model game = ((GameApplication) this.getApplication()).game();
// Right now this is useless Since the game return a Immutable List of Piece.
/*
Map<Position, Integer> m = new HashMap<>();
for (int l = 0; l < 6; l++) {
for (int c = 0; c < 6; c++) {
Position p = new Position(c,l);
Integer i = game.getIdByPos(p);
if (i != null) { m.put(p,i); }
}
}
*/
this.game = game;

this.game = ((GameApplication) this.getApplication()).game();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/android/rushdroid/GameThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void run() {
}
}
try {
Thread.sleep(100);
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
Expand Down
34 changes: 15 additions & 19 deletions app/src/main/java/android/rushdroid/GameView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package android.rushdroid;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
Expand All @@ -16,8 +17,6 @@
import android.view.SurfaceHolder;
import android.view.SurfaceView;

// import android.graphics.Bitmap;

/**
* Created by Corentin-Axel on 08/02/16.
* Classe responsible of the rendering. Use a GameThread for drawing of the UI Thread.
Expand All @@ -31,14 +30,11 @@ final public class GameView extends SurfaceView {
private final int game_height = 6;
private final int[] xs = new int[game_width];
private final int[] ys = new int[game_height];
private final Context context;
private Integer current = null;
private final Model m;
private final Bitmap[] bitmaps = new Bitmap[4];
private int down_x;
private int down_y;
private final Model m;
private boolean ret;
// private Bitmap bitmap;

private void fillArray(int[] a, int game_size, int surface_size) {
int d = game_size / surface_size;
a[0] = 1;
Expand All @@ -49,7 +45,11 @@ private void fillArray(int[] a, int game_size, int surface_size) {

public GameView(@NonNull Context context, @NonNull AttributeSet attrs) {
super(context, attrs);
this.context = context;

this.bitmaps[0] = BitmapFactory.decodeResource(getResources(), R.drawable.vertical2);
this.bitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.vertical3);
this.bitmaps[2] = BitmapFactory.decodeResource(getResources(), R.drawable.horizontal2);
this.bitmaps[3] = BitmapFactory.decodeResource(getResources(), R.drawable.horizontal3);
this.m = ((GameApplication) context.getApplicationContext()).game();

getHolder().addCallback(new SurfaceHolder.Callback() {
Expand Down Expand Up @@ -115,28 +115,26 @@ private void drawGame(@NonNull Canvas c, Iterable<Piece> pieces) {
for (Piece p : pieces) {
int xp = p.getPos().getCol();
int yp = p.getPos().getLig();
int x = xp * ratioX;
int y = yp * ratioY;
int x2, y2;
int id;
int x2;
int y2;
Bitmap bitmap;

if (p.getOrientation() == Direction.VERTICAL) {
x2 = (xp + 1) * ratioX;
y2 = (yp + p.getSize()) * ratioY;
id = (p.getSize() == 2) ? (R.drawable.vertical2) : (R.drawable.vertical3);
bitmap = (p.getSize() == 2) ? (this.bitmaps[0]) : (this.bitmaps[1]);
} else {
x2 = (xp + p.getSize()) * ratioX;
y2 = (yp + 1) * ratioY;
id = (p.getSize() == 2) ? (R.drawable.horizontal2) : (R.drawable.horizontal3);
bitmap = (p.getSize() == 2) ? (this.bitmaps[2]) : (this.bitmaps[3]);
}
c.drawBitmap(BitmapFactory.decodeResource(getResources(), id), null, new RectF(x, y, x2, y2), null);
c.drawBitmap(bitmap, null, new RectF(xp * ratioX, yp * ratioY, x2, y2), null);
}
drawGrid(c);
}

@Override
public void onDraw(@NonNull Canvas c) {
Model m = ((GameApplication) this.context.getApplicationContext()).game();
if (!m.endOfGame()) {
c.drawColor(Color.BLACK);
drawGrid(c);
Expand Down Expand Up @@ -184,9 +182,7 @@ public boolean onTouchEvent(@NonNull MotionEvent e) {
}
case MotionEvent.ACTION_UP: {
if (this.current != null) {
this.ret = m.getOrientation(current) == Direction.VERTICAL
? help_move(down_y, y)
: help_move(down_x, x);
if (m.getOrientation(current) == Direction.VERTICAL) { help_move(down_y, y); } else { help_move(down_x, x); }
this.current = null;
}
return true;
Expand Down

0 comments on commit 889520f

Please sign in to comment.