Initial Commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
1758
Cargo.lock
generated
Normal file
1758
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "oxidizer"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[lib]
|
||||
name = "oxidizer"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
instant = "0.1.12"
|
||||
softbuffer = "0.4.1"
|
||||
timer = "0.2.0"
|
||||
winit = "0.29.10"
|
||||
38
examples/fluctuate.rs
Normal file
38
examples/fluctuate.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
use oxidizer::{AppState, Runtime};
|
||||
|
||||
pub struct State {
|
||||
time: f64,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub fn init() -> Self {
|
||||
Self {
|
||||
time: 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AppState for State {
|
||||
fn update(&mut self, delta: f64) {
|
||||
self.time += delta;
|
||||
}
|
||||
|
||||
fn draw(&self, buffer: &mut [u32], width: NonZeroU32, height: NonZeroU32) {
|
||||
let rgb_value = (127.5 * (f64::sin(2.0 * std::f64::consts::PI * self.time) + 1.0)) as u32;
|
||||
|
||||
for y in 0..height.get() {
|
||||
for x in 0..width.get() {
|
||||
let index = (y * width.get() + x) as usize;
|
||||
buffer[index] = (rgb_value << 16) | (rgb_value << 8) | rgb_value; // Assuming RGB
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = State::init();
|
||||
let runtime = Runtime::new(app);
|
||||
runtime.run();
|
||||
}
|
||||
6
src/app_state.rs
Normal file
6
src/app_state.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
pub trait AppState {
|
||||
fn update(&mut self, delta: f64);
|
||||
fn draw(&self, buffer: &mut [u32], width: NonZeroU32, height: NonZeroU32);
|
||||
}
|
||||
5
src/lib.rs
Normal file
5
src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod app_state;
|
||||
mod runtime;
|
||||
|
||||
pub use app_state::*;
|
||||
pub use runtime::*;
|
||||
78
src/runtime.rs
Normal file
78
src/runtime.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use std::{rc::Rc, time::Instant, num::NonZeroU32};
|
||||
|
||||
use instant::Duration;
|
||||
use softbuffer::Context;
|
||||
use winit::{event_loop::{EventLoop, ControlFlow}, window::{Window, WindowBuilder}, event::{Event, WindowEvent}};
|
||||
|
||||
use crate::app_state::AppState;
|
||||
|
||||
pub struct Runtime<S: AppState> {
|
||||
state: S,
|
||||
event_loop: EventLoop<()>,
|
||||
window: Rc<Window>,
|
||||
context: Context<Rc<Window>>,
|
||||
frame_duration: Duration,
|
||||
}
|
||||
|
||||
impl<S: AppState> Runtime<S> {
|
||||
pub fn new(state: S) -> Self {
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
|
||||
let context = softbuffer::Context::new(window.clone()).unwrap();
|
||||
let frame_duration = std::time::Duration::from_secs_f64(1.0 / 60.0);
|
||||
|
||||
Self {
|
||||
state,
|
||||
event_loop,
|
||||
window,
|
||||
context,
|
||||
frame_duration,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(mut self) {
|
||||
let mut last_update = Instant::now();
|
||||
let mut last_frame_time = Instant::now();
|
||||
let mut surface = softbuffer::Surface::new(&self.context, self.window.clone()).unwrap();
|
||||
|
||||
self.event_loop.run(move |event, elwt| {
|
||||
elwt.set_control_flow(ControlFlow::Poll);
|
||||
//
|
||||
match event {
|
||||
Event::AboutToWait => {
|
||||
if last_update.elapsed() >= self.frame_duration {
|
||||
last_update = Instant::now();
|
||||
let delta = last_frame_time.elapsed().as_secs_f64();
|
||||
last_frame_time = Instant::now();
|
||||
|
||||
self.state.update(delta);
|
||||
self.window.request_redraw();
|
||||
}
|
||||
},
|
||||
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested} if window_id == self.window.id() => {
|
||||
let (width, height) = {
|
||||
let size = self.window.inner_size();
|
||||
(size.width, size.height)
|
||||
};
|
||||
surface.resize(NonZeroU32::new(width).unwrap(),
|
||||
NonZeroU32::new(height).unwrap(),
|
||||
).unwrap();
|
||||
|
||||
let mut buffer = surface.buffer_mut().unwrap();
|
||||
|
||||
// Call your draw function here with the buffer, width, and height
|
||||
self.state.draw(&mut buffer, NonZeroU32::new(width).unwrap(), NonZeroU32::new(height).unwrap());
|
||||
|
||||
buffer.present().unwrap();
|
||||
},
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::CloseRequested,
|
||||
window_id,
|
||||
} if window_id == self.window.id() => {
|
||||
elwt.exit();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user