Skip to content

Commit

Permalink
fixup: game full screen on Web (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovebaihezi authored Nov 14, 2024
1 parent a320071 commit e31d49c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/game_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ pub fn reset_game(
}
}
}

#[cfg(test)]
mod game_logic_test {
#[test]
fn time_paused_on() {}
}
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ use dinosaur::{
};

fn main() {
let default_plugin = DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Dinosaur Game".to_string(),
canvas: Some("#game".to_string()),
fit_canvas_to_parent: true,
..Default::default()
}),
..Default::default()
});
let exit = App::new()
.add_plugins((DefaultPlugins, FrameTimeDiagnosticsPlugin))
.add_plugins((default_plugin, FrameTimeDiagnosticsPlugin))
.insert_resource(GameStatus { speed: 5, score: 0 })
.insert_resource(ClearColor(Color::srgb(1.0, 1.0, 1.0)))
.add_systems(
Expand Down
33 changes: 25 additions & 8 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,36 @@
</style>
</head>
<body style="margin: 0px">
<canvas width="1024" height="960" id="game"></canvas>
<script type="module">
import "./restart-audio-context.js";
import init from "./dinosaur.js";

init().catch((error) => {
if (
!error.message.startsWith(
"Using exceptions for control flow, don't mind me. This isn't actually an error!",
)
) {
throw error;
}
const canvas = document.getElementById("game");

function updateCanvasSize(window, canvas) {
canvas.style.width = window.innerWidth + "px";
canvas.style.height = window.innerHeight + "px";
}

// Resize Canvas on Window Resize
window.addEventListener("resize", () => {
updateCanvasSize(window, canvas);
});

init()
.then(() => {
updateCanvasSize(window, canvas);
})
.catch((error) => {
if (
!error.message.startsWith(
"Using exceptions for control flow, don't mind me. This isn't actually an error!",
)
) {
throw error;
}
});
</script>
</body>
</html>

0 comments on commit e31d49c

Please sign in to comment.