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
#![warn(missing_docs)]
#![allow(unused_imports)]
pub mod interchange;
use interchange::*;
pub fn game_update_and_render(
time: Duration, input: &GameInput, bitmap: &mut Bitmap, sound: &mut SoundBuffer,
) {
game_output_sound(sound, input.tone_hz);
render_weird_gradient(bitmap, input.x_offset, input.y_offset)
}
pub fn render_weird_gradient(bitmap: &mut Bitmap, blue_offset: i32, green_offset: i32) {
let bitmap_memory = bitmap.memory;
let width = bitmap.width;
let height = bitmap.height;
let pitch = bitmap.pitch;
let mut row_start = bitmap_memory;
for y in 0..height {
let mut pixel = row_start as *mut u32;
for x in 0..width {
let blue = (x + blue_offset) as u8 as u32;
let green = (y + green_offset) as u8 as u32;
unsafe {
pixel.write(blue | (green << 8));
pixel = pixel.offset(1);
}
}
row_start = unsafe { row_start.offset(pitch) };
}
}
pub fn game_output_sound(sound_buffer: &mut SoundBuffer, tone_hz: i32) {
static mut tSine: f32 = 0.0;
const VOLUME: i16 = 1000;
const TWO_PI: f32 = 2.0 * std::f32::consts::PI;
let wave_period = sound_buffer.samples_per_second / tone_hz;
let mut sample_out: *mut i16 = sound_buffer.samples;
for _sample_index in 0..sound_buffer.sample_count {
unsafe {
let sine_value = tSine.sin();
let sample_value = (sine_value * VOLUME as f32) as i16;
*sample_out = sample_value;
sample_out = sample_out.offset(1);
*sample_out = sample_value;
sample_out = sample_out.offset(1);
tSine += 2.0 * std::f32::consts::PI * 1.0 / wave_period as f32;
if tSine > TWO_PI {
tSine -= TWO_PI;
}
}
}
}