겨울팥죽 여름빙수


1. MainActivity

아래 코드를 보면 알 수 있듯이, MainActivity자체는 별로 하는 일이 없다. setContentView로 화면에 보여질 뷰를 MainGLSurfaceView로 설정한다. openGLVerstion는 MainGLSurfaceView 클래스 내에서, GL버전을 설정하는데 쓰인다.


public class MainActivity extends Activity {
	public static MainGLSurfaceView view;
	public static Context 			context = null;
	public static final int			openGLVerstion = 2;
        
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		context = this;
		
		/*
		 * Setting OpenGL.
		 * Version : 2.0 
		 * Renderer : MainGLRenderer
		 */
		view = new MainGLSurfaceView(this);
		setContentView(view);
	}
}


2. GLSurfaceView 

  아래 클래스는 GLSurfaceView를 상속받아 커스톰하게 만들었다. 사실 커스톰 클래스로 만들지 않아도 되지만, 나중에 이벤트 처리 때문에, 그렇게 만들었다(자세한 내용은 이벤트 처리 설명에서..). 

public class MainGLSurfaceView extends GLSurfaceView 
{
	MainGLRenderer mRenderer;
	public MainGLSurfaceView(Context con)
	{
		super(con);
		mRenderer = new MainGLRenderer();
		this.setEGLContextClientVersion(MainActivity.openGLVerstion);
		this.setPreserveEGLContextOnPause(MainActivity.saveGLSurface);
		this.setRenderer(mRenderer);
	}
}


  - setEGLContextClientVersion : opengles버전을 설정한다. 버전을 설정하기 위해서는, AndroidManifast.xml, 

jni/Android.mk 파일들도 수정해야 한다(아래 예제 참고).

- setPreserveEGLContextOnPause : 이 함수를 사용하지 않으면, pause시 opengles와 관련된 데이터들이 모두 지워진다. 때문에,

     resume 시 다시 gl데이터를 다시 복구 시켜줘야하는데, 그 과정이 생각보다 귀찮다. API11부터는 이 함수를 이용해 gl데이터가 

     사라지지 않도록 할 수 있다.

  - setRenderer : 렌더러 객체를 설정한다.



3. Renderer 인터페이스

  Renderer인터페이스를 사용하기 위해서는, onDrawFrame, onSurfaceChanged, onSurfaceCreated함수를 구현해 주어야 한다. 아래 클래스는 그 예이다.


public class MainGLRenderer implements Renderer { long preTime; long diffTime; boolean isFirst = true; float frameTime = 1000/30; @Override public void onDrawFrame(GL10 gl) { if(isFirst) { MainNDK.render(0); preTime = System.currentTimeMillis(); isFirst = false;

return;

} diffTime = System.currentTimeMillis() - preTime; MainNDK.render(diffTime);

} @Override public void onSurfaceChanged(GL10 gl, int width, int height) { Log.i("AndroidOpenGL", "onSurfaceChanged start"); MainNDK.onResize(width, height); Log.i("AndroidOpenGL", "onSurfaceChanged end"); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { Log.i("AndroidOpenGL", "onSurfaceCreated start"); MainNDK.init(); MainActivity.isGLCreated = true; Log.i("AndroidOpenGL", "onSurfaceCreated end"); } }


  - onDrawFrame : 매 프레임마다 호출되는 함수이다. 내가 설정한 프레임 속도에 맞춰 c++의 render함수를 호출한다

    (추후 JNI글에서 설명).

  - onSurfaceChanged : 화면이 생성되거나, 회전 등에 의해 화면 크기가 변경되는 경우에 호출 된다. 여기서 c++의 onResize함수 호출

  - onSurfaceCreated : gl뷰가 생성되었을 때, 딱 한번 호출된다. 이 함수가 호출돼야 c++에서 gl함수를 사용할 수 있다. 한마디로 gl문맥이 만들어 졌다고 생각하면 된다. 여기서 c++의 init함수 호출




4. AndroidManifast.xml파일


<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.snj.opengles"

    android:versionCode="1"

    android:versionName="1.0" >


    <uses-sdk

        android:minSdkVersion="11"

        android:targetSdkVersion="18" />

    <uses-feature android:glEsVersion="0x00020000" android:required="true" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" 

        android:hardwareAccelerated="true">

        <meta-data android:name="com.google.android.gms.version"

               android:value="@integer/google_play_services_version"/>

        <activity

            android:name="com.snj.opengles.MainActivity"

            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

            android:label="@string/app_name" 

             android:screenOrientation="landscape"

            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"

         >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

<activity android:name="com.google.android.gms.ads.AdActivity"

             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    </application>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.INTERNET"/>

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

  <uses-permission android:name="android.permission.BLUETOOTH" />

</manifest>



'게임을 만들자 > NDK & OpenGL ES' 카테고리의 다른 글

OpenGLES2.0 GLSL, 2D Basic shader  (1) 2014.09.18
OpenGLES 2.0,GLSL lighting code  (0) 2014.09.15
GLSL, OpenGLES2.0 glFog  (0) 2014.09.12
NDK AdMob, Can't see admob on Framelayout  (0) 2014.09.10
이클립스 NDK설정  (0) 2014.04.15
profile

겨울팥죽 여름빙수

@여름빙수

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!