Skip to content

Commit b23bd90

Browse files
committed
Add a flappy bird example
1 parent e24f6f5 commit b23bd90

File tree

5 files changed

+183
-3
lines changed

5 files changed

+183
-3
lines changed

Cargo.lock

Lines changed: 71 additions & 0 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
@@ -11,7 +11,7 @@ edition = "2021"
1111
version = "0.1.0"
1212

1313
[workspace]
14-
members = ["backends/*", "common", "editor", "macros", "modules/*"]
14+
members = ["backends/*", "common", "editor", "macros", "modules/*", "samples/*"]
1515

1616
[profile.dev.package."*"]
1717
opt-level = 3

backends/sdl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct Window {
2323

2424
/// Settings for a window.
2525
pub struct WindowSettings {
26-
pub title: &'static str,
26+
pub title: String,
2727
pub width: u32,
2828
pub height: u32,
2929
pub vsync_enabled: bool,
@@ -33,7 +33,7 @@ pub struct WindowSettings {
3333
impl Default for WindowSettings {
3434
fn default() -> Self {
3535
Self {
36-
title: "Surreal",
36+
title: "Surreal".to_string(),
3737
width: 1024,
3838
height: 768,
3939
vsync_enabled: true,

samples/flappy-bird/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "flappy-bird"
3+
authors.workspace = true
4+
edition.workspace = true
5+
version.workspace = true
6+
7+
[dependencies]
8+
surreal = { path = "../../", features = ["graphics", "sdl"] }
9+
hecs = "0.10.4"

samples/flappy-bird/src/main.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//! A simple example of a Flappy Bird game using surreal and hecs.
2+
3+
use hecs::{EntityBuilder, World};
4+
use surreal::{
5+
backends::sdl::{Window, WindowSettings},
6+
common::{DeltaClock, StringName},
7+
graphics::{Color, Color32, GraphicsEngine, RenderQueue},
8+
};
9+
10+
fn main() {
11+
let mut window = Window::new(WindowSettings {
12+
title: "Flappy Bird".to_string(),
13+
width: 800,
14+
height: 600,
15+
vsync_enabled: true,
16+
icon: None,
17+
})
18+
.unwrap();
19+
20+
let graphics = GraphicsEngine::opengl(&window);
21+
22+
let mut game = Game::new();
23+
let mut queue = RenderQueue::new();
24+
25+
while window.update() {
26+
game.update();
27+
game.draw(&mut queue);
28+
29+
queue.flush(&graphics).expect("Failed to flush render queue");
30+
31+
window.present();
32+
}
33+
}
34+
35+
struct Game {
36+
world: World,
37+
clock: DeltaClock,
38+
sprite_cache: SpriteCache,
39+
}
40+
41+
struct Position {
42+
x: f32,
43+
y: f32,
44+
}
45+
46+
struct Velocity {
47+
x: f32,
48+
y: f32,
49+
}
50+
51+
struct Sprite {
52+
texture: StringName,
53+
tint: Color32,
54+
}
55+
56+
struct SpriteCache {}
57+
58+
impl Game {
59+
pub fn new() -> Self {
60+
let mut world = World::new();
61+
62+
// create entities
63+
for _ in 0..10 {
64+
let mut builder = EntityBuilder::default();
65+
66+
builder.add(Position { x: 0.0, y: 0.0 });
67+
builder.add(Velocity { x: 1.0, y: 1.0 });
68+
builder.add(Sprite {
69+
texture: "bird".into(),
70+
tint: Color32::WHITE,
71+
});
72+
73+
world.spawn(builder.build());
74+
}
75+
76+
Self {
77+
world,
78+
clock: DeltaClock::new(),
79+
sprite_cache: SpriteCache {},
80+
}
81+
}
82+
83+
pub fn update(&mut self) {
84+
let delta_time = self.clock.tick();
85+
86+
// update positions
87+
for (_, (position, velocity)) in self.world.query_mut::<(&mut Position, &mut Velocity)>() {
88+
position.x += velocity.x * delta_time;
89+
position.y += velocity.y * delta_time;
90+
}
91+
}
92+
93+
pub fn draw(&self, queue: &mut RenderQueue) {
94+
queue.clear_color_buffer(Color::BLACK);
95+
96+
for (_, (_position, _sprite)) in &mut self.world.query::<(&Position, &Sprite)>() {
97+
// queue
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)