summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArne Dußin2021-05-05 23:40:17 +0200
committerArne Dußin2021-05-05 23:40:17 +0200
commitf3178df0a92fb3b87087e78cad7b9313f947be6a (patch)
treea201943ce821f18bbb4e9bba393c87155d68a61a /src
downloadpmd_coop-f3178df0a92fb3b87087e78cad7b9313f947be6a.tar.gz
pmd_coop-f3178df0a92fb3b87087e78cad7b9313f947be6a.zip
Initial commit
Diffstat (limited to 'src')
-rw-r--r--src/components.rs15
-rw-r--r--src/main.rs45
-rw-r--r--src/systems/draw.rs58
-rw-r--r--src/systems/input.rs41
-rw-r--r--src/systems/mod.rs5
-rw-r--r--src/texture_manager.rs62
6 files changed, 226 insertions, 0 deletions
diff --git a/src/components.rs b/src/components.rs
new file mode 100644
index 0000000..c92ca03
--- /dev/null
+++ b/src/components.rs
@@ -0,0 +1,15 @@
+use specs::prelude::*;
+use specs_derive::*;
+
+#[derive(Component, Debug, Clone, Copy)]
+pub struct Pos
+{
+ pub x: f64,
+ pub y: f64,
+}
+
+#[derive(Component)]
+pub struct StaticDrawable
+{
+ pub texture_name: String,
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..60186b3
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,45 @@
+pub mod components;
+pub mod systems;
+pub mod texture_manager;
+
+use components::{Pos, StaticDrawable};
+use specs::prelude::*;
+use specs::{DispatcherBuilder, World};
+use systems::{SysDraw, SysInput};
+
+fn main()
+{
+ let sdl = sdl2::init().expect("Unable to start SDL context");
+ let video_subsys = sdl.video().expect("Unable to get SDL Video context");
+
+ let window = video_subsys
+ .window("Pokemon Mystery Dungeon coop clone", 800, 600)
+ .position_centered()
+ .opengl()
+ .resizable()
+ .build()
+ .expect("Unable to create window");
+
+ let mut world = World::new();
+ world.register::<Pos>();
+ world.register::<StaticDrawable>();
+
+ world
+ .create_entity()
+ .with(Pos { x: 0., y: 0. })
+ .with(StaticDrawable {
+ texture_name: "portraits.png".to_string(),
+ })
+ .build();
+
+ let sys_input = SysInput::new(&sdl);
+ let sys_draw = SysDraw::new(window);
+ let mut dispatcher = DispatcherBuilder::new()
+ .with_thread_local(sys_input)
+ .with_thread_local(sys_draw)
+ .build();
+
+ loop {
+ dispatcher.dispatch(&mut world);
+ }
+}
diff --git a/src/systems/draw.rs b/src/systems/draw.rs
new file mode 100644
index 0000000..1d5ee6b
--- /dev/null
+++ b/src/systems/draw.rs
@@ -0,0 +1,58 @@
+use sdl2::pixels::Color;
+use sdl2::render::WindowCanvas;
+use sdl2::video::{Window, WindowContext};
+use specs::prelude::*;
+
+use crate::components::{Pos, StaticDrawable};
+use crate::texture_manager::TextureManager;
+
+pub struct SysDraw
+{
+ canvas: WindowCanvas,
+ texture_manager: TextureManager<WindowContext>,
+}
+
+impl SysDraw
+{
+ pub fn new(window: Window) -> Self
+ {
+ let mut canvas = window
+ .into_canvas()
+ .build()
+ .expect("Unable to create canvas");
+ canvas.set_draw_color(Color::RGB(255, 255, 0));
+
+ let texture_manager = TextureManager::new(canvas.texture_creator());
+
+ Self {
+ canvas,
+ texture_manager,
+ }
+ }
+}
+
+impl<'a> System<'a> for SysDraw
+{
+ type SystemData = (ReadStorage<'a, Pos>, ReadStorage<'a, StaticDrawable>);
+
+ fn run(&mut self, (pos, drawable): Self::SystemData)
+ {
+ self.canvas.clear();
+
+ // XXX: This is so slow.. yaaaawn. Replace with ids and texture manager
+ // lifetime?
+ for (_pos, drawable) in (&pos, &drawable).join() {
+ let texture = self
+ .texture_manager
+ .get(&drawable.texture_name)
+ .expect("Unable to load texture");
+
+ // let texture_info = texture.query();
+ self.canvas
+ .copy(&texture, None, None)
+ .expect("Unable to draw texture");
+ }
+
+ self.canvas.present();
+ }
+}
diff --git a/src/systems/input.rs b/src/systems/input.rs
new file mode 100644
index 0000000..96ac3b8
--- /dev/null
+++ b/src/systems/input.rs
@@ -0,0 +1,41 @@
+use sdl2::event::{Event, WindowEvent};
+use sdl2::{EventPump, Sdl};
+use specs::prelude::*;
+
+pub struct SysInput
+{
+ event_pump: EventPump,
+}
+
+impl SysInput
+{
+ pub fn new(sdl: &Sdl) -> Self
+ {
+ Self {
+ event_pump: sdl.event_pump().unwrap(),
+ }
+ }
+}
+
+impl<'a> System<'a> for SysInput
+{
+ type SystemData = ();
+
+ fn run(&mut self, (): Self::SystemData)
+ {
+ for event in self.event_pump.poll_iter() {
+ match event {
+ Event::Quit { .. } => {
+ panic!("Quitting due to exit signal");
+ },
+ Event::Window {
+ win_event: WindowEvent::Resized(x, y),
+ ..
+ } => {
+ println!("Window resized to {}x{}", x, y);
+ },
+ _ => {},
+ }
+ }
+ }
+}
diff --git a/src/systems/mod.rs b/src/systems/mod.rs
new file mode 100644
index 0000000..2c2af6c
--- /dev/null
+++ b/src/systems/mod.rs
@@ -0,0 +1,5 @@
+pub mod draw;
+pub use self::draw::*;
+
+pub mod input;
+pub use self::input::*;
diff --git a/src/texture_manager.rs b/src/texture_manager.rs
new file mode 100644
index 0000000..32a61a0
--- /dev/null
+++ b/src/texture_manager.rs
@@ -0,0 +1,62 @@
+use std::collections::HashMap;
+use std::path::Path;
+
+use sdl2::image::LoadTexture;
+use sdl2::render::{Texture, TextureCreator};
+
+pub const ASSET_PATH_STR: &str = "assets";
+
+pub struct TextureManager<T>
+{
+ texture_creator: TextureCreator<T>,
+ textures: HashMap<String, Texture>,
+}
+
+impl<T> TextureManager<T>
+{
+ pub fn new(texture_creator: TextureCreator<T>) -> Self
+ {
+ Self {
+ texture_creator,
+ textures: HashMap::new(),
+ }
+ }
+
+ pub fn preload<S>(&mut self, filename: S) -> Result<(), String>
+ where
+ S: AsRef<Path>,
+ {
+ let filename_str = filename
+ .as_ref()
+ .to_str()
+ .expect("Only filenames with valid Unicode are allowed.");
+ if self.textures.contains_key(filename_str) {
+ return Ok(());
+ }
+
+ let texture = self
+ .texture_creator
+ .load_texture(Path::new(ASSET_PATH_STR).join(&filename))?;
+ self.textures.insert(filename_str.to_string(), texture);
+ Ok(())
+ }
+
+ pub fn get<S>(&mut self, filename: S) -> Result<&Texture, String>
+ where
+ S: AsRef<Path>,
+ {
+ self.preload(&filename)?;
+
+ Ok(self
+ .textures
+ .get(filename.as_ref().to_str().unwrap())
+ .unwrap())
+ }
+
+ pub fn get_never_load<S>(&self, filename: S) -> Option<&Texture>
+ where
+ S: AsRef<Path>,
+ {
+ self.textures.get(filename.as_ref().to_str().unwrap())
+ }
+}