Skip to content

Commit

Permalink
Basic rendering in place
Browse files Browse the repository at this point in the history
  • Loading branch information
aochagavia committed Dec 1, 2017
1 parent 1bcc6fa commit 2e17c55
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 100 deletions.
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
name = "rocket"
version = "1.0.0"
authors = ["Adolfo Ochagavía <[email protected]>"]
build = "build.rs"

[dependencies]
clippy = { version = "0.0.118", optional = true }
Expand Down
35 changes: 0 additions & 35 deletions appveyor.yml

This file was deleted.

23 changes: 0 additions & 23 deletions build.rs

This file was deleted.

78 changes: 59 additions & 19 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@
</head>

<body>
<h2>Rocket MVP</h2>
<p>
Update the state by 0.1 seconds by clicking the button below.
</p>
<button id="button">Click me :)</button>
<p>Last state: <span id="number">none</span></p>
<h2>Rocket</h2>
<canvas id="canvas" width="1024" height="600"></canvas>

<!-- bundle.js is fully stolen from hellorust.org -->
<script src="bundle.js"></script>
<script>
// Canvas related stuff
let canvas = document.getElementById('canvas');
var ctx = canvas.getContext("2d");

let env = {};
env.clear_screen = () => {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
env.draw_player = (x, y, angle) => {
ctx.fillStyle = "red";
ctx.fillRect(x, y, 20, 20);
}
env.draw_enemy = (x, y) => {
ctx.fillStyle = "yellow";
ctx.fillRect(x, y, 20, 20);
}

// Wasm related stuff
window.Module = {};

// Terribly inefficient way of showing the dependencies
fetch('program.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
Expand All @@ -26,30 +42,54 @@ <h2>Rocket MVP</h2>
imports.forEach(i => console.log(i));
});

let imports = {
env: {
Math_atan: Math.atan,
sin: Math.sin,
cos: Math.cos
}
};
// The real loading and running of our wasm starts here
env.Math_atan = Math.atan;
env.sin = Math.sin;
env.cos = Math.cos;

fetchAndInstantiate("program.wasm", imports)
fetchAndInstantiate("program.wasm", { env })
.then(mod => {
Module.alloc = mod.exports.alloc;
Module.dealloc = mod.exports.dealloc;
Module.dealloc_str = mod.exports.dealloc_str;
Module.update = mod.exports.update;
Module.draw = mod.exports.draw;
Module.memory = mod.exports.memory;

let button = document.getElementById('button');
let number = document.getElementById('number');

button.addEventListener("click", function(e) {
let outptr = Module.update(0.2);
number.innerText = copyCStr(Module, outptr);
Module.dealloc(outptr);
});
let start = null;
let prevTimestamp = null;
let drawAndUpdate = (timestamp) => {
if (!prevTimestamp) {
start = timestamp;
prevTimestamp = timestamp;

// Request next frame
requestAnimationFrame(drawAndUpdate);
return;
}

if (timestamp - start > 20000) {
return;
}

// Deal with time
let progress = (timestamp - prevTimestamp) / 1000;
prevTimestamp = timestamp;

// Update
Module.update(progress);

// Draw
Module.draw();

// Request next frame
requestAnimationFrame(drawAndUpdate);
};

drawAndUpdate();
});
</script>
</body>
Expand Down
5 changes: 5 additions & 0 deletions post_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from shutil import copyfile
from subprocess import call

copyfile('target/wasm32-unknown-unknown/release/rocket.wasm', 'html/rocket.wasm')
call(['wasm-gc', 'html/rocket.wasm', 'html/program.wasm'])
Empty file removed src/geometry/position.rs
Empty file.
29 changes: 24 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,38 @@ struct GameData {
time_controller: TimeController<Pcg32Basic>
}

extern "C" {
fn clear_screen();
fn draw_player(_: c_double, _: c_double, _: c_double);
fn draw_enemy(_: c_double, _: c_double);
}

#[no_mangle]
pub extern "C" fn draw() {
use geometry::{Advance, Position};
let data = &mut DATA.lock().unwrap();
unsafe {
clear_screen();
draw_player(data.state.world.player.x(), data.state.world.player.y(), data.state.world.player.direction());
for enemy in &data.state.world.enemies {
draw_enemy(enemy.x(), enemy.y());
}
}
}

#[no_mangle]
pub extern "C" fn update(time: c_double) -> *mut c_char {
CString::new(_update(time)).unwrap().into_raw()
pub extern "C" fn update(time: c_double) {
_update(time)
}

pub fn _update(time: f64) -> String {
pub fn _update(time: f64) {
let data: &mut GameData = &mut DATA.lock().unwrap();
data.time_controller.update_seconds(time, data.input_controller.actions(), &mut data.state);
CollisionsController::handle_collisions(&mut data.state);

// Stats
let player_pos = data.state.world.player.vector.position;
format!("Player: {}, {}\nEnemies: {}", player_pos.x, player_pos.y, data.state.world.enemies.len())
//let player_pos = data.state.world.player.vector.position;
//format!("Player: {}, {}\nEnemies: {}", player_pos.x, player_pos.y, data.state.world.enemies.len())
}

pub fn main() {}
Expand Down

0 comments on commit 2e17c55

Please sign in to comment.