summaryrefslogtreecommitdiff
path: root/Noddybox.Emulation/Clock.cs
blob: 79beafb52f0d8e594afb5801b5ddc863376f627f (plain)
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
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Noddybox.Emulation
{
    /// <summary>
    /// Provides a basic clock implementation that is geared towards the VBL.
    /// </summary>
    public class Clock
    {
        #region Private fields

        private uint    perSecond;
        private uint    perFrame;
        private uint    perLine;
        private uint    frameCount;

        #endregion

        #region Public properties

        /// <summary>
        /// Returns true if the frame has finished.
        /// </summary>
        public bool FrameDone
        {
            get
            {
                return frameCount > perFrame;
            }
        }

        /// <summary>
        /// Returns the current raster line.
        /// </summary>
        public uint RasterLine
        {
            get
            {
                return frameCount / perLine;
            }
        }

        #endregion

        #region Public members

        /// <summary>
        /// Starts a new frame.
        /// </summary>
        public void StartFrame()
        {
            if (FrameDone)
            {
                frameCount -= perFrame;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Defines a clock.
        /// </summary>
        /// <param name="ticksPerSecond">The number of ticks per second.</param>
        /// <param name="framesPerSecond">The number of frames per second.</param>
        public Clock(uint ticksPerSecond, uint framesPerSecond)
        {
            this.perSecond = ticksPerSecond;
            this.perFrame = ticksPerSecond / framesPerSecond;
            this.perLine = 1;
            this.frameCount = 0;
        }

        /// <summary>
        /// Defines a clock.
        /// </summary>
        /// <param name="ticksPerSecond">The number of ticks per second.</param>
        /// <param name="framesPerSecond">The number of frames per second.</param>
        /// <param name="ticksPerLine">The number of clock ticks per raster line.</param>
        public Clock(uint ticksPerSecond, uint framesPerSecond, uint ticksPerLine)
        {
            this.perSecond = ticksPerSecond;
            this.perFrame = ticksPerSecond / framesPerSecond;
            this.perLine = ticksPerLine;
            this.frameCount = 0;
        }

        #endregion
    }
}