검색결과 리스트
글
///////////////////////////////////////////////////////////////////////////
///////////////////////// 명령어 /////////////////////////
///////////////////////////////////////////////////////////////////////////
////////////////////////////// 뷰와 캔버스 //////////////////////////////
public class SJView extends View {
public SJView(Context context){
super(context);
}
protected void onDraw(Canvas canvas){
Paint paint = new Paint();
paint.setARGB(a, r, g, b); //알파 투명도와 RGB색상 설정
paint.setAlpha(a); //투명도 설정
paint.setAntiAlias(aa) //경계면의 부드러움 설정
paint.setColor(Color.RED); //색상 설정
paint.setStyle(Paint.Style.STROKE); //FILL : 색상 채움, FILL_AND_STROKE : 색상과 선을 그림, STROKE : 선을 그린다.
paint.setTextSize(size); //글자크기 설정
paint.setTextAlign(align); //글자를 정렬
canvas.drawColor(Color);
canvas.drawRect(x1, y1, x2, y2, paint);
canvas.drawPoint(x, y, paint);
canvas.drawLine(x1, y1, x2, y2, paint);
canvas.drawCircle(x, y, radius, paint);
canvas.drawRect(RectF, rx, ry, paint);
canvas.drawOval(RectF, r, paint);
canvas.drawArc(RectF, startAngle, sweepAngle, useCenter, Paint);
canvas.drawText(text, x, y, paint);
canvas.drawBitmap(bitmap, x, y, paint);
//비트맵 이미지 생성
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap bitmap = Bitmap.createScaledBitmap(bitmap, 150, 150, false);
Bitmap bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
bitmap.recycle(); //비트맵 자원 반환
//메트릭스
Matrix matrix = new Matrix();
matrix.preRotate(90);
matrix.preScale(1, -1);
matrix.prescale(-1, 1);
}
}
////////////////////////////// 서피스 뷰 //////////////////////////////
public class MainSView extends SurfaceView implements SurfaceHolder.Callback{
private GameActivity gameActivity;
MainThread mainThread;
Handler handler;
Context mainConext;
boolean drawCls = false;
public MainSView(Context r, AttributeSet a){
super(r, a);
getHolder().addCallback(this);
mainThread = new MainThread(getHolder(), this);
setrFocusable(true);
mainContext = r;
}
public init(int w, int h, GameActivity gameActivity){
this.gameActivity = gameActivity;
drawCls = true;
}
public void onDraw(Canvas canvas){
if (drwCls == false){
return;
}
//그림...
}
public boolean onTouchEvent(MotionEvent event){
return true;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
}
public void surfaceCreated(SurfaceHolder holder){
mainThread.setRunning(true);
try{
if(mainThread.getState() == Thread.State.TERMINATED){
mainThread = new MainThread(getHolder(), this);
mainThread.setRunning(true);
setFocusable(true);
mainThread.start();
}else{
mainThread.start();
}
}catch(Exception ex){
}
}
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
mainThread.setRunning(false);
while(retry){
try{
mainThread.join();
retry = false;
}catch(Exception e){
}
}
}
}
////////////////////////////// 터치 이벤트 //////////////////////////////
int pointer_id = -1;
public boolean onTouchEvent(MotionEvent event){
if(drawCls == true) {
final int action = event.getAction();
pointer_id = event.getPointerId(0);
final float x = event.getX();
final float y = event.getY();
switch(action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
break.
case MotionEvent.ACTION_MOVE:
break.
case MotionEvent.ACTION_UP:
break.
case MotionEvent.ACTION_CANCEL:
break.
}
}
return true;
}
////////////////////////////// 멀티터치 이벤트 //////////////////////////////
public boolean onTouchEvent(MotionEvent event){
if(drawCls == true) {
final int action = event.getAction();
pointer_id = event.getPointerId(0);
switch(action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
final float x = event.getX();
final float y = event.getY();
break.
case MotionEvent.ACTION_MOVE:
for(int i=0; i<event.getPointerCount(); i++){
pointer_id = event.getPointerId(i);
final float x = (int)event.getX(i);
final float y = (int)event.getY(i);
}
break.
case MotionEvent.ACTION_UP:
pointer_id = event.getPointerId(0);
final float x = (int)event.getX();
final float y = (int)event.getY();
break.
case MotionEvent.ACTION_CANCEL:
pointer_id = event.getPointerId(0);
final float x = (int)event.getX();
final float y = (int)event.getY();
break.
case MotionEvent.ACTION_POINTER_DOWN:
final int pointerIndex = (action& MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.getPointerId(pointerIndex);
final float x = event.getX(pointerId);
final float y = event.getX(pointerId);
break;
case MotionEvent.ACTION_POINTER_UP:
final int pointerIndex = (action& MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.getPointerId(pointerIndex);
final float x = event.getX(pointerId);
final float y = event.getX(pointerId);
}
}
return true;
}
////////////////////////////// 가상화면 설정 //////////////////////////////
////////////////////////////// 파일 출력 //////////////////////////////
FileoutputStream fos = openFileoutput("test.txt", MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE);
String fileCont = "hahaha";
fos.write(fileCont.getBytes());
fos.close();
////////////////////////////// 파일 입력 //////////////////////////////
FileInputStream fis = new FileInputStream(getFilesDir() + "/" + "test.txt");
DataInputStream dataIO = new DataInputStream(fis);
String strLine = null;
while((strLine = dataIO.readLine()) != null){
}
dataIO.close();
fis.close();
////////////////////////////// 게임 사운드 //////////////////////////////
SoundPool soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
try{
AssetManager assetManager = getAssets();
AssetFileDescriptor assetFileDescriptor = assetManager.openFd("walking.ogg");
sound_walking = soundPool.load(assetFileDescriptor, 1);
soundPool.play(sound_walking, 1.0f, 1.0f, 0, 0, 1.0f);
}catch(Exception e){
}
////////////////////////////// 스프라이트 이미지 //////////////////////////////
////////////////////////////// 센서 사용 //////////////////////////////
public GameSensorActivity extends Activity implements SensorEventListener{
private SensorManager mSensorManager = null;
private Sensor mOrientationSensor = null;
private Sensor mAccelerometersSensor = null;
private GameSensorView mGameSensorView;
public void onCreate(bundle savedInstanceState){
super.onCreate(savedInstanceState);
mGameSensorView = new GameSensorView(this);
setContentView(mGameSensorView);
//센서 관리자
mSensorManager = (SensorManager)getSyystemService(context.SENSOR_SERVICE);
//가속계 센서
mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//방향계 센서
mOrientationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
//onStart되었을 때
protected void onStart(){
super.onStart();
if(mAccelerometerSensor != null){
mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
}
if (mOrientationSensor != null){
mSensorManager.registerListener(this, mOrientationSensor, SensorManager.SENSOR_DELAY_GAME);
}
}
//onStop되었을 때
protected void onStop(){
if(mAccelerometerSensor !=null || mOrientationSensor !=null){
mSensorManager.unregisterListener(this);
}
super.onStop();
}
//onDestroy되었을 때
protected void onDestroy(){
if (mAccelerometerSensor != null || mOrientationSensor != null){
mSensorManager.unregisterListener(this);
}
super.onDestroy();
}
//센서가 ???
public void onAccuracyChanged(Sensor sensor, int accuracy){
}
//센서 내용이 바뀌었을 때
public void onSensorChanged(SensorEvent event){
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION){
mGameSENSORView.moveSensorOrientation(event.values[0], event.values[1], event.values[2]);
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETTER){
mGameSensorView.moveSensorAccelerometer(event.values[0], event.values[1], event.values[2]);
}
mGameSensorView.invalidate();
}
}
////////////////////////////// OpenGL ES //////////////////////////////
//////////////////// 서피스 뷰 만들기
//////////////////// OpenGL ES를 위한 스레드를 만들기(GL객체는 생성된 스레드에서만 수행 가능)
//////////////////// 초기화 작업 (해당 스레드에서 EGL, GL객체 생성)
EGL10 mEGL;
EGLDisplay mGLDisplay;
EGLConfig mGLConfig;
EGLSurface mGLSurface;
EGLContext mGLContext;
GL10 mGL;
///// 오류와 스레드를 점검해서 EGL객체를 생성
m EGL = (EGL10) GLDebugHelper.wrap(EGLContext.getEGL(), GLDebugHelper.CONFIG_CHECK_GL_ERROR | GLDebugHelper.CONFIG_CHECK_THREAD, null);
///// EGLDisplay객체를 얻는다.
mGLDisplay = mEGL.eglGetDisplay(EGL10, EGL10.EGL_DEFAULT_DISPLAY);
int[] curGLVersion = new int[2];
///// EGL을 초기화
mEGL.eglInitialize(mGLDisplay, curGLVersion);
///// GLDisplay의 구성을 요청
int[] mConfigSpec = {EGL10.EGL_RED_SIZE, 5, EGL10.EGL_GREEN_SIZE, 5, EGL10.EGL_BLUE_SIZE, 5, EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE);
EGLConfig[] configs = new EGLConfig[1];
mEGL.eglChooseConfig(mGLDisplay, mConfigSpec, configs, 1, num_config);
///// EGL표면을 생성
mGLConfig = configs[0];
mGLSurface = mEGL.eglCreateWindowSurface(mGLDisplay, mGLConfig, mMainView.getHolder(), null);
///// GLContext를 구한다.
mGLContext = mEGL.eglCreateContext(mGLDisplay, mGLConfig, EGL10.EGL_NO_CONTEXT, null);
///// EGL에 디스플레이, 표면, 컨텍스트 객체를 명시
mEGL.eglMakeCurrent(mGLDisplay, mGLSurface, mGLSurface, mGLContext);
///// 오류와 스레드를 점검해서 GL10 객체 생성
mGL = (GL10)GLDebugHelper.wrap(mGLContext.getGL(), GLDebugHelper.CONFIG_CHECK_GL_ERROR | GLDebugHelper.CONFIG_CHECK_THREAD, null);
//////////////////// 스레드에서 뷰로 parameter를 이용해 넘기고, 뷰에서 화면을 설정
///// glOrthof의 영역은 glViewport안에서 가상(?)범위 지정
mGl.glOrthof(0, 480, 0, 800, 1, -1);
///// glViewport로 정한 영역 안이 전체 화면
mGl.glViewport(0, 0, mWidth, mHeight);
///// 화면 초기화
mGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
///// 직교투영 모드 설정 //GL_PROJECTION=직교 투영, GL_MODELVIEW=원근투영
mGL.glMatrixMode(GL10.GL_PROJECTION);
///// 행렬 좌표 연산을 초기화한다.
mGL.glLoadIdentity();
//////////////////// 정점과 선
///// 정점과 선을 이어 그림을 그린다. X, Y좌표 3곳을 설정
float[] vertexArray = {0.0f, 0.0f, 240.0f, 400.0f, 480.0f, 0.0f};
///// float형 좌표를 이용해 FloatBuffer 만들기
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertexArray.length*4);
byteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuffer.asFloatBuffer();
vertexBuffer.put(vertexArray);
vertexBuffer.flip();
///// verticeArray : 정점과 이미지 좌표를 하나로 나열
///// indexArray :
///// normalArray :
//////////////////// 매핑
///// vertexBufferTex라는 FloatBuffer 만들기
///// 비트맵 이미지 부르기
Bitmap bm = null;
AssetManager am = mMainContext.getAssets();
BufferedInputStream buf = new Buffered InputStream(am.open("hahaha.png"));
bitmap = BitmapFactory.decodeStream(buf);
///// 텍스처 이미지 매핑
int[] textures = new int[1];
mGL.glGenTextures = new int[1];
mGL.glGenTextrues(1, textures, 0);
texBitmap = textures[0];
mGL.glBindTexture(GL10.GL_TEXTURE_2D, texBitmap);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0 , bitmap, 0);
///// 필터 적용 이미지 매핑, 확대축소될 경우를 적용
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
mGL.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
mGL.glBindTexture(GL10.GL_TEXTURE_2D, 0);
bitmap.recycle();
//////////////////// onDraw에 그림 그리기
///// RGBA 지정
mGl.glColor4f(1f, 0f, 0f, 1f);
///// 정점 배열을 사용 가능케 함
mGL.glEnaleClientState(GL10.GL_VERTEX_ARRAY);
///// 저장한 vertexBuffer의 좌표 배열을 지정
mGL.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
///// 해당 좌표로 삼각형을 그림
mGL.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
///// 백 버퍼를 표현한다? 그린다?
mEGL.eglSwapBuffers(mGLDisplay, mGLSurface);
//////////////////// onDraw에 그림 그리기 (매핑의 경우)
mGl.glClear(GL10.GL_COLOR_BUFFER_BIT);
mGL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
///// 텍스처가 조립된 배열을 사용 가능케 함
mGL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
///// vertexBuffer.position의 인덱스값을 2로 설정
mGL.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
mGL.glEnable(GL10.GL_TEXTURE_2D);
mGL.glBindTexture(GL10.GL_TEXTURE_2D, texBitmap);
mGL.glTexCoordPointer(2, GL10.GL_FLOAT, 0, vertexBufferTex);
mGL.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
mEGL.eglSwapBuffers(mGLDisplay, mGLSurface);
//////////////////// onDraw에 그림 그리기 (매핑의 경우2)
mGl.glClear(GL10.GL_COLOR_BUFFER_BIT);
mGL.glEnable(GL10.GL_TEXTURE_2D);
mGL.glBindTexture(GL10.GL_TEXTURE_2D, texBitmap);
///// 버텍스 배열을 사용 가능케 함
mGL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
///// 텍스처가 조립된 배열을 사용 가능케 함
mGL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
///// vertexBuffer.position의 인덱스값을 2로 설정
mGL.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertexBuffer);
vertexBuffer.position(2);
mGL.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertexBuffer);
///// 삼각형 그리기
mGL.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
mGL.glDrawElements(GL10.GL_LINE_LOOP, 6, GL10.GL_UNSIGNED_SHORT, indexBuffer);
mEGL.eglSwapBuffers(mGLDisplay, mGLSurface);
mGL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
///// 텍스처 배열 사용 가능케 함
mGL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
mGL.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
///// 색상을 지정
mGL.glEnableClientState(GL10.GL_VERTEX_ARRAY);
mGL.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
mGL.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
//////////////////// 뷰가 종료될 때, 스레드도 종료시킨다.
///// 서피스뷰가 종료될 때, 해당 스레드의 requestStop메소드를 호출
public void surfaceDestroyed(SurfaceHolder holder){
if (mMainThread != null) mMainThread.requestStop();
}
///// 해당 스레드의 reqeustStop 메소드에서 EGL객체를 종료
mEGL.eglMakeCurrent(mGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
mEGL.eglDestroySurface(mGLDisplay, mGLSurface);
mEGL.eglDestroyContext(mGLDisplay, mGLContext);
mEGL.eglTerminate(mGLDisplay);
'LANGUAGE > ANDROID' 카테고리의 다른 글
Android 기초 (0) | 2015.05.04 |
---|---|
OpenGL (0) | 2015.05.04 |