Jypeli  9
The simple game programming library
DebugScreen.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System.Text;
3 
4 namespace Jypeli
5 {
6  public partial class Game
7  {
8  private string fpsText = "00";
9  private int fpsSkipCounter;
11  private Matrix canvasTransform = Matrix.Identity;
12 
17  public Layer DebugLayer { get; private set; }
18 
22  public bool DebugKeyEnabled { get; set; }
23 
27  public bool DebugScreenVisible { get; set; }
28 
32  public Window FPSWindow { get; private set; }
33 
37  public Label FPSDisplay { get; private set; }
38 
42  public Window LayerWindow { get; private set; }
43 
47  public Label LayerDisplay { get; private set; }
48 
49  private void InitDebugScreen()
50  {
51  debugCanvas = new Canvas();
52 
54  DebugLayer.Objects.ItemAdded += OnObjectAdded;
55  DebugLayer.Objects.ItemRemoved += OnObjectRemoved;
56 
57  FPSWindow = new Jypeli.Window();
58  FPSWindow.IsModal = false;
59  FPSWindow.Color = new Color( Color.HotPink, 100 );
61 
62  FPSDisplay = new Label( "00" );
63  FPSDisplay.Color = Color.HotPink;
64  FPSWindow.Size = 1.5 * FPSDisplay.Size;
65  FPSWindow.Add( FPSDisplay );
66 
67  FPSWindow.Right = Screen.Right - Screen.Width / 16;
68  FPSWindow.Top = Screen.Top;
69 
70  LayerWindow = new Jypeli.Window();
71  LayerWindow.IsModal = false;
72  LayerWindow.Color = new Color( Color.Blue, 100 );
74 
75  LayerDisplay = new Label( "Layers: no data" );
79  LayerWindow.Left = Screen.Left;
80  LayerWindow.Top = Screen.Top - 200;
81 
82  DebugKeyEnabled = true;
83  DebugScreenVisible = false;
84  }
85 
86  private StringBuilder layerTextBuilder = new StringBuilder();
87  private const string layerTextTitle = "Layers:\n";
88 
94  private void UpdateFps(GameTime gameTime)
95  {
96  fpsText = (10000000.0 / gameTime.ElapsedGameTime.Ticks).ToString("F2");
97  if (fpsSkipCounter++ > 10)
98  {
99  fpsSkipCounter = 0;
100  }
101  }
102 
103  private void UpdateDebugScreen( Time time )
104  {
105  if ( DebugKeyEnabled && Keyboard.GetKeyState( Key.F12 ) == ButtonState.Pressed )
107 
108  DebugLayer.Update( time );
109 
110  if ( !DebugScreenVisible )
111  return;
112 
114 
115  layerTextBuilder.Clear();
117 
118  for (int i = Layers.FirstIndex; i <= Layers.LastIndex; i++)
119  {
120  layerTextBuilder.Append( "[" );
121  layerTextBuilder.Append( i );
122  layerTextBuilder.Append( "]: " );
123  layerTextBuilder.Append( Layers[i].Objects.Count );
124  layerTextBuilder.AppendLine();
125  }
126 
127  LayerDisplay.Text = layerTextBuilder.ToString();
129  }
130 
131  private void DrawDebugScreen()
132  {
133  if ( !DebugScreenVisible )
134  return;
135 
138  debugCanvas.End();
139 
141  }
142 
143  private void PaintShapeOutlines( Canvas canvas, IGameObject obj )
144  {
145  var vertexes = obj.Shape.Cache.OutlineVertices;
146  double wmul = obj.Shape.IsUnitSize ? obj.Width : 1;
147  double hmul = obj.Shape.IsUnitSize ? obj.Height : 1;
148 
149  canvas.BrushColor = ( obj is GameObject && Mouse.IsCursorOn( (GameObject)obj ) ) ? Color.LightGreen : Color.LightGray;
150 
151  Vector3 center = (Vector3)Camera.WorldToScreen( obj.AbsolutePosition, obj.Layer );
152  Matrix transform = Matrix.CreateRotationZ( (float)obj.AbsoluteAngle.Radians ) * Matrix.CreateTranslation( center );
153 
154  for ( int j = 0; j < vertexes.Length - 1; j++ )
155  {
156  double x1 = wmul * vertexes[j].X;
157  double y1 = hmul * vertexes[j].Y;
158  double x2 = wmul * vertexes[j + 1].X;
159  double y2 = hmul * vertexes[j + 1].Y;
160 
161  var t1 = Vector2.Transform( new Vector2( (float)x1, (float)y1 ), transform );
162  var t2 = Vector2.Transform( new Vector2( (float)x2, (float)y2 ), transform );
163 
164  canvas.DrawLine( t1.X, t1.Y, t2.X, t2.Y );
165  }
166 
167  if ( vertexes.Length > 2 )
168  {
169  double x1 = wmul * vertexes[vertexes.Length - 1].X;
170  double y1 = hmul * vertexes[vertexes.Length - 1].Y;
171  double x2 = wmul * vertexes[0].X;
172  double y2 = hmul * vertexes[0].Y;
173 
174  var t1 = Vector2.Transform( new Vector2( (float)x1, (float)y1 ), transform );
175  var t2 = Vector2.Transform( new Vector2( (float)x2, (float)y2 ), transform );
176 
177  canvas.DrawLine( t1.X, t1.Y, t2.X, t2.Y );
178  }
179  }
180 
181  private void PaintPhysicsOutlines( Canvas canvas, PhysicsObject obj )
182  {
183  if ( obj.Body == null || obj.Body.Shape == null || obj.Body.Shape.Cache == null )
184  return;
185 
186  var vertexes = obj.Body.Shape.Cache.OutlineVertices;
187  double wmul = obj.Body.Shape.IsUnitSize ? obj.Width : 1;
188  double hmul = obj.Body.Shape.IsUnitSize ? obj.Height : 1;
189 
190  canvas.BrushColor = ( obj is GameObject && Mouse.IsCursorOn( (GameObject)obj ) ) ? Color.Salmon : Color.DarkRed;
191 
192  Vector3 center = (Vector3)Camera.WorldToScreen( obj.Body.Position, obj.Layer );
193  Matrix transform = Matrix.CreateRotationZ( (float)obj.Body.Angle ) * Matrix.CreateTranslation( center );
194 
195  for ( int j = 0; j < vertexes.Length - 1; j++ )
196  {
197  double x1 = wmul * vertexes[j].X;
198  double y1 = hmul * vertexes[j].Y;
199  double x2 = wmul * vertexes[j + 1].X;
200  double y2 = hmul * vertexes[j + 1].Y;
201 
202  var t1 = Vector2.Transform( new Vector2( (float)x1, (float)y1 ), transform );
203  var t2 = Vector2.Transform( new Vector2( (float)x2, (float)y2 ), transform );
204 
205  canvas.DrawLine( t1.X, t1.Y, t2.X, t2.Y );
206  }
207 
208  if ( vertexes.Length > 2 )
209  {
210  double x1 = wmul * vertexes[vertexes.Length - 1].X;
211  double y1 = hmul * vertexes[vertexes.Length - 1].Y;
212  double x2 = wmul * vertexes[0].X;
213  double y2 = hmul * vertexes[0].Y;
214 
215  var t1 = Vector2.Transform( new Vector2( (float)x1, (float)y1 ), transform );
216  var t2 = Vector2.Transform( new Vector2( (float)x2, (float)y2 ), transform );
217 
218  canvas.DrawLine( t1.X, t1.Y, t2.X, t2.Y );
219  }
220 
221  /*var vertexes = obj.Body.Shape.Cache.OutlineVertices;
222  var center = Camera.WorldToScreen( obj.Body.Position, obj.Layer );
223  double wmul = obj.Body.Shape.IsUnitSize ? obj.Width : 1;
224  double hmul = obj.Body.Shape.IsUnitSize ? obj.Height : 1;
225 
226  canvas.BrushColor = ( obj is GameObject && Mouse.IsCursorOn( (GameObject)obj ) ) ? Color.Salmon : Color.DarkRed;
227 
228  for ( int j = 0; j < vertexes.Length - 1; j++ )
229  {
230  double x1 = center.X + wmul * vertexes[j].X;
231  double y1 = center.Y + hmul * vertexes[j].Y;
232  double x2 = center.X + wmul * vertexes[j + 1].X;
233  double y2 = center.Y + hmul * vertexes[j + 1].Y;
234 
235  canvas.DrawLine( x1, y1, x2, y2 );
236  }
237 
238  if ( vertexes.Length > 2 )
239  {
240  double x1 = center.X + wmul * vertexes[vertexes.Length - 1].X;
241  double y1 = center.Y + hmul * vertexes[vertexes.Length - 1].Y;
242  double x2 = center.X + wmul * vertexes[0].X;
243  double y2 = center.Y + hmul * vertexes[0].Y;
244 
245  canvas.DrawLine( x1, y1, x2, y2 );
246  }*/
247  }
248 
249  private void PaintDebugScreen( Canvas canvas )
250  {
251  // Draw the object outlines as determined by their shape
252 
253  for ( int i = Layers.FirstIndex; i <= Layers.LastIndex; i++ )
254  {
255  foreach ( var obj in Layers[i].Objects )
256  {
257  if ( obj == null || obj.Shape == null || obj.Shape.Cache == null || obj.Layer == null )
258  continue;
259 
260  PaintShapeOutlines( canvas, obj );
261 
262  if ( obj is PhysicsObject )
263  PaintPhysicsOutlines( canvas, (PhysicsObject)obj );
264  }
265  }
266  }
267  }
268 }
Jypeli.ShapeCache.OutlineVertices
readonly Vector[] OutlineVertices
Ulkoreunan verteksit, lueteltuna vastapäivään.
Definition: Shapes.cs:591
Jypeli.Layer.Add
void Add(IGameObject o)
Definition: Layer.cs:179
Jypeli.Matrix
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.PhysicsObject.Body
IPhysicsBody Body
Definition: Dimensions.cs:10
Jypeli.Color.HotPink
static readonly Color HotPink
Pinkki.
Definition: Color.cs:668
Jypeli.Vector.X
double X
Definition: Vector.cs:312
Jypeli.Keyboard.GetKeyState
ButtonState GetKeyState(Key k)
Palauttaa annetun näppäimen tilan (ks. ButtonState).
Definition: Keyboard.cs:105
Jypeli.GameObject.Angle
override Angle Angle
Olion kulma tai rintamasuunta. Nolla = osoittaa oikealle.
Definition: Dimensions.cs:74
Jypeli.Game.LayerDisplay
Label LayerDisplay
"Layers"-näyttö.
Definition: DebugScreen.cs:47
Jypeli.Game.PaintShapeOutlines
void PaintShapeOutlines(Canvas canvas, IGameObject obj)
Definition: DebugScreen.cs:143
Jypeli
Definition: Automobile.cs:5
Jypeli.Game.canvasTransform
Matrix canvasTransform
Definition: DebugScreen.cs:11
Jypeli.Widget.IsModal
bool IsModal
Jos true, pelin sekä ikkunan alla olevien widgettien ohjaimet eivät ole käytössä kun ikkuna on näkyvi...
Definition: Control.cs:22
Microsoft
Definition: JypeliContentManager.cs:6
Jypeli.Canvas.Begin
void Begin(ref Matrix worldMatrix, Dimensional dimensionSource)
Definition: Canvas.cs:93
Jypeli.Canvas.End
void End()
Definition: Canvas.cs:101
Jypeli.Game.FPSDisplay
Label FPSDisplay
FPS-näyttö.
Definition: DebugScreen.cs:37
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.Label
Tekstikenttä.
Definition: Label.cs:66
Jypeli.Canvas.BrushColor
Color BrushColor
Pensselin väri.
Definition: Canvas.cs:86
Jypeli.Layer.CreateStaticLayer
static Layer CreateStaticLayer()
Luo staattisen kerroksen (ei liiku kameran mukana)
Definition: Layer.cs:131
Jypeli.IGameObject
Yhteinen rajapinta kaikille peliolioille.
Definition: IGameObject.cs:11
Jypeli.ScreenView.Width
double Width
Näytön leveys x-suunnassa.
Definition: View.cs:222
Jypeli.Game.UpdateDebugScreen
void UpdateDebugScreen(Time time)
Definition: DebugScreen.cs:103
Jypeli.ScreenView.Top
double Top
Näytön yläreunan y-koordinaatti.
Definition: View.cs:308
Jypeli.Canvas
Piirtoalusta.
Definition: Canvas.cs:39
Jypeli.Game.fpsSkipCounter
int fpsSkipCounter
Definition: DebugScreen.cs:9
Jypeli.Camera
Kamera. Määrittää mikä osa pelitasosta on kerralla näkyvissä.
Definition: Camera.cs:40
Jypeli.Mouse
Hiiri.
Definition: Mouse.cs:46
Jypeli.Game.OnObjectAdded
virtual void OnObjectAdded(IGameObject obj)
Definition: Layers.cs:95
Jypeli.Window.Color
override Color Color
Ikkunan väri.
Definition: Window.cs:51
Jypeli.Game.LayerWindow
Window LayerWindow
"Layers"-ikkuna. Huom. asettaa kokonsa automaattisesti.
Definition: DebugScreen.cs:42
Jypeli.DimensionalRW.Height
new double Height
Korkeus.
Definition: Dimensional.cs:82
Jypeli.Color.LightGreen
static readonly Color LightGreen
Vaalea vihreä.
Definition: Color.cs:703
Jypeli.Game.DebugKeyEnabled
bool DebugKeyEnabled
Debug-ruutu F12-näppäimestä päällä / pois.
Definition: DebugScreen.cs:22
Jypeli.Shape.Cache
abstract ShapeCache Cache
Definition: Shapes.cs:55
Jypeli.Game.OnObjectRemoved
virtual void OnObjectRemoved(IGameObject obj)
Definition: Layers.cs:106
Jypeli.ScreenView.Right
double Right
Näytön oikean reunan x-koordinaatti.
Definition: View.cs:300
Jypeli.Color.DarkRed
static readonly Color DarkRed
Tumma punainen.
Definition: Color.cs:598
Jypeli.Game.layerTextTitle
const string layerTextTitle
Definition: DebugScreen.cs:87
Jypeli.Layer
Kerros. Vastaa olioiden piirtämisestä.
Definition: Layer.cs:32
Jypeli.Game.FPSWindow
Window FPSWindow
FPS-ikkuna.
Definition: DebugScreen.cs:32
Jypeli.Time
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
Definition: Time.cs:14
Jypeli.Game.Screen
static ScreenView Screen
Näytön dimensiot, eli koko ja reunat.
Definition: Graphics.cs:90
Jypeli.DimensionalRW.Width
new double Width
Leveys.
Definition: Dimensional.cs:77
Jypeli.Color.Blue
static readonly Color Blue
Sininen.
Definition: Color.cs:513
Jypeli.Layer.Draw
void Draw(Camera camera)
Definition: Layer.cs:218
Jypeli.Game.layerTextBuilder
StringBuilder layerTextBuilder
Definition: DebugScreen.cs:86
Jypeli.Color
Väri.
Definition: Color.cs:13
Jypeli.Canvas.DrawLine
void DrawLine(Vector startPoint, Vector endPoint)
Piirtää janan.
Definition: Canvas.cs:131
Jypeli.Game.InitDebugScreen
void InitDebugScreen()
Definition: DebugScreen.cs:49
Jypeli.Color.Salmon
static readonly Color Salmon
Lohenpunainen.
Definition: Color.cs:833
Jypeli.Game.debugCanvas
Canvas debugCanvas
Definition: DebugScreen.cs:10
Jypeli.Label.TextColor
Color TextColor
Tekstin väri.
Definition: Label.cs:207
Jypeli.Camera.WorldToScreen
Vector WorldToScreen(Vector point)
Muuntaa annetun pisteen maailmankoordinaateista ruutukoordinaatteihin.
Definition: Camera.cs:175
Jypeli.Label.Size
override Vector Size
Tekstikentän koko. Jos SizeMode on SizeMode.StretchText, teksti venytetään kentän koon mukaiseksi.
Definition: Label.cs:256
Jypeli.Game.DrawDebugScreen
void DrawDebugScreen()
Definition: DebugScreen.cs:131
Jypeli.ButtonState
ButtonState
Napin (minkä tahansa) asento.
Definition: ButtonState.cs:37
System
Definition: CFFauxAttributes.cs:29
Jypeli.Mouse.IsCursorOn
static bool IsCursorOn(ScreenView screen, MouseState state, GameObject obj)
Onko hiiren kursori annetun olion päällä.
Definition: Mouse.cs:320
Jypeli.PhysicsObject
Definition: Collisions.cs:6
Jypeli.Game.DebugScreenVisible
bool DebugScreenVisible
Debug-ruutu näkyvissä / pois.
Definition: DebugScreen.cs:27
Jypeli.Layer.Update
void Update(Time time)
Definition: Layer.cs:209
Jypeli.Window
Ikkuna.
Definition: Window.cs:37
Jypeli.GameObject
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: Appearance.cs:34
Jypeli.Keyboard
Näppäimistö.
Definition: Keyboard.cs:41
Jypeli.Color.White
static readonly Color White
Valkoinen.
Definition: Color.cs:903
Jypeli.Game.fpsText
string fpsText
Definition: DebugScreen.cs:8
Jypeli.Game.UpdateFps
void UpdateFps(GameTime gameTime)
Jypelin aika pitää sisällään tiedon edellisestä pelin päivityksestä, MonoGamen aika edellisestä ruudu...
Definition: DebugScreen.cs:94
Jypeli.Label.Text
virtual string Text
Teksti.
Definition: Label.cs:92
Jypeli.Game.PaintDebugScreen
void PaintDebugScreen(Canvas canvas)
Definition: DebugScreen.cs:249
Jypeli.Game.Layers
SynchronousList< Layer > Layers
Kerrokset, joilla pelioliot viihtyvät.
Definition: Layers.cs:14
Jypeli.Game
Definition: Content.cs:46
Jypeli.Game.DebugLayer
Layer DebugLayer
Debug-ruutukerros, joka näkyy kun painetaan F12. Voit lisätä olioita myös tälle kerrokselle.
Definition: DebugScreen.cs:17
Jypeli.Shape.IsUnitSize
abstract bool IsUnitSize
If true, the shape must be scaled by the size of the object that has the shape. Typically,...
Definition: Shapes.cs:53
Jypeli.Key
Key
Näppäimistön näppäin.
Definition: Key.cs:38
Jypeli.IGameObject.Shape
Shape Shape
Definition: IGameObject.cs:40
Jypeli.ScreenView.Left
double Left
Näytön vasemman reunan x-koordinaatti.
Definition: View.cs:292
Jypeli.Layer.Objects
SynchronousList< IGameObject > Objects
Definition: Layer.cs:76
Jypeli.Color.LightGray
static readonly Color LightGray
Vaalea harmaa.
Definition: Color.cs:698
Jypeli.Game.PaintPhysicsOutlines
void PaintPhysicsOutlines(Canvas canvas, PhysicsObject obj)
Definition: DebugScreen.cs:181