Skip to content

Commit

Permalink
Added window resized event, made it possible to get a reference to th…
Browse files Browse the repository at this point in the history
…e underlying glow handle, added a way to get the total duration of the app
  • Loading branch information
VictorKoenders committed Sep 19, 2024
1 parent e3e6656 commit 0bca465
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ mod shader;
mod text;
mod texture;

use std::cell::Cell;
use std::rc::Rc;
use std::{cell::Cell, sync::Arc};

use glam::Mat4;
use glow::{Context, HasContext};
Expand All @@ -28,7 +28,7 @@ pub use texture::*;
use crate::window::Window;

struct State {
gl: Context,
gl: Arc<Context>,

current_vertex_buffer: Cell<Option<glow::Buffer>>,
current_index_buffer: Cell<Option<glow::Buffer>>,
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Graphics {

Graphics {
state: Rc::new(State {
gl,
gl: Arc::new(gl),
current_vertex_buffer: Cell::new(None),
current_index_buffer: Cell::new(None),
current_shader: Cell::new(None),
Expand Down Expand Up @@ -228,6 +228,15 @@ impl Graphics {
}
}
}

/// Get the raw OpenGL context.
///
/// # Safety
///
/// You have full access to the raw OpenGL context, so you can do anything you want with it.
pub unsafe fn gl(&self) -> &Arc<Context> {
&self.state.gl
}
}

pub trait Target {
Expand Down
1 change: 1 addition & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Input {
self.axes.set_value(*gamepad_id, *axis, *value);
}
}
Event::WindowResized { .. } => {}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/input/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub enum Event {
axis: GamepadAxis,
value: f32,
},
WindowResized {
width: u32,
height: u32,
},
}

impl Event {
Expand Down Expand Up @@ -157,6 +161,15 @@ impl Event {
}
}

SDL_WINDOWEVENT => {
let e = &event.window;
if e.data1 > 0 && e.data2 > 0 {
let width = e.data1 as u32;
let height = e.data2 as u32;
return Some(Event::WindowResized { width, height });
}
}

_ => {}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::{Duration, Instant};

pub struct Timer {
start_time: Instant,
last_time: Instant,
accumulated_time: Duration,
target_time: Duration,
Expand All @@ -12,6 +13,7 @@ impl Timer {
let target_time = Duration::from_secs_f64(1.0 / tick_rate);

Timer {
start_time: Instant::now(),
last_time: Instant::now(),
accumulated_time: Duration::ZERO,
target_time,
Expand Down Expand Up @@ -73,4 +75,8 @@ impl Timer {
self.accumulated_time = self.max_lag;
}
}

pub fn total_time(&self) -> Duration {
self.start_time.elapsed()
}
}

0 comments on commit 0bca465

Please sign in to comment.