Skip to content

Commit f858778

Browse files
committed
WIP: Yew frontend
1 parent 030c11a commit f858778

File tree

8 files changed

+1167
-8
lines changed

8 files changed

+1167
-8
lines changed

Cargo.lock

Lines changed: 1069 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["crates/chip8-interpreter", "crates/chip8-cli"]
2+
members = ["crates/chip8-interpreter", "crates/chip8-cli", "crates/frontend"]
33
resolver = "2"
44

55
[workspace.package]

crates/chip8-interpreter/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#![no_std]
55
#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
66

7+
use core::fmt;
8+
79
const RAM_SIZE: usize = 4096;
810
// The original implementation of the Chip-8 language used a 64x32-pixel monochrome display with this format:
911
pub const SCREEN_HEIGHT: usize = 32;
@@ -94,6 +96,19 @@ impl rustler::Resource for Chip8Emulator {}
9496
#[rustler::resource_impl]
9597
impl Chip8Emulator {}
9698

99+
impl fmt::Display for Chip8Emulator {
100+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101+
for line in self.display.as_slice().chunks(SCREEN_WIDTH) {
102+
for &segment in line {
103+
let symbol = if segment == true { '◻' } else { '◼' };
104+
write!(f, "{}", symbol)?;
105+
}
106+
write!(f, "\n")?;
107+
}
108+
Ok(())
109+
}
110+
}
111+
97112
impl Chip8Emulator {
98113
#[must_use]
99114
pub fn new() -> Self {

crates/frontend/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

crates/frontend/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "frontend"
3+
authors.workspace = true
4+
version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
10+
[dependencies]
11+
yew = { version = "0.21.0", features = ["csr"] }
12+
chip8-interpreter = { path = "../chip8-interpreter" }

crates/frontend/NOTES.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Serve html from Yew with:
2+
`trunk serve --open`
3+
4+
Next steps:
5+
- Investigate:
6+
- `wasm_bindgen`
7+
- Look at creating the js file using [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame) to create the needed render loop for calling `emu.tick()`
8+
9+
10+
Links:
11+
12+
- https://rustwasm.github.io/docs/book/game-of-life/hello-world.html
13+
- https://github.com/rustwasm/create-wasm-app
14+
- https://github.com/rustwasm/wasm-pack-template
15+
- https://yew.rs/docs/getting-started/build-a-sample-app
16+
- https://alexcrichton.github.io/wasm-bindgen/examples/2d-canvas.html

crates/frontend/index.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head></head>
5+
6+
<style>
7+
body {
8+
position: absolute;
9+
top: 0;
10+
left: 0;
11+
width: 100%;
12+
height: 100%;
13+
display: flex;
14+
flex-direction: column;
15+
align-items: center;
16+
justify-content: center;
17+
}
18+
</style>
19+
20+
<body></body>
21+
22+
</html>

crates/frontend/src/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use chip8_interpreter::Chip8Emulator;
2+
use yew::{prelude::*, virtual_dom::VNode};
3+
4+
#[function_component(App)]
5+
fn app() -> Html {
6+
let mut emu = Chip8Emulator::new();
7+
let pong = include_bytes!("../../roms/PONG");
8+
emu.load_data(pong);
9+
let display: Vec<VNode> = emu
10+
.to_string()
11+
.split("\n")
12+
.into_iter()
13+
.map(|row| {
14+
html! {
15+
<p>{format!("{row}")}</p>
16+
}
17+
})
18+
.collect();
19+
html! {
20+
<>
21+
<h1>{ "Chip8 Emulator state" }</h1>
22+
<div>
23+
{display}
24+
</div>
25+
</>
26+
}
27+
}
28+
29+
fn main() {
30+
yew::Renderer::<App>::new().render();
31+
}

0 commit comments

Comments
 (0)