summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hogenson <rhogenson@posteo.net>2018-08-18 11:01:49 -0400
committerRaymond Hogenson <rhogenson@posteo.net>2018-08-18 11:01:49 -0400
commitdcf41c0ab884c124ffda8f616ec617c1e584255e (patch)
treeb7e44080dc9f607bd591ce2e52b30b04b668fb13
parent950e60067ce036e831e81afeec180a63511f9e65 (diff)
downloadstallman-shooter-dcf41c0ab884c124ffda8f616ec617c1e584255e.tar.zst
Add textures
These were fun to make
-rw-r--r--Cargo.toml7
-rw-r--r--assets/binary_blob.pngbin0 -> 1674 bytes
-rw-r--r--assets/binary_blob.xcfbin0 -> 2936 bytes
-rw-r--r--assets/gates.pngbin0 -> 3484 bytes
-rw-r--r--assets/gates.xcfbin0 -> 8595 bytes
-rw-r--r--assets/player_pepp.pngbin0 -> 2439 bytes
-rw-r--r--assets/player_pepp.xcfbin0 -> 3723 bytes
-rw-r--r--assets/stallman.pngbin0 -> 4740 bytes
-rw-r--r--assets/stallman.xcfbin0 -> 7364 bytes
-rw-r--r--default.nix2
-rw-r--r--src/main.rs24
-rw-r--r--src/player.rs6
-rw-r--r--src/state.rs41
13 files changed, 44 insertions, 36 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 8b4a64e..b539f62 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,5 +4,10 @@ version = "0.1.0"
authors = ["Raymond Hogenson <rhogenson@posteo.net>"]
[dependencies]
-sdl2 = "0.31.0"
rand = "0.5.5"
+lazy_static = "1.1.0"
+
+[dependencies.sdl2]
+git = "https://github.com/AngryLawyer/rust-sdl2"
+default-features = false
+features = ["image"]
diff --git a/assets/binary_blob.png b/assets/binary_blob.png
new file mode 100644
index 0000000..e1dab24
--- /dev/null
+++ b/assets/binary_blob.png
Binary files differ
diff --git a/assets/binary_blob.xcf b/assets/binary_blob.xcf
new file mode 100644
index 0000000..f8eb16a
--- /dev/null
+++ b/assets/binary_blob.xcf
Binary files differ
diff --git a/assets/gates.png b/assets/gates.png
new file mode 100644
index 0000000..e86a5b6
--- /dev/null
+++ b/assets/gates.png
Binary files differ
diff --git a/assets/gates.xcf b/assets/gates.xcf
new file mode 100644
index 0000000..7eda13a
--- /dev/null
+++ b/assets/gates.xcf
Binary files differ
diff --git a/assets/player_pepp.png b/assets/player_pepp.png
new file mode 100644
index 0000000..35df0cb
--- /dev/null
+++ b/assets/player_pepp.png
Binary files differ
diff --git a/assets/player_pepp.xcf b/assets/player_pepp.xcf
new file mode 100644
index 0000000..e160c04
--- /dev/null
+++ b/assets/player_pepp.xcf
Binary files differ
diff --git a/assets/stallman.png b/assets/stallman.png
new file mode 100644
index 0000000..03dae08
--- /dev/null
+++ b/assets/stallman.png
Binary files differ
diff --git a/assets/stallman.xcf b/assets/stallman.xcf
new file mode 100644
index 0000000..2e0ebc4
--- /dev/null
+++ b/assets/stallman.xcf
Binary files differ
diff --git a/default.nix b/default.nix
index a9e20f7..738d469 100644
--- a/default.nix
+++ b/default.nix
@@ -1,6 +1,6 @@
with import <nixpkgs> {};
((import ./bullet_hell.nix).bullet_hell {}).override {
crateOverrides = defaultCrateOverrides // {
- bullet-hell = attrs: { buildInputs = [ SDL2 ]; };
+ bullet-hell = attrs: { buildInputs = [ SDL2 SDL2_image ]; };
};
}
diff --git a/src/main.rs b/src/main.rs
index 76b7a5b..5c62576 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,6 +13,13 @@ enum Result {
Nothing,
}
+struct Surfaces<'a> {
+ player: sdl2::surface::Surface<'a>,
+ enemy: sdl2::surface::Surface<'a>,
+ pbullet: sdl2::surface::Surface<'a>,
+ ebullet: sdl2::surface::Surface<'a>,
+}
+
fn spawn(count: u64) -> Vec<state::Enemy> {
if count == 0 {
vec![state::Enemy::new()]
@@ -21,7 +28,7 @@ fn spawn(count: u64) -> Vec<state::Enemy> {
}
}
-fn frame(canvas: &mut sdl2::render::WindowCanvas, state: &mut state::State, events: &mut sdl2::event::EventPollIterator) -> Result {
+fn frame(canvas: &mut sdl2::render::WindowCanvas, state: &mut state::State, events: &mut sdl2::event::EventPollIterator, surfaces: &Surfaces) -> Result {
let mut new_bullets = Vec::new();
let count = state.count;
for enemy in state.enemies.iter_mut() {
@@ -57,15 +64,15 @@ fn frame(canvas: &mut sdl2::render::WindowCanvas, state: &mut state::State, even
state.bullets.append(&mut new_bullets);
state.player_bullets.retain(|bullet| !state::PBullet::outside(bullet));
for bullet in state.player_bullets.iter() {
- bullet.draw(canvas);
+ bullet.draw(canvas, &surfaces.pbullet);
}
for enemy in state.enemies.iter() {
- enemy.draw(canvas);
+ enemy.draw(canvas, &surfaces.enemy);
}
for bullet in state.bullets.iter() {
- bullet.draw(canvas);
+ bullet.draw(canvas, &surfaces.ebullet);
}
- state.player.draw(canvas);
+ state.player.draw(canvas, &surfaces.player);
state.count += 1;
Result::Nothing
}
@@ -86,6 +93,11 @@ fn main() {
let mut frame_count = 0;
let mut state = state::State::new();
let mut won = false;
+ let player_surface = sdl2::image::LoadSurface::from_file("assets/stallman.png").unwrap();
+ let enemy_surface = sdl2::image::LoadSurface::from_file("assets/gates.png").unwrap();
+ let ebullet_surface = sdl2::image::LoadSurface::from_file("assets/binary_blob.png").unwrap();
+ let pbullet_surface = sdl2::image::LoadSurface::from_file("assets/player_pepp.png").unwrap();
+ let surfaces = Surfaces { player: player_surface, enemy: enemy_surface, pbullet: pbullet_surface, ebullet: ebullet_surface };
'running: loop {
if won {
for event in event_pump.poll_iter() {
@@ -99,7 +111,7 @@ fn main() {
} else {
canvas.set_draw_color(sdl2::pixels::Color::RGB(255, 255, 255));
canvas.clear();
- match frame(&mut canvas, &mut state, &mut event_pump.poll_iter()) {
+ match frame(&mut canvas, &mut state, &mut event_pump.poll_iter(), &surfaces) {
Result::Loss => {
break;
}
diff --git a/src/player.rs b/src/player.rs
index efd396e..9e4d418 100644
--- a/src/player.rs
+++ b/src/player.rs
@@ -104,12 +104,12 @@ impl state::Positioned for Player {
self.y
}
fn radius(&self) -> u32 {
- 5
+ 13
}
}
impl state::Drawable for Player {
- fn color(&self) -> sdl2::pixels::Color {
- sdl2::pixels::Color::RGB(0, 0, 0)
+ fn center(&self) -> (u32, u32) {
+ (30, 19)
}
}
diff --git a/src/state.rs b/src/state.rs
index da53054..f70c0ba 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -239,42 +239,33 @@ impl Positioned for Enemy {
}
pub trait Drawable : Positioned {
- fn color(&self) -> sdl2::pixels::Color;
- fn draw(&self, canvas: &mut sdl2::render::WindowCanvas) {
- let x = self.x();
- let y = self.y();
- let r = self.radius();
- let r = r as i32;
- let x_min = x - r;
- let x_max = x + r;
- let y_min = y - r;
- let y_max = y + r;
- canvas.set_draw_color(self.color());
- for x in x_min..x_max + 1 {
- for y in y_min..y_max + 1 {
- if self.contains(x, y) {
- canvas.fill_rect(sdl2::rect::Rect::new(x, y, 1, 1)).unwrap();
- }
- }
- }
+ fn center(&self) -> (u32, u32);
+ fn draw(&self, canvas: &mut sdl2::render::WindowCanvas, surface: &sdl2::surface::Surface) {
+ let texture_creator = canvas.texture_creator();
+ let texture = texture_creator.create_texture_from_surface(surface).unwrap();
+ let tw = texture.query().width;
+ let th = texture.query().height;
+ let (cx, cy) = self.center();
+ let x0 = self.x() - cx as i32;
+ let y0 = self.y() - cy as i32;
+ canvas.copy(&texture, None, Some(sdl2::rect::Rect::new(x0, y0, tw, th))).unwrap();
}
}
impl Drawable for Enemy {
- fn color(&self) -> sdl2::pixels::Color {
- let red_scale = self.health as f64 / STARTING_HEALTH as f64 * 255.;
- sdl2::pixels::Color::RGB(red_scale as u8, 255 - red_scale as u8, 0)
+ fn center(&self) -> (u32, u32) {
+ (0, 0)
}
}
impl Drawable for EBullet {
- fn color(&self) -> sdl2::pixels::Color {
- sdl2::pixels::Color::RGB(0, 0, 255)
+ fn center(&self) -> (u32, u32) {
+ (20, 20)
}
}
impl Drawable for PBullet {
- fn color(&self) -> sdl2::pixels::Color {
- sdl2::pixels::Color::RGB(230, 255, 230)
+ fn center(&self) -> (u32, u32) {
+ (16, 22)
}
}