Skip to content

Commit

Permalink
Initial Android commit (Not the same functionality as the desktop ver…
Browse files Browse the repository at this point in the history
…sion yet!)
  • Loading branch information
svenar-nl committed Aug 22, 2016
1 parent 9bcc947 commit 6223882
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.winspeednl.libz;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
13 changes: 13 additions & 0 deletions Android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.winspeednl.libz">

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">

<activity android:name=".core.GameCore"></activity>
</application>

</manifest>
131 changes: 131 additions & 0 deletions Android/src/main/java/me/winspeednl/libz/core/GameCore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package me.winspeednl.libz.core;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import me.winspeednl.libz.screen.Render;

public class GameCore extends SurfaceView implements Runnable {

private SurfaceHolder surfaceHolder;
private Canvas canvas;
private Thread thread;
private LibZ game;
private Render renderer;
private Bitmap image;

private boolean isRunning;
private double frameCap = 1D / 60D;
private int fps;

public GameCore(Context context) {
super(context);

surfaceHolder = getHolder();
}

public void run() {
while (true) {
if (getWidth() > 0 && getHeight() > 0) {
renderer = new Render(this);
image = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
break;
}
}

double currTime;
double lastTime = System.nanoTime() / 1000000000D;
double passedTime;
double unprocessedTime = 0;
double frameTime = 0;
int frames = 0;

while(isRunning) {
if (!surfaceHolder.getSurface().isValid())
continue;

boolean render = false;

currTime = System.nanoTime() / 1000000000D;
passedTime = currTime - lastTime;
lastTime = currTime;

unprocessedTime += passedTime;
frameTime += passedTime;

while (unprocessedTime >= frameCap) {
game.update(this);
unprocessedTime -= frameCap;
render = true;

if(frameTime >= 1) {
frameTime = 0;
fps = frames;
frames = 0;
}

if (render) {
canvas = surfaceHolder.lockCanvas();
game.render(this, renderer);
image.setPixels(renderer.getPixels(), 0, getWidth(), 0, 0, getWidth(), getHeight());
canvas.drawBitmap(image, 0, 0, null);
surfaceHolder.unlockCanvasAndPost(canvas);

frames++;
} else {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

public void pause() {
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
thread = null;
}

public void resume() {
if (game != null) {
game.init(this);
isRunning = true;
thread = new Thread(this);
thread.start();
}
}

public void setGame(LibZ game) {
this.game = game;
}

public void clear(Canvas canvas) {
Paint clearPaint = new Paint();
clearPaint.setColor(Color.TRANSPARENT);
clearPaint.setStyle(Paint.Style.FILL);
Rect clearRect = new Rect();
clearRect.set(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawRect(clearRect, clearPaint);
}

public Canvas getCanvas() {
return canvas;
}

public int getFPS() {
return fps;
}
}
10 changes: 10 additions & 0 deletions Android/src/main/java/me/winspeednl/libz/core/LibZ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.winspeednl.libz.core;

import me.winspeednl.libz.screen.Render;

public abstract class LibZ {

public abstract void init(GameCore gc);
public abstract void update(GameCore gc);
public abstract void render(GameCore gc, Render r);
}
30 changes: 30 additions & 0 deletions Android/src/main/java/me/winspeednl/libz/screen/Render.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.winspeednl.libz.screen;

import me.winspeednl.libz.core.GameCore;

public class Render {
private int[] pixels;

private GameCore gc;

public Render(GameCore gc) {
this.gc = gc;
pixels = new int[gc.getWidth() * gc.getHeight()];
}

public void setPixel(int id, int color) {
if (id < pixels.length)
pixels[id] = color;
}

public void setPixel(int x, int y, int color) {
if ((x < 0 || x >= gc.getWidth() || y < 0 || y >= gc.getHeight()) || color == 0x00000000)
return;

pixels[x + y * gc.getWidth()] = color;
}

public int[] getPixels() {
return pixels;
}
}
Binary file added Android/src/main/res/drawable/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Android/src/main/res/drawable/cherry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Android/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
5 changes: 5 additions & 0 deletions Android/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
3 changes: 3 additions & 0 deletions Android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">LibZ</string>
</resources>
15 changes: 15 additions & 0 deletions Android/src/test/java/me/winspeednl/libz/ExampleUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.winspeednl.libz;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}

0 comments on commit 6223882

Please sign in to comment.