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
|
public Window1()
{
InitializeComponent();
// Set up the initial state for the D3DImage.
HRESULT.Check(SetSize(512, 512));
HRESULT.Check(SetAlpha(false));
HRESULT.Check(SetNumDesiredSamples(4));
/* CALL À INITIALIZE DispatcherTimer */
int myFPS = 30;
createTheRenderingWithCorrectFPS(myFPS);
/* out CompositionTarget*/
//CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
_adapterTimer = new DispatcherTimer();
_adapterTimer.Tick += new EventHandler(AdapterTimer_Tick);
_adapterTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
_adapterTimer.Start();
//
_sizeTimer = new DispatcherTimer(DispatcherPriority.Render);
_sizeTimer.Tick += new EventHandler(SizeTimer_Tick);
_sizeTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
_sizeTimer.Start();
}
void AdapterTimer_Tick(object sender, EventArgs e)
{
POINT p = new POINT(imgelt.PointToScreen(new Point(0, 0)));
HRESULT.Check(SetAdapter(p));
}
void SizeTimer_Tick(object sender, EventArgs e)
{
// Given that the D3DImage is at 96.0 DPI, its Width and Height
// properties will always be integers. ActualWidth/Height
// may not be integers, so they are cast to integers.
uint actualWidth = (uint)imgelt.ActualWidth;
uint actualHeight = (uint)imgelt.ActualHeight;
if ((actualWidth > 0 && actualHeight > 0) &&
(actualWidth != (uint)d3dimg.Width || actualHeight != (uint)d3dimg.Height))
{
HRESULT.Check(SetSize(actualWidth, actualHeight));
}
}
/*ICI INITIALIZE DispatcherTimer*/
public void createTheRenderingWithCorrectFPS(int whichFPS)
{
DispatcherTimer _timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(1000d / whichFPS);
_timer.Start();
_timer.Tick += new EventHandler(_timer_Tick);
}
/* DANS TIMER_TICK LE CODE de CompositionTarget_Rendering */
void _timer_Tick(object sender, EventArgs e)
{
//Refresh the 3D scene
if (d3dimg.IsFrontBufferAvailable )
{
IntPtr pSurface = IntPtr.Zero;
HRESULT.Check(GetBackBufferNoRef(out pSurface));
if (pSurface != IntPtr.Zero)
{
d3dimg.Lock();
// Repeatedly calling SetBackBuffer with the same IntPtr is
// a no-op. There is no performance penalty.
d3dimg.SetBackBuffer(D3DResourceType.IDirect3DSurface9, pSurface);
HRESULT.Check(Render());
d3dimg.AddDirtyRect(new Int32Rect(0, 0, d3dimg.PixelWidth, d3dimg.PixelHeight));
d3dimg.Unlock();
}
}
} |
Partager