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
//! 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 {
  /// u32 order: A R G B
  pub memory: *mut u8,
  /// width in pixels
  pub width: i32,
  /// height in pixels
  pub height: i32,
  /// pitch in BYTES, pass directly to .offset
  pub pitch: isize,
}

/// Debug sound buffer.
#[derive(Debug)]
#[allow(missing_docs)]
pub struct SoundBuffer {
  // TODO: why the heck aren't these u32?
  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, Default)]
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 scroll_x: u8,
  pub scroll_y: u8,
  pub tone_hz: i32,
  pub t_sine: f32,
}

/// Responds to input, renders the frame, and prepares sound.
pub type GameUpdateAndRenderFn = extern "C" fn(&mut GameMemory, &GameInput, &mut Bitmap);

/// Fills the sound buffer based on the prepared sound.
///
/// This must be very quick, less than 1ms.
pub type GameGetSoundSamplesFn = extern "C" fn(&mut GameMemory, &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())));
  };
}