Browse Source

Switch to renderer component

master
Noelle 4 years ago
parent
commit
b83316cac8
2 changed files with 38 additions and 53 deletions
  1. 3
    53
      src/main.rs
  2. 35
    0
      src/renderer.rs

+ 3
- 53
src/main.rs View File

@@ -2,16 +2,15 @@ mod components;
mod physics;
mod animator;
mod keyboard;
mod renderer;

use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::render::{WindowCanvas, Texture};
use sdl2::rect::{Point, Rect};
use sdl2::image::{self, LoadTexture, InitFlag};

use specs::prelude::*;
use specs_derive::Component;

use std::time::Duration;

@@ -52,56 +51,6 @@ fn character_animation_frames(spritesheet: usize, top_left_frame: Rect, directio
return frames
}

fn render (
canvas: &mut WindowCanvas,
color: Color,
texture: &Texture,
player: &Player
) -> Result<(), String> {
canvas.set_draw_color(color);
canvas.clear();

let (width, height) = canvas.output_size()?;

let (frame_width, frame_height) = player.sprite.size();
let current_frame = Rect::new(
player.sprite.x() + frame_width as i32 * player.current_frame,
player.sprite.y() + frame_height as i32 * direction_spritesheet_row(player.direction),
frame_width,
frame_height,
);

let screen_position = player.position + Point::new(width as i32 / 2, height as i32 / 2);
let screen_rect = Rect::from_center(screen_position, frame_width, frame_height);
canvas.copy(texture, current_frame, screen_rect)?;

canvas.present();

Ok(())
}

fn update_player(player: &mut Player) {
use self::Direction::*;
match player.direction {
Left => {
player.position = player.position.offset(-player.speed, 0);
},
Right => {
player.position = player.position.offset(player.speed, 0);
},
Up => {
player.position = player.position.offset(0, -player.speed);
},
Down => {
player.position = player.position.offset(0, player.speed);
}
}

if player.speed != 0 {
player.current_frame = (player.current_frame + 1) % 3;
}
}

fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let video_subsystem = sdl_context.video()?;
@@ -124,6 +73,7 @@ fn main() -> Result<(), String> {

let mut world = World::new();
dispatcher.setup(&mut world.res);
renderer::SystemData::setup(&mut world.res);

let movement_command: Option<MovementCommand> = None;
world.add_resource(movement_command);
@@ -190,7 +140,7 @@ fn main() -> Result<(), String> {
dispatcher.dispatch(&mut world.res);
world.maintain();

render(&mut canvas, Color::RGB(i, 64, 255 - i), &texture, &player)?;
renderer::render(&mut canvas, Color::RGB(i, 64, 255 - i), &textures, world.system_data())?;

::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 20));


+ 35
- 0
src/renderer.rs View File

@@ -0,0 +1,35 @@
use specs::prelude::*;
use sdl2::rect::{Point, Rect};
use sdl2::pixels::Color;
use sdl2::render::{WindowCanvas, Texture};

use crate::components::*;

pub type SystemData<'a> = (
ReadStorage<'a, Position>,
ReadStorage<'a, Sprite>,
);

pub fn render(
canvas: &mut WindowCanvas,
background: Color,
textures: &[Texture],
data: SystemData
) -> Result<(), String> {
canvas.set_draw_color(background);
canvas.clear();

let (width, height) = canvas.output_size()?;

for (pos, sprite) in (&data.0, &data.1).join() {
let current_frame = sprite.region;

let screen_position = pos.0 + Point::new(width as i32 / 2, height as i32 / 2);
let screen_rect = Rect::from_center(screen_position, current_frame.width(), current_frame.height());
canvas.copy(&textures[sprite.spritesheet], current_frame, screen_rect)?;
}

canvas.present();

Ok(())
}

Loading…
Cancel
Save