package com.dubiousoft.sakana;

public class GameScreenThread extends Thread {
	
	public interface Listener {
        public void onThreadEvent( long milliSeconds );
    }
	private Listener m_listener;	
	private boolean m_running;
	
	public GameScreenThread( Listener listener ) {
		m_listener = listener;
	}
	
	@Override
	public void run() {
		m_running = true;
		long lastTime = System.currentTimeMillis()-16;
		while (m_running) {
			try {
				long elapsed = System.currentTimeMillis() - lastTime;
				lastTime = System.currentTimeMillis();				
				m_listener.onThreadEvent( elapsed );	
				elapsed = 16 - (System.currentTimeMillis() - lastTime);
				if (elapsed > 0) {
					Thread.sleep( elapsed );
				}				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void stopRun() {
		m_running = false;
	}
}
