Skip to content

Commit

Permalink
update quad-gl version
Browse files Browse the repository at this point in the history
  • Loading branch information
not-fl3 committed Aug 27, 2024
1 parent 62dda96 commit 8731016
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 41 deletions.
4 changes: 0 additions & 4 deletions examples/basic_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ async fn game(ctx: macroquad::Context) {
// and will get an additiona red circle each frame.
canvas2.draw(Circle::new(10.0), p1, RED);

canvas2.draw(Circle::new(10.0), p1, RED);
canvas2.draw(Circle::new(10.0), p1, GREEN);

canvas2.draw(Text::new("asd", 30), p1, RED);
// canvas3 is a "dynamic" canvas. starts from scratch each frame.
// Useful for animated content.
canvas3.clear();
Expand Down
2 changes: 1 addition & 1 deletion examples/gltf/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use macroquad::{
camera::{Camera, Environment, Projection},
color,
},
shapes::*,

Check warning on line 10 in examples/gltf/main.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

unused import: `shapes`

Check warning on line 10 in examples/gltf/main.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unused import: `shapes`
window::next_frame,
};

Expand All @@ -25,7 +26,6 @@ async fn game(ctx: macroquad::Context) {
.await
.unwrap();
let _helmet = scene.add_model(&helmet);

let skybox = ctx
.resources
.load_cubemap(
Expand Down
14 changes: 8 additions & 6 deletions src/gizmos.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub use crate::{math::Vec3, window::next_frame};
pub use crate::{
math::{vec2, vec3, Vec3},
window::next_frame,
};
pub use quad_gl::{
color::*,
draw_calls_batcher::{DrawCallsBatcher, DrawMode},
draw_calls_batcher::{DrawCallsBatcher, DrawMode, Vertex},
};

use std::{
Expand Down Expand Up @@ -42,13 +45,12 @@ pub fn init_gizmos(ctx: &crate::Context) {
}

fn draw_line(gl: &mut DrawCallsBatcher, p0: Vec3, p1: Vec3) {
let uv = [0., 0.];
let color: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
let uv = vec2(0., 0.);
let indices = [0, 1];

let line = [
([p0.x, p0.y, p0.z], uv, color),
([p1.x, p1.y, p1.z], uv, color),
Vertex::new2(vec3(p0.x, p0.y, p0.z), uv, BLUE),
Vertex::new2(vec3(p1.x, p1.y, p1.z), uv, BLUE),
];
gl.texture(None);
gl.draw_mode(DrawMode::Lines);
Expand Down
66 changes: 36 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ use std::future::Future;
use std::pin::Pin;

mod exec;
mod time;

pub use quad_gl::camera;
pub use quad_gl::color;
pub use quad_gl::models;
pub use quad_gl::scene;
pub use quad_gl::shapes;
pub use quad_gl::texture;

pub mod file;
pub mod input;
Expand Down Expand Up @@ -153,6 +161,7 @@ struct Stage {
quad_gl: Arc<Mutex<quad_gl::QuadGl>>,
ui: Arc<Mutex<quad_gl::ui::Ui>>,
input: Arc<Mutex<input::InputContext>>,
time: Arc<Mutex<time::Time>>,
}

impl EventHandler for Stage {
Expand Down Expand Up @@ -282,32 +291,19 @@ impl EventHandler for Stage {
// });
// }

// fn key_down_event(&mut self, keycode: KeyCode, modifiers: KeyMods, repeat: bool) {
// let context = get_context();
// context.keys_down.insert(keycode);
// if repeat == false {
// context.keys_pressed.insert(keycode);
// }

// context.input_events.iter_mut().for_each(|arr| {
// arr.push(MiniquadInputEvent::KeyDown {
// keycode,
// modifiers,
// repeat,
// })
// });
// }

// fn key_up_event(&mut self, keycode: KeyCode, modifiers: KeyMods) {
// let context = get_context();
// context.keys_down.remove(&keycode);
// context.keys_released.insert(keycode);
fn key_down_event(&mut self, keycode: KeyCode, modifiers: KeyMods, repeat: bool) {
let mut context = self.input.lock().unwrap();
context.keys_down.insert(keycode);
if repeat == false {
context.keys_pressed.insert(keycode);
}
}

// context
// .input_events
// .iter_mut()
// .for_each(|arr| arr.push(MiniquadInputEvent::KeyUp { keycode, modifiers }));
// }
fn key_up_event(&mut self, keycode: KeyCode, modifiers: KeyMods) {
let mut context = self.input.lock().unwrap();
context.keys_down.remove(&keycode);
context.keys_released.insert(keycode);
}

fn update(&mut self) {
let _z = telemetry::ZoneGuard::new("Event::update");
Expand All @@ -323,6 +319,7 @@ impl EventHandler for Stage {
}

fn draw(&mut self) {
self.time.lock().unwrap().update();
//let result = maybe_unwind(get_context().unwind, || {
if let Some(future) = self.main_future.as_mut() {
let _z = telemetry::ZoneGuard::new("user code");
Expand Down Expand Up @@ -362,8 +359,8 @@ pub struct Context {
quad_gl: Arc<Mutex<quad_gl::QuadGl>>,
pub resources: resources::Resources,
pub input: Arc<Mutex<input::InputContext>>,
time: Arc<Mutex<time::Time>>,
ui: Arc<Mutex<quad_gl::ui::Ui>>,
start_time: f64,
}

impl Context {
Expand All @@ -374,20 +371,23 @@ impl Context {
let quad_gl = Arc::new(Mutex::new(quad_gl));
let (w, h) = miniquad::window::screen_size();
let ui = quad_gl::ui::Ui::new(quad_ctx.clone(), w, h);
let start_time = miniquad::date::now();

let time = time::Time::new();
Context {
quad_ctx: quad_ctx.clone(),
quad_gl: quad_gl.clone(),
resources: resources::Resources::new(quad_ctx.clone(), quad_gl.clone()),
input: Arc::new(Mutex::new(input::InputContext::new())),
ui: Arc::new(Mutex::new(ui)),
start_time
time: Arc::new(Mutex::new(time)),
}
}

pub fn frame_time(&self) -> f32 {
self.time.lock().unwrap().frame_time as f32
}

pub fn time_since_start(&self) -> f32 {
(miniquad::date::now() - self.start_time) as f32
self.time.lock().unwrap().time_since_start()
}

pub fn clear_screen(&self, color: quad_gl::color::Color) {
Expand Down Expand Up @@ -430,10 +430,15 @@ impl Context {
.unwrap()
.from_rgba8(width, height, &data)
}

pub fn mesh(&self, mesh: CpuMesh, texture: Option<Arc<Texture2D>>) -> Model {
self.quad_gl.lock().unwrap().mesh(mesh, texture)
}

pub fn gl<'a>(&'a mut self) -> impl std::ops::DerefMut<Target = quad_gl::QuadGl> + 'a {
self.quad_gl.lock().unwrap()
}

pub fn root_ui<'a>(&'a self) -> impl std::ops::DerefMut<Target = quad_gl::ui::Ui> + 'a {
self.ui.lock().unwrap()
}
Expand All @@ -453,6 +458,7 @@ pub fn start<F: Fn(Context) -> Fut + 'static, Fut: Future<Output = ()> + 'static
quad_ctx: ctx.quad_ctx.clone(),
quad_gl: ctx.quad_gl.clone(),
ui: ctx.ui.clone(),
time: ctx.time.clone(),
main_future: Some(Box::pin(future(ctx))),
})
});
Expand Down
28 changes: 28 additions & 0 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub struct Time {
pub start_time: f64,
pub frame_time: f64,
pub last_frame_time: f64,
}

impl Time {
pub fn new() -> Time {
let start_time = miniquad::date::now();
let last_frame_time = miniquad::date::now();
let frame_time = 0.0;

Time {
start_time,
frame_time,
last_frame_time,
}
}

pub fn time_since_start(&self) -> f32 {
(miniquad::date::now() - self.start_time) as f32
}

pub fn update(&mut self) {
self.frame_time = miniquad::date::now() - self.last_frame_time;
self.last_frame_time = miniquad::date::now();
}
}

0 comments on commit 8731016

Please sign in to comment.