Skip to content

Commit

Permalink
rename draw_canvas -> blit_canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
not-fl3 committed Aug 30, 2024
1 parent 48a2b68 commit 65f434b
Show file tree
Hide file tree
Showing 12 changed files with 1,070 additions and 5 deletions.
507 changes: 507 additions & 0 deletions examples/MetalRoughSpheres.gltf

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/aa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asd

Check failure on line 1 in examples/aa.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

expected one of `!` or `::`, found `<eof>`

Check failure on line 1 in examples/aa.rs

View workflow job for this annotation

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

expected one of `!` or `::`, found `<eof>`
6 changes: 3 additions & 3 deletions examples/basic_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ async fn game(ctx: macroquad::Context) {

// .draws order defines "Z" order, here canvas1 content will be
// on the background and canvas3's blue circle will be on top of everything
ctx.draw_canvas(&mut canvas1);
ctx.draw_canvas(&mut canvas2);
ctx.draw_canvas(&mut canvas3);
ctx.blit_canvas(&mut canvas1);
ctx.blit_canvas(&mut canvas2);
ctx.blit_canvas(&mut canvas3);

next_frame().await;
}
Expand Down
110 changes: 110 additions & 0 deletions examples/bunnymark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use macroquad::{
camera::{Camera, Environment, Projection},
color::*,
math::{vec2, vec3},
shapes::{DrawParams, Sprite, Text},
};
use miniquad::KeyCode;

use macroquad::compat::*;

async fn game(ctx: macroquad::Context) {
let mut scene = ctx.new_scene();

let texture = ctx
.resources
.load_texture("examples/ferris2.png")
.await
.unwrap();
let sprite = Sprite::new(&texture);
let bunny = scene.into_model(sprite, DrawParams::default());
let bunny = scene.add_model(&bunny);
scene.set_scale(&bunny, vec3(0.01, 1.0, 0.01));
let mut camera = Camera {
environment: Environment::SolidColor(WHITE),
depth_enabled: false,
projection: Projection::Orthographic,
position: vec3(0., 10.0, 0.),
up: vec3(0., 1., 0.),
target: vec3(0.0, 0., 1.),
z_near: 0.1,
z_far: 15.0,
..Default::default()
};

let mut canvas = ctx.new_canvas();
let mut bunnies = vec![];
let mut bunnies_dir = vec![];

for _ in 0..1 {
bunnies.push(vec3(
quad_rand::gen_range(-20.0, 20.0),
0.0,
quad_rand::gen_range(-20.0, 20.0),
));
bunnies_dir.push(vec3(
quad_rand::gen_range(-1.0, 1.0),
0.,
quad_rand::gen_range(-1.0, 1.0),
));
}
scene.update_multi_positions(&bunny, &bunnies);

loop {
if ctx.is_key_down(KeyCode::Space) {
for _ in 0..1 {
bunnies.push(vec3(
quad_rand::gen_range(-20.0, 20.0),
0.0,
quad_rand::gen_range(-20.0, 20.0),
));
bunnies_dir.push(vec3(
quad_rand::gen_range(-1.0, 1.0),
0.,
quad_rand::gen_range(-1.0, 1.0),
));
}
scene.update_multi_positions(&bunny, &bunnies);
}

ctx.clear_screen(WHITE);

for (bunny, dir) in bunnies.iter_mut().zip(bunnies_dir.iter_mut()) {
*bunny += *dir;
if bunny.x >= 20.0 || bunny.x <= -20.0 {
dir.x *= -1.0;
}
if bunny.z >= 20.0 || bunny.z <= -20.0 {
dir.z *= -1.0;
}
}

scene.update_multi_positions(&bunny, &bunnies);

scene.draw(&mut camera);

canvas.clear();
canvas.draw(
Text::new(&format!("fps: {:0.1}", 1.0 / ctx.frame_time()), 16),
vec2(0.0, 16.0),
BLACK,
);
canvas.draw(
Text::new(&format!("bunnies: {}", bunnies.len()), 16),
vec2(0.0, 32.0),
BLACK,
);
canvas.draw(
Text::new(&format!("Press any key"), 16),
vec2(0.0, 48.0),
BLACK,
);
canvas.blit();

next_frame().await;
}
}

fn main() {
macroquad::start(Default::default(), |ctx| game(ctx));
}
Binary file added examples/ferris2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/heightmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
197 changes: 197 additions & 0 deletions examples/mmmesh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
use macroquad::{
file::load_file,
gizmos::{draw_gizmos, gizmos_add_line, init_gizmos},

Check warning on line 3 in examples/mmmesh.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unused import: `draw_gizmos`
math::{vec2, vec3, Vec2, Vec3},
quad_gl::{color, scene::Shader},
time::get_time,

Check failure on line 6 in examples/mmmesh.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unresolved import `macroquad::time::get_time`

Check failure on line 6 in examples/mmmesh.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

module `time` is private
window::next_frame,
};

mod orbit_camera;

Check failure on line 10 in examples/mmmesh.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

file not found for module `orbit_camera`

const vertices: &[Vec3] = &[
vec3(-0.5, 0.5, 0.8),
vec3(-0.5, 0.5, -0.8),
vec3(-0.5, -0.5, 0.8),
vec3(-0.5, 0.5, -0.8),
vec3(-0.5, -0.5, -0.8),
vec3(-0.5, -0.5, 0.8),
vec3(-0.5, 0.5, -0.8),
vec3(0.5, 0.5, -0.8),
vec3(-0.5, -0.5, -0.8),
vec3(0.5, 0.5, -0.8),
vec3(0.5, -0.5, -0.8),
vec3(-0.5, -0.5, -0.8),
vec3(0.5, 0.5, -0.8),
vec3(0.5, 0.5, 0.8),
vec3(0.5, -0.5, 0.8),
vec3(0.5, -0.5, -0.8),
vec3(0.5, 0.5, -0.8),
vec3(0.5, -0.5, 0.8),
vec3(0.5, 0.5, 0.8),
vec3(-0.5, 0.5, 0.8),
vec3(-0.5, -0.5, 0.8),
vec3(0.5, -0.5, 0.8),
vec3(0.5, 0.5, 0.8),
vec3(-0.5, -0.5, 0.8),
vec3(-0.5, -0.5, 0.8),
vec3(-0.5, -0.5, -0.8),
vec3(0.5, -0.5, -0.8),
vec3(0.5, -0.5, 0.8),
vec3(-0.5, -0.5, 0.8),
vec3(0.5, -0.5, -0.8),
vec3(0.5, 0.5, 0.8),
vec3(0.5, 0.5, -0.8),
vec3(-0.5, 0.5, -0.8),
vec3(-0.5, 0.5, 0.8),
vec3(0.5, 0.5, 0.8),
vec3(-0.5, 0.5, -0.8),
];
const uvs: &[Vec2] = &[
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
vec2(0.0, 0.0),
];
const normals: &[Vec3] = &[
vec3(-1.0, 0.0, 0.0),
vec3(-1.0, 0.0, 0.0),
vec3(-1.0, 0.0, 0.0),
vec3(-1.0, 0.0, 0.0),
vec3(-1.0, 0.0, 0.0),
vec3(-1.0, 0.0, 0.0),
vec3(0.0, 0.0, -1.0),
vec3(0.0, 0.0, -1.0),
vec3(0.0, 0.0, -1.0),
vec3(0.0, 0.0, -1.0),
vec3(0.0, 0.0, -1.0),
vec3(0.0, 0.0, -1.0),
vec3(1.0, -0.0, 0.0),
vec3(1.0, -0.0, 0.0),
vec3(1.0, -0.0, 0.0),
vec3(1.0, 0.0, 0.0),
vec3(1.0, 0.0, 0.0),
vec3(1.0, 0.0, 0.0),
vec3(0.0, 0.0, 1.0),
vec3(0.0, 0.0, 1.0),
vec3(0.0, 0.0, 1.0),
vec3(0.0, 0.0, 1.0),
vec3(0.0, 0.0, 1.0),
vec3(0.0, 0.0, 1.0),
vec3(0.0, -1.0, 0.0),
vec3(0.0, -1.0, 0.0),
vec3(0.0, -1.0, 0.0),
vec3(0.0, -1.0, 0.0),
vec3(0.0, -1.0, 0.0),
vec3(0.0, -1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 1.0, 0.0),
];
const indices: &[u16] = &[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
];

async fn game(ctx: macroquad::Context) {
unsafe {
macroquad::miniquad::gl::glEnable(macroquad::miniquad::gl::GL_TEXTURE_CUBE_MAP_SEAMLESS)
};

init_gizmos(&ctx);

let mut scene = ctx.new_scene();

let texture = load_file("examples/ferris.png").await.unwrap();
let texture = quad_gl::image::decode(&texture).unwrap();

let mesh = quad_gl::models::CpuMesh(

Check failure on line 143 in examples/mmmesh.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

expected function, tuple struct or tuple variant, found struct `quad_gl::models::CpuMesh`
vertices.to_vec(),
uvs.to_vec(),
normals.to_vec(),
indices.to_vec(),
);
let mut mesh = ctx.mesh(mesh, None);
mesh.nodes[0].materials[0].shader = Shader::new(
ctx.quad_ctx.lock().unwrap().as_mut(),
vec![],
Some(FRAGMENT),
None,
);

let _mesh = scene.add_model(&mesh);
let mut orbit = orbit_camera::OrbitCamera::new();

Check failure on line 158 in examples/mmmesh.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

failed to resolve: could not find `OrbitCamera` in `orbit_camera`

loop {
let t = get_time();
let p = vec3(t.sin() as f32, 0.0, t.cos() as f32);

gizmos_add_line(true, p, p * 1.2);
gizmos_add_line(false, vec3(0.0, 0.0, 0.0), p);

ctx.clear_screen(color::BLACK);
orbit.orbit(&ctx);
scene.draw(&orbit.camera);
//draw_gizmos(&orbit.camera);
next_frame().await
}
}

fn main() {
macroquad::start(Default::default(), |ctx| game(ctx));
}

const VERTEX: &str = r#"
#include "common_vertex.glsl"
void vertex() {
}
"#;

const FRAGMENT: &str = r#"
varying vec3 out_normal;
void main() {
vec3 norm = normalize(out_normal);
vec3 lightDir = normalize(vec3(1.0, -1.0, 0.5));
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * vec3(1.0) + vec3(0.3);
gl_FragColor = vec4(diffuse,1.0);
}
"#;
Loading

0 comments on commit 65f434b

Please sign in to comment.