Rajawali / Rajawali

Android OpenGL ES 2.0/3.0 Engine
https://rajawali.github.io/Rajawali/
Other
2.35k stars 700 forks source link

Need assistance: Failure loading Renderer #664

Closed power7714 closed 11 years ago

power7714 commented 11 years ago

I have this in my Wallpaper service but when it loads, it doesn't render the model or call the renderer:

public class DemoWallpaperService extends Wallpaper {
    private TestRenderer mRenderer;

    @Override
    public Engine onCreateEngine() {
        mRenderer = new TestRenderer(this);
        return new DemoWallpaperEngine(this.getSharedPreferences(SHARED_PREFS_NAME,
                Context.MODE_PRIVATE), getBaseContext(), mRenderer);
    }

    private class DemoWallpaperEngine extends Engine {
        private boolean mVisible = false;
        private int mColor = 0;
        private int mAlpha = 0;
        private boolean mIsCountingUp=true;
        private String mText = "Hello!";
        private final Handler mHandler = new Handler();
        private final Runnable mUpdateDisplay = new Runnable() {
        @Override
        public void run() {
            draw();
        }};

        public DemoWallpaperEngine(SharedPreferences sharedPreferences,
                Context baseContext, TestRenderer mRenderer) {
            // TODO Auto-generated constructor stub
        }

        private void draw() {
           SurfaceHolder holder = getSurfaceHolder();
           Canvas c = null;
           try {
              c = holder.lockCanvas();
              if (c != null) {
                 //android.os.Debug.waitForDebugger(); 
                 Paint p = new Paint();
                 p.setTextSize(40);
                 p.setAntiAlias(true);
                 String text = mText;
                 float w = p.measureText(text, 0, text.length());
                 int offset = (int) w / 2;
                 int x = c.getWidth()/2 - offset;
                 int y = c.getHeight()/2;
                 p.setColor(Color.BLACK);
                 c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
                 p.setColor(mColor);
                 p.setAlpha(mAlpha);
                 c.drawText(text, x, y, p);
                 if (mIsCountingUp) {
                     mAlpha+=10;
                     if (mAlpha > 255) {
                         mAlpha = 255;
                         mIsCountingUp=false;
                     }
                 } else {
                     mAlpha-=10;
                     if (mAlpha < 0) {
                         mAlpha = 0;
                         mIsCountingUp=true;
                     }
                 }

              }
           } finally {
              if (c != null)
                 holder.unlockCanvasAndPost(c);
           }
           mHandler.removeCallbacks(mUpdateDisplay);
           if (mVisible) {
               mHandler.postDelayed(mUpdateDisplay, 100);
           }
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            mVisible = visible;
            if (visible) {
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(DemoWallpaperService.this);
                mText = prefs.getString("text_to_display", "This is the default.");
                String tmp = prefs.getString("text_color", "#ffffff");
                String r = tmp.substring(1,3);
                String g = tmp.substring(3,5);
                String b = tmp.substring(5,7);
                mColor = Color.rgb(Integer.parseInt(r, 16), Integer.parseInt(g, 16), Integer.parseInt(b, 16));
                draw();
            } else {
                mHandler.removeCallbacks(mUpdateDisplay);
            }
        }

         @Override
          public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
             draw();
          }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mHandler.removeCallbacks(mUpdateDisplay);
        }

        @Override
        public void onDestroy() {
             super.onDestroy();
             mVisible = false;
             mHandler.removeCallbacks(mUpdateDisplay);
        }
    }    
}

The Renderer just has the basic code from the examples. Loading a primitive cube. There are no errors in the logcat. If anyone could help me with this, I would greatly appreciate it. Thank you.

ToxicBakery commented 11 years ago

We can only support Rajawali based questions. As this is about using Canvas for Live Wallpapers it is out of scope. Stackoverflow would be a good place to start for this though.

Also as a tip from my experience. Do not use Canvas on Live Wallpapers. Canvas is extremely CPU dependent and many devices will throttle your wallpaper and CPU to save battery life. What you will experience is a huge change in frame rate such as 30 when you are swiping around the screen then drop down to 10 when you let the phone idle for a few seconds. Even with simple animations canvas for live wallpapers has extremely poor performance.

power7714 commented 11 years ago

The reason I asked this question is because the live wallpaper works. The text displays. What doesn't is the rendered model. It doesn't even call the renderer and I don't know why. So the question was geared toward the Rajawali framework.

MasDennis commented 11 years ago

This is actually a generic Java/Android question for Stackoverflow. But give this a try. Replace

public DemoWallpaperEngine(SharedPreferences sharedPreferences,
                Context baseContext, TestRenderer mRenderer) {
          // TODO Auto-generated constructor stub
}

With this:

public DemoWallpaperEngine(SharedPreferences sharedPreferences,
                Context baseContext, TestRenderer mRenderer) {
          super(sharedPreferences, baseContext, mRenderer);
}
power7714 commented 11 years ago

Didn't work. I changed return new DemoWallpaperEngine To return new WallpaperEngine And the model was rendered but not the text. When I change back, the text shows but not the model

I tried removing the DemoWallpaperEngine class but kept the content. Only the model was shown. If anyone knows an easier way of doing this, a simple example would be helpful.

MasDennis commented 11 years ago

So are you trying to combine a regular canvas live wallpaper with an OpenGL live wallpaper? If so, I have no idea how to do that if it is even possible.

jwoolston commented 11 years ago

Closing as stale.