2014년 3월 9일 일요일

cocos2dx를 이용한 live wallpaper만들기

cocos2dx 안드로이드 프로젝트의 기본 main activity를 라이브웰 페이퍼 구성을 위해 아래와 같이 WallpaperService로 변경하는 것으로 구현이 가능합니다.
(cocos2d-x 2.x버전에서 테스트)

import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.lib.Cocos2dxHelper;
import org.cocos2dx.lib.Cocos2dxHelper.Cocos2dxHelperListener;
import org.cocos2dx.lib.Cocos2dxRenderer;

import android.content.Context;
import android.content.SharedPreferences;
import android.opengl.GLSurfaceView.Renderer;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class WallpaperCocos2dxService extends WallpaperService
{

static
{
System.loadLibrary("cocos2dcpp");
}

@Override
public Engine onCreateEngine()
{
        return new Cocos2dxWallpaperEngine();
}

public class Cocos2dxWallpaperEngine extends Engine implements Cocos2dxHelperListener,
SharedPreferences.OnSharedPreferenceChangeListener
{

protected class WallpaperCocos2dxGLSurfaceView extends Cocos2dxGLSurfaceView 
{
            WallpaperCocos2dxGLSurfaceView(Context context) 
            {
                super(context);
            }

            @Override
            public SurfaceHolder getHolder() 
            {
                return getSurfaceHolder();
            }

            public void onDestroy() 
            {
                super.onDetachedFromWindow();
            }
}

protected class WallpaperCocos2dxRenderer extends Cocos2dxRenderer
{
// prevent the cocos2dxRenderer from calling nativeInit
@Override
public void onSurfaceCreated(final GL10 pGL10, final EGLConfig pEGLConfig)
{
}

@Override
public void onSurfaceChanged(final GL10 pGL10, final int pWidth, final int pHeight)
{
setScreenWidthAndHeight(pWidth, pHeight);
super.onSurfaceCreated(null, null);
}

}

private WallpaperCocos2dxGLSurfaceView m_WallpaperCocos2dxGLSurfaceView;
@Override
public void onCreate(SurfaceHolder surfaceHolder)
{
super.onCreate(surfaceHolder);
//setTouchEventsEnabled(true);
            m_WallpaperCocos2dxGLSurfaceView = new WallpaperCocos2dxGLSurfaceView(WallpaperCocos2dxService.this);
            m_WallpaperCocos2dxGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
            
            //mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
            
            setRenderer(getNewRenderer());
            Cocos2dxHelper.init(WallpaperCocos2dxService.this, this);
}

@Override
public void onVisibilityChanged(boolean visible)
{
            super.onVisibilityChanged(visible);
            if (visible) 
            {
                Cocos2dxHelper.onResume();
                m_WallpaperCocos2dxGLSurfaceView.onResume();
            } 
            else 
            {
                Cocos2dxHelper.onPause();
                m_WallpaperCocos2dxGLSurfaceView.onPause();            
            }
}

@Override
public void onDestroy()
{
            super.onDestroy();

            Cocos2dxHelper.end();

            m_WallpaperCocos2dxGLSurfaceView.onDestroy();
}

protected void setRenderer(Renderer renderer) 
{
            m_WallpaperCocos2dxGLSurfaceView.setCocos2dxRenderer((Cocos2dxRenderer) renderer);
}
   
Renderer getNewRenderer()
{
            Cocos2dxRenderer renderer = new WallpaperCocos2dxRenderer();
            return renderer;
}

@Override
public void showDialog(String pTitle, String pMessage)
{
// TODO Auto-generated method stub
}

@Override
public void showEditTextDialog(String pTitle, String pMessage, int pInputMode, int pInputFlag, int pReturnType, int pMaxLength)
{
}

@Override
public void runOnGLThread(Runnable pRunnable)
{
m_WallpaperCocos2dxGLSurfaceView.queueEvent(pRunnable);
}

@Override
public void onTouchEvent(MotionEvent event)
{
            m_WallpaperCocos2dxGLSurfaceView.onTouchEvent(event);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String sKey)
{

}
}

}

댓글 없음:

댓글 쓰기