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
// Copyright (c) 2014 by SiegeLord
//
// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.

use core::Core;

use events::EventSource;
use ffi::*;
use libc::*;
use std::mem;

pub struct Timer
{
	allegro_timer: *mut ALLEGRO_TIMER,
}

impl Timer
{
	pub fn new(_: &Core, speed_secs: f64) -> Result<Timer, ()>
	{
		unsafe {
			let t = al_create_timer(speed_secs as c_double);
			if !t.is_null()
			{
				Ok(Timer { allegro_timer: t })
			}
			else
			{
				Err(())
			}
		}
	}

	pub fn start(&self)
	{
		unsafe {
			al_start_timer(self.allegro_timer);
		}
	}

	pub fn stop(&self)
	{
		unsafe {
			al_stop_timer(self.allegro_timer);
		}
	}

	pub fn is_started(&self) -> bool
	{
		unsafe { al_get_timer_started(mem::transmute(self.allegro_timer)) != 0 }
	}

	pub fn get_speed(&self) -> f64
	{
		unsafe { al_get_timer_speed(mem::transmute(self.allegro_timer)) as f64 }
	}

	pub fn set_speed(&self, speed_secs: f64)
	{
		unsafe {
			al_set_timer_speed(self.allegro_timer, speed_secs as c_double);
		}
	}

	pub fn get_count(&self) -> i64
	{
		unsafe { al_get_timer_count(mem::transmute(self.allegro_timer)) as i64 }
	}

	pub fn set_count(&self, count: i64)
	{
		unsafe {
			al_set_timer_count(self.allegro_timer, count);
		}
	}

	pub fn add_count(&self, diff: i64)
	{
		unsafe {
			al_add_timer_count(self.allegro_timer, diff);
		}
	}

	pub fn get_event_source(&self) -> EventSource
	{
		unsafe { EventSource::wrap(al_get_timer_event_source(self.allegro_timer)) }
	}
}

impl Drop for Timer
{
	fn drop(&mut self)
	{
		unsafe {
			al_destroy_timer(self.allegro_timer);
		}
	}
}