summaryrefslogtreecommitdiff
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
downloadpmd_coop-f3178df0a92fb3b87087e78cad7b9313f947be6a.tar.gz
pmd_coop-f3178df0a92fb3b87087e78cad7b9313f947be6a.zip
Initial commit
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock394
-rw-r--r--Cargo.toml15
-rw-r--r--assets/portraits.pngbin0 -> 2041448 bytes
-rw-r--r--rustfmt.toml11
-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
11 files changed, 647 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..4b7c81b
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,394 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "ahash"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e8fd72866655d1904d6b0997d0b07ba561047d070fbe29de039031c641b61217"
+
+[[package]]
+name = "arrayvec"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
+
+[[package]]
+name = "atom"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c9ff149ed9780025acfdb36862d35b28856bb693ceb451259a7164442f22fdc3"
+
+[[package]]
+name = "autocfg"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
+
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "crossbeam-channel"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4"
+dependencies = [
+ "cfg-if 1.0.0",
+ "crossbeam-utils 0.8.4",
+]
+
+[[package]]
+name = "crossbeam-deque"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9"
+dependencies = [
+ "cfg-if 1.0.0",
+ "crossbeam-epoch",
+ "crossbeam-utils 0.8.4",
+]
+
+[[package]]
+name = "crossbeam-epoch"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "52fb27eab85b17fbb9f6fd667089e07d6a2eb8743d02639ee7f6a7a7729c9c94"
+dependencies = [
+ "cfg-if 1.0.0",
+ "crossbeam-utils 0.8.4",
+ "lazy_static",
+ "memoffset",
+ "scopeguard",
+]
+
+[[package]]
+name = "crossbeam-queue"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570"
+dependencies = [
+ "cfg-if 0.1.10",
+ "crossbeam-utils 0.7.2",
+ "maybe-uninit",
+]
+
+[[package]]
+name = "crossbeam-utils"
+version = "0.7.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
+dependencies = [
+ "autocfg",
+ "cfg-if 0.1.10",
+ "lazy_static",
+]
+
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278"
+dependencies = [
+ "autocfg",
+ "cfg-if 1.0.0",
+ "lazy_static",
+]
+
+[[package]]
+name = "either"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
+
+[[package]]
+name = "hashbrown"
+version = "0.7.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96282e96bfcd3da0d3aa9938bedf1e50df3269b6db08b4876d2da0bb1a0841cf"
+dependencies = [
+ "ahash",
+ "autocfg",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "hibitset"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93a1bb8316a44459a7d14253c4d28dd7395cbd23cc04a68c46e851b8e46d64b1"
+dependencies = [
+ "atom",
+ "rayon",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "libc"
+version = "0.2.94"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
+
+[[package]]
+name = "log"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
+dependencies = [
+ "cfg-if 1.0.0",
+]
+
+[[package]]
+name = "maybe-uninit"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
+
+[[package]]
+name = "memchr"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
+
+[[package]]
+name = "memoffset"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f83fb6581e8ed1f85fd45c116db8405483899489e38406156c25eb743554361d"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "mopa"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a785740271256c230f57462d3b83e52f998433a7062fc18f96d5999474a9f915"
+
+[[package]]
+name = "nom"
+version = "5.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
+dependencies = [
+ "memchr",
+ "version_check",
+]
+
+[[package]]
+name = "num_cpus"
+version = "1.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
+dependencies = [
+ "hermit-abi",
+ "libc",
+]
+
+[[package]]
+name = "pmd_coop"
+version = "0.1.0"
+dependencies = [
+ "lazy_static",
+ "sdl2",
+ "specs",
+ "specs-derive",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rayon"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674"
+dependencies = [
+ "autocfg",
+ "crossbeam-deque",
+ "either",
+ "rayon-core",
+]
+
+[[package]]
+name = "rayon-core"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a"
+dependencies = [
+ "crossbeam-channel",
+ "crossbeam-deque",
+ "crossbeam-utils 0.8.4",
+ "lazy_static",
+ "num_cpus",
+]
+
+[[package]]
+name = "scopeguard"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
+
+[[package]]
+name = "sdl2"
+version = "0.34.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "deecbc3fa9460acff5a1e563e05cb5f31bba0aa0c214bb49a43db8159176d54b"
+dependencies = [
+ "bitflags",
+ "lazy_static",
+ "libc",
+ "sdl2-sys",
+]
+
+[[package]]
+name = "sdl2-sys"
+version = "0.34.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41a29aa21f175b5a41a6e26da572d5e5d1ee5660d35f9f9d0913e8a802098f74"
+dependencies = [
+ "cfg-if 0.1.10",
+ "libc",
+ "version-compare",
+]
+
+[[package]]
+name = "shred"
+version = "0.10.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c5f08237e667ac94ad20f8878b5943d91a93ccb231428446c57c21c57779016d"
+dependencies = [
+ "arrayvec",
+ "hashbrown",
+ "mopa",
+ "rayon",
+ "smallvec",
+ "tynm",
+]
+
+[[package]]
+name = "shrev"
+version = "1.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5752e017e03af9d735b4b069f53b7a7fd90fefafa04d8bd0c25581b0bff437f"
+
+[[package]]
+name = "smallvec"
+version = "1.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
+
+[[package]]
+name = "specs"
+version = "0.16.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fff28a29366aff703d5da8a7e2c8875dc8453ac1118f842cbc0fa70c7db51240"
+dependencies = [
+ "crossbeam-queue",
+ "hashbrown",
+ "hibitset",
+ "log",
+ "rayon",
+ "shred",
+ "shrev",
+ "tuple_utils",
+]
+
+[[package]]
+name = "specs-derive"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3e23e09360f3d2190fec4222cd9e19d3158d5da948c0d1ea362df617dd103511"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.72"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
+]
+
+[[package]]
+name = "tuple_utils"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "44834418e2c5b16f47bedf35c28e148db099187dd5feee6367fb2525863af4f1"
+
+[[package]]
+name = "tynm"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4df2caa2dc9c3d1f7641ba981f4cd40ab229775aa7aeb834c9ab2850d50623d"
+dependencies = [
+ "nom",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
+
+[[package]]
+name = "version-compare"
+version = "0.0.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d63556a25bae6ea31b52e640d7c41d1ab27faba4ccb600013837a3d0b3994ca1"
+
+[[package]]
+name = "version_check"
+version = "0.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..75f2730
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "pmd_coop"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+specs = "^0.16"
+specs-derive = "*"
+lazy_static = "*"
+
+[dependencies.sdl2]
+version = "^0.34"
+features = ["ttf", "image", "unsafe_textures"]
diff --git a/assets/portraits.png b/assets/portraits.png
new file mode 100644
index 0000000..5530312
--- /dev/null
+++ b/assets/portraits.png
Binary files differ
diff --git a/rustfmt.toml b/rustfmt.toml
new file mode 100644
index 0000000..b7e6339
--- /dev/null
+++ b/rustfmt.toml
@@ -0,0 +1,11 @@
+brace_style = "AlwaysNextLine"
+condense_wildcard_suffixes = true
+control_brace_style = "ClosingNextLine"
+fn_single_line = true
+format_strings = true
+imports_granularity = "Module"
+match_block_trailing_comma = true
+newline_style = "Unix"
+group_imports = "StdExternalCrate"
+struct_field_align_threshold = 15
+wrap_comments = true \ No newline at end of file
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())
+ }
+}