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
//! This module defines the interface between platform binary and game library.

pub use std::time::Duration;

/// Debug bitmap buffer
#[derive(Debug)]
#[allow(missing_docs)]
pub struct Bitmap {
  pub memory: *mut u8,
  pub width: i32,
  pub height: i32,
  pub pitch: isize,
}

/// Debug sound buffer.
#[derive(Debug)]
#[allow(missing_docs)]
pub struct SoundBuffer {
  pub samples_per_second: i32,
  pub sample_count: i32,
  pub samples: *mut i16,
}

/// A button on a controller (up or down).
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ControllerButton {
  /// How many transitions since the last game frame.
  pub transition_count: u8,
  /// Is the button currently pressed.
  pub pressed: bool,
}

/// Platform independent representation for an XBox controller.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)]
pub struct XBoxController {
  pub packet_number: u32,
  pub top_button: ControllerButton,
  pub left_button: ControllerButton,
  pub right_button: ControllerButton,
  pub bottom_button: ControllerButton,
  pub start_button: ControllerButton,
  pub back_button: ControllerButton,
  pub left_shoulder: ControllerButton,
  pub right_shoulder: ControllerButton,
  pub left_stick: ControllerButton,
  pub right_stick: ControllerButton,
  pub dpad_up: ControllerButton,
  pub dpad_down: ControllerButton,
  pub dpad_left: ControllerButton,
  pub dpad_right: ControllerButton,
  pub left_trigger: u8,
  pub right_trigger: u8,
  pub left_thumb_x: i16,
  pub left_thumb_y: i16,
  pub right_thumb_x: i16,
  pub right_thumb_y: i16,
  pub controller_id: u8,
  pub trigger_threshold: u8,
  pub left_thumb_deadzone: i16,
  pub right_thumb_deadzone: i16,
}

/// Right now we only accept xbox controller input.
#[derive(Debug, Clone)]
pub struct GameInput {
  /// The xbox controllers (via XInput / xboxdrv / whatever)
  pub xbox_controllers: [Option<XBoxController>; 4],
}

/// Debug struct for the game's memory.
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub struct GameMemory {
  pub x_offset: i32,
  pub y_offset: i32,
  pub tone_hz: i32,
}

/// The type signature of an update and render function.
pub type GameUpdateAndRenderFn =
  fn(&mut GameMemory, Duration, &GameInput, &mut Bitmap, &mut SoundBuffer);

/// `file!():line!()> expr: expr_value`, one per expression
///
/// only works with `debug_assertions` on.
#[macro_export]
macro_rules! dump {
  ($($n:expr),*) => (if cfg!(debug_assertions) {
    $(println!(concat!("{}:{}> ",stringify!($n),": {:?}"),file!(),line!(),$n);)*
  })
}

/// `file!():line!()>`, a space, and then the message.
///
/// only works with `debug_assertions` on.
#[macro_export]
macro_rules! debugln {
  ($($arg:tt)*) => (if cfg!(debug_assertions) {
    // This particular prefix was chosen because that's what VS Code will
    // recognize and then let you ctrl+click to jump to that point in that file.
    print!("{}:{}> ",file!(),line!());
    println!($($arg)*);
  })
}

/// Read a value out of a [thread_local](std::thread_local)
/// [Cell](core::cell::Cell)
///
/// ```
/// use core::cell::Cell;
/// use thorium::tlc_read;
///
/// thread_local! {
///   static YES: Cell<bool> = Cell::new(true);
/// }
/// fn main() {
///   assert!(tlc_read!(YES));
/// }
/// ```
#[macro_export]
macro_rules! tlc_read {
  ($cell_name:ident) => {
    $cell_name.with(Cell::get)
  };
}

/// Write a new value to a [thread_local](std::thread_local)
/// [Cell](core::cell::Cell)
///
/// ```
/// use core::cell::Cell;
/// use thorium::{tlc_read, tlc_write};
///
/// thread_local! {
///   static VAL: Cell<i32> = Cell::new(0);
/// }
/// fn main() {
///   tlc_write!(VAL, 7);
///   assert_eq!(tlc_read!(VAL), 7);
/// }
/// ```
#[macro_export]
macro_rules! tlc_write {
  ($cell_name:ident, $new_value:expr) => {
    $cell_name.with(|cell| cell.set($new_value));
  };
}

/// Modify a [thread_local](std::thread_local) [Cell](core::cell::Cell) with the
/// provided function.
///
/// ```
/// use core::cell::Cell;
/// use thorium::{tlc_read, tlc_modify};
///
/// thread_local! {
///   static VAL: Cell<i32> = Cell::new(3);
/// }
/// fn main() {
///   tlc_modify!(VAL, |v| v + 2);
///   assert_eq!(tlc_read!(VAL), 5);
/// }
/// ```
#[macro_export]
macro_rules! tlc_modify {
  ($cell_name:ident, $modify_func:expr) => {
    $cell_name.with(|cell| cell.set($modify_func(cell.get())));
  };
}