/
FlatRedBallGameBase.cs
165 lines (128 loc) · 5.62 KB
/
FlatRedBallGameBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Input;
using FlatRedBall.Math;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace FlatRedBallWpf
{
public class FlatRedBallGameBase : Game
{
private FlatRedBallControl _frbControl;
#region Constructors
public FlatRedBallGameBase(FlatRedBallControl frbControl)
{
this._frbControl = frbControl;
// Get the starting size of the control and start listening
// to its Resize events.
_windowHandle = frbControl.Handle;
RenderWidth = (Int32) frbControl.RenderSize.Width;
RenderHeight = (Int32) frbControl.RenderSize.Height;
frbControl.SizeChanged += XnaControlOnSizeChanged;
// Create the graphics device manager and set the delegate for initializing the graphics device
Graphics = new GraphicsDeviceManager(this);
Graphics.PreferMultiSampling = true;
Graphics.SynchronizeWithVerticalRetrace = true;
Graphics.PreparingDeviceSettings += PreparingDeviceSettings;
StartInitialization();
Content.RootDirectory = "Content";
}
#endregion
#region Methods
protected override void Initialize()
{
// Create a timer that will simulate the gameloop
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1/60);
_timer.Tick += TimerTick;
_timer.Start();
// Create the graphics options and initialize FRB with them
var graphicsOptions = new GraphicsOptions(this, Graphics);
graphicsOptions.SuspendDeviceReset();
graphicsOptions.ResolutionWidth = RenderWidth;
graphicsOptions.ResolutionHeight = RenderHeight;
graphicsOptions.ResumeDeviceReset();
FlatRedBallServices.InitializeFlatRedBall(this, Graphics, graphicsOptions);
FlatRedBall.Screens.ScreenManager.Start(typeof(FlatRedBallWpf.Screens.MainScreen));
if(false)
{
// Don't run this because we want the game's camera setup to run
// Put if(false) so that it doesn't get re-generated
CameraSetup.SetupCamera(SpriteManager.Camera, this.Graphics);
}
GlobalContent.Initialize();
Mouse.ModifyMouseState += HandleModifyMouseState;
FlatRedBall.Gui.GuiManager.Cursor.CustomIsActive = HandleCustomIsActive;
base.Initialize();
}
private void XnaControlOnSizeChanged(object sender, SizeChangedEventArgs e)
{
// Get the new size
var newWidth = (Int32) e.NewSize.Width;
var newHeight = (Int32) e.NewSize.Height;
// Update FRB
FlatRedBallServices.GraphicsOptions.SetResolution(newWidth, newHeight);
Rectangle displayRectangle = new Rectangle(0, 0, newWidth, newHeight);
SpriteManager.Camera.DestinationRectangle = displayRectangle;
double unitPerPixel = SpriteManager.Camera.OrthogonalHeight /
SpriteManager.Cameras[0].DestinationRectangle.Height;
SpriteManager.Camera.OrthogonalHeight = (float)(displayRectangle.Height * unitPerPixel);
SpriteManager.Camera.OrthogonalWidth = (float)(displayRectangle.Width * unitPerPixel);
SpriteManager.Camera.UsePixelCoordinates();
}
private void StartInitialization()
{
Initialize();
}
private void PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
{
PresentationParameters presentationParams = e.GraphicsDeviceInformation.PresentationParameters;
presentationParams.BackBufferWidth = RenderWidth;
presentationParams.BackBufferHeight = RenderHeight;
presentationParams.DeviceWindowHandle = _windowHandle;
presentationParams.RenderTargetUsage = RenderTargetUsage.PreserveContents;
}
private void TimerTick(object sender, EventArgs e)
{
Tick();
}
protected override void EndDraw()
{
base.EndDraw();
GraphicsDevice.Present();
}
private void HandleModifyMouseState(ref Microsoft.Xna.Framework.Input.MouseState mouseState)
{
System.Drawing.Point point = Control.MousePosition;
var screen = _frbControl.PointFromScreen(new System.Windows.Point(point.X, point.Y));
var newMouseState = new Microsoft.Xna.Framework.Input.MouseState(
MathFunctions.RoundToInt(screen.X),
MathFunctions.RoundToInt(screen.Y),
mouseState.ScrollWheelValue,
mouseState.LeftButton,
mouseState.MiddleButton,
mouseState.RightButton,
mouseState.XButton1,
mouseState.XButton2);
mouseState = newMouseState;
}
private bool HandleCustomIsActive()
{
return System.Windows.Application.Current.MainWindow.IsActive;
}
#endregion
#region Fields
protected readonly GraphicsDeviceManager Graphics;
protected readonly Int32 RenderHeight;
protected readonly Int32 RenderWidth;
private readonly IntPtr _windowHandle;
protected GraphicsDeviceManager GraphicsDeviceManager;
protected Boolean IsRenderingPaused;
private DispatcherTimer _timer;
#endregion
}
}