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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) 2014 by SiegeLord
//
// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.

#![crate_name="allegro_primitives"]
#![crate_type = "lib"]
#![allow(non_upper_case_globals)]

extern crate allegro;
extern crate allegro_primitives_sys;
extern crate libc;

use std::cell::RefCell;
use std::marker::PhantomData;
use std::ptr;
use std::sync::{Arc, Mutex};

use allegro::{BitmapLike, Core, Color};
use allegro_primitives_sys::*;
use libc::*;

static mut initialized: bool = false;
thread_local!(static spawned_on_this_thread: RefCell<bool> = RefCell::new(false));

#[repr(u32)]
#[derive(Copy, Clone)]
pub enum PrimType
{
	LineList = ALLEGRO_PRIM_LINE_LIST,
	LineStrip = ALLEGRO_PRIM_LINE_STRIP,
	LineLoop = ALLEGRO_PRIM_LINE_LOOP,
	TriangleList = ALLEGRO_PRIM_TRIANGLE_LIST,
	TriangleStrip = ALLEGRO_PRIM_TRIANGLE_STRIP,
	TriangleFan = ALLEGRO_PRIM_TRIANGLE_FAN,
	PointList = ALLEGRO_PRIM_POINT_LIST,
}

pub struct PrimitivesAddon
{
	core_mutex: Arc<Mutex<()>>,
	no_send_marker: PhantomData<*mut u8>,
}

impl PrimitivesAddon
{
	pub fn init(core: &Core) -> Result<PrimitivesAddon, String>
	{
		let mutex = core.get_core_mutex();
		let _guard = mutex.lock();
		unsafe
		{
			if initialized
			{
				if spawned_on_this_thread.with(|x| *x.borrow())
				{
					Err("The primitives addon has already been created in this task.".to_string())
				}
				else
				{
					spawned_on_this_thread.with(|x| *x.borrow_mut() = true);
					Ok(PrimitivesAddon{ core_mutex: core.get_core_mutex(), no_send_marker: PhantomData })
				}
			}
			else
			{
				if al_init_primitives_addon() != 0
				{
					initialized = true;
					spawned_on_this_thread.with(|x| *x.borrow_mut() = true);
					Ok(PrimitivesAddon{ core_mutex: core.get_core_mutex(), no_send_marker: PhantomData })
				}
				else
				{
					Err("Could not initialize the primitives addon.".to_string())
				}
			}
		}
	}

	pub fn get_version() -> i32
	{
		unsafe
		{
			al_get_allegro_primitives_version() as i32
		}
	}

	pub fn get_core_mutex(&self) -> Arc<Mutex<()>>
	{
		self.core_mutex.clone()
	}

	pub fn draw_prim<T: VertexVector, B: BitmapLike>(&self, vtxs: &T, texture: Option<&B>, start: u32, end: u32, type_: PrimType) -> u32
	{
		let tex = texture.map_or(ptr::null_mut(), |bmp| bmp.get_allegro_bitmap());
		unsafe
		{
			al_draw_prim(vtxs.get_ptr() as *const _, vtxs.get_decl(), tex, start as c_int, end as c_int, type_ as c_int) as u32
		}
	}

	pub fn draw_indexed_prim<T: VertexVector, B: BitmapLike>(&self, vtxs: &T, texture: Option<&B>, indices: &[i32], num_vtx: u32, type_: PrimType) -> u32
	{
		let tex = texture.map_or(ptr::null_mut(), |bmp| bmp.get_allegro_bitmap());
		unsafe
		{
			al_draw_indexed_prim(vtxs.get_ptr() as *const _, vtxs.get_decl(), tex, indices.as_ptr(), num_vtx as c_int, type_ as c_int) as u32
		}
	}

	pub fn draw_line(&self, x1: f32, y1: f32, x2: f32, y2: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_line(x1 as c_float, y1 as c_float, x2 as c_float, y2 as c_float, color.get_allegro_color(), thickness as c_float)
		}
	}

	pub fn draw_triangle(&self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_triangle(x1 as c_float, y1 as c_float, x2 as c_float, y2 as c_float, x3 as c_float, y3 as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_rectangle(&self, x1: f32, y1: f32, x2: f32, y2: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_rectangle(x1 as c_float, y1 as c_float, x2 as c_float, y2 as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_rounded_rectangle(&self, x1: f32, y1: f32, x2: f32, y2: f32, rx: f32, ry: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_rounded_rectangle(x1 as c_float, y1 as c_float, x2 as c_float, y2 as c_float, rx as c_float, ry as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_circle(&self, cx: f32, cy: f32, r: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_circle(cx as c_float, cy as c_float, r as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_ellipse(&self, cx: f32, cy: f32, rx: f32, ry: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_ellipse(cx as c_float, cy as c_float, rx as c_float, ry as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_arc(&self, cx: f32, cy: f32, r: f32, start_theta: f32, delta_theta: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_arc(cx as c_float, cy as c_float, r as c_float, start_theta as c_float, delta_theta as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_elliptical_arc(&self, cx: f32, cy: f32, rx: f32, ry: f32, start_theta: f32, delta_theta: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_elliptical_arc(cx as c_float, cy as c_float, rx as c_float, ry as c_float, start_theta as c_float, delta_theta as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_pieslice(&self, cx: f32, cy: f32, r: f32, start_theta: f32, delta_theta: f32, color: Color, thickness: f32)
	{
		unsafe
		{
			al_draw_pieslice(cx as c_float, cy as c_float, r as c_float, start_theta as c_float, delta_theta as c_float, color.get_allegro_color(), thickness as c_float);
		}
	}

	pub fn draw_spline<T: Iterator<Item = (f32, f32)>>(&self, points: T, color: Color, thickness: f32) -> Result<(), ()>
	{
		let mut c_points: [c_float; 8] = [0.0; 8];
		let mut idx = 0;
		for (x, y) in points
		{
			if idx >= c_points.len()
			{
				return Err(())
			}
			c_points[idx + 0] = x as c_float;
			c_points[idx + 1] = y as c_float;
			idx += 2;
		}

		unsafe
		{
			al_draw_spline(c_points, color.get_allegro_color(), thickness as c_float);
		}
		Ok(())
	}

	pub fn draw_filled_triangle(&self, x1: f32, y1: f32, x2: f32, y2: f32, x3: f32, y3: f32, color: Color)
	{
		unsafe
		{
			al_draw_filled_triangle(x1 as c_float, y1 as c_float, x2 as c_float, y2 as c_float, x3 as c_float, y3 as c_float, color.get_allegro_color());
		}
	}

	pub fn draw_filled_rectangle(&self, x1: f32, y1: f32, x2: f32, y2: f32, color: Color)
	{
		unsafe
		{
			al_draw_filled_rectangle(x1 as c_float, y1 as c_float, x2 as c_float, y2 as c_float, color.get_allegro_color());
		}
	}

	pub fn draw_filled_ellipse(&self, cx: f32, cy: f32, rx: f32, ry: f32, color: Color)
	{
		unsafe
		{
			al_draw_filled_ellipse(cx as c_float, cy as c_float, rx as c_float, ry as c_float, color.get_allegro_color());
		}
	}

	pub fn draw_filled_circle(&self, cx: f32, cy: f32, r: f32, color: Color)
	{
		unsafe
		{
			al_draw_filled_circle(cx as c_float, cy as c_float, r as c_float, color.get_allegro_color());
		}
	}

	pub fn draw_filled_pieslice(&self, cx: f32, cy: f32, r: f32, start_theta: f32, delta_theta: f32, color: Color)
	{
		unsafe
		{
			al_draw_filled_pieslice(cx as c_float, cy as c_float, r as c_float, start_theta as c_float, delta_theta as c_float, color.get_allegro_color());
		}
	}

	pub fn draw_filled_rounded_rectangle(&self, x1: f32, y1: f32, x2: f32, y2: f32, rx: f32, ry: f32, color: Color)
	{
		unsafe
		{
			al_draw_filled_rounded_rectangle(x1 as c_float, y1 as c_float, x2 as c_float, y2 as c_float, rx as c_float, ry as c_float, color.get_allegro_color());
		}
	}
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Vertex
{
	pub x: f32,
	pub y: f32,
	pub z: f32,
	pub u: f32,
	pub v: f32,
	pub color: Color,
}

pub trait VertexVector
{
	fn get_ptr(&self) -> *const u8;
	fn get_decl(&self) -> *const ALLEGRO_VERTEX_DECL;
}

impl<'l> VertexVector for &'l [Vertex]
{
	fn get_ptr(&self) -> *const u8
	{
		self.as_ptr() as *const _
	}

	fn get_decl(&self) -> *const ALLEGRO_VERTEX_DECL
	{
		ptr::null()
	}
}