I guess I really should have posted an example, huh? Here's the
structure that works:
public class MyWallpaperService extends GLWallpaperService {
public MyWallpaperService() {
super();
}
public Engine onCreateEngine() {
MyEngine engine = new MyEngine();
return engine;
}
// prefs and sensor interface is optional, just showing that this is
where you do all of that - everything that would normally be in an
activity is in here.
class MyEngine extends GLEngine implements
SharedPreferences.OnSharedPreferenceChangeListener,
SensorEventListener {
MyRenderer renderer;
public MyEngine() {
super();
// handle prefs, other initialization
renderer = new MyRenderer();
setRenderer(renderer);
setRenderMode(RENDERMODE_WHEN_DIRTY);
}
public void onDestroy() {
super.onDestroy();
if (renderer != null) {
renderer.release(); // assuming yours has this method - it
should!
}
renderer = null;
}
On Feb 14, 11:52 am, Lance Nanek <[email protected]> wrote:
> >public GLSurfaceView mGLSurfaceView;
> ...
> >mGLSurfaceView = new GLSurfaceView(this);
> ...
> >mGLSurfaceView = new GLSurfaceView(MyLiveWallpaper.this);
>
> Remove the lines above. Any use of GLSurfaceView in this is wrong.
>
> >public Engine onCreateEngine() {
> >return new GLEngine();
> >}
>
> You are supposed to return an instance of your subclass of GLEngine
> here, "new CubeEngine()" in your case.
>
> >public class OpenGLRenderer implements GLSurfaceView.Renderer {
>
> You should be implementing GLWallpaperService.Renderer, not
> GLSurfaceView.Renderer.
>
> >private final Runnable mDrawCube = new Runnable() {
> >public void run() {
> >drawFrame();
> >// CHECK OUT drawFrame() --Am I doing things correctly there?
> >}
> >};
>
> Remove all of the above.
>
> >mGLSurfaceView.setRenderer(new OpenGLRenderer());
> >mGLSurfaceView.setRenderMode(RENDERMODE_CONTINUOUSLY);
>
> These should be called on GLEngine, which you are subclassing. They
> should look like this:
> setRenderer(new OpenGLRenderer());
> setRenderMode(RENDERMODE_CONTINUOUSLY);
>
>
>
>
>
> >void drawFrame() {
> >final SurfaceHolder holder = getSurfaceHolder();
>
> >Canvas c = null;
> >try {
> >c = holder.lockCanvas();
> >if (c != null) {
> >// draw something
> >}
> >} finally {
> >if (c != null)
> >holder.unlockCanvasAndPost(c);
> >}
>
> >// Reschedule the next redraw
> >mHandler.removeCallbacks(mDrawCube);
> >if (mVisible) {
> >mHandler.postDelayed(mDrawCube, 1000 / 25);
> >}
> >}
>
> Remove all of the above.
>
> On Feb 14, 10:47 am, joshbeck <[email protected]> wrote:
>
>
>
> > I'm going to post my entire source code as it stands right now in the
> > hope that someone with more experience than I can
> > get me pointed in the right direction. I've been trying lots of
> > different things. Feel free to use caps lock when expressing
> > discontent
> > with my code :')
>
> > /*
> > * Copyright (C) 2009 The Android Open Source Project
> > *
> > * Licensed under the Apache License, Version 2.0 (the "License");
> > * you may not use this file except in compliance with the License.
> > * You may obtain a copy of the License at
> > *
> > * http://www.apache.org/licenses/LICENSE-2.0
> > *
> > * Unless required by applicable law or agreed to in writing, software
> > * distributed under the License is distributed on an "AS IS" BASIS,
> > * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> > implied.
> > * See the License for the specific language governing permissions and
> > * limitations under the License.
> > */
>
> > package com.MyLiveWallpaper;
>
> > imports are all square. --Not included:
>
> > public class MyLiveWallpaper extends GLWallpaperService implements
> > SensorEventListener {
>
> > private final Handler mHandler = new Handler();
> > SensorManager sm = null;
>
> > private static final String TAG = "Compass";
>
> > private final Paint mPaint = new Paint();
> > private float mTouchX = -1;
> > private float mTouchY = -1;
>
> > public GLSurfaceView mGLSurfaceView;
>
> > private float mOffset;
>
> > @Override
> > public void onCreate() {
> > super.onCreate();
> > sm = (SensorManager) getSystemService(SENSOR_SERVICE);
> > sm.registerListener(this,
> > sm.getDefaultSensor(Sensor.TYPE_ORIENTATION) ,
> > SensorManager.SENSOR_DELAY_GAME);
> > mGLSurfaceView = new GLSurfaceView(this);
> > //Do I need this here?
>
> > }
>
> > @Override
> > public void onDestroy() {
> > super.onDestroy();
> > }
>
> > @Override
> > public Engine onCreateEngine() {
> > return new GLEngine();
> > }
> > // THE CLASS BELOW THIS WORKS IN AN ACTIVITY
> > public class OpenGLRenderer implements GLSurfaceView.Renderer {
> > private Square square;
> > private float angle = 45f;
> > public OpenGLRenderer() {
> > // Initialize our square.
> > square = new Square();
> > }
> > /*
> > * (non-Javadoc)
> > *
> > * @see
> > *
> > android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition
> > * .khronos.opengles.GL10,
> > javax.microedition.khronos.egl.EGLConfig)
> > */
> > public void onSurfaceCreated(GL10 gl, EGLConfig config) {
>
> > gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
>
> > gl.glShadeModel(GL10.GL_SMOOTH);
>
> > gl.glClearDepthf(1.0f);
>
> > gl.glEnable(GL10.GL_DEPTH_TEST);
>
> > gl.glDepthFunc(GL10.GL_LEQUAL);
>
> > gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
> > GL10.GL_NICEST);
>
> > }
>
> > /*
> > * (non-Javadoc)
> > *
> > * @see
> > *
> > android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.
> > * khronos.opengles.GL10)
> > */
> > public void onDrawFrame(GL10 gl) {
>
> > gl.glClear(GL10.GL_COLOR_BUFFER_BIT |
> > GL10.GL_DEPTH_BUFFER_BIT);
>
> > gl.glLoadIdentity();
>
> > // Translates 4 units into the screen.
> > gl.glTranslatef(0, 0, -4);
> > //
>
> > gl.glPushMatrix();
> > gl.glRotatef(angle, 0.0f, 0.0f, 1.0f);
>
> > gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
>
> > square.draw(gl);
> > gl.glPopMatrix();
>
> > angle++;
> > }
>
> > /*
> > * (non-Javadoc)
> > *
> > * @see
> > *
> > android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition
> > * .khronos.opengles.GL10, int, int)
> > */
> > public void onSurfaceChanged(GL10 gl, int width, int height) {
>
> > gl.glViewport(0, 0, width, height);
>
> > gl.glMatrixMode(GL10.GL_PROJECTION);
>
> > gl.glLoadIdentity();
>
> > GLU.gluPerspective(gl, 45.0f, (float) width / (float)
> > height,
> > 0.1f,
> > 100.0f);
>
> > gl.glMatrixMode(GL10.GL_MODELVIEW);
>
> > gl.glLoadIdentity();
> > }
> > }
>
> > class CubeEngine extends GLEngine {
> > private SurfaceHolder mSurfaceHolder;
>
> > private long mStartTime;
> > private float mCenterX;
> > private float mCenterY;
>
> > private final Runnable mDrawCube = new Runnable() {
> > public void run() {
> > drawFrame();
> > //CHECK OUT drawFrame() --Am I doing things correctly there?
>
> > }
> > };
> > private boolean mVisible;
>
> > CubeEngine() {
>
> > mGLSurfaceView = new GLSurfaceView(MyLiveWallpaper.this);
> > //The Line Above this is probably horribly
> > incorrect.
> > mGLSurfaceView.setRenderer(new OpenGLRenderer());
>
> > mGLSurfaceView.setRenderMode(RENDERMODE_CONTINUOUSLY);
> > //I'm not sure this is the correct place
> > //Should this go in OnCreate()?
>
> > }
>
> > @Override
> > public void onCreate(SurfaceHolder surfaceHolder) {
> > super.onCreate(surfaceHolder);
> > this.mSurfaceHolder = surfaceHolder;
>
> > // By default we don't get touch events, so enable them.
> > setTouchEventsEnabled(true);
> > }
> > @Override
> > public void onSurfaceChanged(SurfaceHolder holder, int format,
> > int width, int height) {
> > super.onSurfaceChanged(holder, format, width, height);
>
> > }
>
> > @Override
> > public void onSurfaceCreated(SurfaceHolder holder) {
> > super.onSurfaceCreated(holder);
>
> > }
>
> > @Override
> > public void onSurfaceDestroyed(SurfaceHolder holder) {
> > super.onSurfaceDestroyed(holder);
>
> > }
>
> > @Override
> > public void onOffsetsChanged(float xOffset, float yOffset,
> > float xStep, float yStep, int xPixels, int yPixels) {
>
> > }
>
> > /*
> > * Store the position of the touch event so we can use it for
> > drawing later
> > */
>
> > @Override
> > public void onTouchEvent(MotionEvent event) {
>
> > if (event.getAction() == MotionEvent.ACTION_MOVE) {
> > mTouchX = event.getX();
> > mTouchY = event.getY();
> > } else {
> > mTouchX = -1;
> > mTouchY = -1;
> > }
>
> > super.onTouchEvent(event);
> > }
>
> > void drawFrame() {
> > final SurfaceHolder holder = getSurfaceHolder();
>
> > Canvas c = null;
> > try {
> > c = holder.lockCanvas();
> > if (c != null) {
> > // draw something
>
> > }
> > } finally {
> > if (c != null) holder.unlockCanvasAndPost(c);
> > }
>
> > // Reschedule the next redraw
> > mHandler.removeCallbacks(mDrawCube);
> > if (mVisible) {
> > mHandler.postDelayed(mDrawCube, 1000 / 25);
> > }
> > }
>
> > }
>
> > @Override
> > public void onAccuracyChanged(Sensor sensor, int accuracy) {
>
> > }
>
> > @Override
> > public void onSensorChanged(SensorEvent event) {
> >
>
> ...
>
> read more »
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en