/// <summary> /// Creates a new PinballGameControl based on a machine template. /// </summary> /// <param name="machine">Template for the game machine.</param> public GameView(Game game) : base() { Game = game; gameWorld = new GameWorld(Game); HUD = new GameHUD(Game); Camera = new GameFieldCamera(gameWorld, HUD); // Little hack needed so that we get all input events. Focusable = true; Loaded += (s, e) => { Focus(); }; // Init camera Camera.Size = new Size(Width, Height); // Set up the update timer timer = new System.Windows.Forms.Timer(); timer.Interval = 1000 / MAX_FPS; timer.Tick += OnTick; timer.Start(); // Wire up a few event listeners PreviewKeyDown += HandleKeyDown; SizeChanged += ResizeCamera; Game.StatusChanged += delegate { Dispatcher.Invoke(delegate { InvalidateVisual(); }, System.Windows.Threading.DispatcherPriority.Render); }; // Let's draw with high quality SetValue(RenderOptions.BitmapScalingModeProperty, BitmapScalingMode.HighQuality); }
/// <summary> /// Initializes a new camera. /// </summary> /// <param name="world">World the camera looks at.</param> /// <param name="hud">HUD to display somewhere.</param> public GameFieldCamera(GameWorld world, GameHUD hud) { World = world; HUD = hud; this.Translocation = new Vector(0, 0); this.Scale = new Vector(0, 0); }
protected override void OnDispose() { // ElementHost sometimes fails to properly remove all references to a WPF control, // which is why we set all our own references to NULL so that at least those aren't kept alive forever. timer.Dispose(); Game = null; gameWorld = null; HUD = null; Camera = null; }