How to setup the UrhoSharp engine to render on a live wallpaper on Xamarin Android platform ?

asked 2021-04-19T20:32:45.313+00:00
zielony71 1 Reputation point

Hello,

I have been strunggling for several days to setup the UrhoSharp engine as a renderer for live wallpaper in Xamarin on Android platform, but without success. Can anybody show me how to do this ? Below there is my code. But really this is only an outline of the code, probably not compileable, but mayby somebody could see what I do wrong. Thank you in advance.

namespace Demo.Droid  
{  
	[Service(Label = "@string/app_name", Permission = "android.permission.BIND_WALLPAPER")]  
	[IntentFilter(new string[] { "android.service.wallpaper.WallpaperService" })]  
	[MetaData("android.service.wallpaper", Resource = "@xml/cube1")]  
	public class MyWallpaper : WallpaperService  
	{  
		public override WallpaperService.Engine OnCreateEngine()  
		{  
			return new MyEngine(this);  
		}  
  
		public class MyEngine : WallpaperService.Engine  
		{  
			private Handler mHandler = new Handler();  
  
			MySdlSurface mSDLSurface;  
  
			public WallpaperService mWallService;  
			public ISurfaceHolder mMySurfaceHolder;  
  
			MyGame mUrhoApp;  
			public MyEngine(MetalStreamWallpaper wall) : base(wall)  
			{  
				mWallService = wall;  
				// Set up the paint to draw the lines for our cube  
			}  
  
			public override void OnCreate(ISurfaceHolder surfaceHolder)  
			{  
				base.OnCreate(surfaceHolder);  
				mMySurfaceHolder = surfaceHolder;  
  
				var appOpt = new Urho.ApplicationOptions("");  
				appOpt.LimitFps = true;  
				appOpt.ResourcePackagesPaths = new string[] { "Data.pkg" };  
  
				mSDLSurface = new MySdlSurface(mWallService.ApplicationContext, this); //   );  
  
				mSDLSurface.SurfaceCreated(surfaceHolder);  
				this.OnSurfaceCreated(surfaceHolder);  
  
				var currentSurface = typeof(Urho.Application).GetProperty(nameof(Urho.Application.CurrentSurface), BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty);  
				currentSurface.SetValue(default, new WeakReference(mSDLSurface));  
  
				var tcs = new TaskCompletionSource<Urho.Application>();  
				void startedHandler()  
				{  
					Urho.Application.Started -= startedHandler;  
					tcs.TrySetResult(Urho.Application.Current);  
				}  
  
				Urho.Application.Started += startedHandler;  
				// UrhoSurface.SetSdlMain(() => Urho.Application.CreateInstance(appType, options), finishActivityOnExit, SDLSurface);  
				var setSdlMain = typeof(UrhoSurface).GetMethod("SetSdlMain", BindingFlags.NonPublic | BindingFlags.Static);  
				setSdlMain.Invoke(default,   
					new object[] { new Func<Urho.Application>(() => Urho.Application.CreateInstance(typeof(MyGame), appOpt)),   
						false /* finishActivityOnExit */,  
						mSDLSurface  
					});  
  
				tcs.Task.Wait();  
				mUrhoApp = (MyGame )tcs.Task.Result;  
				// launching = false;  
  
				Urho.Application.UnhandledException += (s1, e1) => {  
					Console.WriteLine(e1.Exception.ToString());  
					e1.Handled = true;  
				};  
			}  
  
			public override void OnDestroy()  
			{  
				base.OnDestroy();  
			}  
  
			public override void OnVisibilityChanged(bool visible)  
			{  
				  
			}  
  
			public override void OnSurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)  
			{  
				base.OnSurfaceChanged(holder, format, width, height);  
				mMySurfaceHolder = holder;  
				holder.SetType(SurfaceType.Gpu);  
				mSDLSurface.SurfaceChanged(holder, format, width, height);  
  
			}  
  
			public override void OnSurfaceDestroyed(ISurfaceHolder holder)  
			{  
				base.OnSurfaceDestroyed(holder);  
  
				// mSDLSurface.SurfaceDestroyed(holder);  
			}  
  
			public override void OnOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset)  
			{  
			}  
  
			// Store the position of the touch event so we can use it for drawing later  
			public override void OnTouchEvent(MotionEvent e)  
			{  
			}  
  
		}  
  
	}  


    public class MySdlSurface : global::Org.Libsdl.App.SDLSurface  
    {  
        MyWallpaper.MyEngine mEngine;  
  
        public MySdlSurface(Android.Content.Context p0, MyWallpaper.MyEngine engine) : base(p0)  
        {  
            mEngine = engine;  
            Holder.AddCallback(this);  
        }  
  
        // public void setSurfHolder(ISurfaceHolder mySurfHolder) { mMySurfHolder = mySurfHolder; }  
        public override ISurfaceHolder Holder  
        {  
            get  
            {  
                if (mEngine != null && mEngine.mMySurfaceHolder != null)  
                {  
                    return mEngine.mMySurfaceHolder;  
                }  
                else  
                {  
                    return base.Holder;  
                }  
            }  
        }  
  
        public override Surface NativeSurface  
        {  
            get  
            {  
                return mEngine.mMySurfaceHolder.Surface;  
            }  
        }  
  
  
        public override void SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height)  
        {  
            Holder.AddCallback(this);  
            SDLActivity.MIsSurfaceReady = true;  
  
        }  
  
    }  
}      



  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,803 questions
{count} votes