-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStereoCopy.cs
More file actions
144 lines (109 loc) · 3.78 KB
/
StereoCopy.cs
File metadata and controls
144 lines (109 loc) · 3.78 KB
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
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Runtime.InteropServices;
namespace Stereo
{
public class StereoCopy : GraphicsDeviceControl
{
#region Constructors
public StereoCopy()
{
}
public StereoCopy(Texture2D stereoTexture)
{
backgroundTexture = stereoTexture;
}
#endregion
#region Class Variables
// Our Basic effect
BasicEffect effect;
// Content stuff
public Texture2D backgroundTexture { get; set; }
Rectangle mainFrame;
SpriteBatch spriteBatch;
#endregion
#region Timestep Fixing
Stopwatch stopWatch;
readonly TimeSpan TargetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 60);
readonly TimeSpan MaxElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 10);
TimeSpan accumulatedTime;
TimeSpan lastTime;
void Tick(object sender, EventArgs e)
{
TimeSpan currentTime = stopWatch.Elapsed;
TimeSpan elapsedTime = currentTime - lastTime;
lastTime = currentTime;
if (elapsedTime > MaxElapsedTime)
{
elapsedTime = MaxElapsedTime;
}
accumulatedTime += elapsedTime;
bool updated = false;
while (accumulatedTime >= TargetElapsedTime)
{
Update();
accumulatedTime -= TargetElapsedTime;
updated = true;
}
if (updated)
{
Invalidate();
}
}
// Ticking function
void TickWhileIdle(object sender, EventArgs e)
{
NativeMethods.Message message;
while (!NativeMethods.PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
{
Tick(sender, e);
}
}
#endregion
#region Initialize
/// <summary>
/// Initializes the control.
/// </summary>
protected override void Initialize()
{
// BackBuffer size and stuff
GraphicsDevice.PresentationParameters.BackBufferHeight = 480;
GraphicsDevice.PresentationParameters.BackBufferWidth = 640;
backgroundTexture = new Texture2D(GraphicsDevice, GraphicsDevice.DisplayMode.Width, GraphicsDevice.DisplayMode.Height);
// Create our BasicEffect.
effect = new BasicEffect(GraphicsDevice);
// set a spritebatch
spriteBatch = new SpriteBatch(GraphicsDevice);
// set the frame
mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
// Start the animation timer.
stopWatch = Stopwatch.StartNew();
// Hook the idle event to constantly redraw our animation.
Application.Idle += Tick;
}
#endregion
#region Draw
/// <summary>
/// Draws the control.
/// </summary>
protected override void Draw()
{
// has the window resized?
mainFrame.Width = GraphicsDevice.Viewport.Width;
mainFrame.Height = GraphicsDevice.Viewport.Height;
// Clear the Graphics Device to render the scene
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
spriteBatch.Draw(backgroundTexture, mainFrame,
Color.White);
spriteBatch.End();
}
#endregion
}
}