Sign in to follow this  
marod

How can I handle two SurfaceView in one game loop?

Recommended Posts

Hello everyone

I'm making a game in android, and I have two clases which extends SurfaceView (TitleView and GameView). I have another class too called GameLoopThread that I initialize in the TitleView that draws all my bitmaps in this SurfaceView, but it only works in this view. So when I want to change to the GameView, how I only initialize the GameLoopThread in the TitleView and not in the GameView, this last is a black screen because nothing is drawed on it.

So, I want to know how can I use the same class of the GameLoop to renderize this two views. 

For example, I initialice the GameLoop in the Titleview so it renders the buttons in the canvas, NewGame and Exit. But when I press the new game button and start the GameView activity, how this class doesn't have the GameLoop initializated, it doesn't render nothing.

Someone can help me?

This is my GameLoopThread class:
 

@SuppressLint("WrongCall")
public class GameLoopThread extends Thread {

static final long FPS = 25;
//private GameView view;
private TitleView tview;
private boolean running = false;

public GameLoopThread(TitleView titleView) {
this.tview = titleView;
}
public void setRunning(Boolean run) {
running = run;
}

@Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = tview.getHolder().lockCanvas();
synchronized (tview.getHolder()) {
tview.onDraw(c);
}
} finally {
if (c != null) {
tview.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0) {
sleep(sleepTime);
} else {
sleep(25);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}

} 

Thank you

Edited by marod

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this