-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.java
More file actions
97 lines (76 loc) · 2.11 KB
/
Timer.java
File metadata and controls
97 lines (76 loc) · 2.11 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
package com.vinnstar.myfirstgame;
/**
* Created by Laurent on 1/5/2017.
*/
public class Timer {
long startTicks = 0;
long pausedTicks = 0;
boolean paused = false;
boolean started = false;
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
void start() {
//Start the timer
started = true;
//Un-pause the timer
paused = false;
//Get the current clock time
startTicks = System.nanoTime();
}
void stop() {
//Stop the timer
started = false;
//Un-pause the timer
paused = false;
}
void pause() {
//If the timer is running and isn't already paused
if( ( started ) && ( !paused ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = System.nanoTime() - startTicks;
}
}
void unPause() {
//If the timer is paused
if( paused )
{
//Un-pause the timer
paused = false;
//Reset the starting ticks
startTicks = System.nanoTime() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
long getTicks() {
//Button_Timer_Value = (SDL_GetTicks() - startTicks);
//If the timer is running
if( started )
{
//If the timer is paused
if( paused )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return (System.nanoTime() - startTicks)/1000000;
}
}
//If the timer isn't running
return 0;
}
boolean isStarted() {
return started;
}
boolean isPaused()
{
return paused;
}
}