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
extern crate gfx;
extern crate specs;

use std::{thread, time};
use std::sync::mpsc;

pub type Delta = f32;
pub type Planner = specs::Planner<Delta>;

pub const DRAW_PRIORITY: specs::Priority = 10;
pub const DRAW_NAME: &'static str = "draw";


pub trait Init: 'static {
    type Shell: 'static + Send;
    fn start(self, &mut Planner) -> Self::Shell;
    fn proceed(_: &mut Self::Shell, _: &specs::World) -> bool { true }
}

struct App<I: Init> {
    shell: I::Shell,
    planner: Planner,
    last_time: time::Instant,
}

impl<I: Init> App<I> {
    fn tick(&mut self) -> bool {
        let elapsed = self.last_time.elapsed();
        self.last_time = time::Instant::now();
        let delta = elapsed.subsec_nanos() as f32 / 1e9 + elapsed.as_secs() as f32;
        self.planner.dispatch(delta);
        I::proceed(&mut self.shell, self.planner.mut_world())
    }
}

struct ChannelPair<R: gfx::Resources, C: gfx::CommandBuffer<R>> {
    receiver: mpsc::Receiver<gfx::Encoder<R, C>>,
    sender: mpsc::Sender<gfx::Encoder<R, C>>,
}

pub trait Painter<R: gfx::Resources>: 'static + Send {
    type Visual: specs::Component;
    fn draw<'a, I, C>(&mut self, I, &mut gfx::Encoder<R, C>) where
            I: Iterator<Item = &'a Self::Visual>,
            C: gfx::CommandBuffer<R>;
}

struct DrawSystem<R: gfx::Resources, C: gfx::CommandBuffer<R>, P> {
    painter: P,
    channel: ChannelPair<R, C>,
}

impl<R, C, P> specs::System<Delta> for DrawSystem<R, C, P>
where
    R: 'static + gfx::Resources,
    C: 'static + Send + gfx::CommandBuffer<R>,
    P: Painter<R>,
{
    fn run(&mut self, arg: specs::RunArg, _: Delta) {
        use specs::Join;
        // get a new command buffer
        let mut encoder = match self.channel.receiver.recv() {
            Ok(r) => r,
            Err(_) => return,
        };
        // fetch visuals
        let vis = arg.fetch(|w| w.read::<P::Visual>());
        // render entities
        self.painter.draw((&vis).iter(), &mut encoder);
        // done
        let _ = self.channel.sender.send(encoder);
    }
}

pub struct Pegasus<D: gfx::Device> {
    pub device: D,
    channel: ChannelPair<D::Resources, D::CommandBuffer>,
    _guard: thread::JoinHandle<()>,
}

pub struct Swing<'a, D: 'a + gfx::Device> {
    device: &'a mut D,
}

impl<'a, D: 'a + gfx::Device> Drop for Swing<'a, D> {
    fn drop(&mut self) {
        self.device.cleanup();
    }
}

impl<D: gfx::Device> Pegasus<D> {
    pub fn new<F, I, P>(init: I, device: D, painter: P, mut com_factory: F)
               -> Pegasus<D> where
        I: Init,
        D::CommandBuffer: 'static + Send, //TODO: remove when gfx forces these bounds
        P: Painter<D::Resources>,
        F: FnMut() -> D::CommandBuffer,
    {
        let (app_send, dev_recv) = mpsc::channel();
        let (dev_send, app_recv) = mpsc::channel();

        // double-buffering renderers
        for _ in 0..2 {
            let enc = gfx::Encoder::from(com_factory());
            app_send.send(enc).unwrap();
        }

        let mut app = {
            let draw_sys = DrawSystem {
                painter: painter,
                channel: ChannelPair {
                    receiver: app_recv,
                    sender: app_send,
                },
            };
            let mut w = specs::World::new();
            w.register::<P::Visual>();
            let mut plan = specs::Planner::new(w, 4);
            plan.add_system(draw_sys, DRAW_NAME, DRAW_PRIORITY);
            let shell = init.start(&mut plan);
            App::<I> {
                shell: shell,
                planner: plan,
                last_time: time::Instant::now(),
            }
        };

        Pegasus {
            device: device,
            channel: ChannelPair {
                sender: dev_send,
                receiver: dev_recv,
            },
            _guard: thread::spawn(move || {
                while app.tick() {}
            }),
        }
    }

    pub fn swing(&mut self) -> Option<Swing<D>> {
        match self.channel.receiver.recv() {
            Ok(mut encoder) => {
                // draw a frame
                encoder.flush(&mut self.device);
                if self.channel.sender.send(encoder).is_err() {
                    return None
                }
                Some(Swing {
                    device: &mut self.device,
                })
            },
            Err(_) => None,
        }
    }
}