i am using the following template to program my 2D games in. is there anyway i can improve it?
splash screen:
package com.dingle.template2d;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class template2d extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set screen
setContentView(R.layout.splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
boolean _active;
int _splashTime;
_active = true;
_splashTime = 500;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
startActivity(new Intent("com.dingle.template2d.MENU"));
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
}
}
};
splashTread.start();
}
}
menu screen:
package com.dingle.template2d;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class menu extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.button();
}
private void button(){
Button play_button = (Button)this.findViewById(R.id.play_button);
play_button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
// parentButtonClicked(v);
startActivity(new Intent("com.dingle.template2d.GAME"));
}
});
}
}
GameView class: (this is where i put all the game code)
package com.dingle.template2d;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
public class GameView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
public class Panel extends SurfaceView implements SurfaceHolder.Callback{
private MainThread _thread;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new MainThread(getHolder(), this);
setFocusable(true);
}
final int windowHeight = getResources().getDisplayMetrics().heightPixels;
final int windowWidth = getResources().getDisplayMetrics().widthPixels;
final float tscale = getResources().getDisplayMetrics().density;
final int scale = (int) tscale;
public int _x = 0;
public int _y = 0;
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
/**
*
*I Declare everything here
*
**/
@Override
public void onDraw(Canvas canvas) {
/**
*
*I run all my code here
*
**/
}
/* @Override
public boolean onTouchEvent(MotionEvent event) {
_x = (int) event.getX();
_y = (int) event.getY();
return true;
}*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
}
Main Thread:
package com.dingle.template2d;
import com.dingle.template2d.GameView.Panel;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MainThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public MainThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
Also does anyone know of any good 3d that i could try? Thankyou very much for any help in advance.