From eaefcd3272822aa6377969c511a190d47768bce0 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 2 Oct 2018 21:48:19 +0200 Subject: [PATCH] Preparing things --- src/engine/character/mod.rs | 82 ++++++++++++++++++++++++++++++++++ src/engine/controls/mod.rs | 14 ++++++ src/engine/mod.rs | 12 +++++ src/engine/scene/mod.rs | 71 +++++++++++++++++++++++++++++ src/engine/texture/mod.rs | 68 ++++++++++++++++++++++++++++ src/game/main.rs | 89 ++++++++++++++++++++++++++++++++++++- src/lib.rs | 2 + 7 files changed, 337 insertions(+), 1 deletion(-) create mode 100644 src/engine/character/mod.rs create mode 100644 src/engine/controls/mod.rs create mode 100644 src/engine/mod.rs create mode 100644 src/engine/scene/mod.rs create mode 100644 src/engine/texture/mod.rs diff --git a/src/engine/character/mod.rs b/src/engine/character/mod.rs new file mode 100644 index 0000000..a190839 --- /dev/null +++ b/src/engine/character/mod.rs @@ -0,0 +1,82 @@ +use std::time::Duration; + +use sfml::system::Vector2; +use sfml::window::Event; +use sfml::graphics::{ + Drawable, + RenderStates, + RenderTarget, + Sprite, + Color, +}; + +use engine::scene::Updatable; +use engine::controls::Controls; + +/// A character, enemy or controllable. +pub struct Character { + + /// The position of the character. + position: Vector2, + + /// The speed of the character. + speed: Vector2, + + /// If the player is controlling a character. + controls: Option, +} + +impl Character { + /// Creates a character in (0, 0). + pub fn new() -> Character { + Character { + position: Vector2::new(0.0, 0.0), + speed: Vector2::new(0.0, 0.0), + controls: None, + } + } + + /// Creates a character with the specified controls. + pub fn with_controls(controls: Controls) -> Character { + Character { + position: Vector2::new(0.0, 0.0), + speed: Vector2::new(0.0, 0.0), + controls: Some(controls), + } + } + + /// Sets the position of the character. + pub fn set_position>>(&mut self, position: S) { + self.position = position.into(); + } +} + +impl Updatable for Character { + fn update(&mut self, duration: &Duration) { + + } + + fn manage_event(&mut self, event: &Event) { + + } +} + +impl Drawable for Character { + fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( + &'a self, + target: &mut RenderTarget, + states: RenderStates<'texture, 'shader, 'shader_texture> + ) { + + use sfml::graphics::Transformable; + let mut sprite = Sprite::new(); + sprite.set_origin((32.0, 0.0)); + sprite.set_position(self.position); + sprite.set_color(&if self.controls.is_some() { + Color::rgb(255, 0, 0) + } else { + Color::rgb(255, 255, 255) + }); + sprite.draw(target, states); + } +} diff --git a/src/engine/controls/mod.rs b/src/engine/controls/mod.rs new file mode 100644 index 0000000..cbe1df0 --- /dev/null +++ b/src/engine/controls/mod.rs @@ -0,0 +1,14 @@ + +/// Contains the data needed to manage the controls of the player. +pub struct Controls { + +} + +impl Controls { + /// Creates the default controls. + pub fn new() -> Controls { + Controls { + + } + } +} diff --git a/src/engine/mod.rs b/src/engine/mod.rs new file mode 100644 index 0000000..a005066 --- /dev/null +++ b/src/engine/mod.rs @@ -0,0 +1,12 @@ + +/// This module contains the struct Scene. +pub mod scene; + +/// This module contains everything related to characters, playable or enemies. +pub mod character; + +/// This module contains everything related to the controls of the game. +pub mod controls; + +/// This module helps us dealing with textures. +pub mod texture; diff --git a/src/engine/scene/mod.rs b/src/engine/scene/mod.rs new file mode 100644 index 0000000..169a2a0 --- /dev/null +++ b/src/engine/scene/mod.rs @@ -0,0 +1,71 @@ +use std::time::Duration; + +use sfml::window::Event; +use sfml::graphics::{ + RenderTarget, + RenderStates, + Drawable, +}; + +use engine::character::Character; + +/// Contains everything needed to play. +pub struct Scene { + + /// The characters contained in the scene. + characters: Vec, +} + +impl Scene { + + /// Creates an empty scene. + pub fn new() -> Scene { + Scene { + characters: vec![], + } + } + + /// Adds a character to the scene. + pub fn add(&mut self, character: Character) { + self.characters.push(character); + } + + /// Updates the whole scene. + pub fn update(&mut self, duration: &Duration) { + for c in &mut self.characters { + c.update(duration); + } + } + + /// Transfers an event to the elements contained in the scene that should receive events. + pub fn manage_event(&mut self, event: &Event) { + for c in &mut self.characters { + c.manage_event(event); + } + } +} + +impl Drawable for Scene { + fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>( + &'a self, + target: &mut RenderTarget, + states: RenderStates<'texture, 'shader, 'shader_texture> + ) { + + for c in &self.characters { + let mut s = RenderStates::default(); + s.transform = states.transform.clone(); + s.blend_mode = states.blend_mode; + c.draw(target, s); + } + } +} + +/// Trait that needs to be implemented for everything that can be updatable. +pub trait Updatable { + /// Updates the thing depending on the duration since last frame. + fn update(&mut self, duration: &Duration); + + /// Called when an event arrives. + fn manage_event(&mut self, event: &Event); +} diff --git a/src/engine/texture/mod.rs b/src/engine/texture/mod.rs new file mode 100644 index 0000000..0df1d9f --- /dev/null +++ b/src/engine/texture/mod.rs @@ -0,0 +1,68 @@ +use sfml::graphics::{IntRect, Texture as SfTexture}; + +macro_rules! make_textures { + ( $( + $enum_name: ident, + $texture_name: ident, + $function_name: ident, + $texture_path: tt, ) + *) => { + + /// Describes all the textures that will be used in this game. + #[derive(Copy, Clone)] + pub enum Texture { + $( + /// A texture. + $enum_name, + )* + } + + /// A struct that holds all the textures. + pub struct TextureManager { + $( + /// A texture. + $texture_name: SfTexture, + )* + } + + impl TextureManager { + /// Creates a new texture manager and initializes all textures. + pub fn new() -> TextureManager { + TextureManager { + $( + $texture_name: TextureManager::$function_name(), + )* + } + } + + $( + /// Creates the texture. + fn $function_name() -> SfTexture { + let bytes = include_bytes!($texture_path).to_vec(); + TextureManager::make_texture_from_bytes(bytes) + } + )* + + /// Returns the real texture from the texture id. + pub fn get(&self, id: Texture) -> &SfTexture { + match id { + $( + Texture::$enum_name => &self.$texture_name, + )* + } + } + + } + } +} + +make_textures!( +); + +impl TextureManager { + /// Creates a textures from an array of bytes. + fn make_texture_from_bytes(bytes: Vec) -> SfTexture { + SfTexture::from_memory(&bytes, &IntRect::new(0, 0, 500, 500)) + .expect("Failed to create texture") + } +} diff --git a/src/game/main.rs b/src/game/main.rs index e7a11a9..beeaf1e 100644 --- a/src/game/main.rs +++ b/src/game/main.rs @@ -1,3 +1,90 @@ +extern crate sfml; +extern crate rusty; + +use std::time::{ + Instant, + Duration, +}; + +use sfml::window::{ + ContextSettings, + Style, + Event, + Key, +}; + +use sfml::graphics::{ + RenderWindow, + RenderTarget, + Color, +}; + +use rusty::engine::scene::Scene; +use rusty::engine::character::Character; +use rusty::engine::controls::Controls; + fn main() { - println!("Hello, world!"); + let game_width = 800; + let game_height = 600; + + let mut character = Character::with_controls(Controls::new()); + character.set_position((300.0, 200.0)); + + let mut scene = Scene::new(); + scene.add(character); + scene.add(Character::new()); + + let context_settings = ContextSettings { + ..Default::default() + }; + + let mut window = RenderWindow::new( + (game_width, game_height), + "Free Rusty Maker", + Style::CLOSE, + &context_settings, + ); + + window.set_framerate_limit(60); + + let mut running = true; + + loop { + + // Manage the events + while let Some(event) = window.poll_event() { + + match event { + // Quit the game if window is closed + Event::Closed => running = false, + + // Quit the game if escape is pressed + Event::KeyPressed { + code: Key::Escape, .. + } => running = false, + + _ => (), + } + + + scene.manage_event(&event); + } + + // Manage the physics + // ... + + + // Do the rendering + window.clear(&Color::rgb(50, 200, 50)); + window.draw(&scene); + + // Display and manage the frame rate + window.display(); + + if ! running { + break; + } + + } + } diff --git a/src/lib.rs b/src/lib.rs index 8f9565f..2df834d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,3 +4,5 @@ extern crate sfml; +/// This module contains all the tools needed for the game. +pub mod engine;