Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 17 additions & 52 deletions allegro/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use events::EventSource;
use ffi::*;
use libc::*;
#[cfg(any(allegro_5_2_0, allegro_5_1_0))]
use shader::{Shader, ShaderPlatform};
use std::ffi::CString;
use std::mem;
use std::sync::{Arc, Weak};
use std::sync::Arc;

flag_type! {
DisplayFlags
Expand Down Expand Up @@ -111,7 +110,7 @@ pub struct Display
allegro_display: *mut ALLEGRO_DISPLAY,
backbuffer: Bitmap,
#[cfg(any(allegro_5_2_0, allegro_5_1_0))]
shaders: Vec<Arc<Shader>>,
tokens: Vec<Arc<String>>,
}

impl Display
Expand Down Expand Up @@ -139,6 +138,7 @@ impl Display
Display {
allegro_display: d,
backbuffer: backbuffer,
tokens: vec![],
}
}

Expand All @@ -148,7 +148,7 @@ impl Display
Display {
allegro_display: d,
backbuffer: backbuffer,
shaders: vec![],
tokens: vec![],
}
}

Expand Down Expand Up @@ -279,52 +279,6 @@ impl Display
self.allegro_display
}

/// Create a new shader associated with this display.
///
/// Note that display destruction will panic if a strong reference is held
/// to a shader at that time.
#[cfg(any(allegro_5_2_0, allegro_5_1_0))]
pub fn create_shader(&mut self, platform: ShaderPlatform) -> Result<Weak<Shader>, ()>
{
let shader;
unsafe {
let old_target = al_get_target_bitmap();
al_set_target_bitmap(self.get_backbuffer().get_allegro_bitmap());
shader = al_create_shader(platform as ALLEGRO_SHADER_PLATFORM);
al_set_target_bitmap(old_target);
};
if !shader.is_null()
{
let shader = unsafe { Arc::new(Shader::wrap(shader)) };
let ret = Arc::downgrade(&shader);
self.shaders.push(shader);
Ok(ret)
}
else
{
Err(())
}
}

#[cfg(any(allegro_5_2_0, allegro_5_1_0))]
fn has_outstanding_shaders(&self) -> bool
{
for shader in &self.shaders
{
if Arc::strong_count(&shader) != 1
{
return true;
}
}
false
}

#[cfg(not(any(allegro_5_2_0, allegro_5_1_0)))]
fn has_outstanding_shaders(&self) -> bool
{
false
}

pub fn show_cursor(&self, show: bool) -> Result<(), ()>
{
let ret = unsafe {
Expand All @@ -346,6 +300,11 @@ impl Display
Err(())
}
}

pub fn add_dependency_token(&mut self, token: Arc<String>)
{
self.tokens.push(token);
}
}

unsafe impl Send for Display {}
Expand All @@ -355,9 +314,15 @@ impl Drop for Display
{
fn drop(&mut self)
{
if self.has_outstanding_shaders()
for token in &self.tokens
{
panic!("Display has outstanding shaders!");
if Arc::strong_count(&token) != 1
{
panic!(
"Have an outstanding dependency token! Token name: {}",
token
);
}
}
unsafe {
check_display_targeted_elsewhere(self.allegro_display, "destroy");
Expand Down
31 changes: 28 additions & 3 deletions allegro/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
//
// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.

use bitmap_like::BitmapLike;
use display::Display;
use ffi::*;

use libc::*;
use std::ffi::{CStr, CString};
use std::ptr;
use std::sync::Arc;

/// Shader platform.
#[repr(u32)]
Expand Down Expand Up @@ -47,14 +50,36 @@ pub enum ShaderType
pub struct Shader
{
allegro_shader: *mut ALLEGRO_SHADER,
#[allow(dead_code)]
token: Arc<String>,
}

impl Shader
{
pub unsafe fn wrap(shader: *mut ALLEGRO_SHADER) -> Shader
/// Create a new shader associated with this display.
///
/// Note that display destruction will panic if a shader remains alive at that time.
pub fn new(display: &mut Display, platform: ShaderPlatform) -> Result<Self, ()>
{
Shader {
allegro_shader: shader,
let shader;
unsafe {
let old_target = al_get_target_bitmap();
al_set_target_bitmap(display.get_backbuffer().get_allegro_bitmap());
shader = al_create_shader(platform as ALLEGRO_SHADER_PLATFORM);
al_set_target_bitmap(old_target);
};
if !shader.is_null()
{
let token = Arc::new("Shader".to_string());
display.add_dependency_token(token.clone());
Ok(Shader {
allegro_shader: shader,
token: token,
})
}
else
{
Err(())
}
}

Expand Down
46 changes: 46 additions & 0 deletions allegro_primitives-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ pub mod allegro_primitives
pub const ALLEGRO_LINE_CAP_TRIANGLE: c_uint = 3;
pub const ALLEGRO_LINE_CAP_CLOSED: c_uint = 4;

pub const ALLEGRO_PRIM_BUFFER_STREAM: c_uint = 0x01;
pub const ALLEGRO_PRIM_BUFFER_STATIC: c_uint = 0x02;
pub const ALLEGRO_PRIM_BUFFER_DYNAMIC: c_uint = 0x04;
pub const ALLEGRO_PRIM_BUFFER_READWRITE: c_uint = 0x08;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ALLEGRO_VERTEX_ELEMENT
Expand All @@ -87,6 +92,9 @@ pub mod allegro_primitives
pub color: ALLEGRO_COLOR,
}

opaque!(ALLEGRO_VERTEX_BUFFER);
opaque!(ALLEGRO_INDEX_BUFFER);

extern "C" {
pub fn al_get_allegro_primitives_version() -> u32;
pub fn al_init_primitives_addon() -> c_bool;
Expand All @@ -100,6 +108,15 @@ pub mod allegro_primitives
vtxs: *const c_void, decl: *const ALLEGRO_VERTEX_DECL, texture: *mut ALLEGRO_BITMAP,
indices: *const c_int, num_vtx: c_int, _type: c_int,
) -> c_int;
pub fn al_draw_vertex_buffer(
vertex_buffer: *mut ALLEGRO_VERTEX_BUFFER, texture: *mut ALLEGRO_BITMAP, start: c_int,
end: c_int, type_: c_int,
) -> c_int;

pub fn al_draw_indexed_buffer(
vertex_buffer: *mut ALLEGRO_VERTEX_BUFFER, texture: *mut ALLEGRO_BITMAP,
index_buffer: *mut ALLEGRO_INDEX_BUFFER, start: c_int, end: c_int, type_: c_int,
) -> c_int;

pub fn al_create_vertex_decl(
elements: *const ALLEGRO_VERTEX_ELEMENT, stride: c_int,
Expand Down Expand Up @@ -228,5 +245,34 @@ pub mod allegro_primitives
pub fn al_draw_filled_polygon_with_holes(
vertices: *const c_float, vertex_counts: *const c_int, color: ALLEGRO_COLOR,
);

pub fn al_create_vertex_buffer(
decl: *mut ALLEGRO_VERTEX_DECL, initial_data: *const c_void, num_vertices: c_int,
flags: c_int,
) -> *mut ALLEGRO_VERTEX_BUFFER;

pub fn al_destroy_vertex_buffer(buffer: *mut ALLEGRO_VERTEX_BUFFER);

pub fn al_lock_vertex_buffer(
buffer: *mut ALLEGRO_VERTEX_BUFFER, offset: c_int, length: c_int, flags: c_int,
) -> *mut c_void;

pub fn al_unlock_vertex_buffer(buffer: *mut ALLEGRO_VERTEX_BUFFER);

pub fn al_get_vertex_buffer_size(buffer: *mut ALLEGRO_VERTEX_BUFFER) -> c_int;

pub fn al_create_index_buffer(
index_size: c_int, initial_data: *const c_void, num_indices: c_int, flags: c_int,
) -> *mut ALLEGRO_INDEX_BUFFER;

pub fn al_destroy_index_buffer(buffer: *mut ALLEGRO_INDEX_BUFFER);

pub fn al_lock_index_buffer(
buffer: *mut ALLEGRO_INDEX_BUFFER, offset: c_int, length: c_int, flags: c_int,
) -> *mut c_void;

pub fn al_unlock_index_buffer(buffer: *mut ALLEGRO_INDEX_BUFFER);

pub fn al_get_index_buffer_size(buffer: *mut ALLEGRO_INDEX_BUFFER) -> c_int;
}
}
5 changes: 5 additions & 0 deletions allegro_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ version = "=0.0.48" #auto

path = "../allegro_primitives-sys"
version = "=0.0.48" #auto

[dependencies.allegro_util]

path = "../allegro_util"
version = "=0.0.48" #auto
Loading