public DrawingSurfaceContentProvider(DrawingSurfaceUpdateHandler drawingSurfaceUpdateHandler, Microsoft.Xna.Framework.Game game)
 {
     _drawingSurfaceUpdateHandler = drawingSurfaceUpdateHandler;
     _surfaceUpdateHandler        = new SurfaceUpdateHandler(game);
 }
 public DrawingSurfaceContentProvider(DrawingSurfaceUpdateHandler drawingSurfaceUpdateHandler, Microsoft.Xna.Framework.Game game)
 {
     _drawingSurfaceUpdateHandler = drawingSurfaceUpdateHandler;
     _surfaceUpdateHandler = new SurfaceUpdateHandler(game);
 }
Beispiel #3
0
        /// <summary>
        /// Creates your Game class initializing it to worth within a XAML application window.
        /// </summary>
        /// <param name="launchParameters">The command line arguments from launch.</param>
        /// <param name="page">The XAML page containing the drawing surface to which we render the scene and recieve input events.</param>
        /// <returns></returns>
        ///
        static public T Create(string launchParameters, PhoneApplicationPage page)
        {
            if (launchParameters == null)
            {
                throw new NullReferenceException("The launch parameters cannot be null!");
            }
            if (page == null)
            {
                throw new NullReferenceException("The page parameter cannot be null!");
            }

            UIElement drawingSurface = page.Content as DrawingSurfaceBackgroundGrid;

            MediaElement mediaElement = null;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(page.Content); i++)
            {
                var child = VisualTreeHelper.GetChild(page.Content, i);
                if (child is MediaElement)
                {
                    mediaElement = (MediaElement)child;
                }
                else if (drawingSurface == null && child is DrawingSurface)
                {
                    drawingSurface = (DrawingSurface)child;
                }
            }

            if (!(drawingSurface is DrawingSurfaceBackgroundGrid) && !(drawingSurface is DrawingSurface))
            {
                throw new NullReferenceException("The drawing surface could not be found!");
            }

            if (mediaElement == null)
            {
                throw new NullReferenceException("The media element could not be found! Add it to the GamePage.");
            }

            Microsoft.Xna.Framework.Media.MediaPlayer._mediaElement = mediaElement;

            WindowsPhoneGamePlatform.LaunchParameters = launchParameters;
            WindowsPhoneGameWindow.Width  = ((FrameworkElement)drawingSurface).ActualWidth;
            WindowsPhoneGameWindow.Height = ((FrameworkElement)drawingSurface).ActualHeight;
            WindowsPhoneGameWindow.Page   = page;

            Microsoft.Xna.Framework.Audio.SoundEffect.InitializeSoundEffect();

            page.BackKeyPress += Microsoft.Xna.Framework.Input.GamePad.GamePageWP8_BackKeyPress;

            // Construct the game.
            var game = new T();

            if (game.graphicsDeviceManager == null)
            {
                throw new NullReferenceException("You must create the GraphicsDeviceManager in the Game constructor!");
            }

            SurfaceTouchHandler surfaceTouchHandler = new SurfaceTouchHandler();

            if (drawingSurface is DrawingSurfaceBackgroundGrid)
            {
                // Hookup the handlers for updates and touch.
                DrawingSurfaceBackgroundGrid drawingSurfaceBackgroundGrid = (DrawingSurfaceBackgroundGrid)drawingSurface;
                drawingSurfaceBackgroundGrid.SetBackgroundContentProvider(new DrawingSurfaceBackgroundContentProvider(game));
                drawingSurfaceBackgroundGrid.SetBackgroundManipulationHandler(surfaceTouchHandler);
            }
            else
            {
                var            drawingSurfaceUpdateHandler = new DrawingSurfaceUpdateHandler(game);
                DrawingSurface ds = (DrawingSurface)drawingSurface;

                RoutedEventHandler onLoadedHandler = (object sender, RoutedEventArgs e) =>
                {
                    if (sender != ds)
                    {
                        return;
                    }

                    if (initializedSurfaces.ContainsKey(ds) == false)
                    {
                        // Hook-up native component to DrawingSurface
                        ds.SetContentProvider(drawingSurfaceUpdateHandler.ContentProvider);
                        ds.SetManipulationHandler(surfaceTouchHandler);

                        // Make sure surface is not initialized twice...
                        initializedSurfaces.Add(ds, true);
                    }
                };

                // Don't wait for loaded event here since control might
                // be loaded already.
                onLoadedHandler(ds, null);

                ds.Unloaded += OnDrawingSurfaceUnloaded;
                ds.Loaded   += onLoadedHandler;
            }

            // Return the constructed, but not initialized game.
            return(game);
        }