mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-27 05:37:39 +02:00
game engine progress
This commit is contained in:
parent
fca446d8e9
commit
0ad5b4fbfc
1
unnamed-game-engine/.gitignore
vendored
Normal file
1
unnamed-game-engine/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
||||
46
unnamed-game-engine/Cargo.lock
generated
Normal file
46
unnamed-game-engine/Cargo.lock
generated
Normal file
@ -0,0 +1,46 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.183"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5b646652bf6661599e1da8901b3b9522896f01e736bad5f723fe7a3a27f899d"
|
||||
|
||||
[[package]]
|
||||
name = "sdl3"
|
||||
version = "0.17.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dce0693cede7f7901e968e5539f87174d0fb5fac5d489a3e3be52b2b4a0b49c5"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"sdl3-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sdl3-sys"
|
||||
version = "0.6.1+SDL-3.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d73fc820b0b6b9bd0557de7b799ec9da1ca4f35fcd1cb42f7ceb85e640d1477"
|
||||
|
||||
[[package]]
|
||||
name = "unnamed-game-engine"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"sdl3",
|
||||
]
|
||||
7
unnamed-game-engine/Cargo.toml
Normal file
7
unnamed-game-engine/Cargo.toml
Normal file
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "unnamed-game-engine"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
sdl3 = "0.17.3"
|
||||
1
unnamed-game-engine/src/engine/error.rs
Normal file
1
unnamed-game-engine/src/engine/error.rs
Normal file
@ -0,0 +1 @@
|
||||
pub type Error = String;
|
||||
69
unnamed-game-engine/src/engine/game.rs
Normal file
69
unnamed-game-engine/src/engine/game.rs
Normal file
@ -0,0 +1,69 @@
|
||||
|
||||
|
||||
use std::time::Instant;
|
||||
|
||||
use sdl3::render::Canvas;
|
||||
use sdl3::{EventPump, Sdl, VideoSubsystem};
|
||||
use sdl3::video::Window;
|
||||
use sdl3::pixels::Color;
|
||||
use sdl3::event::Event;
|
||||
use sdl3::keyboard::Keycode;
|
||||
|
||||
use super::error::Error;
|
||||
use super::object::Object;
|
||||
|
||||
pub struct Game<Io, O: Object> {
|
||||
sdl_context: Sdl,
|
||||
video_subsystem: VideoSubsystem,
|
||||
canvas: Canvas<Window>,
|
||||
event_pump: EventPump,
|
||||
io: Io,
|
||||
objects: Vec<O>,
|
||||
}
|
||||
|
||||
impl<Io, O: Object> Game<Io, O> {
|
||||
pub fn new(io: Io) -> Result<Self, Error> {
|
||||
let sdl_context = sdl3::init().unwrap();
|
||||
let video_subsystem = sdl_context.video().unwrap();
|
||||
let window = video_subsystem.window("Game", 1280, 720).position_centered().build().unwrap();
|
||||
let mut canvas = window.into_canvas();
|
||||
|
||||
canvas.set_draw_color(Color::BLACK);
|
||||
canvas.clear();
|
||||
canvas.present();
|
||||
|
||||
let event_pump = sdl_context.event_pump().unwrap();
|
||||
|
||||
Ok(Self {
|
||||
sdl_context,
|
||||
video_subsystem,
|
||||
canvas,
|
||||
event_pump,
|
||||
io,
|
||||
objects: Vec::new()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn run(&mut self) {
|
||||
let mut time_before = Instant::now();
|
||||
let time_per_frame = 1_000_000_000 / 60;
|
||||
'running: loop {
|
||||
for event in self.event_pump.poll_iter() {
|
||||
match event {
|
||||
Event::Quit {..} |
|
||||
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
|
||||
break 'running
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let now = Instant::now();
|
||||
let delta = now - time_before;
|
||||
time_before = now;
|
||||
for object in self.objects.iter_mut() {
|
||||
object.update(delta);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
10
unnamed-game-engine/src/engine/game_example.rs
Normal file
10
unnamed-game-engine/src/engine/game_example.rs
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
pub trait GameObject {
|
||||
fn update(deltaTime: Duration);
|
||||
fn render();
|
||||
}
|
||||
|
||||
pub struct Game<GameObjectTypeetc: GameObject> {
|
||||
|
||||
}
|
||||
|
||||
0
unnamed-game-engine/src/engine/io.rs
Normal file
0
unnamed-game-engine/src/engine/io.rs
Normal file
4
unnamed-game-engine/src/engine/mod.rs
Normal file
4
unnamed-game-engine/src/engine/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod object;
|
||||
pub mod game;
|
||||
mod system;
|
||||
mod error;
|
||||
8
unnamed-game-engine/src/engine/object.rs
Normal file
8
unnamed-game-engine/src/engine/object.rs
Normal file
@ -0,0 +1,8 @@
|
||||
use std::time::Duration;
|
||||
|
||||
pub trait Object {
|
||||
fn update(&mut self, delta: Duration);
|
||||
fn render(&mut self);
|
||||
}
|
||||
|
||||
|
||||
17
unnamed-game-engine/src/engine/system.rs
Normal file
17
unnamed-game-engine/src/engine/system.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use super::error::Error;
|
||||
|
||||
|
||||
pub trait System {
|
||||
|
||||
fn on_add(&self) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_update(&self) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn on_remove(&self) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
11
unnamed-game-engine/src/main.rs
Normal file
11
unnamed-game-engine/src/main.rs
Normal file
@ -0,0 +1,11 @@
|
||||
mod engine;
|
||||
|
||||
use crate::engine::game::Game;
|
||||
|
||||
|
||||
fn main() {
|
||||
|
||||
let mut game = Game::new().unwrap();
|
||||
game.run();
|
||||
println!("Hello, world!");
|
||||
}
|
||||
0
unnamed-game-engine/src/player.rs
Normal file
0
unnamed-game-engine/src/player.rs
Normal file
Loading…
x
Reference in New Issue
Block a user