기존에 사용하던 소스에서 Thread를 별도의 클래스로 빼냄.

GameMain.java
public class GameMain extends Activity {
/** Called when the activity is first created. */
	
	public static GLGameSurfaceView mGLView;
	public static GLGameRenderer mGLRenderer;
	public static GameThread mThread;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        
        mGLRenderer = new GLGameRenderer(this);
        mGLView = new GLGameSurfaceView(this);
        mThread = new GameThread(this);

        setContentView(mGLView);
        mGLView.setVisibility(View.VISIBLE);
        
        mThread.start();
    }
    
	@Override
	public void onPause() {
		mThread.pauseThread();
		super.onPause();
	}

	@Override
	public void onResume() {
		mThread.resumeThread();
		super.onResume();
	}
}

GLGameRenderer, GLGameSurfaceView, GameThread 객체를 생성하고 Thread를 시작한다.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 화면을 항상 켬


GLGameRenderer.java
package pe.berabue.game;

import javax.microedition.khronos.egl.*;
import javax.microedition.khronos.opengles.*;

import android.content.*;
import android.opengl.GLSurfaceView.Renderer;

public class GLGameRenderer implements Renderer
{

	public static Context context;

	public GLGameRenderer(Context context)
	{
		GLGameRenderer.context = context;
	}
	
	@Override
	public void onDrawFrame(GL10 gl)
	{
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height)
	{
		gl.glOrthof(0.0f, 800.0f, 480.0f, 0.0f, 1.0f, -1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glViewport(0, 0, width, height);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig arg1)
	{
		gl.glClearDepthf(1.0f);
		gl.glDisable(GL10.GL_DEPTH_TEST);
		gl.glMatrixMode(GL10.GL_PROJECTION);
	}
}

onDrawFrame();
 지속적인 호출을 통해 그림을 그리는 부분.
onSurfaceChanged();
 화면이 바뀔때 호출
onSurfaceCreated();
 화면이 생성될때 호출


GLGameSurfaceView.java
package pe.berabue.game;

import android.content.*;
import android.opengl.*;
import android.view.*;

public class GLGameSurfaceView extends GLSurfaceView
{
	public GLGameSurfaceView(Context context)
	{
		super(context);
		setRenderer(GameMain.mGLRenderer);
		setRenderMode(RENDERMODE_WHEN_DIRTY); // One drawing
		setFocusableInTouchMode(true);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}
}

setRenderMode(RENDERMODE_WHEN_DIRTY);
 GLGameRenderer()의 onDrawFrame()을 1회 호출.



GameThread.java
package pe.berabue.game;

import android.content.*;

public class GameThread extends Thread 
{
	public static boolean isRun;
	public static boolean isWait;
	
	public GameThread(Context context)
	{
		isRun = true;
		isWait = false;
	}

	public synchronized void run()
	{
		while(isRun)
		{
			try
			{
				GameMain.mGLView.requestRender(); // onDrawFrame
				Thread.sleep(10);
			}
			catch (InterruptedException e1)
			{
				e1.printStackTrace();
			}
			if (isWait)
			{
				try
				{
					wait();
				}
				catch (Exception e)
				{
					// Ignore error
				}
			}
		}
	}
	
	/* Thread pause */
	public void pauseThread()
	{
		isWait = true;
		synchronized (this)
		{
			this.notify();
		}
	}

	/* Thread Resume */
	public void resumeThread()
	{
		isWait = false;
        synchronized (this)
        {
             this.notify();
        }
    }
}

GameMain.mGLView.requestRender();
 GLGameRenderer()의 onDrawFrame() 호출 



+ Recent posts