From 37fe2ea9b2bad7bb73d77343f9ab270127f29587 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Mon, 1 Apr 2024 01:01:49 +0200 Subject: [PATCH] engine --- Cargo.lock | 56 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/engine.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 ++++ 4 files changed, 129 insertions(+) create mode 100644 src/engine.rs diff --git a/Cargo.lock b/Cargo.lock index 66a98c1..d19cb63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,62 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[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.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + [[package]] name = "pvp-game-dilapidation" version = "0.1.0" +dependencies = [ + "sdl2", +] + +[[package]] +name = "sdl2" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8356b2697d1ead5a34f40bcc3c5d3620205fe0c7be0a14656223bfeec0258891" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26bcacfdd45d539fb5785049feb0038a63931aa896c7763a2a12e125ec58bd29" +dependencies = [ + "cfg-if", + "libc", + "version-compare", +] + +[[package]] +name = "version-compare" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" diff --git a/Cargo.toml b/Cargo.toml index 94f33e3..f2d76a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +sdl2 = { version = "0.36.0", features = ["ttf", "image", "mixer"] } diff --git a/src/engine.rs b/src/engine.rs new file mode 100644 index 0000000..a37da1b --- /dev/null +++ b/src/engine.rs @@ -0,0 +1,67 @@ +use std::time::Duration; + +use sdl2::{ + event::Event, keyboard::Keycode, pixels::Color, render::Canvas, video::Window, Sdl, + VideoSubsystem, +}; + +pub struct Game { + sdl_context: Sdl, + video_subsystem: VideoSubsystem, + canvas: Canvas, + event_pump: sdl2::EventPump, +} + +#[derive(Debug, Clone)] +pub struct Error(String); + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl From for Error { + fn from(value: T) -> Self { + Self(value.to_string()) + } +} + +impl Game { + pub fn new() -> Result { + let sdl_context = sdl2::init()?; + let video_subsystem = sdl_context.video()?; + let window = video_subsystem + .window("pvp-game-dilapidation", 1280, 720) + .position_centered() + .build()?; + let mut canvas = window.into_canvas().build()?; + canvas.set_draw_color(Color::RGB(60, 180, 180)); + canvas.clear(); + let event_pump = sdl_context.event_pump()?; + Ok(Self { + sdl_context, + video_subsystem, + canvas, + event_pump, + }) + } + + pub fn run(mut self) { + 'running: loop { + for event in self.event_pump.poll_iter() { + match event { + Event::Quit { .. } + | Event::KeyDown { + keycode: Some(Keycode::Escape), + .. + } => break 'running, + _ => {} + } + } + self.canvas.present(); + } + self.canvas.present(); + std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)) + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..66ace41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +#[allow(dead_code)] +mod engine; + fn main() { + let game = engine::Game::new().unwrap(); + game.run(); println!("Hello, world!"); }