-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Android commit (Not the same functionality as the desktop ver…
…sion yet!)
- Loading branch information
Showing
11 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
Android/src/androidTest/java/me/winspeednl/libz/ApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
131
Android/src/main/java/me/winspeednl/libz/core/GameCore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
Android/src/main/java/me/winspeednl/libz/screen/Render.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
Android/src/test/java/me/winspeednl/libz/ExampleUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |