mirror of
https://github.com/Xevion/rust-sdl2-emscripten.git
synced 2026-01-31 05:11:56 -06:00
Simplify code
This commit is contained in:
Vendored
+1
-1
@@ -9,7 +9,7 @@ jobs:
|
|||||||
pages: write
|
pages: write
|
||||||
id-token: write
|
id-token: write
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2 # repo checkout
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
- uses: mymindstorm/setup-emsdk@v11
|
- uses: mymindstorm/setup-emsdk@v11
|
||||||
with:
|
with:
|
||||||
|
|||||||
-50
@@ -1,50 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::process;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use sdl2::event::Event;
|
|
||||||
use sdl2::keyboard::Keycode;
|
|
||||||
use sdl2::pixels::Color;
|
|
||||||
use sdl2::rect::Rect;
|
|
||||||
use sdl2::render::WindowCanvas;
|
|
||||||
use sdl2::Sdl;
|
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
|
||||||
pub mod emscripten;
|
|
||||||
|
|
||||||
static BLACK: Color = Color::RGB(0, 0, 0);
|
|
||||||
static WHITE: Color = Color::RGB(255, 255, 255);
|
|
||||||
|
|
||||||
pub fn main_loop(ctx: Rc<RefCell<Sdl>>, rect: Rc<RefCell<Rect>>, canvas: Rc<RefCell<WindowCanvas>>) -> impl FnMut() {
|
|
||||||
let mut events = ctx.borrow_mut().event_pump().unwrap();
|
|
||||||
|
|
||||||
move || {
|
|
||||||
for event in events.poll_iter() {
|
|
||||||
match event {
|
|
||||||
Event::Quit {..} | Event::KeyDown {keycode: Some(Keycode::Escape), ..} => {
|
|
||||||
process::exit(1);
|
|
||||||
},
|
|
||||||
Event::KeyDown { keycode: Some(Keycode::Left), ..} => {
|
|
||||||
rect.borrow_mut().x -= 10;
|
|
||||||
},
|
|
||||||
Event::KeyDown { keycode: Some(Keycode::Right), ..} => {
|
|
||||||
rect.borrow_mut().x += 10;
|
|
||||||
},
|
|
||||||
Event::KeyDown { keycode: Some(Keycode::Up), ..} => {
|
|
||||||
rect.borrow_mut().y -= 10;
|
|
||||||
},
|
|
||||||
Event::KeyDown { keycode: Some(Keycode::Down), ..} => {
|
|
||||||
rect.borrow_mut().y += 10;
|
|
||||||
},
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = canvas.borrow_mut().set_draw_color(BLACK);
|
|
||||||
let _ = canvas.borrow_mut().clear();
|
|
||||||
let _ = canvas.borrow_mut().set_draw_color(WHITE);
|
|
||||||
let _ = canvas.borrow_mut().fill_rect(rect.borrow().clone());
|
|
||||||
let _ = canvas.borrow_mut().present();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
+63
-21
@@ -1,40 +1,35 @@
|
|||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
use std::process;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use sdl2::event::Event;
|
||||||
|
use sdl2::keyboard::Keycode;
|
||||||
|
use sdl2::pixels::Color;
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
|
|
||||||
use pacman::main_loop;
|
static BLACK: Color = Color::RGB(0, 0, 0);
|
||||||
|
static WHITE: Color = Color::RGB(255, 255, 255);
|
||||||
|
|
||||||
// Resources
|
|
||||||
// https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_Wasm
|
|
||||||
// https://puddleofcode.com/story/definitive-guide-to-rust-sdl2-and-emscriptem/
|
|
||||||
|
|
||||||
// To build locally:
|
|
||||||
// cargo run
|
|
||||||
|
|
||||||
// To build for the web:
|
|
||||||
// rustup target add asmjs-unknown-emscripten
|
|
||||||
// export EMCC_CFLAGS="-s USE_SDL=2"
|
// export EMCC_CFLAGS="-s USE_SDL=2"
|
||||||
// cargo build --target asmjs-unknown-emscripten && open index.html
|
// cargo build --target asmjs-unknown-emscripten && open index.html
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let ctx = sdl2::init().unwrap();
|
let ctx = sdl2::init().unwrap();
|
||||||
let video_ctx = ctx.video().unwrap();
|
let video_ctx = ctx.video().unwrap();
|
||||||
|
|
||||||
let window = match video_ctx
|
let window = match video_ctx
|
||||||
.window("Hello, Rust / SDL2 / WASM!", 640, 480)
|
.window("Hello, Rust / SDL2 / WASM!", 640, 480)
|
||||||
.position_centered()
|
.position_centered()
|
||||||
.opengl()
|
.opengl()
|
||||||
.build() {
|
.build()
|
||||||
|
{
|
||||||
Ok(window) => window,
|
Ok(window) => window,
|
||||||
Err(err) => panic!("failed to create window: {}", err)
|
Err(err) => panic!("failed to create window: {}", err),
|
||||||
};
|
};
|
||||||
|
|
||||||
let canvas = match window
|
let canvas = match window.into_canvas().present_vsync().build() {
|
||||||
.into_canvas()
|
|
||||||
.present_vsync()
|
|
||||||
.build() {
|
|
||||||
Ok(canvas) => canvas,
|
Ok(canvas) => canvas,
|
||||||
Err(err) => panic!("failed to create canvas: {}", err)
|
Err(err) => panic!("failed to create canvas: {}", err),
|
||||||
};
|
};
|
||||||
|
|
||||||
let rect = Rect::new(0, 0, 10, 10);
|
let rect = Rect::new(0, 0, 10, 10);
|
||||||
@@ -43,19 +38,66 @@ fn main() {
|
|||||||
let rect = Rc::new(RefCell::new(rect));
|
let rect = Rc::new(RefCell::new(rect));
|
||||||
let canvas = Rc::new(RefCell::new(canvas));
|
let canvas = Rc::new(RefCell::new(canvas));
|
||||||
|
|
||||||
|
let mut events = ctx.borrow_mut().event_pump().unwrap();
|
||||||
|
|
||||||
|
let mut main_loop = move || {
|
||||||
|
for event in events.poll_iter() {
|
||||||
|
match event {
|
||||||
|
Event::Quit { .. }
|
||||||
|
| Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Escape),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Left),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
rect.borrow_mut().x -= 10;
|
||||||
|
}
|
||||||
|
Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Right),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
rect.borrow_mut().x += 10;
|
||||||
|
}
|
||||||
|
Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Up),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
rect.borrow_mut().y -= 10;
|
||||||
|
}
|
||||||
|
Event::KeyDown {
|
||||||
|
keycode: Some(Keycode::Down),
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
rect.borrow_mut().y += 10;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = canvas.borrow_mut().set_draw_color(BLACK);
|
||||||
|
let _ = canvas.borrow_mut().clear();
|
||||||
|
let _ = canvas.borrow_mut().set_draw_color(WHITE);
|
||||||
|
let _ = canvas.borrow_mut().fill_rect(rect.borrow().clone());
|
||||||
|
let _ = canvas.borrow_mut().present();
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
#[cfg(target_family = "wasm")]
|
||||||
use pacman::emscripten;
|
use pacman::emscripten;
|
||||||
|
|
||||||
#[cfg(target_family = "wasm")]
|
#[cfg(target_family = "wasm")]
|
||||||
emscripten::set_main_loop_callback(main_loop(Rc::clone(&ctx), Rc::clone(&rect), Rc::clone(&canvas)));
|
emscripten::set_main_loop_callback(main_loop);
|
||||||
|
|
||||||
#[cfg(not(target_family = "wasm"))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
{
|
{
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
loop {
|
loop {
|
||||||
main_loop(Rc::clone(&ctx), Rc::clone(&rect), Rc::clone(&canvas))();
|
main_loop();
|
||||||
sleep(Duration::from_millis(10))
|
sleep(Duration::from_millis(10))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user