update examples with new input device and main loop event handling stuff
This commit is contained in:
parent
3dfb8fe466
commit
f2f7ae5dee
|
@ -55,7 +55,6 @@ impl AudioGenerator for SineWaveGenerator {
|
|||
fn main() -> Result<()> {
|
||||
let mut system = SystemBuilder::new().window_title("Audio Playback").vsync(true).build()?;
|
||||
|
||||
let mut is_running = true;
|
||||
let mut using_queue_commands = false;
|
||||
let mut volume = 1.0;
|
||||
|
||||
|
@ -69,24 +68,15 @@ fn main() -> Result<()> {
|
|||
|
||||
let mut statuses = [AudioChannelStatus { size: 0, position: 0, playing: false }; NUM_CHANNELS];
|
||||
|
||||
while is_running {
|
||||
system.do_events_with(|event| {
|
||||
match event {
|
||||
SystemEvent::Quit => {
|
||||
is_running = false;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
is_running = false;
|
||||
while !system.do_events() {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
break;
|
||||
}
|
||||
|
||||
let mut audio_device = system.audio.lock();
|
||||
audio_device.volume = volume;
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Num1) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Num1) {
|
||||
if using_queue_commands {
|
||||
system.audio_queue.play_buffer(&sounds[0], false);
|
||||
} else {
|
||||
|
@ -94,7 +84,7 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Num2) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Num2) {
|
||||
if using_queue_commands {
|
||||
system.audio_queue.play_buffer(&sounds[1], false);
|
||||
} else {
|
||||
|
@ -102,7 +92,7 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Num3) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Num3) {
|
||||
if using_queue_commands {
|
||||
system.audio_queue.play_buffer(&sounds[2], false);
|
||||
|
||||
|
@ -111,7 +101,7 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Num4) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Num4) {
|
||||
if using_queue_commands {
|
||||
system.audio_queue.play_buffer(&sounds[3], false);
|
||||
|
||||
|
@ -120,7 +110,7 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Num5) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Num5) {
|
||||
if using_queue_commands {
|
||||
system.audio_queue.play_buffer(&sounds[4], false);
|
||||
} else {
|
||||
|
@ -128,7 +118,7 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Num6) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Num6) {
|
||||
if using_queue_commands {
|
||||
system.audio_queue.play_generator(Box::new(SineWaveGenerator::new()), false);
|
||||
} else {
|
||||
|
@ -136,7 +126,7 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Num7) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Num7) {
|
||||
let index = rnd_value(0, sounds.len() - 1);
|
||||
if using_queue_commands {
|
||||
system.audio_queue.play_buffer_on_channel(7, &sounds[index], false)?;
|
||||
|
@ -145,7 +135,7 @@ fn main() -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::S) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::S) {
|
||||
if using_queue_commands {
|
||||
system.audio_queue.stop_all();
|
||||
} else {
|
||||
|
@ -155,13 +145,13 @@ fn main() -> Result<()> {
|
|||
|
||||
system.audio_queue.apply_to_device(&mut audio_device)?;
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::KpMinus) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::KpMinus) {
|
||||
volume -= 0.1;
|
||||
}
|
||||
if system.keyboard.is_key_pressed(Scancode::KpPlus) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::KpPlus) {
|
||||
volume += 0.1;
|
||||
}
|
||||
if system.keyboard.is_key_pressed(Scancode::Q) {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Q) {
|
||||
using_queue_commands = !using_queue_commands;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,8 +27,6 @@ fn main() -> Result<()> {
|
|||
.vsync(true)
|
||||
.build()?;
|
||||
|
||||
let mut is_running = true;
|
||||
|
||||
let font = BitmaskFont::new_vga_font()?;
|
||||
|
||||
let (balls_bmp, balls_palette) = Bitmap::load_pcx_file(Path::new("./assets/balls.pcx"))?;
|
||||
|
@ -61,21 +59,12 @@ fn main() -> Result<()> {
|
|||
balls.push(ball);
|
||||
}
|
||||
|
||||
while is_running {
|
||||
system.do_events_with(|event| {
|
||||
match event {
|
||||
SystemEvent::Quit => {
|
||||
is_running = false;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
is_running = false;
|
||||
while !system.do_events() {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
break;
|
||||
}
|
||||
|
||||
if system.keyboard.is_key_up(Scancode::S) {
|
||||
if system.input_devices.keyboard.is_key_up(Scancode::S) {
|
||||
for i in 0..NUM_BALLS {
|
||||
let ball = &mut balls[i];
|
||||
ball.x += ball.dir_x;
|
||||
|
|
|
@ -17,21 +17,10 @@ fn main() -> Result<()> {
|
|||
let mut states = States::new();
|
||||
states.push(SimulationState)?;
|
||||
|
||||
let mut is_running = true;
|
||||
|
||||
let tick_frequency = game.context.system.tick_frequency();
|
||||
let mut last_ticks = game.context.system.ticks();
|
||||
|
||||
while is_running && !states.is_empty() {
|
||||
game.context.system.do_events_with(|event| {
|
||||
match event {
|
||||
SystemEvent::Quit => {
|
||||
is_running = false;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
||||
while !game.context.system.do_events() && !states.is_empty() {
|
||||
let ticks = game.context.system.ticks();
|
||||
let elapsed = ticks - last_ticks;
|
||||
last_ticks = ticks;
|
||||
|
|
|
@ -78,12 +78,12 @@ pub struct SimulationState;
|
|||
|
||||
impl AppState<Game> for SimulationState {
|
||||
fn update(&mut self, _state: State, context: &mut Game) -> Option<StateChange<Game>> {
|
||||
if context.context.system.keyboard.is_key_up(Scancode::S) {
|
||||
if context.context.system.input_devices.keyboard.is_key_up(Scancode::S) {
|
||||
context.do_events();
|
||||
context.component_systems.update(&mut context.context);
|
||||
}
|
||||
|
||||
if context.context.system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
if context.context.system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
return Some(StateChange::Pop(1));
|
||||
}
|
||||
|
||||
|
|
|
@ -27,17 +27,17 @@ impl MainMenuState {
|
|||
impl AppState<Game> for MainMenuState {
|
||||
fn update(&mut self, state: State, context: &mut Game) -> Option<StateChange<Game>> {
|
||||
if state == State::Active {
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
return Some(StateChange::Pop(1));
|
||||
}
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Up) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Up) {
|
||||
self.selection = (self.selection - 1).clamp(0, 1);
|
||||
}
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Down) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Down) {
|
||||
self.selection = (self.selection + 1).clamp(0, 1);
|
||||
}
|
||||
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Return) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Return) {
|
||||
match self.selection {
|
||||
0 => return Some(StateChange::Push(Box::new(GamePlayState::new()))),
|
||||
1 => return Some(StateChange::Pop(1)),
|
||||
|
@ -113,17 +113,17 @@ impl AppState<Game> for GamePlayState {
|
|||
fn update(&mut self, state: State, context: &mut Game) -> Option<StateChange<Game>> {
|
||||
if state == State::Active {
|
||||
if self.in_menu {
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
self.in_menu = false;
|
||||
}
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Up) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Up) {
|
||||
self.selection = (self.selection - 1).clamp(0, 1);
|
||||
}
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Down) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Down) {
|
||||
self.selection = (self.selection + 1).clamp(0, 1);
|
||||
}
|
||||
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Return) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Return) {
|
||||
match self.selection {
|
||||
0 => self.in_menu = false,
|
||||
1 => return Some(StateChange::Pop(1)),
|
||||
|
@ -131,24 +131,24 @@ impl AppState<Game> for GamePlayState {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
self.in_menu = true;
|
||||
}
|
||||
|
||||
if let Some((player_entity, _)) = context.core.entities.components::<Player>().single() {
|
||||
if context.core.system.keyboard.is_key_down(Scancode::Up) {
|
||||
if context.core.system.input_devices.keyboard.is_key_down(Scancode::Up) {
|
||||
context.core.event_publisher.queue(Event::TurnAndMove(*player_entity, Direction::North));
|
||||
}
|
||||
if context.core.system.keyboard.is_key_down(Scancode::Down) {
|
||||
if context.core.system.input_devices.keyboard.is_key_down(Scancode::Down) {
|
||||
context.core.event_publisher.queue(Event::TurnAndMove(*player_entity, Direction::South));
|
||||
}
|
||||
if context.core.system.keyboard.is_key_down(Scancode::Left) {
|
||||
if context.core.system.input_devices.keyboard.is_key_down(Scancode::Left) {
|
||||
context.core.event_publisher.queue(Event::TurnAndMove(*player_entity, Direction::West));
|
||||
}
|
||||
if context.core.system.keyboard.is_key_down(Scancode::Right) {
|
||||
if context.core.system.input_devices.keyboard.is_key_down(Scancode::Right) {
|
||||
context.core.event_publisher.queue(Event::TurnAndMove(*player_entity, Direction::East));
|
||||
}
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Space) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Space) {
|
||||
context.core.event_publisher.queue(Event::Attack(*player_entity));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,7 +100,7 @@ impl DemoState {
|
|||
impl AppState<App> for DemoState {
|
||||
fn update(&mut self, state: State, context: &mut App) -> Option<StateChange<App>> {
|
||||
if state == State::Active {
|
||||
if context.core.system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
if context.core.system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
return Some(StateChange::Pop(1))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,24 +8,14 @@ use libretrogd::utils::rnd_value;
|
|||
fn main() -> Result<()> {
|
||||
let mut system = SystemBuilder::new().window_title("Minimal Template").vsync(true).build()?;
|
||||
|
||||
let mut is_running = true;
|
||||
let font = BitmaskFont::new_vga_font()?;
|
||||
|
||||
system.video.clear(0);
|
||||
system.video.print_string("Hello, world!", 20, 20, FontRenderOpts::Color(15), &font);
|
||||
|
||||
while is_running {
|
||||
system.do_events_with(|event| {
|
||||
match event {
|
||||
SystemEvent::Quit => {
|
||||
is_running = false;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
||||
if system.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
is_running = false;
|
||||
while !system.do_events() {
|
||||
if system.input_devices.keyboard.is_key_pressed(Scancode::Escape) {
|
||||
break;
|
||||
}
|
||||
|
||||
let x = rnd_value(0, SCREEN_RIGHT) as i32;
|
||||
|
|
Loading…
Reference in a new issue