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
//! This is a simple crate that makes making qwac games easier.
//!
//! It has one feature, `alloc`.  You should enable it if you plan on using liballoc (including if
//! `std` is enabled), otherwise, leave it disabled to save some space.

#![no_std]

pub mod event;
pub mod ffi;

#[cfg(feature = "alloc")]
pub extern crate alloc;

#[derive(Default, Clone, Copy, Hash, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Rectangle {
    pub x: u16,
    pub y: u16,
    pub width: u16,
    pub height: u16,
}

#[derive(Default, Clone, Copy, Hash, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct OpaqueColor {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

#[derive(Default, Clone, Copy, Hash, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

pub trait Game {
    /// Create a new Game of this type, starting at the given time in seconds.
    fn new(timestamp: i64) -> Self;

    /// Handle an input event.
    fn event(&mut self, event: event::Event);

    /// Do processing and rendering.  delta tells you the time in seconds between this frame and
    /// the last one.
    fn update(&mut self, frame_milliseconds: u32);
}

#[macro_export]
macro_rules! qwac_game {
    ($game_type:ty) => {
        #[cfg(not(feature = "alloc"))]
        static mut QWAC_RUST_GAME: Option<$game_type> = None;

        #[no_mangle]
        pub extern "C" fn qwac_game_create(timestamp: i64) -> *mut $game_type {
            let game = <$game_type as $crate::Game>::new(timestamp);

            #[cfg(not(feature = "alloc"))]
            {
                unsafe {
                    QWAC_RUST_GAME = None;
                    QWAC_RUST_GAME = Some(game);
                }
                core::ptr::null_mut()
            }

            #[cfg(feature = "alloc")]
            $crate::alloc::boxed::Box::new(game).into_raw()
        }

        #[no_mangle]
        pub extern "C" fn qwac_game_event(
            _game: *mut $game_type,
            event_type: $crate::ffi::EventType,
            player: u8,
            button: u8,
            x: u16,
            y: u16,
        ) {
            #[cfg(not(feature = "alloc"))]
            let game = unsafe { QWAC_RUST_GAME.as_mut().unwrap() };

            #[cfg(feature = "alloc")]
            let game = unsafe { &mut *_game };

            match event_type {
                $crate::ffi::EventType::GamepadButtonPress => {
                    if let Some(button) = $crate::event::GamepadButton::from_code(button) {
                        $crate::Game::event(
                            game,
                            $crate::event::Event::GamepadButton {
                                button: button,
                                player: player,
                                state: $crate::event::ButtonState::Pressed,
                            },
                        );
                    }
                }
                $crate::ffi::EventType::GamepadButtonRelease => {
                    if let Some(button) = $crate::event::GamepadButton::from_code(button) {
                        $crate::Game::event(
                            game,
                            $crate::event::Event::GamepadButton {
                                button: button,
                                player: player,
                                state: $crate::event::ButtonState::Released,
                            },
                        );
                    }
                }
                _ => (),
            }
        }

        #[no_mangle]
        pub extern "C" fn qwac_game_update(_game: *mut $game_type, frame_milliseconds: u32) {
            #[cfg(not(feature = "alloc"))]
            let game = unsafe { QWAC_RUST_GAME.as_mut().unwrap() };

            #[cfg(feature = "alloc")]
            let game = unsafe { &mut *_game };

            $crate::Game::update(game, frame_milliseconds);
        }

        #[no_mangle]
        pub extern "C" fn qwac_game_destroy(_game: *mut $game_type) {
            #[cfg(not(feature = "alloc"))]
            unsafe {
                QWAC_RUST_GAME = None;
            }

            #[cfg(feature = "alloc")]
            unsafe {
                $crate::alloc::boxed::Box::from_raw(game);
            }

        }
    };
}

pub fn log(string: &str) {
    let pointer = string.as_ptr();
    let len = string.len() as u32;
    unsafe {
        ffi::qwac_log(pointer, len);
    }
}

pub fn display(width_px: u16, height_px: u16) {
    unsafe {
        ffi::qwac_display(width_px, height_px);
    }
}

pub fn clear_color(color: OpaqueColor) {
    unsafe {
        ffi::qwac_clear_color(color.r, color.g, color.b);
    }
}

pub fn clear() {
    unsafe {
        ffi::qwac_clear();
    }
}

pub fn fill_rectangle(rectangle: Rectangle, color: Color) {
    unsafe {
        ffi::qwac_fill_rectangle(
            rectangle.x,
            rectangle.y,
            rectangle.width,
            rectangle.height,
            color.r,
            color.g,
            color.b,
            color.a,
        );
    }
}