summaryrefslogtreecommitdiffstats
path: root/src
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 /src
parent950e60067ce036e831e81afeec180a63511f9e65 (diff)
downloadstallman-shooter-dcf41c0ab884c124ffda8f616ec617c1e584255e.tar.zst
Add textures
These were fun to make
Diffstat (limited to 'src')
-rw-r--r--src/main.rs24
-rw-r--r--src/player.rs6
-rw-r--r--src/state.rs41
3 files changed, 37 insertions, 34 deletions
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)
}
}