Urho.SimpleApplication in a Xamarin.Android project

lahellerlaheller USMember
edited September 26 in UrhoSharp

Hello

I am completely new in UrhoSharp and now I am just colletcion informations.
Is it possible/enough to use only the Urho.SimpleApplication class to create a simple Scene in a Xamarin.Android project?

Best Answer

  • lahellerlaheller USMember
    Accepted Answer

    After a bit of experimenting I answered my own question B)
    Here is how to do it when anybody interested:

    using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Content.PM;
    using System.Threading;
    using SysTrace = System.Diagnostics.Trace;
    using Urho;
    using Urho.Droid;
    using Urho.Shapes;
    
    namespace SimpleUrhoApp
    {
        [Activity(
            Label = "SimpleUrhoApp",
            MainLauncher = true,
            Icon = "@drawable/icon",
            Name="hu.lheller.SimpleUrhoApp",
            ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation,
            ScreenOrientation = ScreenOrientation.Portrait
        )]
        public class MainActivity : Activity
        {
            SimpleApplication app = null;
    
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                SimpleApplication.UnhandledException += (s, e) =>
                {
                    SysTrace.WriteLine(e.Exception.ToString());
                    e.Handled = true;
                };
                var al = new AbsoluteLayout(this);
                var surface = UrhoSurface.CreateSurface(this);
                var result = surface.Show<SimpleApplication>(null);
                ThreadPool.QueueUserWorkItem((o) => {
                    while (!result.IsCompleted) { /* wait until application initializes */ }
                    app = result.Result;
                    SysTrace.WriteLine("Platform: " + SimpleApplication.Platform);
                    SimpleApplication.InvokeOnMain(() =>
                    {
                        var mainNode = app.Scene.CreateChild("mainNode");
    
                        app.LightNode.Position = new Vector3(0, 15, 0);
    
                        var boxNode = mainNode.CreateChild("boxNode");
                        boxNode.Position = new Vector3(2, -1.5f, 7);
                        var box = boxNode.CreateComponent<Box>();
                        box.Color = Urho.Color.Blue;
    
                        app.CameraNode.Position = new Vector3(0, 0, 0);
                        app.CameraNode.SetDirection(new Vector3(0, 0, 1));
    
                        var sphereNode = mainNode.CreateChild("sphereNode");
                        sphereNode.Position = new Vector3(1, 1, 5);
                        var sphere = sphereNode.CreateComponent<Sphere>();
                        sphere.Color = Urho.Color.Green;
                    });
                });
                al.AddView(surface);
                SetContentView(al);
            }
    
            protected override void OnResume()
            {
                UrhoSurface.OnResume();
                base.OnResume();
            }
    
            protected override void OnPause()
            {
                UrhoSurface.OnPause();
                base.OnPause();
            }
    
            public override void OnLowMemory()
            {
                UrhoSurface.OnLowMemory();
                base.OnLowMemory();
            }
    
            protected override void OnDestroy()
            {
                UrhoSurface.OnDestroy();
                base.OnDestroy();
            }
    
            public override bool DispatchKeyEvent(Android.Views.KeyEvent e)
            {
                if (!UrhoSurface.DispatchKeyEvent(e)) return false;
                return base.DispatchKeyEvent(e);
            }
    
            public override void OnWindowFocusChanged(bool hasFocus)
            {
                UrhoSurface.OnWindowFocusChanged(hasFocus);
                base.OnWindowFocusChanged(hasFocus);
            }
        }
    }
    

Answers

  • lahellerlaheller USMember
    edited September 30

    After a bit of experimenting I answered my question B)
    Here is how to do it when anybody interested.
    The example below draws a box and a sphere:

    /* Example Xamarin.Android project */
    using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Content.PM;
    using System.Threading;
    using SysTrace = System.Diagnostics.Trace;
    using Urho;
    using Urho.Droid;
    using Urho.Shapes;
    
    namespace SimpleUrhoApp
    {
        [Activity(Label = "SimpleUrhoApp", MainLauncher = true, Icon = "@drawable/icon", Name="abc.def.SimpleUrhoApp", ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
        public class MainActivity : Activity
        {
            SimpleApplication app = null;
    
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                SimpleApplication.UnhandledException += (s, e) =>
                {
                    SysTrace.WriteLine(e.Exception.ToString());
                    e.Handled = true;
                };
                var al = new AbsoluteLayout(this);
                var surface = UrhoSurface.CreateSurface(this);
                var result = surface.Show<SimpleApplication>(null);
                ThreadPool.QueueUserWorkItem((o) => {
                    while (!result.IsCompleted) { /* wait until application initializes */ }
                    app = result.Result;
                    SysTrace.WriteLine("Platform: " + SimpleApplication.Platform);
                    SimpleApplication.InvokeOnMain(() =>
                    {
                        var mainNode = app.Scene.CreateChild("mainNode");
    
                        app.LightNode.Position = new Vector3(0, 15, 0);
    
                        var boxNode = mainNode.CreateChild("boxNode");
                        boxNode.Position = new Vector3(2, -1.5f, 7);
                        var box = boxNode.CreateComponent<Box>();
                        box.Color = Urho.Color.Blue;
    
                        app.CameraNode.Position = new Vector3(0, 0, 0);
                        app.CameraNode.SetDirection(new Vector3(0, 0, 1));
    
                        var sphereNode = mainNode.CreateChild("sphereNode");
                        sphereNode.Position = new Vector3(1, 1, 5);
                        var sphere = sphereNode.CreateComponent<Sphere>();
                        sphere.Color = Urho.Color.Green;
                    });
                });
                al.AddView(surface);
                SetContentView(al);
            }
    
            protected override void OnResume()
            {
                UrhoSurface.OnResume();
                base.OnResume();
            }
    
            protected override void OnPause()
            {
                UrhoSurface.OnPause();
                base.OnPause();
            }
    
            public override void OnLowMemory()
            {
                UrhoSurface.OnLowMemory();
                base.OnLowMemory();
            }
    
            protected override void OnDestroy()
            {
                UrhoSurface.OnDestroy();
                base.OnDestroy();
            }
    
            public override bool DispatchKeyEvent(Android.Views.KeyEvent e)
            {
                if (!UrhoSurface.DispatchKeyEvent(e)) return false;
                return base.DispatchKeyEvent(e);
            }
    
            public override void OnWindowFocusChanged(bool hasFocus)
            {
                UrhoSurface.OnWindowFocusChanged(hasFocus);
                base.OnWindowFocusChanged(hasFocus);
            }
        }
    }
    
  • lahellerlaheller USMember
    Accepted Answer

    After a bit of experimenting I answered my own question B)
    Here is how to do it when anybody interested:

    using Android.App;
    using Android.Widget;
    using Android.OS;
    using Android.Content.PM;
    using System.Threading;
    using SysTrace = System.Diagnostics.Trace;
    using Urho;
    using Urho.Droid;
    using Urho.Shapes;
    
    namespace SimpleUrhoApp
    {
        [Activity(
            Label = "SimpleUrhoApp",
            MainLauncher = true,
            Icon = "@drawable/icon",
            Name="hu.lheller.SimpleUrhoApp",
            ConfigurationChanges = ConfigChanges.KeyboardHidden | ConfigChanges.Orientation,
            ScreenOrientation = ScreenOrientation.Portrait
        )]
        public class MainActivity : Activity
        {
            SimpleApplication app = null;
    
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);
    
                SimpleApplication.UnhandledException += (s, e) =>
                {
                    SysTrace.WriteLine(e.Exception.ToString());
                    e.Handled = true;
                };
                var al = new AbsoluteLayout(this);
                var surface = UrhoSurface.CreateSurface(this);
                var result = surface.Show<SimpleApplication>(null);
                ThreadPool.QueueUserWorkItem((o) => {
                    while (!result.IsCompleted) { /* wait until application initializes */ }
                    app = result.Result;
                    SysTrace.WriteLine("Platform: " + SimpleApplication.Platform);
                    SimpleApplication.InvokeOnMain(() =>
                    {
                        var mainNode = app.Scene.CreateChild("mainNode");
    
                        app.LightNode.Position = new Vector3(0, 15, 0);
    
                        var boxNode = mainNode.CreateChild("boxNode");
                        boxNode.Position = new Vector3(2, -1.5f, 7);
                        var box = boxNode.CreateComponent<Box>();
                        box.Color = Urho.Color.Blue;
    
                        app.CameraNode.Position = new Vector3(0, 0, 0);
                        app.CameraNode.SetDirection(new Vector3(0, 0, 1));
    
                        var sphereNode = mainNode.CreateChild("sphereNode");
                        sphereNode.Position = new Vector3(1, 1, 5);
                        var sphere = sphereNode.CreateComponent<Sphere>();
                        sphere.Color = Urho.Color.Green;
                    });
                });
                al.AddView(surface);
                SetContentView(al);
            }
    
            protected override void OnResume()
            {
                UrhoSurface.OnResume();
                base.OnResume();
            }
    
            protected override void OnPause()
            {
                UrhoSurface.OnPause();
                base.OnPause();
            }
    
            public override void OnLowMemory()
            {
                UrhoSurface.OnLowMemory();
                base.OnLowMemory();
            }
    
            protected override void OnDestroy()
            {
                UrhoSurface.OnDestroy();
                base.OnDestroy();
            }
    
            public override bool DispatchKeyEvent(Android.Views.KeyEvent e)
            {
                if (!UrhoSurface.DispatchKeyEvent(e)) return false;
                return base.DispatchKeyEvent(e);
            }
    
            public override void OnWindowFocusChanged(bool hasFocus)
            {
                UrhoSurface.OnWindowFocusChanged(hasFocus);
                base.OnWindowFocusChanged(hasFocus);
            }
        }
    }
    
Sign In or Register to comment.