This commit is contained in:
SimonFJ20 2024-04-01 01:01:49 +02:00
parent d3abfbeaa0
commit 37fe2ea9b2
4 changed files with 129 additions and 0 deletions

56
Cargo.lock generated
View File

@ -2,6 +2,62 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "pvp-game-dilapidation" name = "pvp-game-dilapidation"
version = "0.1.0" 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"

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
sdl2 = { version = "0.36.0", features = ["ttf", "image", "mixer"] }

67
src/engine.rs Normal file
View File

@ -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<Window>,
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<T: std::fmt::Display> From<T> for Error {
fn from(value: T) -> Self {
Self(value.to_string())
}
}
impl Game {
pub fn new() -> Result<Self, Error> {
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))
}
}

View File

@ -1,3 +1,8 @@
#[allow(dead_code)]
mod engine;
fn main() { fn main() {
let game = engine::Game::new().unwrap();
game.run();
println!("Hello, world!"); println!("Hello, world!");
} }