Created
March 17, 2017 08:55
-
-
Save sugiartocokrowibowo/db47faf909f0cbc6833eac3a4a7f7fbd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package dev.androidgame.gameloop; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.pm.ActivityInfo; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
import android.view.View; | |
import android.view.Window; | |
import android.view.WindowManager; | |
import android.view.View.OnTouchListener; | |
public class MainActivity extends Activity { | |
GameView gameView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
requestWindowFeature(Window.FEATURE_NO_TITLE); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
/*inisialisasi gameView dan menetapkannya sebagai view dari Activity*/ | |
gameView = new GameView(this); | |
setContentView(gameView); | |
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | |
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
gameView.resume(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
gameView.pause(); | |
} | |
class GameView extends SurfaceView implements OnTouchListener, Runnable{ | |
private Context context; | |
private Thread thread; | |
private SurfaceHolder surfaceHolder; | |
private volatile boolean running; | |
private final static int MAX_FPS = 60; | |
private final static int MAX_FRAME_SKIPS = 5; | |
private final static int FRAME_PERIOD = 1000 / MAX_FPS; | |
//sebuah objek canvas dan paint | |
Canvas canvas; | |
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
float sx,sy; | |
int pembagi = 100; | |
boolean initialized = false; | |
public GameView(Context context) { | |
super(context); | |
this.context = context; | |
surfaceHolder = getHolder(); | |
setOnTouchListener(this); | |
} | |
public void resume() { | |
thread = new Thread(this); | |
running = true; | |
thread.start(); | |
} | |
public void pause(){ | |
running = false; | |
try { | |
thread.join(); | |
} catch (InterruptedException e) { | |
Log.e("Error:", "joining thread"); | |
} | |
} | |
public void exit(){ | |
running = false; | |
System.exit(0); | |
} | |
private void initialize(Canvas canvas){ | |
if(!initialized){ | |
sx = canvas.getWidth()/(float)pembagi; | |
sy = canvas.getHeight()/(float)pembagi; | |
initialized = true; | |
} | |
} | |
private void update() { | |
// | |
} | |
private void draw() { | |
if (surfaceHolder.getSurface().isValid()) { | |
canvas = surfaceHolder.lockCanvas(); | |
initialize(canvas); | |
canvas.drawColor(Color.WHITE); | |
//gambar disini untuk ditampilkan ke screen | |
for(int i=0;i<pembagi;i++){ | |
paint.setColor(Color.rgb(52, 152, 219)); | |
canvas.drawLine(0, sy*i, canvas.getWidth(), sy*i, paint); | |
paint.setColor(Color.rgb(231, 76, 60)); | |
canvas.drawLine(sx*i, 0, sx*i, canvas.getHeight(), paint); | |
} | |
surfaceHolder.unlockCanvasAndPost(canvas); | |
} | |
} | |
@Override | |
public void run() { | |
long beginTime; | |
long timeDiff; | |
int sleepTime = 0; | |
int framesSkipped; | |
//GAMELOOP | |
while (running) { | |
try { | |
beginTime = System.currentTimeMillis(); | |
framesSkipped = 0; | |
synchronized(surfaceHolder){ | |
update(); | |
draw(); | |
} | |
timeDiff = System.currentTimeMillis() - beginTime; | |
sleepTime = (int)(FRAME_PERIOD - timeDiff); | |
if (sleepTime > 0) { | |
try { | |
thread.sleep(sleepTime); | |
} catch (InterruptedException e) {} | |
} | |
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) { | |
update(); | |
sleepTime += FRAME_PERIOD; | |
framesSkipped++; | |
} | |
}finally{} | |
} | |
} | |
@Override | |
public boolean onTouch(View view, MotionEvent event) { | |
float x = event.getX(); | |
float y = event.getY(); | |
switch(event.getAction()& MotionEvent.ACTION_MASK) { | |
case MotionEvent.ACTION_DOWN: | |
// | |
break; | |
case MotionEvent.ACTION_UP: | |
// | |
break; | |
case MotionEvent.ACTION_MOVE: | |
// | |
break; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment